@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/LogicFlow.js CHANGED
@@ -36,7 +36,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
36
36
  };
37
37
  import { jsx as _jsx } from "preact/jsx-runtime";
38
38
  import { createElement as h, render } from 'preact/compat';
39
- import { cloneDeep, forEach, indexOf } from 'lodash-es';
39
+ import { cloneDeep, forEach, indexOf, isNil } from 'lodash-es';
40
40
  import { observer } from '.';
41
41
  import { Options as LFOptions } from './options';
42
42
  import * as _Model from './model';
@@ -777,12 +777,18 @@ var LogicFlow = /** @class */ (function () {
777
777
  */
778
778
  LogicFlow.prototype.updateEditConfig = function (config) {
779
779
  var _a = this.graphModel, editConfigModel = _a.editConfigModel, transformModel = _a.transformModel;
780
+ var currentSnapGrid = editConfigModel.snapGrid;
780
781
  editConfigModel.updateEditConfig(config);
781
782
  if ((config === null || config === void 0 ? void 0 : config.stopMoveGraph) !== undefined) {
782
783
  transformModel.updateTranslateLimits(config.stopMoveGraph);
783
784
  }
784
785
  // 静默模式切换时,修改快捷键的启用状态
785
786
  (config === null || config === void 0 ? void 0 : config.isSilentMode) ? this.keyboard.disable() : this.keyboard.enable(true);
787
+ // 切换网格对齐状态时,修改网格尺寸
788
+ if (!isNil(config === null || config === void 0 ? void 0 : config.snapGrid) && config.snapGrid !== currentSnapGrid) {
789
+ var _b = this.graphModel.grid.size, size = _b === void 0 ? 1 : _b;
790
+ this.graphModel.updateGridSize(config.snapGrid ? size : 1);
791
+ }
786
792
  };
787
793
  /**
788
794
  * 获取流程图当前编辑相关设置
@@ -907,6 +913,8 @@ var LogicFlow = /** @class */ (function () {
907
913
  */
908
914
  LogicFlow.prototype.clearData = function () {
909
915
  this.graphModel.clearData();
916
+ // 强制刷新数据, 让 preact 清除对已删除节点的引用
917
+ this.render({});
910
918
  };
911
919
  /*********************************************************
912
920
  * LogicFlow Render方法
@@ -1168,7 +1176,19 @@ var LogicFlow = /** @class */ (function () {
1168
1176
  };
1169
1177
  /** 销毁当前实例 */
1170
1178
  LogicFlow.prototype.destroy = function () {
1179
+ var _a;
1180
+ this.clearData();
1181
+ render(null, this.container);
1182
+ this.keyboard.destroy();
1171
1183
  this.graphModel.destroy();
1184
+ this.tool.destroy();
1185
+ this.history.destroy();
1186
+ for (var extensionName in this.extension) {
1187
+ var extensionInstance = this.extension[extensionName];
1188
+ if ('destroy' in extensionInstance) {
1189
+ (_a = extensionInstance.destroy) === null || _a === void 0 ? void 0 : _a.call(extensionInstance);
1190
+ }
1191
+ }
1172
1192
  };
1173
1193
  // 全局配置的插件,所有的LogicFlow示例都会使用
1174
1194
  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 };
@@ -84,6 +84,9 @@ var EventEmitter = /** @class */ (function () {
84
84
  EventEmitter.prototype.getEvents = function () {
85
85
  return this._events;
86
86
  };
87
+ EventEmitter.prototype.destroy = function () {
88
+ this._events = {};
89
+ };
87
90
  return EventEmitter;
88
91
  }());
89
92
  export default 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;
@@ -104,6 +104,12 @@ var History = /** @class */ (function () {
104
104
  }
105
105
  }, this.waitTime));
106
106
  };
107
+ History.prototype.destroy = function () {
108
+ this.undos = [];
109
+ this.redos = [];
110
+ this.curData = null;
111
+ this.stopWatch && this.stopWatch();
112
+ };
107
113
  return History;
108
114
  }());
