@logicflow/core 2.0.9 → 2.0.11

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.
Files changed (85) hide show
  1. package/.turbo/turbo-build$colon$dev.log +2 -2
  2. package/.turbo/turbo-build.log +7 -7
  3. package/CHANGELOG.md +26 -0
  4. package/dist/index.min.js +1 -1
  5. package/dist/index.min.js.map +1 -1
  6. package/es/LogicFlow.js +21 -1
  7. package/es/event/eventEmitter.d.ts +1 -0
  8. package/es/event/eventEmitter.js +3 -0
  9. package/es/history/index.d.ts +1 -0
  10. package/es/history/index.js +6 -0
  11. package/es/keyboard/index.d.ts +1 -0
  12. package/es/keyboard/index.js +3 -0
  13. package/es/model/EditConfigModel.d.ts +3 -1
  14. package/es/model/EditConfigModel.js +5 -0
  15. package/es/model/GraphModel.d.ts +13 -1
  16. package/es/model/GraphModel.js +41 -41
  17. package/es/model/edge/BaseEdgeModel.js +6 -3
  18. package/es/model/edge/BezierEdgeModel.js +2 -2
  19. package/es/model/edge/PolylineEdgeModel.d.ts +1 -0
  20. package/es/model/edge/PolylineEdgeModel.js +9 -2
  21. package/es/model/node/BaseNodeModel.js +7 -2
  22. package/es/tool/index.d.ts +1 -0
  23. package/es/tool/index.js +6 -2
  24. package/es/util/geometry.d.ts +1 -1
  25. package/es/util/geometry.js +4 -1
  26. package/es/util/node.d.ts +1 -1
  27. package/es/util/node.js +32 -16
  28. package/es/view/Graph.js +19 -2
  29. package/es/view/behavior/dnd.js +3 -2
  30. package/es/view/edge/BaseEdge.js +9 -2
  31. package/es/view/node/BaseNode.js +20 -11
  32. package/es/view/node/HtmlNode.js +1 -0
  33. package/es/view/overlay/CanvasOverlay.js +6 -4
  34. package/es/view/text/BaseText.js +1 -0
  35. package/lib/LogicFlow.js +20 -0
  36. package/lib/event/eventEmitter.d.ts +1 -0
  37. package/lib/event/eventEmitter.js +3 -0
  38. package/lib/history/index.d.ts +1 -0
  39. package/lib/history/index.js +6 -0
  40. package/lib/keyboard/index.d.ts +1 -0
  41. package/lib/keyboard/index.js +3 -0
  42. package/lib/model/EditConfigModel.d.ts +3 -1
  43. package/lib/model/EditConfigModel.js +5 -0
  44. package/lib/model/GraphModel.d.ts +13 -1
  45. package/lib/model/GraphModel.js +40 -40
  46. package/lib/model/edge/BaseEdgeModel.js +6 -3
  47. package/lib/model/edge/BezierEdgeModel.js +1 -1
  48. package/lib/model/edge/PolylineEdgeModel.d.ts +1 -0
  49. package/lib/model/edge/PolylineEdgeModel.js +8 -1
  50. package/lib/model/node/BaseNodeModel.js +7 -2
  51. package/lib/tool/index.d.ts +1 -0
  52. package/lib/tool/index.js +6 -2
  53. package/lib/util/geometry.d.ts +1 -1
  54. package/lib/util/geometry.js +4 -1
  55. package/lib/util/node.d.ts +1 -1
  56. package/lib/util/node.js +32 -16
  57. package/lib/view/Graph.js +19 -2
  58. package/lib/view/behavior/dnd.js +3 -2
  59. package/lib/view/edge/BaseEdge.js +9 -2
  60. package/lib/view/node/BaseNode.js +19 -10
  61. package/lib/view/node/HtmlNode.js +1 -0
  62. package/lib/view/overlay/CanvasOverlay.js +6 -4
  63. package/lib/view/text/BaseText.js +1 -0
  64. package/package.json +1 -1
  65. package/src/LogicFlow.tsx +25 -1
  66. package/src/event/eventEmitter.ts +4 -0
  67. package/src/history/index.ts +7 -0
  68. package/src/keyboard/index.ts +4 -0
  69. package/src/model/EditConfigModel.ts +4 -0
  70. package/src/model/GraphModel.ts +62 -70
  71. package/src/model/edge/BaseEdgeModel.ts +7 -2
  72. package/src/model/edge/BezierEdgeModel.ts +2 -2
  73. package/src/model/edge/PolylineEdgeModel.ts +10 -2
  74. package/src/model/node/BaseNodeModel.ts +9 -2
  75. package/src/tool/index.ts +7 -2
  76. package/src/util/geometry.ts +3 -1
  77. package/src/util/node.ts +19 -18
  78. package/src/view/Graph.tsx +19 -2
  79. package/src/view/behavior/dnd.ts +5 -2
  80. package/src/view/edge/BaseEdge.tsx +9 -2
  81. package/src/view/node/BaseNode.tsx +20 -12
  82. package/src/view/node/HtmlNode.tsx +1 -0
  83. package/src/view/overlay/CanvasOverlay.tsx +6 -5
  84. package/src/view/text/BaseText.tsx +1 -0
  85. package/stats.html +1 -1
