@logicflow/core 2.1.4 → 2.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -124,6 +124,10 @@ var GraphModel = /** @class */ (function () {
124
124
  for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) {
125
125
  var entry = entries_1_1.value;
126
126
  if (entry.target === _this.rootEl) {
127
+ // 检查元素是否仍在DOM中
128
+ var isElementInDOM = document.body.contains(_this.rootEl);
129
+ if (!isElementInDOM)
130
+ return;
127
131
  _this.resize();
128
132
  _this.eventCenter.emit('graph:resize', {
129
133
  target: _this.rootEl,
@@ -1382,12 +1386,30 @@ var GraphModel = /** @class */ (function () {
1382
1386
  * 重新设置画布的宽高
1383
1387
  */
1384
1388
  GraphModel.prototype.resize = function (width, height) {
1385
- this.width = width !== null && width !== void 0 ? width : this.rootEl.getBoundingClientRect().width;
1386
- this.isContainerWidth = isNil(width);
1387
- this.height = height !== null && height !== void 0 ? height : this.rootEl.getBoundingClientRect().height;
1388
- this.isContainerHeight = isNil(height);
1389
- if (!this.width || !this.height) {
1390
- console.warn('渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675');
1389
+ // 检查当前实例是否已被销毁或rootEl不存在
1390
+ if (!this.rootEl)
1391
+ return;
1392
+ // 检查元素是否仍在DOM中
1393
+ var isElementInDOM = document.body.contains(this.rootEl);
1394
+ if (!isElementInDOM)
1395
+ return;
1396
+ // 检查元素是否可见
1397
+ var isVisible = this.rootEl.offsetParent !== null;
1398
+ if (!isVisible)
1399
+ return;
1400
+ try {
1401
+ this.width = width !== null && width !== void 0 ? width : this.rootEl.getBoundingClientRect().width;
1402
+ this.isContainerWidth = isNil(width);
1403
+ this.height = height !== null && height !== void 0 ? height : this.rootEl.getBoundingClientRect().height;
1404
+ this.isContainerHeight = isNil(height);
1405
+ // 只有在元素可见且应该有宽高的情况下才显示警告
1406
+ if (isVisible && (!this.width || !this.height)) {
1407
+ console.warn('渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675');
1408
+ }
1409
+ }
1410
+ catch (error) {
1411
+ // 捕获可能的DOM操作错误
1412
+ console.warn('获取画布宽高时发生错误:', error);
1391
1413
  }
1392
1414
  };
1393
1415
  /**
@@ -19,6 +19,7 @@ export declare abstract class BaseNode<P extends IProps = IProps> extends Compon
19
19
  mouseUpDrag?: boolean;
20
20
  startTime?: number;
21
21
  modelDisposer: IReactionDisposer;
22
+ mouseDownPosition?: LogicFlow.Position;
22
23
  constructor(props: IProps);
23
24
  componentWillUnmount(): void;
24
25
  componentDidMount(): void;
@@ -54,7 +54,7 @@ var __read = (this && this.__read) || function (o, n) {
54
54
  import { jsx as _jsx, jsxs as _jsxs } from "preact/jsx-runtime";
55
55
  import { Component } from 'preact/compat';
56
56
  import { reaction } from 'mobx';
57
- import { map } from 'lodash-es';
57
+ import { map, isNil, isFunction } from 'lodash-es';
58
58
  import Anchor from '../Anchor';
59
59
  import { BaseText } from '../text';
60
60
  import { ElementState, EventType, TextMode } from '../../constant';
@@ -88,11 +88,15 @@ var BaseNode = /** @class */ (function (_super) {
88
88
  var _g = graphModel.editConfigModel, stopMoveGraph = _g.stopMoveGraph, autoExpand = _g.autoExpand, snapGrid = _g.snapGrid, transformModel = graphModel.transformModel, selectNodes = graphModel.selectNodes, width = graphModel.width, height = graphModel.height, gridSize = graphModel.gridSize;
89
89
  model.isDragging = true;
90
90
  var _h = event, clientX = _h.clientX, clientY = _h.clientY;
91
- var _j = graphModel.getPointByClient({
91
+ var _j = _this.mouseDownPosition, mouseDownX = _j.x, mouseDownY = _j.y;
92
+ if (clientX - mouseDownX > gridSize || clientY - mouseDownY > gridSize) {
93
+ model.isDragging = true;
94
+ }
95
+ var _k = graphModel.getPointByClient({
92
96
  x: clientX,
93
97
  y: clientY,
94
- }).canvasOverlayPosition, x = _j.x, y = _j.y;
95
- var _k = __read(transformModel.CanvasPointToHtmlPoint([x, y]), 2), x1 = _k[0], y1 = _k[1];
98
+ }).canvasOverlayPosition, x = _k.x, y = _k.y;
99
+ var _l = __read(transformModel.CanvasPointToHtmlPoint([x, y]), 2), x1 = _l[0], y1 = _l[1];
96
100
  // 1. 考虑画布被缩放
97
101
  // 2. 考虑鼠标位置不再节点中心
98
102
  x = x + ((_c = (_b = _this.moveOffset) === null || _b === void 0 ? void 0 : _b.dx) !== null && _c !== void 0 ? _c : 0);
@@ -110,14 +114,14 @@ var BaseNode = /** @class */ (function (_super) {
110
114
  return;
111
115
  }
112
116
  // 取节点左上角和右下角,计算节点移动是否超出范围
113
- var _l = __read(transformModel.CanvasPointToHtmlPoint([
117
+ var _m = __read(transformModel.CanvasPointToHtmlPoint([
114
118
  x - model.width / 2,
115
119
  y - model.height / 2,
116
- ]), 2), leftTopX = _l[0], leftTopY = _l[1];
117
- var _m = __read(transformModel.CanvasPointToHtmlPoint([
120
+ ]), 2), leftTopX = _m[0], leftTopY = _m[1];
121
+ var _o = __read(transformModel.CanvasPointToHtmlPoint([
118
122
  x + model.width / 2,
119
123
  y + model.height / 2,
120
- ]), 2), rightBottomX = _m[0], rightBottomY = _m[1];
124
+ ]), 2), rightBottomX = _o[0], rightBottomY = _o[1];
121
125
  var size = Math.max(gridSize, 20);
122
126
  var nearBoundary = [];
123
127
  if (leftTopX < 0) {
@@ -180,7 +184,7 @@ var BaseNode = /** @class */ (function (_super) {
180
184
  var _a = _this.props, model = _a.model, graphModel = _a.graphModel;
181
185
  // 这里会有一种极端情况:当网格大小是1或者关闭网格吸附时,用触摸板点击节点会触发拖拽事件导致节点无法选中
182
186
  // 当触摸板点击节点时,为了防止误触发拖拽导致节点无法选中,允许在非拖拽状态且时间间隔小于100ms时触发点击事件
183
- if (!isDragging && timeInterval > 100)
187
+ if (!isDragging && timeInterval > 300)
184
188
  return;
185
189
  if (!isDragging) {
186
190
  _this.onDragEnd();
@@ -235,6 +239,14 @@ var BaseNode = /** @class */ (function (_super) {
235
239
  else {
236
240
  graphModel.eventCenter.emit(EventType.ELEMENT_CLICK, eventOptions);
237
241
  graphModel.eventCenter.emit(EventType.NODE_CLICK, eventOptions);
242
+ // 复制粘贴后会出现点击节点时,节点会失去焦点的问题,这里手动让节点获焦以解决这个问题
243
+ var el_1 = e.currentTarget;
244
+ var rAF = !isNil(window) && isFunction(window.requestAnimationFrame)
245
+ ? window.requestAnimationFrame.bind(window)
246
+ : function (fn) { return setTimeout(fn, 0); };
247
+ rAF(function () {
248
+ el_1.focus();
249
+ });
238
250
  }
239
251
  };
240
252
  _this.handleContextMenu = function (ev) {
@@ -263,6 +275,7 @@ var BaseNode = /** @class */ (function (_super) {
263
275
  };
264
276
  _this.handleMouseDown = function (ev) {
265
277
  var _a = _this.props, model = _a.model, graphModel = _a.graphModel;
278
+ _this.mouseDownPosition = { x: ev.clientX, y: ev.clientY };
266
279
  _this.startTime = new Date().getTime();
267
280
  var editConfigModel = graphModel.editConfigModel;
268
281
  if (editConfigModel.adjustNodePosition && model.draggable) {
@@ -130,6 +130,10 @@ var GraphModel = /** @class */ (function () {
130
130
  for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) {
131
131
  var entry = entries_1_1.value;
132
132
  if (entry.target === _this.rootEl) {
133
+ // 检查元素是否仍在DOM中
134
+ var isElementInDOM = document.body.contains(_this.rootEl);
135
+ if (!isElementInDOM)
136
+ return;
133
137
  _this.resize();
134
138
  _this.eventCenter.emit('graph:resize', {
135
139
  target: _this.rootEl,
@@ -1388,12 +1392,30 @@ var GraphModel = /** @class */ (function () {
1388
1392
  * 重新设置画布的宽高
1389
1393
  */
1390
1394
  GraphModel.prototype.resize = function (width, height) {
1391
- this.width = width !== null && width !== void 0 ? width : this.rootEl.getBoundingClientRect().width;
1392
- this.isContainerWidth = (0, lodash_es_1.isNil)(width);
1393
- this.height = height !== null && height !== void 0 ? height : this.rootEl.getBoundingClientRect().height;
1394
- this.isContainerHeight = (0, lodash_es_1.isNil)(height);
1395
- if (!this.width || !this.height) {
1396
- console.warn('渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675');
1395
+ // 检查当前实例是否已被销毁或rootEl不存在
1396
+ if (!this.rootEl)
1397
+ return;
1398
+ // 检查元素是否仍在DOM中
1399
+ var isElementInDOM = document.body.contains(this.rootEl);
1400
+ if (!isElementInDOM)
1401
+ return;
1402
+ // 检查元素是否可见
1403
+ var isVisible = this.rootEl.offsetParent !== null;
1404
+ if (!isVisible)
1405
+ return;
1406
+ try {
1407
+ this.width = width !== null && width !== void 0 ? width : this.rootEl.getBoundingClientRect().width;
1408
+ this.isContainerWidth = (0, lodash_es_1.isNil)(width);
1409
+ this.height = height !== null && height !== void 0 ? height : this.rootEl.getBoundingClientRect().height;
1410
+ this.isContainerHeight = (0, lodash_es_1.isNil)(height);
1411
+ // 只有在元素可见且应该有宽高的情况下才显示警告
1412
+ if (isVisible && (!this.width || !this.height)) {
1413
+ console.warn('渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675');
1414
+ }
1415
+ }
1416
+ catch (error) {
1417
+ // 捕获可能的DOM操作错误
1418
+ console.warn('获取画布宽高时发生错误:', error);
1397
1419
  }
1398
1420
  };
1399
1421
  /**
@@ -19,6 +19,7 @@ export declare abstract class BaseNode<P extends IProps = IProps> extends Compon
19
19
  mouseUpDrag?: boolean;
20
20
  startTime?: number;
21
21
  modelDisposer: IReactionDisposer;
22
+ mouseDownPosition?: LogicFlow.Position;
22
23
  constructor(props: IProps);
23
24
  componentWillUnmount(): void;
24
25
  componentDidMount(): void;
@@ -92,11 +92,15 @@ var BaseNode = /** @class */ (function (_super) {
92
92
  var _g = graphModel.editConfigModel, stopMoveGraph = _g.stopMoveGraph, autoExpand = _g.autoExpand, snapGrid = _g.snapGrid, transformModel = graphModel.transformModel, selectNodes = graphModel.selectNodes, width = graphModel.width, height = graphModel.height, gridSize = graphModel.gridSize;
93
93
  model.isDragging = true;
94
94
  var _h = event, clientX = _h.clientX, clientY = _h.clientY;
95
- var _j = graphModel.getPointByClient({
95
+ var _j = _this.mouseDownPosition, mouseDownX = _j.x, mouseDownY = _j.y;
96
+ if (clientX - mouseDownX > gridSize || clientY - mouseDownY > gridSize) {
97
+ model.isDragging = true;
98
+ }
99
+ var _k = graphModel.getPointByClient({
96
100
  x: clientX,
97
101
  y: clientY,
98
- }).canvasOverlayPosition, x = _j.x, y = _j.y;
99
- var _k = __read(transformModel.CanvasPointToHtmlPoint([x, y]), 2), x1 = _k[0], y1 = _k[1];
102
+ }).canvasOverlayPosition, x = _k.x, y = _k.y;
103
+ var _l = __read(transformModel.CanvasPointToHtmlPoint([x, y]), 2), x1 = _l[0], y1 = _l[1];
100
104
  // 1. 考虑画布被缩放
101
105
  // 2. 考虑鼠标位置不再节点中心
102
106
  x = x + ((_c = (_b = _this.moveOffset) === null || _b === void 0 ? void 0 : _b.dx) !== null && _c !== void 0 ? _c : 0);
@@ -114,14 +118,14 @@ var BaseNode = /** @class */ (function (_super) {
114
118
  return;
115
119
  }
116
120
  // 取节点左上角和右下角,计算节点移动是否超出范围
117
- var _l = __read(transformModel.CanvasPointToHtmlPoint([
121
+ var _m = __read(transformModel.CanvasPointToHtmlPoint([
118
122
  x - model.width / 2,
119
123
  y - model.height / 2,
120
- ]), 2), leftTopX = _l[0], leftTopY = _l[1];
121
- var _m = __read(transformModel.CanvasPointToHtmlPoint([
124
+ ]), 2), leftTopX = _m[0], leftTopY = _m[1];
125
+ var _o = __read(transformModel.CanvasPointToHtmlPoint([
122
126
  x + model.width / 2,
123
127
  y + model.height / 2,
124
- ]), 2), rightBottomX = _m[0], rightBottomY = _m[1];
128
+ ]), 2), rightBottomX = _o[0], rightBottomY = _o[1];
125
129
  var size = Math.max(gridSize, 20);
126
130
  var nearBoundary = [];
127
131
  if (leftTopX < 0) {
@@ -184,7 +188,7 @@ var BaseNode = /** @class */ (function (_super) {
184
188
  var _a = _this.props, model = _a.model, graphModel = _a.graphModel;
185
189
  // 这里会有一种极端情况:当网格大小是1或者关闭网格吸附时,用触摸板点击节点会触发拖拽事件导致节点无法选中
186
190
  // 当触摸板点击节点时,为了防止误触发拖拽导致节点无法选中,允许在非拖拽状态且时间间隔小于100ms时触发点击事件
187
- if (!isDragging && timeInterval > 100)
191
+ if (!isDragging && timeInterval > 300)
188
192
  return;
189
193
  if (!isDragging) {
190
194
  _this.onDragEnd();
@@ -239,6 +243,14 @@ var BaseNode = /** @class */ (function (_super) {
239
243
  else {
240
244
  graphModel.eventCenter.emit(constant_1.EventType.ELEMENT_CLICK, eventOptions);
241
245
  graphModel.eventCenter.emit(constant_1.EventType.NODE_CLICK, eventOptions);
246
+ // 复制粘贴后会出现点击节点时,节点会失去焦点的问题,这里手动让节点获焦以解决这个问题
247
+ var el_1 = e.currentTarget;
248
+ var rAF = !(0, lodash_es_1.isNil)(window) && (0, lodash_es_1.isFunction)(window.requestAnimationFrame)
249
+ ? window.requestAnimationFrame.bind(window)
250
+ : function (fn) { return setTimeout(fn, 0); };
251
+ rAF(function () {
252
+ el_1.focus();
253
+ });
242
254
  }
243
255
  };
244
256
  _this.handleContextMenu = function (ev) {
@@ -267,6 +279,7 @@ var BaseNode = /** @class */ (function (_super) {
267
279
  };
268
280
  _this.handleMouseDown = function (ev) {
269
281
  var _a = _this.props, model = _a.model, graphModel = _a.graphModel;
282
+ _this.mouseDownPosition = { x: ev.clientX, y: ev.clientY };
270
283
  _this.startTime = new Date().getTime();
271
284
  var editConfigModel = graphModel.editConfigModel;
272
285
  if (editConfigModel.adjustNodePosition && model.draggable) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logicflow/core",
3
- "version": "2.1.4",
3
+ "version": "2.1.6",
4
4
  "description": "LogicFlow, help you quickly create flowcharts",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",
@@ -192,6 +192,9 @@ export class GraphModel {
192
192
  ((entries) => {
193
193
  for (const entry of entries) {
194
194
  if (entry.target === this.rootEl) {
195
+ // 检查元素是否仍在DOM中
196
+ const isElementInDOM = document.body.contains(this.rootEl)
197
+ if (!isElementInDOM) return
195
198
  this.resize()
196
199
  this.eventCenter.emit('graph:resize', {
197
200
  target: this.rootEl,
@@ -1584,15 +1587,32 @@ export class GraphModel {
1584
1587
  * 重新设置画布的宽高
1585
1588
  */
1586
1589
  @action resize(width?: number, height?: number): void {
1587
- this.width = width ?? this.rootEl.getBoundingClientRect().width
1588
- this.isContainerWidth = isNil(width)
1589
- this.height = height ?? this.rootEl.getBoundingClientRect().height
1590
- this.isContainerHeight = isNil(height)
1591
-
1592
- if (!this.width || !this.height) {
1593
- console.warn(
1594
- '渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675',
1595
- )
1590
+ // 检查当前实例是否已被销毁或rootEl不存在
1591
+ if (!this.rootEl) return
1592
+
1593
+ // 检查元素是否仍在DOM中
1594
+ const isElementInDOM = document.body.contains(this.rootEl)
1595
+ if (!isElementInDOM) return
1596
+
1597
+ // 检查元素是否可见
1598
+ const isVisible = this.rootEl.offsetParent !== null
1599
+ if (!isVisible) return
1600
+
1601
+ try {
1602
+ this.width = width ?? this.rootEl.getBoundingClientRect().width
1603
+ this.isContainerWidth = isNil(width)
1604
+ this.height = height ?? this.rootEl.getBoundingClientRect().height
1605
+ this.isContainerHeight = isNil(height)
1606
+
1607
+ // 只有在元素可见且应该有宽高的情况下才显示警告
1608
+ if (isVisible && (!this.width || !this.height)) {
1609
+ console.warn(
1610
+ '渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675',
1611
+ )
1612
+ }
1613
+ } catch (error) {
1614
+ // 捕获可能的DOM操作错误
1615
+ console.warn('获取画布宽高时发生错误:', error)
1596
1616
  }
1597
1617
  }
1598
1618
 
@@ -1,6 +1,6 @@
1
1
  import { createElement as h, Component } from 'preact/compat'
2
2
  import { reaction, IReactionDisposer } from 'mobx'
3
- import { map } from 'lodash-es'
3
+ import { map, isNil, isFunction } from 'lodash-es'
4
4
  import Anchor from '../Anchor'
5
5
  import { BaseText } from '../text'
6
6
  import LogicFlow from '../../LogicFlow'
@@ -42,6 +42,7 @@ export abstract class BaseNode<P extends IProps = IProps> extends Component<
42
42
  mouseUpDrag?: boolean
43
43
  startTime?: number
44
44
  modelDisposer: IReactionDisposer
45
+ mouseDownPosition?: LogicFlow.Position
45
46
 
46
47
  constructor(props: IProps) {
47
48
  super()
@@ -88,9 +89,9 @@ export abstract class BaseNode<P extends IProps = IProps> extends Component<
88
89
  }
89
90
  }
90
91
 
91
- componentDidMount() { }
92
+ componentDidMount() {}
92
93
 
93
- componentDidUpdate() { }
94
+ componentDidUpdate() {}
94
95
 
95
96
  abstract getShape(): h.JSX.Element | null
96
97
 
@@ -249,6 +250,10 @@ export abstract class BaseNode<P extends IProps = IProps> extends Component<
249
250
  } = graphModel
250
251
  model.isDragging = true
251
252
  const { clientX, clientY } = event!
253
+ const { x: mouseDownX, y: mouseDownY } = this.mouseDownPosition!
254
+ if (clientX - mouseDownX > gridSize || clientY - mouseDownY > gridSize) {
255
+ model.isDragging = true
256
+ }
252
257
  let {
253
258
  canvasOverlayPosition: { x, y },
254
259
  } = graphModel.getPointByClient({
@@ -342,7 +347,7 @@ export abstract class BaseNode<P extends IProps = IProps> extends Component<
342
347
  const { model, graphModel } = this.props
343
348
  // 这里会有一种极端情况:当网格大小是1或者关闭网格吸附时,用触摸板点击节点会触发拖拽事件导致节点无法选中
344
349
  // 当触摸板点击节点时,为了防止误触发拖拽导致节点无法选中,允许在非拖拽状态且时间间隔小于100ms时触发点击事件
345
- if (!isDragging && timeInterval > 100) return
350
+ if (!isDragging && timeInterval > 300) return
346
351
  if (!isDragging) {
347
352
  this.onDragEnd()
348
353
  this.handleMouseUp()
@@ -398,6 +403,15 @@ export abstract class BaseNode<P extends IProps = IProps> extends Component<
398
403
  } else {
399
404
  graphModel.eventCenter.emit(EventType.ELEMENT_CLICK, eventOptions)
400
405
  graphModel.eventCenter.emit(EventType.NODE_CLICK, eventOptions)
406
+ // 复制粘贴后会出现点击节点时,节点会失去焦点的问题,这里手动让节点获焦以解决这个问题
407
+ const el = e.currentTarget as HTMLElement
408
+ const rAF =
409
+ !isNil(window) && isFunction(window.requestAnimationFrame)
410
+ ? window.requestAnimationFrame.bind(window)
411
+ : (fn: () => void) => setTimeout(fn, 0)
412
+ rAF(() => {
413
+ el.focus()
414
+ })
401
415
  }
402
416
  }
403
417
 
@@ -433,6 +447,7 @@ export abstract class BaseNode<P extends IProps = IProps> extends Component<
433
447
 
434
448
  handleMouseDown = (ev: MouseEvent) => {
435
449
  const { model, graphModel } = this.props
450
+ this.mouseDownPosition = { x: ev.clientX, y: ev.clientY }
436
451
  this.startTime = new Date().getTime()
437
452
  const { editConfigModel } = graphModel
438
453
  if (editConfigModel.adjustNodePosition && model.draggable) {