109
115
  export { 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
  }
@@ -68,6 +68,9 @@ var Keyboard = /** @class */ (function () {
68
68
  }
69
69
  }
70
70
  };
71
+ Keyboard.prototype.destroy = function () {
72
+ this.mousetrap.reset();
73
+ };
71
74
  Keyboard.prototype.getKeys = function (keys) {
72
75
  var _this = this;
73
76
  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
  }
@@ -33,6 +33,7 @@ var allKeys = [
33
33
  'stopZoomGraph', // 禁止缩放画布
34
34
  'stopScrollGraph', // 禁止鼠标滚动移动画布
35
35
  'stopMoveGraph', // 禁止拖动画布
36
+ 'snapGrid', // 是否开启网格对齐
36
37
  'adjustEdge', // 允许调整边
37
38
  'adjustEdgeMiddle', // 允许调整边中点
38
39
  'adjustEdgeStartAndEnd', // 允许调整边起点和终点
@@ -74,6 +75,7 @@ var EditConfigModel = /** @class */ (function () {
74
75
  this.stopZoomGraph = false;
75
76
  this.stopMoveGraph = false;
76
77
  this.stopScrollGraph = false;
78
+ this.snapGrid = false;
77
79
  /*********************************************************
78
80
  * 文本相关配置(全局)
79
81
  ********************************************************/
@@ -188,6 +190,9 @@ var EditConfigModel = /** @class */ (function () {
188
190
  __decorate([
189
191
  observable
190
192
  ], EditConfigModel.prototype, "stopScrollGraph", void 0);
193
+ __decorate([
194
+ observable
195
+ ], EditConfigModel.prototype, "snapGrid", void 0);
191
196
  __decorate([
192
197
  observable
193
198
  ], 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
  /**
@@ -51,7 +51,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
51
51
  }
52
52
  return to.concat(ar || Array.prototype.slice.call(from));
53
53
  };
54
- import { find, forEach, map, merge, isBoolean, debounce, isEqual } from 'lodash-es';
54
+ import { find, forEach, map, merge, isBoolean, debounce, isNil, } from 'lodash-es';
55
55
  import { action, computed, observable } from 'mobx';
56
56
  import { EditConfigModel, TransformModel, } from '.';
57
57
  import { DEFAULT_VISIBLE_SPACE, ELEMENT_MAX_Z_INDEX, ElementState, ElementType, EventType, ModelType, OverlapMode, TextMode, } from '../constant';
@@ -61,6 +61,7 @@ import { Grid } from '../view/overlay';
61
61
  var GraphModel = /** @class */ (function () {
62
62
  function GraphModel(options) {
63
63
  var _this = this;
64
+ var _a, _b;
64
65
  // 维护所有节点和边类型对应的 model
65
66
  this.modelMap = new Map();
66
67
  // Remind:用于记录当前画布上所有节点和边的 model 的 Map
@@ -94,11 +95,13 @@ var GraphModel = /** @class */ (function () {
94
95
  // 控制是否开启局部渲染
95
96
  this.partial = false;
96
97
  this.waitCleanEffects = [];
97
- 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;
98
+ 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;
98
99
  this.rootEl = container;
99
100
  this.partial = !!partial;
100
101
  this.background = background;
101
- if (typeof grid === 'object') {
102
+ if (typeof grid === 'object' && options.snapGrid) {
103
+ // 开启网格对齐时才根据网格尺寸设置步长
104
+ // TODO:需要让用户设置成 0 吗?后面可以讨论一下
102
105
  this.gridSize = grid.size || 1; // 默认 gridSize 设置为 1
103
106
  }
104
107
  this.theme = setupTheme(options.style);
@@ -106,8 +109,10 @@ var GraphModel = /** @class */ (function () {
106
109
  this.edgeType = options.edgeType || 'polyline';
107
110
  this.animation = setupAnimation(animation);
108
111
  this.overlapMode = options.overlapMode || OverlapMode.DEFAULT;
109
- this.width = options.width || this.rootEl.getBoundingClientRect().width;
110
- this.height = options.height || this.rootEl.getBoundingClientRect().height;
112
+ this.width = (_a = options.width) !== null && _a !== void 0 ? _a : this.rootEl.getBoundingClientRect().width;
113
+ this.isContainerWidth = isNil(options.width);
114
+ this.height = (_b = options.height) !== null && _b !== void 0 ? _b : this.rootEl.getBoundingClientRect().height;
115
+ this.isContainerHeight = isNil(options.height);
111
116
  var resizeObserver = new ResizeObserver(debounce((function (entries) {
112
117
  var e_1, _a;
113
118
  try {
@@ -389,17 +394,23 @@ var GraphModel = /** @class */ (function () {
389
394
  */
390
395
  GraphModel.prototype.graphDataToModel = function (graphData) {
391
396
  var _this = this;
392
- if (!this.width || !this.height) {
393
- this.resize();
394
- }
397
+ // 宽度必然存在,取消重新计算
398
+ // if (!this.width || !this.height) {
399
+ // this.resize()
400
+ // }
395
401
  if (!graphData) {
396
- this.nodes = [];
397
- this.edges = [];
402
+ this.clearData();
398
403
  return;
399
404
  }
405
+ this.elementsModelMap.clear();
406
+ this.nodeModelMap.clear();
407
+ this.edgeModelMap.clear();
400
408
  if (graphData.nodes) {
401
409
  this.nodes = map(graphData.nodes, function (node) {
402
- return _this.getModelAfterSnapToGrid(node);
410
+ var nodeModel = _this.getModelAfterSnapToGrid(node);
411
+ _this.elementsModelMap.set(nodeModel.id, nodeModel);
412
+ _this.nodeModelMap.set(nodeModel.id, nodeModel);
413
+ return nodeModel;
403
414
  });
404
415
  }
405
416
  else {
@@ -414,31 +425,6 @@ var GraphModel = /** @class */ (function () {
414
425
  throw new Error("\u627E\u4E0D\u5230".concat(edge.type, "\u5BF9\u5E94\u7684\u8FB9\u3002"));
415
426
  }
416
427
  var edgeModel = new Model(edge, _this);
417
- // 根据edgeModel中存储的数据找到当前画布上的起终锚点坐标
418
- // 判断当前起终锚点数据和Model中存储的起终点数据是否一致,不一致更新起终点信息
419
- 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;
420
- var updateAnchorPoint = function (node, anchorId, point, updatePoint) {
421
- var anchor = node === null || node === void 0 ? void 0 : node.anchors.find(function (anchor) { return anchor.id === anchorId; });
422
- if (anchor && !isEqual(anchor, point)) {
423
- updatePoint(anchor);
424
- }
425
- };
426
- var sourceNode = _this.getNodeModelById(sourceNodeId);
427
- var targetNode = _this.getNodeModelById(targetNodeId);
428
- updateAnchorPoint(sourceNode, sourceAnchorId, startPoint, edgeModel.updateStartPoint.bind(edgeModel));
429
- updateAnchorPoint(targetNode, targetAnchorId, endPoint, edgeModel.updateEndPoint.bind(edgeModel));
430
- // 而文本需要先算一下文本与默认文本位置之间的相对位置差
431
- // 再计算新路径的文本默认位置,加上相对位置差,得到调整后边的文本的位置
432
- if (text) {
433
- var x = text.x, y = text.y;
434
- var defaultX = textPosition.x, defaultY = textPosition.y;
435
- if (x && y && defaultX && defaultY) {
436
- var deltaX = x - defaultX;
437
- var deltaY = y - defaultY;
438
- edgeModel.resetTextPosition();
439
- edgeModel.moveText(deltaX, deltaY);
440
- }
441
- }
442
428
  _this.edgeModelMap.set(edgeModel.id, edgeModel);
443
429
  _this.elementsModelMap.set(edgeModel.id, edgeModel);
444
430
  return edgeModel;
@@ -772,14 +758,15 @@ var GraphModel = /** @class */ (function () {
772
758
  */
773
759
  GraphModel.prototype.getModelAfterSnapToGrid = function (node) {
774
760
  var Model = this.getModel(node.type);
761
+ var snapGrid = this.editConfigModel.snapGrid;
775
762
  if (!Model) {
776
763
  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"));
777
764
  }
778
765
  var nodeX = node.x, nodeY = node.y;
779
766
  // 根据 grid 修正节点的 x, y
780
767
  if (nodeX && nodeY) {
781
- node.x = snapToGrid(nodeX, this.gridSize);
782
- node.y = snapToGrid(nodeY, this.gridSize);
768
+ node.x = snapToGrid(nodeX, this.gridSize, snapGrid);
769
+ node.y = snapToGrid(nodeY, this.gridSize, snapGrid);
783
770
  if (typeof node.text === 'object' && node.text !== null) {
784
771
  // 原来的处理是:node.text.x -= getGridOffset(nodeX, this.gridSize)
785
772
  // 由于snapToGrid()使用了Math.round()四舍五入的做法,因此无法判断需要执行
@@ -1315,7 +1302,13 @@ var GraphModel = /** @class */ (function () {
1315
1302
  merge(this.grid, options);
1316
1303
  };
1317
1304
  /**
1318
- * 更新网格配置
1305
+ * 更新网格尺寸
1306
+ */
1307
+ GraphModel.prototype.updateGridSize = function (size) {
1308
+ this.gridSize = size;
1309
+ };
1310
+ /**
1311
+ * 更新背景配置
1319
1312
  */
1320
1313
  GraphModel.prototype.updateBackgroundOptions = function (options) {
1321
1314
  if (isBoolean(options) || isBoolean(this.background)) {
@@ -1329,8 +1322,10 @@ var GraphModel = /** @class */ (function () {
1329
1322
  * 重新设置画布的宽高
1330
1323
  */
1331
1324
  GraphModel.prototype.resize = function (width, height) {
1332
- this.width = width || this.rootEl.getBoundingClientRect().width;
1333
- this.height = height || this.rootEl.getBoundingClientRect().height;
1325
+ this.width = width !== null && width !== void 0 ? width : this.rootEl.getBoundingClientRect().width;
1326
+ this.isContainerWidth = isNil(width);
1327
+ this.height = height !== null && height !== void 0 ? height : this.rootEl.getBoundingClientRect().height;
1328
+ this.isContainerHeight = isNil(height);
1334
1329
  if (!this.width || !this.height) {
1335
1330
  console.warn('渲染画布的时候无法获取画布宽高,请确认在container已挂载到DOM。@see https://github.com/didi/LogicFlow/issues/675');
1336
1331
  }
@@ -1341,6 +1336,10 @@ var GraphModel = /** @class */ (function () {
1341
1336
  GraphModel.prototype.clearData = function () {
1342
1337
  this.nodes = [];
1343
1338
  this.edges = [];
1339
+ // 清除对已清除节点的引用
1340
+ this.edgeModelMap.clear();
1341
+ this.nodeModelMap.clear();
1342
+ this.elementsModelMap.clear();
1344
1343
  };
1345
1344
  /**
1346
1345
  * 获取图形区域虚拟矩型的尺寸和中心坐标
@@ -1457,6 +1456,7 @@ var GraphModel = /** @class */ (function () {
1457
1456
  console.warn('error on destroy GraphModel', err);
1458
1457
  }
1459
1458
  this.waitCleanEffects.length = 0;
1459
+ this.eventCenter.destroy();
1460
1460
  };
1461
1461
  __decorate([
1462
1462
  observable
@@ -317,6 +317,8 @@ var BaseEdgeModel = /** @class */ (function () {
317
317
  properties: properties,
318
318
  sourceNodeId: this.sourceNodeId,
319
319
  targetNodeId: this.targetNodeId,
320
+ sourceAnchorId: this.sourceAnchorId,
321
+ targetAnchorId: this.targetAnchorId,
320
322
  startPoint: assign({}, this.startPoint),
321
323
  endPoint: assign({}, this.endPoint),
322
324
  };
@@ -416,14 +418,15 @@ var BaseEdgeModel = /** @class */ (function () {
416
418
  */
417
419
  BaseEdgeModel.prototype.formatText = function (data) {
418
420
  var _a, _b, _c;
419
- var _d = this.textPosition, x = _d.x, y = _d.y;
421
+ var _d = this.graphModel.editConfigModel, edgeTextDraggable = _d.edgeTextDraggable, edgeTextEdit = _d.edgeTextEdit;
422
+ var _e = this.textPosition, x = _e.x, y = _e.y;
420
423
  var text = data.text;
421
424
  var textConfig = {
422
425
  value: '',
423
426
  x: x,
424
427
  y: y,
425
- draggable: false,
426
- editable: true,
428
+ draggable: edgeTextDraggable,
429
+ editable: edgeTextEdit,
427
430
  };
428
431
  if (text) {
429
432
  if (typeof text === 'string') {
@@ -46,7 +46,7 @@ var __read = (this && this.__read) || function (o, n) {
46
46
  }
47
47
  return ar;
48
48
  };
49
- import { assign, cloneDeep } from 'lodash-es';
49
+ import { get, assign, cloneDeep } from 'lodash-es';
50
50
  import { action, observable } from 'mobx';
51
51
  import BaseEdgeModel from './BaseEdgeModel';
52
52
  import { ModelType } from '../../constant';
@@ -62,7 +62,7 @@ var BezierEdgeModel = /** @class */ (function (_super) {
62
62
  return _this;
63
63
  }
64
64
  BezierEdgeModel.prototype.initEdgeData = function (data) {
65
- this.offset = 100;
65
+ this.offset = get(data, 'properties.offset', 100);
66
66
  _super.prototype.initEdgeData.call(this, data);
67
67
  };
68
68
  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;
@@ -55,7 +55,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
55
55
  }
56
56
  return to.concat(ar || Array.prototype.slice.call(from));
57
57
  };
58
- import { assign, cloneDeep } from 'lodash-es';
58
+ import { get, assign, cloneDeep } from 'lodash-es';
59
59
  import { observable, action } from 'mobx';
60
60
  import { BaseEdgeModel } from '.';
61
61
  import { ModelType, SegmentDirection } from '../../constant';
@@ -69,7 +69,10 @@ var PolylineEdgeModel = /** @class */ (function (_super) {
69
69
  return _this;
70
70
  }
71
71
  PolylineEdgeModel.prototype.initEdgeData = function (data) {
72
- this.offset = 30;
72
+ this.offset = get(data, 'properties.offset', 30);
73
+ if (data.pointsList) {
74
+ this.pointsList = data.pointsList;
75
+ }
73
76
  _super.prototype.initEdgeData.call(this, data);
74
77
  };
75
78
  PolylineEdgeModel.prototype.getEdgeStyle = function () {
@@ -282,6 +285,10 @@ var PolylineEdgeModel = /** @class */ (function (_super) {
282
285
  }
283
286
  return list;
284
287
  };
288
+ PolylineEdgeModel.prototype.updatePath = function (pointList) {
289
+ this.pointsList = pointList;
290
+ this.points = this.getPath(this.pointsList);
291
+ };
285
292
  PolylineEdgeModel.prototype.getData = function () {
286
293
  var data = _super.prototype.getData.call(this);
287
294
  var pointsList = this.pointsList.map(function (_a) {
@@ -223,13 +223,14 @@ var BaseNodeModel = /** @class */ (function () {
223
223
  */
224
224
  BaseNodeModel.prototype.formatText = function (data) {
225
225
  var _a, _b, _c;
226
+ var _d = this.graphModel.editConfigModel, nodeTextDraggable = _d.nodeTextDraggable, nodeTextEdit = _d.nodeTextEdit;
226
227
  var x = data.x, y = data.y, text = data.text;
227
228
  var textConfig = {
228
229
  value: '',
229
230
  x: x,
230
231
  y: y,
231
- draggable: false,
232
- editable: true,
232
+ draggable: nodeTextDraggable,
233
+ editable: nodeTextEdit,
233
234
  };
234
235
  if (text) {
235
236
  if (typeof text === 'string') {
@@ -644,6 +645,10 @@ var BaseNodeModel = /** @class */ (function () {
644
645
  this.text && this.moveText(0, deltaY);
645
646
  moveY = deltaY;
646
647
  }
648
+ this.transform = new TranslateMatrix(-this.x, -this.y)
649
+ .rotate(this.rotate)
650
+ .translate(this.x, this.y)
651
+ .toString();
647
652
  return [moveX, moveY];
648
653
  };
649
654
  BaseNodeModel.prototype.moveTo = function (x, y, isIgnoreRule) {
@@ -20,5 +20,6 @@ export declare class Tool {
20
20
  enableTool(name: string): boolean | Error;
21
21
  getTools(): ToolConstructor[];
22
22
  getInstance(): LogicFlow;
23
+ destroy(): void;
23
24
  }
24
25
  export default Tool;
package/es/tool/index.js CHANGED
@@ -50,7 +50,7 @@ var Tool = /** @class */ (function () {
50
50
  this.toolMap.delete(name);
51
51
  return true;
52
52
  }
53
- throw new Error('禁用失败,不存在名为 ${tool} 的工具');
53
+ throw new Error("\u7981\u7528\u5931\u8D25\uFF0C\u4E0D\u5B58\u5728\u540D\u4E3A ".concat(tool, " \u7684\u5DE5\u5177"));
54
54
  };
55
55
  Tool.prototype.enableTool = function (name) {
56
56
  var tool = this.disabledToolMap.get(name);
@@ -59,7 +59,7 @@ var Tool = /** @class */ (function () {
59
59
  this.disabledToolMap.delete(name);
60
60
  return true;
61
61
  }
62
- throw new Error('不存在名为 ${tool} 的工具');
62
+ throw new Error("\u4E0D\u5B58\u5728\u540D\u4E3A ".concat(tool, " \u7684\u5DE5\u5177"));
63
63
  };
64
64
  Tool.prototype.getTools = function () {
65
65
  return Array.from(this.toolMap.values());
@@ -67,6 +67,10 @@ var Tool = /** @class */ (function () {
67
67
  Tool.prototype.getInstance = function () {
68
68
  return this.instance;
69
69
  };
70
+ Tool.prototype.destroy = function () {
71
+ this.toolMap.clear();
72
+ this.disabledToolMap.clear();
73
+ };
70
74
  __decorate([
71
75
  observable
72
76
  ], Tool.prototype, "toolMap", void 0);
@@ -1,6 +1,6 @@
1
1
  import LogicFlow from '../LogicFlow';
2
2
  import PointTuple = LogicFlow.PointTuple;
3
- export declare function snapToGrid(point: number, gridSize: number): number;
3
+ export declare function snapToGrid(point: number, gridSize: number, snapGrid: boolean): number;
4
4
  export declare function getGridOffset(distance: number, gridSize: number): number;
5
5
  /**
6
6
  * 多边形设置 points 后,坐标平移至原点 并 根据 width、height 缩放
@@ -23,7 +23,10 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
23
23
  }
24
24
  return to.concat(ar || Array.prototype.slice.call(from));
25
25
  };
26
- export function snapToGrid(point, gridSize) {
26
+ export function snapToGrid(point, gridSize, snapGrid) {
27
+ // 开启节网格对齐时才根据网格尺寸校准坐标
28
+ if (!snapGrid)
29
+ return point;
27
30
  // 保证 x, y 的值为 gridSize 的整数倍
28
31
  return gridSize * Math.round(point / gridSize) || point;
29
32
  }
package/es/util/node.d.ts CHANGED
@@ -15,7 +15,7 @@ export type NodeContaint = {
15
15
  export declare const targetNodeInfo: (position: Point, graphModel: GraphModel) => NodeContaint;
16
16
  export declare const getClosestAnchor: (position: Point, node: BaseNodeModel) => AnchorInfo;
17
17
  export declare const distance: (x1: number, y1: number, x2: number, y2: number) => number;
18
- export declare const isInNode: (position: Point, node: BaseNodeModel) => boolean;
18
+ export declare const isInNode: (position: Point, node: BaseNodeModel, offset?: number) => boolean;
19
19
  export declare const isInNodeBbox: (position: Point, node: BaseNodeModel) => boolean;
20
20
  export type NodeBBox = {
21
21
  x: number;
package/es/util/node.js CHANGED
@@ -9,10 +9,27 @@ var __assign = (this && this.__assign) || function () {
9
9
  };
10
10
  return __assign.apply(this, arguments);
11
11
  };
12
+ var __read = (this && this.__read) || function (o, n) {
13
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
14
+ if (!m) return o;
15
+ var i = m.call(o), r, ar = [], e;
16
+ try {
17
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
18
+ }
19
+ catch (error) { e = { error: error }; }
20
+ finally {
21
+ try {
22
+ if (r && !r.done && (m = i["return"])) m.call(i);
23
+ }
24
+ finally { if (e) throw e.error; }
25
+ }
26
+ return ar;
27
+ };
12
28
  import { pick } from 'lodash-es';
13
29
  import { getBytesLength } from './edge';
14
30
  import { SegmentDirection } from '../constant';
15
31
  import { isInSegment } from '../algorithm/edge';
32
+ import { Matrix } from './matrix';
16
33
  /* 获取所有锚点 */
17
34
  export var getAnchors = function (data) {
18
35
  var anchors = data.anchors;
@@ -72,29 +89,28 @@ export var getClosestAnchor = function (position, node) {
72
89
  /* 两点之间距离 */
73
90
  export var distance = function (x1, y1, x2, y2) { return Math.hypot(x1 - x2, y1 - y2); };
74
91
  /* 是否在某个节点内,手否进行连接,有offset控制粒度,与outline有关,可以优化 */
75
- export var isInNode = function (position, node) {
92
+ export var isInNode = function (position, node, offset) {
93
+ if (offset === void 0) { offset = 0; }
76
94
  var inNode = false;
77
- var offset = 0;
78
95
  var bBox = getNodeBBox(node);
79
- if (position.x >= bBox.minX - offset &&
80
- position.x <= bBox.maxX + offset &&
81
- position.y >= bBox.minY - offset &&
82
- position.y <= bBox.maxY + offset) {
96
+ var _a = __read(new Matrix([position.x, position.y, 1])
97
+ .translate(-node.x, -node.y)
98
+ .rotate(-node.rotate)
99
+ .translate(node.x, node.y)[0], 2), x = _a[0], y = _a[1];
100
+ var reverseRotatedPosition = {
101
+ x: x,
102
+ y: y,
103
+ };
104
+ if (reverseRotatedPosition.x >= bBox.minX - offset &&
105
+ reverseRotatedPosition.x <= bBox.maxX + offset &&
106
+ reverseRotatedPosition.y >= bBox.minY - offset &&
107
+ reverseRotatedPosition.y <= bBox.maxY + offset) {
83
108
  inNode = true;
84
109
  }
85
110
  return inNode;
86
111
  };
87
112
  export var isInNodeBbox = function (position, node) {
88
- var inNode = false;
89
- var offset = 5;
90
- var bBox = getNodeBBox(node);
91
- if (position.x >= bBox.minX - offset &&
92
- position.x <= bBox.maxX + offset &&
93
- position.y >= bBox.minY - offset &&
94
- position.y <= bBox.maxY + offset) {
95
- inNode = true;
96
- }
97
- return inNode;
113
+ return isInNode(position, node, 5);
98
114
  };
99
115
  /* 获取节点bbox */
100
116
  export var getNodeBBox = function (node) {