package/es/view/Graph.js CHANGED
@@ -55,9 +55,26 @@ var Graph = /** @class */ (function (_super) {
55
55
  function Graph() {
56
56
  var _this = _super.apply(this, __spreadArray([], __read(arguments), false)) || this;
57
57
  _this.handleResize = function () {
58
- _this.props.graphModel.resize();
58
+ var _a = _this.props, graphModel = _a.graphModel, options = _a.options;
59
+ var width = graphModel.width, height = graphModel.height, isContainerWidth = graphModel.isContainerWidth, isContainerHeight = graphModel.isContainerHeight;
60
+ var resizeWidth = width;
61
+ var resizeHeight = height;
62
+ var needUpdate = false;
63
+ if (isContainerWidth) {
64
+ resizeWidth = undefined;
65
+ needUpdate = true;
66
+ }
67
+ if (isContainerHeight) {
68
+ resizeHeight = undefined;
69
+ needUpdate = true;
70
+ }
71
+ if (needUpdate) {
72
+ graphModel.resize(resizeWidth, resizeHeight);
73
+ }
74
+ options.width = width;
75
+ options.height = height;
59
76
  };
60
- _this.throttleResize = function () { return throttle(_this.handleResize, 200); };
77
+ _this.throttleResize = throttle(_this.handleResize, 200);
61
78
  return _this;
62
79
  }
63
80
  Graph.prototype.componentDidMount = function () {
@@ -81,10 +81,11 @@ var Dnd = /** @class */ (function () {
81
81
  });
82
82
  // 处理缩放和偏移
83
83
  var _b = position.canvasOverlayPosition, x1 = _b.x, y1 = _b.y;
84
+ var snapGrid = this.lf.graphModel.editConfigModel.snapGrid;
84
85
  // x, y 对齐到网格的 size
85
86
  return {
86
- x: snapToGrid(x1, gridSize),
87
- y: snapToGrid(y1, gridSize),
87
+ x: snapToGrid(x1, gridSize, snapGrid),
88
+ y: snapToGrid(y1, gridSize, snapGrid),
88
89
  };
89
90
  };
90
91
  Dnd.prototype.startDrag = function (nodeConfig) {
@@ -99,12 +99,16 @@ var BaseEdge = /** @class */ (function (_super) {
99
99
  clearTimeout(_this.clickTimer);
100
100
  }
101
101
  var _a = _this.props, model = _a.model, graphModel = _a.graphModel;
102
+ var editConfigModel = graphModel.editConfigModel;
102
103
  var position = graphModel.getPointByClient({
103
104
  x: ev.clientX,
104
105
  y: ev.clientY,
105
106
  });
106
107
  graphModel.setElementStateById(model.id, ElementState.SHOW_MENU, position.domOverlayPosition);
107
- _this.toFront();
108
+ // 静默模式下点击节点不变更节点层级
109
+ if (!editConfigModel.isSilentMode) {
110
+ _this.toFront();
111
+ }
108
112
  if (!model.isSelected) {
109
113
  graphModel.selectEdgeById(model.id);
110
114
  }
@@ -192,7 +196,10 @@ var BaseEdge = /** @class */ (function (_super) {
192
196
  }
193
197
  var editConfigModel = graphModel.editConfigModel;
194
198
  graphModel.selectEdgeById(model.id, isMultipleSelect(e, editConfigModel));
195
- _this.toFront();
199
+ // 静默模式下点击节点不变更节点层级
200
+ if (!editConfigModel.isSilentMode) {
201
+ _this.toFront();
202
+ }
196
203
  };
197
204
  _this.handleFocus = function () {
198
205
  var _a = _this.props, model = _a.model, graphModel = _a.graphModel;
@@ -58,7 +58,7 @@ import { map } from 'lodash-es';
58
58
  import Anchor from '../Anchor';
59
59
  import { BaseText } from '../text';
60
60
  import { ElementState, EventType, TextMode } from '../../constant';
61
- import { StepDrag, snapToGrid, isIe, isMultipleSelect, cancelRaf, createRaf, TranslateMatrix,
61
+ import { StepDrag, snapToGrid, isIe, isMultipleSelect, cancelRaf, createRaf,
62
62
  // RotateMatrix,
63
63
  } from '../../util';
64
64
  import RotateControlPoint from '../Rotate';
@@ -85,7 +85,7 @@ var BaseNode = /** @class */ (function (_super) {
85
85
  var _b, _c, _d, _e;
86
86
  var event = _a.event;
87
87
  var _f = _this.props, model = _f.model, graphModel = _f.graphModel;
88
- var _g = graphModel.editConfigModel, stopMoveGraph = _g.stopMoveGraph, autoExpand = _g.autoExpand, transformModel = graphModel.transformModel, selectNodes = graphModel.selectNodes, width = graphModel.width, height = graphModel.height, gridSize = graphModel.gridSize;
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
91
  var _j = graphModel.getPointByClient({
@@ -97,9 +97,9 @@ var BaseNode = /** @class */ (function (_super) {
97
97
  // 2. 考虑鼠标位置不再节点中心
98
98
  x = x + ((_c = (_b = _this.moveOffset) === null || _b === void 0 ? void 0 : _b.dx) !== null && _c !== void 0 ? _c : 0);
99
99
  y = y + ((_e = (_d = _this.moveOffset) === null || _d === void 0 ? void 0 : _d.dy) !== null && _e !== void 0 ? _e : 0);
100
- // 将x, y移动到grid上
101
- x = snapToGrid(x, gridSize);
102
- y = snapToGrid(y, gridSize);
100
+ // 校准坐标
101
+ x = snapToGrid(x, gridSize, snapGrid);
102
+ y = snapToGrid(y, gridSize, snapGrid);
103
103
  if (!width || !height) {
104
104
  graphModel.moveNode2Coordinate(model.id, x, y);
105
105
  return;
@@ -135,10 +135,6 @@ var BaseNode = /** @class */ (function (_super) {
135
135
  if (_this.t) {
136
136
  cancelRaf(_this.t);
137
137
  }
138
- model.transform = new TranslateMatrix(-x, -y)
139
- .rotate(model.rotate)
140
- .translate(x, y)
141
- .toString();
142
138
  var moveNodes = selectNodes.map(function (node) { return node.id; });
143
139
  // 未被选中的节点也可以拖动
144
140
  if (moveNodes.indexOf(model.id) === -1) {
@@ -213,7 +209,10 @@ var BaseNode = /** @class */ (function (_super) {
213
209
  else {
214
210
  graphModel.selectNodeById(model.id, isMultiple);
215
211
  eventOptions.isSelected = true;
216
- _this.toFront();
212
+ // 静默模式下点击节点不变更节点层级
213
+ if (!editConfigModel.isSilentMode) {
214
+ _this.toFront();
215
+ }
217
216
  }
218
217
  // 不是双击的,默认都是单击
219
218
  if (isDoubleClick) {
@@ -233,6 +232,7 @@ var BaseNode = /** @class */ (function (_super) {
233
232
  _this.handleContextMenu = function (ev) {
234
233
  ev.preventDefault();
235
234
  var _a = _this.props, model = _a.model, graphModel = _a.graphModel;
235
+ var editConfigModel = graphModel.editConfigModel;
236
236
  // 节点数据,多为事件对象数据抛出
237
237
  var nodeData = model.getData();
238
238
  var position = graphModel.getPointByClient({
@@ -248,7 +248,10 @@ var BaseNode = /** @class */ (function (_super) {
248
248
  e: ev,
249
249
  position: position,
250
250
  });
251
- _this.toFront();
251
+ // 静默模式下点击节点不变更节点层级
252
+ if (!editConfigModel.isSilentMode) {
253
+ _this.toFront();
254
+ }
252
255
  };
253
256
  _this.handleMouseDown = function (ev) {
254
257
  var _a = _this.props, model = _a.model, graphModel = _a.graphModel;
@@ -322,6 +325,12 @@ var BaseNode = /** @class */ (function (_super) {
322
325
  if (this.modelDisposer) {
323
326
  this.modelDisposer();
324
327
  }
328
+ // 以下是 mobx-preact 中 componentWillUnmount 的回调逻辑,但是不知道出于什么考虑,mobx-preact 没有混入这一段逻辑
329
+ // @ts-ignore
330
+ if (this.render.$mobx) {
331
+ // @ts-ignore
332
+ this.render.$mobx.dispose();
333
+ }
325
334
  };
326
335
  BaseNode.prototype.componentDidMount = function () { };
327
336
  BaseNode.prototype.componentDidUpdate = function () { };
@@ -113,6 +113,7 @@ var HtmlNode = /** @class */ (function (_super) {
113
113
  }
114
114
  };
115
115
  HtmlNode.prototype.componentWillUnmount = function () {
116
+ _super.prototype.componentWillUnmount.call(this);
116
117
  this.rootEl.innerHTML = '';
117
118
  };
118
119
  HtmlNode.prototype.getShape = function () {
@@ -63,8 +63,9 @@ var CanvasOverlay = /** @class */ (function (_super) {
63
63
  _this.zoomHandler = function (ev) {
64
64
  var _a = _this.props, _b = _a.graphModel, editConfigModel = _b.editConfigModel, transformModel = _b.transformModel, gridSize = _b.gridSize, graphModel = _a.graphModel;
65
65
  var eX = ev.deltaX, eY = ev.deltaY;
66
+ var stopScrollGraph = editConfigModel.stopScrollGraph, stopZoomGraph = editConfigModel.stopZoomGraph;
66
67
  // 如果没有禁止滚动移动画布, 并且当前触发的时候ctrl键、cmd键没有按住, 那么移动画布
67
- if (!editConfigModel.stopScrollGraph && !ev.ctrlKey && !ev.metaKey) {
68
+ if (!stopScrollGraph && !ev.ctrlKey && !ev.metaKey) {
68
69
  ev.preventDefault();
69
70
  _this.stepScrollX += eX;
70
71
  _this.stepScrollY += eY;
@@ -83,7 +84,7 @@ var CanvasOverlay = /** @class */ (function (_super) {
83
84
  return;
84
85
  }
85
86
  // 如果没有禁止缩放画布,那么进行缩放. 在禁止缩放画布后,按住 ctrl、cmd 键也不能缩放了。
86
- if (!editConfigModel.stopZoomGraph) {
87
+ if (!stopZoomGraph) {
87
88
  ev.preventDefault();
88
89
  var position = graphModel.getPointByClient({
89
90
  x: ev.clientX,
@@ -124,10 +125,11 @@ var CanvasOverlay = /** @class */ (function (_super) {
124
125
  // 鼠标、触摸板 按下
125
126
  _this.mouseDownHandler = function (ev) {
126
127
  var _a = _this.props.graphModel, eventCenter = _a.eventCenter, editConfigModel = _a.editConfigModel, SCALE_X = _a.transformModel.SCALE_X, gridSize = _a.gridSize;
128
+ var adjustEdge = editConfigModel.adjustEdge, adjustNodePosition = editConfigModel.adjustNodePosition, stopMoveGraph = editConfigModel.stopMoveGraph;
127
129
  var target = ev.target;
128
- var isFrozenElement = !editConfigModel.adjustEdge && !editConfigModel.adjustNodePosition;
130
+ var isFrozenElement = !adjustEdge && !adjustNodePosition;
129
131
  if (target.getAttribute('name') === 'canvas-overlay' || isFrozenElement) {
130
- if (editConfigModel.stopMoveGraph !== true) {
132
+ if (stopMoveGraph !== true) {
131
133
  _this.stepDrag.setStep(gridSize * SCALE_X);
132
134
  _this.stepDrag.handleMouseDown(ev);
133
135
  }
@@ -54,6 +54,7 @@ var BaseText = /** @class */ (function (_super) {
54
54
  var _a = _this.props, draggable = _a.draggable, model = _a.model, graphModel = _a.graphModel;
55
55
  var nodeTextDraggable = graphModel.editConfigModel.nodeTextDraggable;
56
56
  if (draggable !== null && draggable !== void 0 ? draggable : nodeTextDraggable) {
57
+ e.stopPropagation();
57
58
  _this.stepperDrag.model = model;
58
59
  _this.stepperDrag.handleMouseDown(e);
59
60
  }
package/lib/LogicFlow.js CHANGED
@@ -806,12 +806,18 @@ var LogicFlow = /** @class */ (function () {
806
806
  */
807
807
  LogicFlow.prototype.updateEditConfig = function (config) {
808
808
  var _a = this.graphModel, editConfigModel = _a.editConfigModel, transformModel = _a.transformModel;
809
+ var currentSnapGrid = editConfigModel.snapGrid;
809
810
  editConfigModel.updateEditConfig(config);
810
811
  if ((config === null || config === void 0 ? void 0 : config.stopMoveGraph) !== undefined) {
811
812
  transformModel.updateTranslateLimits(config.stopMoveGraph);
812
813
  }
813
814
  // 静默模式切换时,修改快捷键的启用状态
814
815
  (config === null || config === void 0 ? void 0 : config.isSilentMode) ? this.keyboard.disable() : this.keyboard.enable(true);
816
+ // 切换网格对齐状态时,修改网格尺寸
817
+ if (!(0, lodash_es_1.isNil)(config === null || config === void 0 ? void 0 : config.snapGrid) && config.snapGrid !== currentSnapGrid) {
818
+ var _b = this.graphModel.grid.size, size = _b === void 0 ? 1 : _b;
819
+ this.graphModel.updateGridSize(config.snapGrid ? size : 1);
820
+ }
815
821
  };
816
822
  /**
817
823
  * 获取流程图当前编辑相关设置
@@ -936,6 +942,8 @@ var LogicFlow = /** @class */ (function () {
936
942
  */
937
943
  LogicFlow.prototype.clearData = function () {
938
944
  this.graphModel.clearData();
945
+ // 强制刷新数据, 让 preact 清除对已删除节点的引用
946
+ this.render({});
939
947
  };
940
948
  /*********************************************************
941
949
  * LogicFlow Render方法
@@ -1197,7 +1205,19 @@ var LogicFlow = /** @class */ (function () {
1197
1205
  };
1198
1206
  /** 销毁当前实例 */
1199
1207
  LogicFlow.prototype.destroy = function () {
1208
+ var _a;
1209
+ this.clearData();
1210
+ (0, compat_1.render)(null, this.container);
1211
+ this.keyboard.destroy();
1200
1212
  this.graphModel.destroy();
1213
+ this.tool.destroy();
1214
+ this.history.destroy();
1215
+ for (var extensionName in this.extension) {
1216
+ var extensionInstance = this.extension[extensionName];
1217
+ if ('destroy' in extensionInstance) {
1218
+ (_a = extensionInstance.destroy) === null || _a === void 0 ? void 0 : _a.call(extensionInstance);
1219
+ }
1220
+ }
1201
1221
  };
1202
1222
  // 全局配置的插件,所有的LogicFlow示例都会使用
1203
1223
  LogicFlow.extensions = new Map();
@@ -44,5 +44,6 @@ export default class EventEmitter {
44
44
  off<T extends keyof EventArgs>(evts: T, callback?: (args: EventArgs[T]) => void): void;
45
45
  off<T extends string>(evts: T, callback?: EventCallback<T>): void;
46
46
  getEvents(): EventsType<string>;
47
+ destroy(): void;
47
48
  }
48
49
  export { EventEmitter, EventArgs };
@@ -87,6 +87,9 @@ var EventEmitter = /** @class */ (function () {
87
87
  EventEmitter.prototype.getEvents = function () {
88
88
  return this._events;
89
89
  };
90
+ EventEmitter.prototype.destroy = function () {
91
+ this._events = {};
92
+ };
90
93
  return EventEmitter;
91
94
  }());
92
95
  exports.EventEmitter = EventEmitter;
@@ -19,5 +19,6 @@ export declare class History {
19
19
  redoAble(): boolean;
20
20
  redo(): LogicFlow.GraphData | undefined;
21
21
  watch(model: GraphModel): void;
22
+ destroy(): void;
22
23
  }
23
24
  export default History;
@@ -107,6 +107,12 @@ var History = /** @class */ (function () {
107
107
  }
108
108
  }, this.waitTime));
109
109
  };
110
+ History.prototype.destroy = function () {
111
+ this.undos = [];
112
+ this.redos = [];
113
+ this.curData = null;
114
+ this.stopWatch && this.stopWatch();
115
+ };
110
116
  return History;
111
117
  }());
112
118
  exports.History = History;
@@ -26,6 +26,7 @@ export declare class Keyboard {
26
26
  off(keys: string | string[], action?: Action): void;
27
27
  enable(force: boolean): void;
28
28
  disable(): void;
29
+ destroy(): void;
29
30
  private getKeys;
30
31
  protected formatKey(key: string): string;
31
32
  }
@@ -88,6 +88,9 @@ var Keyboard = /** @class */ (function () {
88
88
  }
89
89
  }
90
90
  };
91
+ Keyboard.prototype.destroy = function () {
92
+ this.mousetrap.reset();
93
+ };
91
94
  Keyboard.prototype.getKeys = function (keys) {
92
95
  var _this = this;
93
96
  return (Array.isArray(keys) ? keys : [keys]).map(function (key) {
@@ -113,6 +113,7 @@ export interface IEditConfigType {
113
113
  textMode: TextMode;
114
114
  nodeTextMode: TextMode;
115
115
  edgeTextMode: TextMode;
116
+ snapGrid: boolean;
116
117
  }
117
118
  export type IConfigKeys = keyof IEditConfigType;
118
119
  /**
@@ -127,6 +128,7 @@ export declare class EditConfigModel {
127
128
  stopZoomGraph: boolean;
128
129
  stopMoveGraph: boolean;
129
130
  stopScrollGraph: boolean;
131
+ snapGrid: boolean;
130
132
  /*********************************************************
131
133
  * 文本相关配置(全局)
132
134
  ********************************************************/
@@ -168,7 +170,7 @@ export declare class EditConfigModel {
168
170
  multipleSelectKey: string;
169
171
  constructor(config: Partial<IEditConfigType>);
170
172
  updateEditConfig(config: Partial<IEditConfigType>): void;
171
- computeConfig(config: Partial<IEditConfigType>): Partial<IEditConfigType> & Pick<Partial<IEditConfigType>, "textMode" | "adjustEdgeStartAndEnd" | "isSilentMode" | "stopZoomGraph" | "stopScrollGraph" | "stopMoveGraph" | "adjustEdge" | "adjustEdgeMiddle" | "adjustEdgeStart" | "adjustEdgeEnd" | "adjustNodePosition" | "hideAnchors" | "allowRotate" | "allowResize" | "autoExpand" | "hoverOutline" | "nodeSelectedOutline" | "edgeSelectedOutline" | "textEdit" | "nodeTextEdit" | "edgeTextEdit" | "textDraggable" | "nodeTextDraggable" | "edgeTextDraggable" | "multipleSelectKey" | "nodeTextMode" | "edgeTextMode" | "nodeTextMultiple" | "edgeTextMultiple" | "nodeTextVertical" | "edgeTextVertical">;
173
+ computeConfig(config: Partial<IEditConfigType>): Partial<IEditConfigType> & Pick<Partial<IEditConfigType>, "textMode" | "adjustEdgeStartAndEnd" | "edgeTextDraggable" | "edgeTextEdit" | "nodeTextDraggable" | "nodeTextEdit" | "isSilentMode" | "stopZoomGraph" | "stopScrollGraph" | "stopMoveGraph" | "snapGrid" | "adjustEdge" | "adjustEdgeMiddle" | "adjustEdgeStart" | "adjustEdgeEnd" | "adjustNodePosition" | "hideAnchors" | "allowRotate" | "allowResize" | "autoExpand" | "hoverOutline" | "nodeSelectedOutline" | "edgeSelectedOutline" | "textEdit" | "textDraggable" | "multipleSelectKey" | "nodeTextMode" | "edgeTextMode" | "nodeTextMultiple" | "edgeTextMultiple" | "nodeTextVertical" | "edgeTextVertical">;
172
174
  updateTextMode(textMode: TextMode): void;
173
175
  getConfig(): IEditConfigType;
174
176
  }
@@ -36,6 +36,7 @@ var allKeys = [
36
36
  'stopZoomGraph', // 禁止缩放画布
37
37
  'stopScrollGraph', // 禁止鼠标滚动移动画布
38
38
  'stopMoveGraph', // 禁止拖动画布
39
+ 'snapGrid', // 是否开启网格对齐
39
40
  'adjustEdge', // 允许调整边
40
41
  'adjustEdgeMiddle', // 允许调整边中点
41
42
  'adjustEdgeStartAndEnd', // 允许调整边起点和终点
@@ -77,6 +78,7 @@ var EditConfigModel = /** @class */ (function () {
77
78
  this.stopZoomGraph = false;
78
79
  this.stopMoveGraph = false;
79
80
  this.stopScrollGraph = false;
81
+ this.snapGrid = false;
80
82
  /*********************************************************
81
83
  * 文本相关配置(全局)
82
84
  ********************************************************/
@@ -191,6 +193,9 @@ var EditConfigModel = /** @class */ (function () {
191
193
  __decorate([
192
194
  mobx_1.observable
193
195
  ], EditConfigModel.prototype, "stopScrollGraph", void 0);
196
+ __decorate([
197
+ mobx_1.observable
198
+ ], EditConfigModel.prototype, "snapGrid", void 0);
194
199
  __decorate([
195
200
  mobx_1.observable
196
201
  ], EditConfigModel.prototype, "textMode", void 0);
@@ -46,6 +46,14 @@ export declare class GraphModel {
46
46
  * 获取自定义连线轨迹
47
47
  */
48
48
  customTrajectory: LFOptions.Definition['customTrajectory'];
49
+ /**
50
+ * 判断是否使用的是容器的宽度
51
+ */
52
+ isContainerWidth: boolean;
53
+ /**
54
+ * 判断是否使用的是容器的高度
55
+ */
56
+ isContainerHeight: boolean;
49
57
  edgeType: string;
50
58
  nodes: BaseNodeModel[];
51
59
  edges: BaseEdgeModel[];
@@ -391,7 +399,11 @@ export declare class GraphModel {
391
399
  */
392
400
  updateGridOptions(options: Partial<Grid.GridOptions>): void;
393
401
  /**
394
- * 更新网格配置
402
+ * 更新网格尺寸
403
+ */
404
+ updateGridSize(size: number): void;
405
+ /**
406
+ * 更新背景配置
395
407
  */
396
408
  updateBackgroundOptions(options: boolean | Partial<LFOptions.BackgroundConfig>): void;
397
409
  /**
@@ -67,6 +67,7 @@ var overlay_1 = require("../view/overlay");
67
67
  var GraphModel = /** @class */ (function () {
68
68
  function GraphModel(options) {
69
69
  var _this = this;
70
+ var _a, _b;
70
71
  // 维护所有节点和边类型对应的 model
71
72
  this.modelMap = new Map();
72
73
  // Remind:用于记录当前画布上所有节点和边的 model 的 Map
@@ -100,11 +101,13 @@ var GraphModel = /** @class */ (function () {
100
101
  // 控制是否开启局部渲染
101
102
  this.partial = false;
102
103
  this.waitCleanEffects = [];
103
- var container = options.container, partial = options.partial, _a = options.background, background = _a === void 0 ? {} : _a, grid = options.grid, idGenerator = options.idGenerator, edgeGenerator = options.edgeGenerator, animation = options.animation, customTrajectory = options.customTrajectory;
104
+ var container = options.container, partial = options.partial, _c = options.background, background = _c === void 0 ? {} : _c, grid = options.grid, idGenerator = options.idGenerator, edgeGenerator = options.edgeGenerator, animation = options.animation, customTrajectory = options.customTrajectory;
104
105
  this.rootEl = container;
105
106
  this.partial = !!partial;
106
107
  this.background = background;
107
- if (typeof grid === 'object') {
108
+ if (typeof grid === 'object' && options.snapGrid) {
109
+ // 开启网格对齐时才根据网格尺寸设置步长
110
+ // TODO:需要让用户设置成 0 吗?后面可以讨论一下
108
111
  this.gridSize = grid.size || 1; // 默认 gridSize 设置为 1
109
112
  }
110
113
  this.theme = (0, util_1.setupTheme)(options.style);
@@ -112,8 +115,10 @@ var GraphModel = /** @class */ (function () {
112
115
  this.edgeType = options.edgeType || 'polyline';
113
116
  this.animation = (0, util_1.setupAnimation)(animation);
114
117
  this.overlapMode = options.overlapMode || constant_1.OverlapMode.DEFAULT;
115
- this.width = options.width || this.rootEl.getBoundingClientRect().width;
116
- this.height = options.height || this.rootEl.getBoundingClientRect().height;
118
+ this.width = (_a = options.width) !== null && _a !== void 0 ? _a : this.rootEl.getBoundingClientRect().width;
119
+ this.isContainerWidth = (0, lodash_es_1.isNil)(options.width);
120
+ this.height = (_b = options.height) !== null && _b !== void 0 ? _b : this.rootEl.getBoundingClientRect().height;
121
+ this.isContainerHeight = (0, lodash_es_1.isNil)(options.height);
117
122
  var resizeObserver = new ResizeObserver((0, lodash_es_1.debounce)((function (entries) {
118
123
  var e_1, _a;
119
124
  try {
@@ -395,17 +400,23 @@ var GraphModel = /** @class */ (function () {
395
400
  */
396
401
  GraphModel.prototype.graphDataToModel = function (graphData) {
397
402
  var _this = this;
398
- if (!this.width || !this.height) {
399
- this.resize();
400
- }
403
+ // 宽度必然存在,取消重新计算
404
+ // if (!this.width || !this.height) {
405
+ // this.resize()
406
+ // }
401
407
  if (!graphData) {
402
- this.nodes = [];
403
- this.edges = [];
408
+ this.clearData();
404
409
  return;
405
410
  }
411
+ this.elementsModelMap.clear();
412
+ this.nodeModelMap.clear();
413
+ this.edgeModelMap.clear();
406
414
  if (graphData.nodes) {
407
415
  this.nodes = (0, lodash_es_1.map)(graphData.nodes, function (node) {
408
- return _this.getModelAfterSnapToGrid(node);
416
+ var nodeModel = _this.getModelAfterSnapToGrid(node);
417
+ _this.elementsModelMap.set(nodeModel.id, nodeModel);
418
+ _this.nodeModelMap.set(nodeModel.id, nodeModel);
419
+ return nodeModel;
409
420
  });
410
421
  }
411
422
  else {
@@ -420,31 +431,6 @@ var GraphModel = /** @class */ (function () {
420
431
  throw new Error("\u627E\u4E0D\u5230".concat(edge.type, "\u5BF9\u5E94\u7684\u8FB9\u3002"));
421
432
  }
422
433
  var edgeModel = new Model(edge, _this);
423
- // 根据edgeModel中存储的数据找到当前画布上的起终锚点坐标
424
- // 判断当前起终锚点数据和Model中存储的起终点数据是否一致,不一致更新起终点信息
425
- var sourceNodeId = edgeModel.sourceNodeId, targetNodeId = edgeModel.targetNodeId, _b = edgeModel.sourceAnchorId, sourceAnchorId = _b === void 0 ? '' : _b, _c = edgeModel.targetAnchorId, targetAnchorId = _c === void 0 ? '' : _c, startPoint = edgeModel.startPoint, endPoint = edgeModel.endPoint, text = edgeModel.text, textPosition = edgeModel.textPosition;
426
- var updateAnchorPoint = function (node, anchorId, point, updatePoint) {
427
- var anchor = node === null || node === void 0 ? void 0 : node.anchors.find(function (anchor) { return anchor.id === anchorId; });
428
- if (anchor && !(0, lodash_es_1.isEqual)(anchor, point)) {
429
- updatePoint(anchor);
430
- }
431
- };
432
- var sourceNode = _this.getNodeModelById(sourceNodeId);
433
- var targetNode = _this.getNodeModelById(targetNodeId);
434
- updateAnchorPoint(sourceNode, sourceAnchorId, startPoint, edgeModel.updateStartPoint.bind(edgeModel));
435
- updateAnchorPoint(targetNode, targetAnchorId, endPoint, edgeModel.updateEndPoint.bind(edgeModel));
436
- // 而文本需要先算一下文本与默认文本位置之间的相对位置差
437
- // 再计算新路径的文本默认位置,加上相对位置差,得到调整后边的文本的位置
438
- if (text) {
439
- var x = text.x, y = text.y;
440
- var defaultX = textPosition.x, defaultY = textPosition.y;
441
- if (x && y && defaultX && defaultY) {
442
- var deltaX = x - defaultX;
443
- var deltaY = y - defaultY;
444
- edgeModel.resetTextPosition();
445
- edgeModel.moveText(deltaX, deltaY);
446
- }
447
- }
448
434
  _this.edgeModelMap.set(edgeModel.id, edgeModel);
449
435
  _this.elementsModelMap.set(edgeModel.id, edgeModel);
450
436
  return edgeModel;
@@ -778,14 +764,15 @@ var GraphModel = /** @class */ (function () {
778
764
  */
779
765
  GraphModel.prototype.getModelAfterSnapToGrid = function (node) {
780
766
  var Model = this.getModel(node.type);
767
+ var snapGrid = this.editConfigModel.snapGrid;
781
768
  if (!Model) {
782
769
  throw new Error("\u627E\u4E0D\u5230".concat(node.type, "\u5BF9\u5E94\u7684\u8282\u70B9\uFF0C\u8BF7\u786E\u8BA4\u662F\u5426\u5DF2\u6CE8\u518C\u6B64\u7C7B\u578B\u8282\u70B9\u3002"));
783
770
  }
784
771
  var nodeX = node.x, nodeY = node.y;
785
772
  // 根据 grid 修正节点的 x, y
786
773
  if (nodeX && nodeY) {
787
- node.x = (0, util_1.snapToGrid)(nodeX, this.gridSize);
788
- node.y = (0, util_1.snapToGrid)(nodeY, this.gridSize);
774
+ node.x = (0, util_1.snapToGrid)(nodeX, this.gridSize, snapGrid);
775
+ node.y = (0, util_1.snapToGrid)(nodeY, this.gridSize, snapGrid);
789
776
  if (typeof node.text === 'object' && node.text !== null) {
790
777
  // 原来的处理是:node.text.x -= getGridOffset(nodeX, this.gridSize)
791
778
  // 由于snapToGrid()使用了Math.round()四舍五入的做法,因此无法判断需要执行
@@ -1321,7 +1308,13 @@ var GraphModel = /** @class */ (function () {
1321
1308
  (0, lodash_es_1.merge)(this.grid, options);
1322
1309
  };
1323
1310
  /**
1324
- * 更新网格配置
1311
+ * 更新网格尺寸
1312
+ */
1313
+ GraphModel.prototype.updateGridSize = function (size) {
1314
+ this.gridSize = size;
1315
+ };
1316
+ /**
1317
+ * 更新背景配置
1325
1318
  */
1326
1319
  GraphModel.prototype.updateBackgroundOptions = function (options) {
1327
1320
  if ((0, lodash_es_1.isBoolean)(options) || (0, lodash_es_1.isBoolean)(this.background)) {
@@ -1335,8 +1328,10 @@ var GraphModel = /** @class */ (function () {
1335
1328
  * 重新设置画布的宽高
1336
1329
  */
1337
1330
  GraphModel.prototype.resize = function (width, height) {
1338
- this.width = width || this.rootEl.getBoundingClientRect().width;
1339
- this.height = height || this.rootEl.getBoundingClientRect().height;
1331
+ this.width = width !== null && width !== void 0 ? width : this.rootEl.getBoundingClientRect().width;
1332
+ this.isContainerWidth = (0, lodash_es_1.isNil)(width);
1333
+ this.height = height !== null && height !== void 0 ? height : this.rootEl.getBoundingClientRect().height;
1334
+ this.isContainerHeight = (0, lodash_es_1.isNil)(height);
1340
1335
  if (!this.width || !this.height) {
1341
1336
  console.warn('渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675');
1342
1337
  }
@@ -1347,6 +1342,10 @@ var GraphModel = /** @class */ (function () {
1347
1342
  GraphModel.prototype.clearData = function () {
1348
1343
  this.nodes = [];
1349
1344
  this.edges = [];
1345
+ // 清除对已清除节点的引用
1346
+ this.edgeModelMap.clear();
1347
+ this.nodeModelMap.clear();
1348
+ this.elementsModelMap.clear();
1350
1349
  };
1351
1350
  /**
1352
1351
  * 获取图形区域虚拟矩型的尺寸和中心坐标
@@ -1463,6 +1462,7 @@ var GraphModel = /** @class */ (function () {
1463
1462
  console.warn('error on destroy GraphModel', err);
1464
1463
  }
1465
1464
  this.waitCleanEffects.length = 0;
1465
+ this.eventCenter.destroy();
1466
1466
  };
1467
1467
  __decorate([
1468
1468
  mobx_1.observable
@@ -320,6 +320,8 @@ var BaseEdgeModel = /** @class */ (function () {
320
320
  properties: properties,
321
321
  sourceNodeId: this.sourceNodeId,
322
322
  targetNodeId: this.targetNodeId,
323
+ sourceAnchorId: this.sourceAnchorId,
324
+ targetAnchorId: this.targetAnchorId,
323
325
  startPoint: (0, lodash_es_1.assign)({}, this.startPoint),
324
326
  endPoint: (0, lodash_es_1.assign)({}, this.endPoint),
325
327
  };
@@ -419,14 +421,15 @@ var BaseEdgeModel = /** @class */ (function () {
419
421
  */
420
422
  BaseEdgeModel.prototype.formatText = function (data) {
421
423
  var _a, _b, _c;
422
- var _d = this.textPosition, x = _d.x, y = _d.y;
424
+ var _d = this.graphModel.editConfigModel, edgeTextDraggable = _d.edgeTextDraggable, edgeTextEdit = _d.edgeTextEdit;
425
+ var _e = this.textPosition, x = _e.x, y = _e.y;
423
426
  var text = data.text;
424
427
  var textConfig = {
425
428
  value: '',
426
429
  x: x,
427
430
  y: y,
428
- draggable: false,
429
- editable: true,
431
+ draggable: edgeTextDraggable,
432
+ editable: edgeTextEdit,
430
433
  };
431
434
  if (text) {
432
435
  if (typeof text === 'string') {
@@ -68,7 +68,7 @@ var BezierEdgeModel = /** @class */ (function (_super) {
68
68
  return _this;
69
69
  }
70
70
  BezierEdgeModel.prototype.initEdgeData = function (data) {
71
- this.offset = 100;
71
+ this.offset = (0, lodash_es_1.get)(data, 'properties.offset', 100);
72
72
  _super.prototype.initEdgeData.call(this, data);
73
73
  };
74
74
  BezierEdgeModel.prototype.getEdgeStyle = function () {
@@ -33,6 +33,7 @@ export declare class PolylineEdgeModel extends BaseEdgeModel {
33
33
  removeCrossPoints(startIndex: number, endIndex: number, pointList: Point[]): LogicFlow.Point[];
34
34
  getDraggingPoints(direction: SegmentDirection, positionType: string, position: Position, anchorList: AnchorConfig[], draggingPointList: Point[]): LogicFlow.Point[];
35
35
  updateCrossPoints(pointList: Point[]): LogicFlow.Point[];
36
+ updatePath(pointList: Point[]): void;
36
37
  getData(): LogicFlow.EdgeData & {
37
38
  pointsList: {
38
39
  x: number;
@@ -72,7 +72,10 @@ var PolylineEdgeModel = /** @class */ (function (_super) {
72
72
  return _this;
73
73
  }
74
74
  PolylineEdgeModel.prototype.initEdgeData = function (data) {
75
- this.offset = 30;
75
+ this.offset = (0, lodash_es_1.get)(data, 'properties.offset', 30);
76
+ if (data.pointsList) {
77
+ this.pointsList = data.pointsList;
78
+ }
76
79
  _super.prototype.initEdgeData.call(this, data);
77
80
  };
78
81
  PolylineEdgeModel.prototype.getEdgeStyle = function () {
@@ -285,6 +288,10 @@ var PolylineEdgeModel = /** @class */ (function (_super) {
285
288
  }
286
289
  return list;
287
290
  };
291
+ PolylineEdgeModel.prototype.updatePath = function (pointList) {
292
+ this.pointsList = pointList;
293
+ this.points = this.getPath(this.pointsList);
294
+ };
288
295
  PolylineEdgeModel.prototype.getData = function () {
289
296
  var data = _super.prototype.getData.call(this);
290
297
  var pointsList = this.pointsList.map(function (_a) {