@logicflow/core 2.2.0-alpha.3 → 2.2.0-alpha.5

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.
package/es/LogicFlow.js CHANGED
@@ -1217,7 +1217,6 @@ var LogicFlow = /** @class */ (function () {
1217
1217
  this.graphModel.destroy();
1218
1218
  this.tool.destroy();
1219
1219
  this.history.destroy();
1220
- clearThemeMode();
1221
1220
  for (var extensionName in this.extension) {
1222
1221
  var extensionInstance = this.extension[extensionName];
1223
1222
  if ('destroy' in extensionInstance) {
@@ -131,6 +131,10 @@ var GraphModel = /** @class */ (function () {
131
131
  for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) {
132
132
  var entry = entries_1_1.value;
133
133
  if (entry.target === _this.rootEl) {
134
+ // 检查元素是否仍在DOM中
135
+ var isElementInDOM = document.body.contains(_this.rootEl);
136
+ if (!isElementInDOM)
137
+ return;
134
138
  _this.resize();
135
139
  _this.eventCenter.emit('graph:resize', {
136
140
  target: _this.rootEl,
@@ -499,7 +503,7 @@ var GraphModel = /** @class */ (function () {
499
503
  edgeDragging = true;
500
504
  break;
501
505
  }
502
- else {
506
+ if (!edgeMode.virtual) {
503
507
  edges.push(edgeMode.getHistoryData());
504
508
  }
505
509
  }
@@ -1392,12 +1396,30 @@ var GraphModel = /** @class */ (function () {
1392
1396
  * 重新设置画布的宽高
1393
1397
  */
1394
1398
  GraphModel.prototype.resize = function (width, height) {
1395
- this.width = width !== null && width !== void 0 ? width : this.rootEl.getBoundingClientRect().width;
1396
- this.isContainerWidth = isNil(width);
1397
- this.height = height !== null && height !== void 0 ? height : this.rootEl.getBoundingClientRect().height;
1398
- this.isContainerHeight = isNil(height);
1399
- if (!this.width || !this.height) {
1400
- console.warn('渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675');
1399
+ // 检查当前实例是否已被销毁或rootEl不存在
1400
+ if (!this.rootEl)
1401
+ return;
1402
+ // 检查元素是否仍在DOM中
1403
+ var isElementInDOM = document.body.contains(this.rootEl);
1404
+ if (!isElementInDOM)
1405
+ return;
1406
+ // 检查元素是否可见
1407
+ var isVisible = this.rootEl.offsetParent !== null;
1408
+ if (!isVisible)
1409
+ return;
1410
+ try {
1411
+ this.width = width !== null && width !== void 0 ? width : this.rootEl.getBoundingClientRect().width;
1412
+ this.isContainerWidth = isNil(width);
1413
+ this.height = height !== null && height !== void 0 ? height : this.rootEl.getBoundingClientRect().height;
1414
+ this.isContainerHeight = isNil(height);
1415
+ // 只有在元素可见且应该有宽高的情况下才显示警告
1416
+ if (isVisible && (!this.width || !this.height)) {
1417
+ console.warn('渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675');
1418
+ }
1419
+ }
1420
+ catch (error) {
1421
+ // 捕获可能的DOM操作错误
1422
+ console.warn('获取画布宽高时发生错误:', error);
1401
1423
  }
1402
1424
  };
1403
1425
  /**
@@ -55,6 +55,10 @@ var MultipleSelect = /** @class */ (function (_super) {
55
55
  function MultipleSelect(props) {
56
56
  var _this = _super.call(this, props) || this;
57
57
  _this.handleMouseDown = function (ev) {
58
+ // 多选区域的拖拽步长随缩放变化
59
+ var _a = _this.props, gridSize = _a.graphModel.gridSize, lf = _a.lf;
60
+ var SCALE_X = lf.getTransform().SCALE_X;
61
+ _this.stepDrag.setStep(gridSize * SCALE_X);
58
62
  _this.stepDrag.handleMouseDown(ev);
59
63
  };
60
64
  // 使多选区域的滚轮事件可以触发画布的滚轮事件
@@ -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, isFunction, isNil } from 'lodash-es';
58
58
  import Anchor from '../Anchor';
59
59
  import { BaseText } from '../text';
60
60
  import { ElementState, EventType, TextMode } from '../../constant';
@@ -242,6 +242,14 @@ var BaseNode = /** @class */ (function (_super) {
242
242
  else {
243
243
  graphModel.eventCenter.emit(EventType.ELEMENT_CLICK, eventOptions);
244
244
  graphModel.eventCenter.emit(EventType.NODE_CLICK, eventOptions);
245
+ // 复制粘贴后会出现点击节点时,节点会失去焦点的问题,这里手动让节点获焦以解决这个问题
246
+ var el_1 = e.currentTarget;
247
+ var rAF = !isNil(window) && isFunction(window.requestAnimationFrame)
248
+ ? window.requestAnimationFrame.bind(window)
249
+ : function (fn) { return setTimeout(fn, 0); };
250
+ rAF(function () {
251
+ el_1.focus();
252
+ });
245
253
  }
246
254
  };
247
255
  _this.handleContextMenu = function (ev) {
@@ -97,6 +97,9 @@ var CanvasOverlay = /** @class */ (function (_super) {
97
97
  if (selectElements.size > 0) {
98
98
  graphModel.clearSelectElements();
99
99
  }
100
+ // 如果是拖拽状态,不触发点击事件
101
+ if (_this.state.isDragging)
102
+ return;
100
103
  graphModel.eventCenter.emit(EventType.BLANK_CLICK, { e: ev });
101
104
  }
102
105
  };
@@ -158,8 +161,6 @@ var CanvasOverlay = /** @class */ (function (_super) {
158
161
  else {
159
162
  eventCenter.emit(EventType.BLANK_MOUSEDOWN, { e: ev });
160
163
  }
161
- // 为了处理画布移动的时候,编辑和菜单仍然存在的问题。
162
- _this.clickHandler(ev);
163
164
  }
164
165
  };
165
166
  _this.pointerMoveHandler = function (ev) {
@@ -217,6 +218,8 @@ var CanvasOverlay = /** @class */ (function (_super) {
217
218
  var editConfigModel = _this.props.graphModel.editConfigModel;
218
219
  // 标记退出捏合,框选等交互可恢复
219
220
  editConfigModel.updateEditConfig({ isPinching: false });
221
+ // 为了处理画布移动的时候,编辑和菜单仍然存在的问题。
222
+ _this.clickHandler(ev);
220
223
  }
221
224
  };
222
225
  var _a = props.graphModel, gridSize = _a.gridSize, eventCenter = _a.eventCenter;
@@ -57,7 +57,7 @@ var OutlineOverlay = /** @class */ (function (_super) {
57
57
  var isHovered = element.isHovered, isSelected = element.isSelected, x = element.x, y = element.y, width = element.width, height = element.height;
58
58
  if ((nodeSelectedOutline && isSelected) ||
59
59
  (hoverOutline && isHovered)) {
60
- var style_1 = element.getOutlineStyle();
60
+ var style_1 = element.getOutlineStyle() || {};
61
61
  var attributes_1 = {};
62
62
  Object.keys(style_1).forEach(function (key) {
63
63
  if (key !== 'hover') {
package/lib/LogicFlow.js CHANGED
@@ -1246,7 +1246,6 @@ var LogicFlow = /** @class */ (function () {
1246
1246
  this.graphModel.destroy();
1247
1247
  this.tool.destroy();
1248
1248
  this.history.destroy();
1249
- (0, util_1.clearThemeMode)();
1250
1249
  for (var extensionName in this.extension) {
1251
1250
  var extensionInstance = this.extension[extensionName];
1252
1251
  if ('destroy' in extensionInstance) {
@@ -137,6 +137,10 @@ var GraphModel = /** @class */ (function () {
137
137
  for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) {
138
138
  var entry = entries_1_1.value;
139
139
  if (entry.target === _this.rootEl) {
140
+ // 检查元素是否仍在DOM中
141
+ var isElementInDOM = document.body.contains(_this.rootEl);
142
+ if (!isElementInDOM)
143
+ return;
140
144
  _this.resize();
141
145
  _this.eventCenter.emit('graph:resize', {
142
146
  target: _this.rootEl,
@@ -505,7 +509,7 @@ var GraphModel = /** @class */ (function () {
505
509
  edgeDragging = true;
506
510
  break;
507
511
  }
508
- else {
512
+ if (!edgeMode.virtual) {
509
513
  edges.push(edgeMode.getHistoryData());
510
514
  }
511
515
  }
@@ -1398,12 +1402,30 @@ var GraphModel = /** @class */ (function () {
1398
1402
  * 重新设置画布的宽高
1399
1403
  */
1400
1404
  GraphModel.prototype.resize = function (width, height) {
1401
- this.width = width !== null && width !== void 0 ? width : this.rootEl.getBoundingClientRect().width;
1402
- this.isContainerWidth = (0, lodash_es_1.isNil)(width);
1403
- this.height = height !== null && height !== void 0 ? height : this.rootEl.getBoundingClientRect().height;
1404
- this.isContainerHeight = (0, lodash_es_1.isNil)(height);
1405
- if (!this.width || !this.height) {
1406
- console.warn('渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675');
1405
+ // 检查当前实例是否已被销毁或rootEl不存在
1406
+ if (!this.rootEl)
1407
+ return;
1408
+ // 检查元素是否仍在DOM中
1409
+ var isElementInDOM = document.body.contains(this.rootEl);
1410
+ if (!isElementInDOM)
1411
+ return;
1412
+ // 检查元素是否可见
1413
+ var isVisible = this.rootEl.offsetParent !== null;
1414
+ if (!isVisible)
1415
+ return;
1416
+ try {
1417
+ this.width = width !== null && width !== void 0 ? width : this.rootEl.getBoundingClientRect().width;
1418
+ this.isContainerWidth = (0, lodash_es_1.isNil)(width);
1419
+ this.height = height !== null && height !== void 0 ? height : this.rootEl.getBoundingClientRect().height;
1420
+ this.isContainerHeight = (0, lodash_es_1.isNil)(height);
1421
+ // 只有在元素可见且应该有宽高的情况下才显示警告
1422
+ if (isVisible && (!this.width || !this.height)) {
1423
+ console.warn('渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675');
1424
+ }
1425
+ }
1426
+ catch (error) {
1427
+ // 捕获可能的DOM操作错误
1428
+ console.warn('获取画布宽高时发生错误:', error);
1407
1429
  }
1408
1430
  };
1409
1431
  /**
@@ -57,6 +57,10 @@ var MultipleSelect = /** @class */ (function (_super) {
57
57
  function MultipleSelect(props) {
58
58
  var _this = _super.call(this, props) || this;
59
59
  _this.handleMouseDown = function (ev) {
60
+ // 多选区域的拖拽步长随缩放变化
61
+ var _a = _this.props, gridSize = _a.graphModel.gridSize, lf = _a.lf;
62
+ var SCALE_X = lf.getTransform().SCALE_X;
63
+ _this.stepDrag.setStep(gridSize * SCALE_X);
60
64
  _this.stepDrag.handleMouseDown(ev);
61
65
  };
62
66
  // 使多选区域的滚轮事件可以触发画布的滚轮事件
@@ -246,6 +246,14 @@ var BaseNode = /** @class */ (function (_super) {
246
246
  else {
247
247
  graphModel.eventCenter.emit(constant_1.EventType.ELEMENT_CLICK, eventOptions);
248
248
  graphModel.eventCenter.emit(constant_1.EventType.NODE_CLICK, eventOptions);
249
+ // 复制粘贴后会出现点击节点时,节点会失去焦点的问题,这里手动让节点获焦以解决这个问题
250
+ var el_1 = e.currentTarget;
251
+ var rAF = !(0, lodash_es_1.isNil)(window) && (0, lodash_es_1.isFunction)(window.requestAnimationFrame)
252
+ ? window.requestAnimationFrame.bind(window)
253
+ : function (fn) { return setTimeout(fn, 0); };
254
+ rAF(function () {
255
+ el_1.focus();
256
+ });
249
257
  }
250
258
  };
251
259
  _this.handleContextMenu = function (ev) {
@@ -100,6 +100,9 @@ var CanvasOverlay = /** @class */ (function (_super) {
100
100
  if (selectElements.size > 0) {
101
101
  graphModel.clearSelectElements();
102
102
  }
103
+ // 如果是拖拽状态,不触发点击事件
104
+ if (_this.state.isDragging)
105
+ return;
103
106
  graphModel.eventCenter.emit(constant_1.EventType.BLANK_CLICK, { e: ev });
104
107
  }
105
108
  };
@@ -161,8 +164,6 @@ var CanvasOverlay = /** @class */ (function (_super) {
161
164
  else {
162
165
  eventCenter.emit(constant_1.EventType.BLANK_MOUSEDOWN, { e: ev });
163
166
  }
164
- // 为了处理画布移动的时候,编辑和菜单仍然存在的问题。
165
- _this.clickHandler(ev);
166
167
  }
167
168
  };
168
169
  _this.pointerMoveHandler = function (ev) {
@@ -220,6 +221,8 @@ var CanvasOverlay = /** @class */ (function (_super) {
220
221
  var editConfigModel = _this.props.graphModel.editConfigModel;
221
222
  // 标记退出捏合,框选等交互可恢复
222
223
  editConfigModel.updateEditConfig({ isPinching: false });
224
+ // 为了处理画布移动的时候,编辑和菜单仍然存在的问题。
225
+ _this.clickHandler(ev);
223
226
  }
224
227
  };
225
228
  var _a = props.graphModel, gridSize = _a.gridSize, eventCenter = _a.eventCenter;
@@ -60,7 +60,7 @@ var OutlineOverlay = /** @class */ (function (_super) {
60
60
  var isHovered = element.isHovered, isSelected = element.isSelected, x = element.x, y = element.y, width = element.width, height = element.height;
61
61
  if ((nodeSelectedOutline && isSelected) ||
62
62
  (hoverOutline && isHovered)) {
63
- var style_1 = element.getOutlineStyle();
63
+ var style_1 = element.getOutlineStyle() || {};
64
64
  var attributes_1 = {};
65
65
  Object.keys(style_1).forEach(function (key) {
66
66
  if (key !== 'hover') {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logicflow/core",
3
- "version": "2.2.0-alpha.3",
3
+ "version": "2.2.0-alpha.5",
4
4
  "description": "LogicFlow, help you quickly create flowcharts",
5
5
  "main": "lib/index.js",
6
6
  "module": "es/index.js",
package/src/LogicFlow.tsx CHANGED
@@ -1445,7 +1445,6 @@ export class LogicFlow {
1445
1445
  this.graphModel.destroy()
1446
1446
  this.tool.destroy()
1447
1447
  this.history.destroy()
1448
- clearThemeMode()
1449
1448
  for (const extensionName in this.extension) {
1450
1449
  const extensionInstance = this.extension[extensionName]
1451
1450
  if ('destroy' in extensionInstance) {
@@ -23,10 +23,10 @@ export interface IEditConfigType {
23
23
  * - [number, number, number, number]:[minX, minY, maxX, maxY] 画布可拖动范围
24
24
  */
25
25
  stopMoveGraph:
26
- | boolean
27
- | 'vertical'
28
- | 'horizontal'
29
- | [number, number, number, number]
26
+ | boolean
27
+ | 'vertical'
28
+ | 'horizontal'
29
+ | [number, number, number, number]
30
30
  /**
31
31
  * 允许调整边
32
32
  */
@@ -206,6 +206,9 @@ export class GraphModel {
206
206
  ((entries) => {
207
207
  for (const entry of entries) {
208
208
  if (entry.target === this.rootEl) {
209
+ // 检查元素是否仍在DOM中
210
+ const isElementInDOM = document.body.contains(this.rootEl)
211
+ if (!isElementInDOM) return
209
212
  this.resize()
210
213
  this.eventCenter.emit('graph:resize', {
211
214
  target: this.rootEl,
@@ -573,7 +576,8 @@ export class GraphModel {
573
576
  if (edgeMode.isDragging) {
574
577
  edgeDragging = true
575
578
  break
576
- } else {
579
+ }
580
+ if (!edgeMode.virtual) {
577
581
  edges.push(edgeMode.getHistoryData())
578
582
  }
579
583
  }
@@ -1601,15 +1605,32 @@ export class GraphModel {
1601
1605
  * 重新设置画布的宽高
1602
1606
  */
1603
1607
  @action resize(width?: number, height?: number): void {
1604
- this.width = width ?? this.rootEl.getBoundingClientRect().width
1605
- this.isContainerWidth = isNil(width)
1606
- this.height = height ?? this.rootEl.getBoundingClientRect().height
1607
- this.isContainerHeight = isNil(height)
1608
-
1609
- if (!this.width || !this.height) {
1610
- console.warn(
1611
- '渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675',
1612
- )
1608
+ // 检查当前实例是否已被销毁或rootEl不存在
1609
+ if (!this.rootEl) return
1610
+
1611
+ // 检查元素是否仍在DOM中
1612
+ const isElementInDOM = document.body.contains(this.rootEl)
1613
+ if (!isElementInDOM) return
1614
+
1615
+ // 检查元素是否可见
1616
+ const isVisible = this.rootEl.offsetParent !== null
1617
+ if (!isVisible) return
1618
+
1619
+ try {
1620
+ this.width = width ?? this.rootEl.getBoundingClientRect().width
1621
+ this.isContainerWidth = isNil(width)
1622
+ this.height = height ?? this.rootEl.getBoundingClientRect().height
1623
+ this.isContainerHeight = isNil(height)
1624
+
1625
+ // 只有在元素可见且应该有宽高的情况下才显示警告
1626
+ if (isVisible && (!this.width || !this.height)) {
1627
+ console.warn(
1628
+ '渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675',
1629
+ )
1630
+ }
1631
+ } catch (error) {
1632
+ // 捕获可能的DOM操作错误
1633
+ console.warn('获取画布宽高时发生错误:', error)
1613
1634
  }
1614
1635
  }
1615
1636
 
@@ -57,8 +57,7 @@ export interface IBaseNodeModel<P extends PropertiesType>
57
57
  }
58
58
 
59
59
  export class BaseNodeModel<P extends PropertiesType = PropertiesType>
60
- implements IBaseNodeModel<P>
61
- {
60
+ implements IBaseNodeModel<P> {
62
61
  readonly BaseType = ElementType.NODE
63
62
  static BaseType: ElementType = ElementType.NODE
64
63
 
@@ -226,7 +225,7 @@ export class BaseNodeModel<P extends PropertiesType = PropertiesType>
226
225
  *
227
226
  * @overridable 支持重写
228
227
  */
229
- public setAttributes() {}
228
+ public setAttributes() { }
230
229
 
231
230
  /**
232
231
  * @overridable 支持重写,自定义此类型节点默认生成方式
@@ -307,7 +306,7 @@ export class BaseNodeModel<P extends PropertiesType = PropertiesType>
307
306
  }
308
307
 
309
308
  // TODO: 等比例缩放
310
- proportionalResize() {}
309
+ proportionalResize() { }
311
310
 
312
311
  /**
313
312
  * 获取被保存时返回的数据
@@ -27,6 +27,13 @@ export default class MultipleSelect extends Component<IToolProps> {
27
27
  }
28
28
 
29
29
  handleMouseDown = (ev: PointerEvent) => {
30
+ // 多选区域的拖拽步长随缩放变化
31
+ const {
32
+ graphModel: { gridSize },
33
+ lf,
34
+ } = this.props
35
+ const { SCALE_X } = lf.getTransform()
36
+ this.stepDrag.setStep(gridSize * SCALE_X)
30
37
  this.stepDrag.handleMouseDown(ev)
31
38
  }
32
39
  // 使多选区域的滚轮事件可以触发画布的滚轮事件
@@ -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, isFunction, isNil } from 'lodash-es'
4
4
  import Anchor from '../Anchor'
5
5
  import { BaseText } from '../text'
6
6
  import LogicFlow from '../../LogicFlow'
@@ -407,6 +407,15 @@ export abstract class BaseNode<P extends IProps = IProps> extends Component<
407
407
  } else {
408
408
  graphModel.eventCenter.emit(EventType.ELEMENT_CLICK, eventOptions)
409
409
  graphModel.eventCenter.emit(EventType.NODE_CLICK, eventOptions)
410
+ // 复制粘贴后会出现点击节点时,节点会失去焦点的问题,这里手动让节点获焦以解决这个问题
411
+ const el = e.currentTarget as HTMLElement
412
+ const rAF =
413
+ !isNil(window) && isFunction(window.requestAnimationFrame)
414
+ ? window.requestAnimationFrame.bind(window)
415
+ : (fn: () => void) => setTimeout(fn, 0)
416
+ rAF(() => {
417
+ el.focus()
418
+ })
410
419
  }
411
420
  }
412
421
 
@@ -115,6 +115,8 @@ export class CanvasOverlay extends Component<IProps, IState> {
115
115
  if (selectElements.size > 0) {
116
116
  graphModel.clearSelectElements()
117
117
  }
118
+ // 如果是拖拽状态,不触发点击事件
119
+ if (this.state.isDragging) return
118
120
  graphModel.eventCenter.emit(EventType.BLANK_CLICK, { e: ev })
119
121
  }
120
122
  }
@@ -184,8 +186,6 @@ export class CanvasOverlay extends Component<IProps, IState> {
184
186
  } else {
185
187
  eventCenter.emit(EventType.BLANK_MOUSEDOWN, { e: ev })
186
188
  }
187
- // 为了处理画布移动的时候,编辑和菜单仍然存在的问题。
188
- this.clickHandler(ev)
189
189
  }
190
190
  }
191
191
  pointerMoveHandler = (ev: PointerEvent) => {
@@ -249,6 +249,8 @@ export class CanvasOverlay extends Component<IProps, IState> {
249
249
  } = this.props
250
250
  // 标记退出捏合,框选等交互可恢复
251
251
  editConfigModel.updateEditConfig({ isPinching: false })
252
+ // 为了处理画布移动的时候,编辑和菜单仍然存在的问题。
253
+ this.clickHandler(ev)
252
254
  }
253
255
  }
254
256
 
@@ -51,7 +51,7 @@ export class OutlineOverlay extends Component<IProps> {
51
51
  (nodeSelectedOutline && isSelected) ||
52
52
  (hoverOutline && isHovered)
53
53
  ) {
54
- const style = element.getOutlineStyle()
54
+ const style = element.getOutlineStyle() || {}
55
55
  let attributes = {}
56
56
  Object.keys(style).forEach((key) => {
57
57
  if (key !== 'hover') {