@logicflow/core 2.1.3 → 2.1.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.
@@ -119,6 +119,7 @@ var GraphModel = /** @class */ (function () {
119
119
  this.edgeType = options.edgeType || 'polyline';
120
120
  this.animation = (0, util_1.setupAnimation)(animation);
121
121
  this.overlapMode = options.overlapMode || constant_1.OverlapMode.DEFAULT;
122
+ this.isMiniMap = options.isMiniMap || false;
122
123
  this.width = (_a = options.width) !== null && _a !== void 0 ? _a : this.rootEl.getBoundingClientRect().width;
123
124
  this.isContainerWidth = (0, lodash_es_1.isNil)(options.width);
124
125
  this.height = (_b = options.height) !== null && _b !== void 0 ? _b : this.rootEl.getBoundingClientRect().height;
@@ -129,6 +130,10 @@ var GraphModel = /** @class */ (function () {
129
130
  for (var entries_1 = __values(entries), entries_1_1 = entries_1.next(); !entries_1_1.done; entries_1_1 = entries_1.next()) {
130
131
  var entry = entries_1_1.value;
131
132
  if (entry.target === _this.rootEl) {
133
+ // 检查元素是否仍在DOM中
134
+ var isElementInDOM = document.body.contains(_this.rootEl);
135
+ if (!isElementInDOM)
136
+ return;
132
137
  _this.resize();
133
138
  _this.eventCenter.emit('graph:resize', {
134
139
  target: _this.rootEl,
@@ -199,7 +204,11 @@ var GraphModel = /** @class */ (function () {
199
204
  * todo: 性能优化
200
205
  */
201
206
  get: function () {
202
- var elements = __spreadArray(__spreadArray([], __read(this.nodes), false), __read(this.edges), false).sort(function (a, b) { return a.zIndex - b.zIndex; });
207
+ var sortElement = function (list) {
208
+ return __spreadArray([], __read(list), false).sort(function (a, b) { return a.zIndex - b.zIndex; });
209
+ };
210
+ // 默认情况下节点与边按照 zIndex 排序
211
+ var elements = sortElement(__spreadArray(__spreadArray([], __read(this.nodes), false), __read(this.edges), false));
203
212
  // 只显示可见区域的节点和边
204
213
  var visibleElements = [];
205
214
  // TODO: 缓存,优化计算效率 by xutao. So how?
@@ -643,6 +652,19 @@ var GraphModel = /** @class */ (function () {
643
652
  return model.textMode || edgeTextMode || textMode || constant_1.TextMode.TEXT;
644
653
  }
645
654
  };
655
+ /**
656
+ * 内部保留方法,请勿直接使用
657
+ */
658
+ /**
659
+ * 设置重叠模式
660
+ * @param mode 重叠模式
661
+ */
662
+ GraphModel.prototype.setOverlapMode = function (mode) {
663
+ this.overlapMode = mode;
664
+ this.eventCenter.emit('overlap:change', {
665
+ overlapMode: mode,
666
+ });
667
+ };
646
668
  /**
647
669
  * 更新元素的文本模式
648
670
  * @param mode
@@ -685,14 +707,19 @@ var GraphModel = /** @class */ (function () {
685
707
  var _a, _b, _c;
686
708
  var element = ((_a = this.nodesMap[id]) === null || _a === void 0 ? void 0 : _a.model) || ((_b = this.edgesMap[id]) === null || _b === void 0 ? void 0 : _b.model);
687
709
  if (element) {
688
- if (this.overlapMode === constant_1.OverlapMode.DEFAULT) {
689
- (_c = this.topElement) === null || _c === void 0 ? void 0 : _c.setZIndex();
690
- element.setZIndex(constant_1.ELEMENT_MAX_Z_INDEX);
691
- this.topElement = element;
710
+ // 静态模式toFront不做处理
711
+ if (this.overlapMode === constant_1.OverlapMode.STATIC) {
712
+ return;
692
713
  }
714
+ // 递增模式下,将需指定元素zIndex设置为当前最大zIndex + 1
693
715
  if (this.overlapMode === constant_1.OverlapMode.INCREASE) {
694
716
  this.setElementZIndex(id, 'top');
717
+ return;
695
718
  }
719
+ // 默认模式(节点在上)和边在上模式下,将原置顶元素重新恢复原有层级,将需指定元素zIndex设置为最大zIndex
720
+ (_c = this.topElement) === null || _c === void 0 ? void 0 : _c.setZIndex();
721
+ element.setZIndex(constant_1.ELEMENT_MAX_Z_INDEX);
722
+ this.topElement = element;
696
723
  }
697
724
  };
698
725
  /**
@@ -1084,7 +1111,7 @@ var GraphModel = /** @class */ (function () {
1084
1111
  /**
1085
1112
  * 如果堆叠模式为默认模式,则将置顶元素重新恢复原有层级
1086
1113
  */
1087
- if (this.overlapMode === constant_1.OverlapMode.DEFAULT) {
1114
+ if ([constant_1.OverlapMode.DEFAULT, constant_1.OverlapMode.EDGE_TOP].includes(this.overlapMode)) {
1088
1115
  (_a = this.topElement) === null || _a === void 0 ? void 0 : _a.setZIndex();
1089
1116
  }
1090
1117
  };
@@ -1365,12 +1392,30 @@ var GraphModel = /** @class */ (function () {
1365
1392
  * 重新设置画布的宽高
1366
1393
  */
1367
1394
  GraphModel.prototype.resize = function (width, height) {
1368
- this.width = width !== null && width !== void 0 ? width : this.rootEl.getBoundingClientRect().width;
1369
- this.isContainerWidth = (0, lodash_es_1.isNil)(width);
1370
- this.height = height !== null && height !== void 0 ? height : this.rootEl.getBoundingClientRect().height;
1371
- this.isContainerHeight = (0, lodash_es_1.isNil)(height);
1372
- if (!this.width || !this.height) {
1373
- 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);
1374
1419
  }
1375
1420
  };
1376
1421
  /**
@@ -1385,7 +1430,7 @@ var GraphModel = /** @class */ (function () {
1385
1430
  this.elementsModelMap.clear();
1386
1431
  };
1387
1432
  /**
1388
- * 获取图形区域虚拟矩型的尺寸和中心坐标
1433
+ * 获取图形区域虚拟矩形的尺寸和中心坐标
1389
1434
  * @returns
1390
1435
  */
1391
1436
  GraphModel.prototype.getVirtualRectSize = function () {
@@ -1409,7 +1454,7 @@ var GraphModel = /** @class */ (function () {
1409
1454
  var maxY = Math.max.apply(Math, __spreadArray([], __read(nodesY), false));
1410
1455
  var virtualRectWidth = maxX - minX || 0;
1411
1456
  var virtualRectHeight = maxY - minY || 0;
1412
- // 获取虚拟矩型的中心坐标
1457
+ // 获取虚拟矩形的中心坐标
1413
1458
  var virtualRectCenterPositionX = minX + virtualRectWidth / 2;
1414
1459
  var virtualRectCenterPositionY = minY + virtualRectHeight / 2;
1415
1460
  return {
@@ -1564,6 +1609,9 @@ var GraphModel = /** @class */ (function () {
1564
1609
  __decorate([
1565
1610
  mobx_1.computed
1566
1611
  ], GraphModel.prototype, "selectNodes", null);
1612
+ __decorate([
1613
+ mobx_1.action
1614
+ ], GraphModel.prototype, "setOverlapMode", null);
1567
1615
  __decorate([
1568
1616
  mobx_1.action
1569
1617
  ], GraphModel.prototype, "setTextMode", null);
@@ -1,6 +1,6 @@
1
1
  import { BaseNodeModel, GraphModel, Model } from '..';
2
2
  import LogicFlow from '../../LogicFlow';
3
- import { ElementState, ElementType, ModelType, TextMode } from '../../constant';
3
+ import { ElementState, ElementType, ModelType, OverlapMode, TextMode } from '../../constant';
4
4
  import Point = LogicFlow.Point;
5
5
  import EdgeData = LogicFlow.EdgeData;
6
6
  import EdgeConfig = LogicFlow.EdgeConfig;
@@ -271,5 +271,6 @@ export declare class BaseEdgeModel<P extends PropertiesType = PropertiesType> im
271
271
  getAdjustStart(): LogicFlow.Point;
272
272
  getAdjustEnd(): LogicFlow.Point;
273
273
  updateAfterAdjustStartAndEnd({ startPoint, endPoint, }: Record<'startPoint' | 'endPoint', Point>): void;
274
+ updateZIndexByOverlap(overlapMode: OverlapMode, defaultZIndex: any): void;
274
275
  }
275
276
  export default BaseEdgeModel;
@@ -76,6 +76,7 @@ var BaseEdgeModel = /** @class */ (function () {
76
76
  * setAttributes除了初始化调用外,还会在properties发生变化后调用。
77
77
  */
78
78
  BaseEdgeModel.prototype.initEdgeData = function (data) {
79
+ var _this = this;
79
80
  if (!data.properties) {
80
81
  data.properties = {};
81
82
  }
@@ -91,10 +92,8 @@ var BaseEdgeModel = /** @class */ (function () {
91
92
  var adjustEdgeStartAndEnd = this.graphModel.editConfigModel.adjustEdgeStartAndEnd;
92
93
  this.isShowAdjustPoint = adjustEdgeStartAndEnd;
93
94
  (0, lodash_es_1.assign)(this, (0, util_1.pickEdgeConfig)(data));
94
- var overlapMode = this.graphModel.overlapMode;
95
- if (overlapMode === constant_1.OverlapMode.INCREASE) {
96
- this.zIndex = data.zIndex || (0, util_1.getZIndex)();
97
- }
95
+ var _a = this.graphModel, overlapMode = _a.overlapMode, eventCenter = _a.eventCenter;
96
+ this.updateZIndexByOverlap(overlapMode, data.zIndex || (0, util_1.getZIndex)());
98
97
  // 设置边的 anchors,也就是边的两个端点
99
98
  // 端点依赖于 edgeData 的 sourceNode 和 targetNode
100
99
  this.setAnchors();
@@ -102,6 +101,10 @@ var BaseEdgeModel = /** @class */ (function () {
102
101
  this.initPoints();
103
102
  // 文本位置依赖于边上的所有拐点
104
103
  this.formatText(data);
104
+ eventCenter.on('overlap:change', function (data) {
105
+ var newMode = data.overlapMode;
106
+ _this.updateZIndexByOverlap(newMode, _this.zIndex || (0, util_1.getZIndex)());
107
+ });
105
108
  };
106
109
  /**
107
110
  * 设置model属性
@@ -325,7 +328,9 @@ var BaseEdgeModel = /** @class */ (function () {
325
328
  startPoint: (0, lodash_es_1.assign)({}, this.startPoint),
326
329
  endPoint: (0, lodash_es_1.assign)({}, this.endPoint),
327
330
  };
328
- if (this.graphModel.overlapMode === constant_1.OverlapMode.INCREASE) {
331
+ // 因为默认模式和边在上模式下,对节点的zIndex要求不高(因为渲染的时候会按照模式对所有元素进行排序)
332
+ // 所以只在递增模式和静态模式下设置zIndex
333
+ if ([constant_1.OverlapMode.INCREASE, constant_1.OverlapMode.STATIC].includes(this.graphModel.overlapMode)) {
329
334
  data.zIndex = this.zIndex;
330
335
  }
331
336
  var _a = this.text, x = _a.x, y = _a.y, value = _a.value;
@@ -589,6 +594,22 @@ var BaseEdgeModel = /** @class */ (function () {
589
594
  this.updateStartPoint({ x: startPoint.x, y: startPoint.y });
590
595
  this.updateEndPoint({ x: endPoint.x, y: endPoint.y });
591
596
  };
597
+ // 堆叠模式变化时,更新zIndex
598
+ BaseEdgeModel.prototype.updateZIndexByOverlap = function (overlapMode, defaultZIndex) {
599
+ switch (overlapMode) {
600
+ case constant_1.OverlapMode.DEFAULT:
601
+ this.zIndex = 0;
602
+ break;
603
+ case constant_1.OverlapMode.EDGE_TOP:
604
+ this.zIndex = 1;
605
+ break;
606
+ case constant_1.OverlapMode.INCREASE:
607
+ this.zIndex = defaultZIndex;
608
+ break;
609
+ default:
610
+ break;
611
+ }
612
+ };
592
613
  BaseEdgeModel.BaseType = constant_1.ElementType.EDGE;
593
614
  __decorate([
594
615
  mobx_1.observable
@@ -758,6 +779,9 @@ var BaseEdgeModel = /** @class */ (function () {
758
779
  __decorate([
759
780
  mobx_1.action
760
781
  ], BaseEdgeModel.prototype, "updateAfterAdjustStartAndEnd", null);
782
+ __decorate([
783
+ mobx_1.action
784
+ ], BaseEdgeModel.prototype, "updateZIndexByOverlap", null);
761
785
  return BaseEdgeModel;
762
786
  }());
763
787
  exports.BaseEdgeModel = BaseEdgeModel;
@@ -1,6 +1,6 @@
1
1
  import { GraphModel, Model } from '..';
2
2
  import LogicFlow from '../../LogicFlow';
3
- import { ElementState, ElementType, ModelType, TextMode } from '../../constant';
3
+ import { ElementState, ElementType, ModelType, OverlapMode, TextMode } from '../../constant';
4
4
  import { ResizeControl } from '../../view/Control';
5
5
  import AnchorConfig = Model.AnchorConfig;
6
6
  import GraphElements = LogicFlow.GraphElements;
@@ -288,6 +288,7 @@ export declare class BaseNodeModel<P extends PropertiesType = PropertiesType> im
288
288
  updateStyles(styles: Record<string, any>): void;
289
289
  setZIndex(zIndex?: number): void;
290
290
  updateAttributes(attributes: any): void;
291
+ updateZIndexByOverlap(overlapMode: OverlapMode, defaultZIndex: any): void;
291
292
  }
292
293
  export declare namespace BaseNodeModel {
293
294
  type PointTuple = [number, number];
@@ -175,6 +175,7 @@ var BaseNodeModel = /** @class */ (function () {
175
175
  * setAttributes 除了初始化调用外,还会在 properties 发生变化了调用。
176
176
  */
177
177
  BaseNodeModel.prototype.initNodeData = function (data) {
178
+ var _this = this;
178
179
  if (!data.properties) {
179
180
  data.properties = {};
180
181
  }
@@ -189,10 +190,12 @@ var BaseNodeModel = /** @class */ (function () {
189
190
  // 在下面又将 NodeConfig 中的数据赋值给了 this,应该会触发 setAttributes,确认是否符合预期
190
191
  // TODO: 确认 constructor 中赋值 properties 是否必要,此处将 NodeConfig 中所有属性赋值给 this,包括 rotate、rotatable,resizable 等
191
192
  (0, lodash_es_1.assign)(this, (0, util_1.pickNodeConfig)(data));
192
- var overlapMode = this.graphModel.overlapMode;
193
- if (overlapMode === constant_1.OverlapMode.INCREASE) {
194
- this.zIndex = data.zIndex || (0, util_1.getZIndex)();
195
- }
193
+ var _a = this.graphModel, overlapMode = _a.overlapMode, eventCenter = _a.eventCenter;
194
+ this.updateZIndexByOverlap(overlapMode, data.zIndex || (0, util_1.getZIndex)());
195
+ eventCenter.on('overlap:change', function (data) {
196
+ var newMode = data.overlapMode;
197
+ _this.updateZIndexByOverlap(newMode, _this.zIndex || (0, util_1.getZIndex)());
198
+ });
196
199
  };
197
200
  /**
198
201
  * 设置model属性,每次properties发生变化会触发
@@ -305,7 +308,9 @@ var BaseNodeModel = /** @class */ (function () {
305
308
  if (this.rotate) {
306
309
  data.rotate = this.rotate;
307
310
  }
308
- if (this.graphModel.overlapMode === constant_1.OverlapMode.INCREASE) {
311
+ // 因为默认模式和节点在上模式下,对边的zIndex要求不高(因为渲染的时候会按照模式对所有元素进行排序)
312
+ // 所以只在递增模式和静态模式下设置zIndex
313
+ if ([constant_1.OverlapMode.INCREASE, constant_1.OverlapMode.STATIC].includes(this.graphModel.overlapMode)) {
309
314
  data.zIndex = this.zIndex;
310
315
  }
311
316
  if (value) {
@@ -794,6 +799,22 @@ var BaseNodeModel = /** @class */ (function () {
794
799
  BaseNodeModel.prototype.updateAttributes = function (attributes) {
795
800
  (0, lodash_es_1.assign)(this, attributes);
796
801
  };
802
+ // 堆叠模式变化时,更新zIndex
803
+ BaseNodeModel.prototype.updateZIndexByOverlap = function (overlapMode, defaultZIndex) {
804
+ switch (overlapMode) {
805
+ case constant_1.OverlapMode.DEFAULT:
806
+ this.zIndex = 1;
807
+ break;
808
+ case constant_1.OverlapMode.EDGE_TOP:
809
+ this.zIndex = 0;
810
+ break;
811
+ case constant_1.OverlapMode.INCREASE:
812
+ this.zIndex = defaultZIndex;
813
+ break;
814
+ default:
815
+ break;
816
+ }
817
+ };
797
818
  BaseNodeModel.BaseType = constant_1.ElementType.NODE;
798
819
  __decorate([
799
820
  mobx_1.observable
@@ -948,6 +969,9 @@ var BaseNodeModel = /** @class */ (function () {
948
969
  __decorate([
949
970
  mobx_1.action
950
971
  ], BaseNodeModel.prototype, "updateAttributes", null);
972
+ __decorate([
973
+ mobx_1.action
974
+ ], BaseNodeModel.prototype, "updateZIndexByOverlap", null);
951
975
  return BaseNodeModel;
952
976
  }());
953
977
  exports.BaseNodeModel = BaseNodeModel;
@@ -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) {
@@ -177,11 +181,19 @@ var BaseNode = /** @class */ (function (_super) {
177
181
  // 节点拖拽进画布之后,不触发click事件相关emit
178
182
  // 点拖拽进画布没有触发mousedown事件,没有startTime,用这个值做区分
179
183
  var isDragging = _this.mouseUpDrag === false;
184
+ var curTime = new Date().getTime();
180
185
  if (!_this.startTime)
181
186
  return;
187
+ var timeInterval = curTime - _this.startTime;
182
188
  var _a = _this.props, model = _a.model, graphModel = _a.graphModel;
183
- if (!isDragging)
184
- return; // 如果是拖拽, 不触发click事件。
189
+ // 这里会有一种极端情况:当网格大小是1或者关闭网格吸附时,用触摸板点击节点会触发拖拽事件导致节点无法选中
190
+ // 当触摸板点击节点时,为了防止误触发拖拽导致节点无法选中,允许在非拖拽状态且时间间隔小于100ms时触发点击事件
191
+ if (!isDragging && timeInterval > 300)
192
+ return;
193
+ if (!isDragging) {
194
+ _this.onDragEnd();
195
+ _this.handleMouseUp();
196
+ }
185
197
  // 节点数据,多为事件对象数据抛出
186
198
  var nodeData = model.getData();
187
199
  var position = graphModel.getPointByClient({
@@ -259,6 +271,7 @@ var BaseNode = /** @class */ (function (_super) {
259
271
  };
260
272
  _this.handleMouseDown = function (ev) {
261
273
  var _a = _this.props, model = _a.model, graphModel = _a.graphModel;
274
+ _this.mouseDownPosition = { x: ev.clientX, y: ev.clientY };
262
275
  _this.startTime = new Date().getTime();
263
276
  var editConfigModel = graphModel.editConfigModel;
264
277
  if (editConfigModel.adjustNodePosition && model.draggable) {
@@ -272,6 +285,9 @@ var BaseNode = /** @class */ (function (_super) {
272
285
  });
273
286
  };
274
287
  _this.handleBlur = function () {
288
+ // 当节点通过自定义锚点实现节点删除时,这里props会变成undefined,需兼容一下
289
+ if (!_this.props)
290
+ return;
275
291
  var _a = _this.props, model = _a.model, graphModel = _a.graphModel;
276
292
  graphModel.eventCenter.emit(constant_1.EventType.NODE_BLUR, {
277
293
  data: model.getData(),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@logicflow/core",
3
- "version": "2.1.3",
3
+ "version": "2.1.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
@@ -26,7 +26,12 @@ import Tool from './tool'
26
26
  import History from './history'
27
27
  import Keyboard, { initDefaultShortcut } from './keyboard'
28
28
  import { EventCallback, CallbackArgs, EventArgs } from './event/eventEmitter'
29
- import { ElementType, EventType, SegmentDirection } from './constant'
29
+ import {
30
+ ElementType,
31
+ EventType,
32
+ OverlapMode,
33
+ SegmentDirection,
34
+ } from './constant'
30
35
  import { Grid } from './view/overlay'
31
36
 
32
37
  import Extension = LogicFlow.Extension
@@ -1088,6 +1093,9 @@ export class LogicFlow {
1088
1093
  this.render({})
1089
1094
  }
1090
1095
 
1096
+ setOverlapMode(mode: OverlapMode) {
1097
+ this.graphModel.setOverlapMode(mode)
1098
+ }
1091
1099
  /*********************************************************
1092
1100
  * LogicFlow Render方法
1093
1101
  ********************************************************/
@@ -162,8 +162,10 @@ export enum EventType {
162
162
  }
163
163
 
164
164
  export enum OverlapMode {
165
+ STATIC = -1, // 静态(元素层级不随点击变化)
165
166
  DEFAULT = 0, // 默认
166
167
  INCREASE = 1, // 递增
168
+ EDGE_TOP = 2, // 边固定在顶部
167
169
  }
168
170
 
169
171
  export enum SegmentDirection {
@@ -181,6 +181,7 @@ export class GraphModel {
181
181
  this.animation = setupAnimation(animation)
182
182
  this.overlapMode = options.overlapMode || OverlapMode.DEFAULT
183
183
 
184
+ this.isMiniMap = options.isMiniMap || false
184
185
  this.width = options.width ?? this.rootEl.getBoundingClientRect().width
185
186
  this.isContainerWidth = isNil(options.width)
186
187
  this.height = options.height ?? this.rootEl.getBoundingClientRect().height
@@ -191,6 +192,9 @@ export class GraphModel {
191
192
  ((entries) => {
192
193
  for (const entry of entries) {
193
194
  if (entry.target === this.rootEl) {
195
+ // 检查元素是否仍在DOM中
196
+ const isElementInDOM = document.body.contains(this.rootEl)
197
+ if (!isElementInDOM) return
194
198
  this.resize()
195
199
  this.eventCenter.emit('graph:resize', {
196
200
  target: this.rootEl,
@@ -249,9 +253,11 @@ export class GraphModel {
249
253
  * todo: 性能优化
250
254
  */
251
255
  @computed get sortElements() {
252
- const elements = [...this.nodes, ...this.edges].sort(
253
- (a, b) => a.zIndex - b.zIndex,
254
- )
256
+ const sortElement = (list) => {
257
+ return [...list].sort((a, b) => a.zIndex - b.zIndex)
258
+ }
259
+ // 默认情况下节点与边按照 zIndex 排序
260
+ const elements = sortElement([...this.nodes, ...this.edges])
255
261
 
256
262
  // 只显示可见区域的节点和边
257
263
  const visibleElements: (BaseNodeModel | BaseEdgeModel)[] = []
@@ -717,6 +723,22 @@ export class GraphModel {
717
723
  }
718
724
  }
719
725
 
726
+ /**
727
+ * 内部保留方法,请勿直接使用
728
+ */
729
+
730
+ /**
731
+ * 设置重叠模式
732
+ * @param mode 重叠模式
733
+ */
734
+ @action
735
+ setOverlapMode(mode: OverlapMode) {
736
+ this.overlapMode = mode
737
+ this.eventCenter.emit('overlap:change', {
738
+ overlapMode: mode,
739
+ })
740
+ }
741
+
720
742
  /**
721
743
  * 更新元素的文本模式
722
744
  * @param mode
@@ -767,14 +789,19 @@ export class GraphModel {
767
789
  toFront(id: string) {
768
790
  const element = this.nodesMap[id]?.model || this.edgesMap[id]?.model
769
791
  if (element) {
770
- if (this.overlapMode === OverlapMode.DEFAULT) {
771
- this.topElement?.setZIndex()
772
- element.setZIndex(ELEMENT_MAX_Z_INDEX)
773
- this.topElement = element
792
+ // 静态模式toFront不做处理
793
+ if (this.overlapMode === OverlapMode.STATIC) {
794
+ return
774
795
  }
796
+ // 递增模式下,将需指定元素zIndex设置为当前最大zIndex + 1
775
797
  if (this.overlapMode === OverlapMode.INCREASE) {
776
798
  this.setElementZIndex(id, 'top')
799
+ return
777
800
  }
801
+ // 默认模式(节点在上)和边在上模式下,将原置顶元素重新恢复原有层级,将需指定元素zIndex设置为最大zIndex
802
+ this.topElement?.setZIndex()
803
+ element.setZIndex(ELEMENT_MAX_Z_INDEX)
804
+ this.topElement = element
778
805
  }
779
806
  }
780
807
 
@@ -1229,7 +1256,9 @@ export class GraphModel {
1229
1256
  /**
1230
1257
  * 如果堆叠模式为默认模式,则将置顶元素重新恢复原有层级
1231
1258
  */
1232
- if (this.overlapMode === OverlapMode.DEFAULT) {
1259
+ if (
1260
+ [OverlapMode.DEFAULT, OverlapMode.EDGE_TOP].includes(this.overlapMode)
1261
+ ) {
1233
1262
  this.topElement?.setZIndex()
1234
1263
  }
1235
1264
  }
@@ -1558,15 +1587,32 @@ export class GraphModel {
1558
1587
  * 重新设置画布的宽高
1559
1588
  */
1560
1589
  @action resize(width?: number, height?: number): void {
1561
- this.width = width ?? this.rootEl.getBoundingClientRect().width
1562
- this.isContainerWidth = isNil(width)
1563
- this.height = height ?? this.rootEl.getBoundingClientRect().height
1564
- this.isContainerHeight = isNil(height)
1565
-
1566
- if (!this.width || !this.height) {
1567
- console.warn(
1568
- '渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675',
1569
- )
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)
1570
1616
  }
1571
1617
  }
1572
1618
 
@@ -1584,7 +1630,7 @@ export class GraphModel {
1584
1630
  }
1585
1631
 
1586
1632
  /**
1587
- * 获取图形区域虚拟矩型的尺寸和中心坐标
1633
+ * 获取图形区域虚拟矩形的尺寸和中心坐标
1588
1634
  * @returns
1589
1635
  */
1590
1636
  getVirtualRectSize(): GraphModel.VirtualRectProps {
@@ -1611,7 +1657,7 @@ export class GraphModel {
1611
1657
  const virtualRectWidth = maxX - minX || 0
1612
1658
  const virtualRectHeight = maxY - minY || 0
1613
1659
 
1614
- // 获取虚拟矩型的中心坐标
1660
+ // 获取虚拟矩形的中心坐标
1615
1661
  const virtualRectCenterPositionX = minX + virtualRectWidth / 2
1616
1662
  const virtualRectCenterPositionY = minY + virtualRectHeight / 2
1617
1663
 
@@ -1717,7 +1763,6 @@ export class GraphModel {
1717
1763
  @action setPartial(partial: boolean): void {
1718
1764
  this.partial = partial
1719
1765
  }
1720
-
1721
1766
  /** 销毁当前实例 */
1722
1767
  destroy() {
1723
1768
  try {