@antv/g6 5.0.0-beta.20 → 5.0.0-beta.22

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 (209) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/esm/index.d.ts +16 -9
  3. package/esm/index.js +1 -1
  4. package/esm/runtime/controller/data.d.ts +27 -10
  5. package/esm/runtime/controller/data.js +240 -216
  6. package/esm/runtime/controller/data.js.map +1 -1
  7. package/esm/runtime/controller/item.js +2 -0
  8. package/esm/runtime/controller/item.js.map +1 -1
  9. package/esm/runtime/graph.d.ts +11 -4
  10. package/esm/runtime/graph.js +23 -5
  11. package/esm/runtime/graph.js.map +1 -1
  12. package/esm/stdlib/behavior/activate-relations.d.ts +1 -1
  13. package/esm/stdlib/behavior/brush-select.d.ts +2 -2
  14. package/esm/stdlib/behavior/click-select.d.ts +1 -1
  15. package/esm/stdlib/behavior/click-select.js +3 -1
  16. package/esm/stdlib/behavior/click-select.js.map +1 -1
  17. package/esm/stdlib/behavior/collapse-expand-combo.d.ts +1 -1
  18. package/esm/stdlib/behavior/collapse-expand-tree.d.ts +1 -1
  19. package/esm/stdlib/behavior/drag-canvas.js +1 -1
  20. package/esm/stdlib/behavior/drag-combo.d.ts +1 -1
  21. package/esm/stdlib/behavior/drag-node.d.ts +1 -1
  22. package/esm/stdlib/behavior/shortcuts-call.d.ts +1 -1
  23. package/esm/stdlib/behavior/zoom-canvas.js +1 -1
  24. package/esm/stdlib/data/index.d.ts +1 -0
  25. package/esm/stdlib/data/index.js +1 -0
  26. package/esm/stdlib/data/index.js.map +1 -1
  27. package/esm/stdlib/data/mapNodeSize.d.ts +3 -4
  28. package/esm/stdlib/data/mapNodeSize.js +10 -0
  29. package/esm/stdlib/data/mapNodeSize.js.map +1 -1
  30. package/esm/stdlib/data/processParallelEdges.d.ts +21 -0
  31. package/esm/stdlib/data/processParallelEdges.js +127 -0
  32. package/esm/stdlib/data/processParallelEdges.js.map +1 -0
  33. package/esm/stdlib/data/transformV4Data.d.ts +2 -3
  34. package/esm/stdlib/data/transformV4Data.js +8 -0
  35. package/esm/stdlib/data/transformV4Data.js.map +1 -1
  36. package/esm/stdlib/data/validateData.d.ts +3 -4
  37. package/esm/stdlib/data/validateData.js +9 -1
  38. package/esm/stdlib/data/validateData.js.map +1 -1
  39. package/esm/stdlib/index.d.ts +15 -8
  40. package/esm/stdlib/index.js +2 -1
  41. package/esm/stdlib/index.js.map +1 -1
  42. package/esm/stdlib/item/node/donut.d.ts +3 -3
  43. package/esm/stdlib/plugin/hull/hullComponent.d.ts +1 -1
  44. package/esm/stdlib/plugin/legend/index.d.ts +1 -1
  45. package/esm/stdlib/plugin/snapline/index.d.ts +2 -2
  46. package/esm/stdlib/plugin/snapline/index.js +239 -239
  47. package/esm/stdlib/plugin/snapline/index.js.map +1 -1
  48. package/esm/stdlib/plugin/timebar/index.d.ts +4 -4
  49. package/esm/stdlib/plugin/timebar/index.js +4 -4
  50. package/esm/stdlib/plugin/timebar/index.js.map +1 -1
  51. package/esm/stdlib/plugin/tooltip/index.d.ts +1 -1
  52. package/esm/stdlib/plugin/watermaker/index.d.ts +1 -1
  53. package/esm/stdlib/themeSolver/spec.js +21 -6
  54. package/esm/stdlib/themeSolver/spec.js.map +1 -1
  55. package/esm/types/animate.d.ts +2 -2
  56. package/esm/types/behavior.d.ts +1 -1
  57. package/esm/types/combo.d.ts +7 -7
  58. package/esm/types/common.d.ts +5 -5
  59. package/esm/types/data.d.ts +11 -6
  60. package/esm/types/edge.d.ts +9 -9
  61. package/esm/types/event.d.ts +1 -1
  62. package/esm/types/graph.d.ts +6 -0
  63. package/esm/types/history.d.ts +2 -2
  64. package/esm/types/hook.d.ts +1 -1
  65. package/esm/types/item.d.ts +13 -13
  66. package/esm/types/item.js.map +1 -1
  67. package/esm/types/layout.d.ts +6 -6
  68. package/esm/types/loop.d.ts +2 -2
  69. package/esm/types/node.d.ts +7 -7
  70. package/esm/types/render.d.ts +1 -1
  71. package/esm/types/spec.d.ts +4 -3
  72. package/esm/types/stdlib.d.ts +1 -1
  73. package/esm/types/theme.d.ts +8 -8
  74. package/esm/types/view.d.ts +2 -2
  75. package/esm/util/array.d.ts +1 -0
  76. package/esm/util/array.js +3 -0
  77. package/esm/util/array.js.map +1 -1
  78. package/esm/util/data.d.ts +10 -0
  79. package/esm/util/data.js +36 -0
  80. package/esm/util/data.js.map +1 -1
  81. package/esm/util/event.d.ts +2 -2
  82. package/esm/util/layout.d.ts +1 -1
  83. package/esm/util/loop.d.ts +1 -0
  84. package/esm/util/loop.js +10 -0
  85. package/esm/util/loop.js.map +1 -1
  86. package/esm/util/shape.d.ts +2 -1
  87. package/esm/util/shape.js +2 -1
  88. package/esm/util/shape.js.map +1 -1
  89. package/esm/util/shape3d.d.ts +1 -1
  90. package/esm/util/warn.d.ts +1 -1
  91. package/lib/index.d.ts +16 -9
  92. package/lib/index.js +1 -1
  93. package/lib/runtime/controller/data.d.ts +27 -10
  94. package/lib/runtime/controller/data.js +238 -214
  95. package/lib/runtime/controller/data.js.map +1 -1
  96. package/lib/runtime/controller/item.js +2 -0
  97. package/lib/runtime/controller/item.js.map +1 -1
  98. package/lib/runtime/graph.d.ts +11 -4
  99. package/lib/runtime/graph.js +23 -5
  100. package/lib/runtime/graph.js.map +1 -1
  101. package/lib/stdlib/behavior/activate-relations.d.ts +1 -1
  102. package/lib/stdlib/behavior/brush-select.d.ts +2 -2
  103. package/lib/stdlib/behavior/click-select.d.ts +1 -1
  104. package/lib/stdlib/behavior/click-select.js +3 -1
  105. package/lib/stdlib/behavior/click-select.js.map +1 -1
  106. package/lib/stdlib/behavior/collapse-expand-combo.d.ts +1 -1
  107. package/lib/stdlib/behavior/collapse-expand-tree.d.ts +1 -1
  108. package/lib/stdlib/behavior/drag-canvas.js +1 -1
  109. package/lib/stdlib/behavior/drag-combo.d.ts +1 -1
  110. package/lib/stdlib/behavior/drag-node.d.ts +1 -1
  111. package/lib/stdlib/behavior/shortcuts-call.d.ts +1 -1
  112. package/lib/stdlib/behavior/zoom-canvas.js +1 -1
  113. package/lib/stdlib/data/index.d.ts +1 -0
  114. package/lib/stdlib/data/index.js +1 -0
  115. package/lib/stdlib/data/index.js.map +1 -1
  116. package/lib/stdlib/data/mapNodeSize.d.ts +3 -4
  117. package/lib/stdlib/data/mapNodeSize.js +11 -1
  118. package/lib/stdlib/data/mapNodeSize.js.map +1 -1
  119. package/lib/stdlib/data/processParallelEdges.d.ts +21 -0
  120. package/lib/stdlib/data/processParallelEdges.js +131 -0
  121. package/lib/stdlib/data/processParallelEdges.js.map +1 -0
  122. package/lib/stdlib/data/transformV4Data.d.ts +2 -3
  123. package/lib/stdlib/data/transformV4Data.js +9 -1
  124. package/lib/stdlib/data/transformV4Data.js.map +1 -1
  125. package/lib/stdlib/data/validateData.d.ts +3 -4
  126. package/lib/stdlib/data/validateData.js +10 -2
  127. package/lib/stdlib/data/validateData.js.map +1 -1
  128. package/lib/stdlib/index.d.ts +15 -8
  129. package/lib/stdlib/index.js +2 -1
  130. package/lib/stdlib/index.js.map +1 -1
  131. package/lib/stdlib/item/node/donut.d.ts +3 -3
  132. package/lib/stdlib/plugin/hull/hullComponent.d.ts +1 -1
  133. package/lib/stdlib/plugin/legend/index.d.ts +1 -1
  134. package/lib/stdlib/plugin/snapline/index.d.ts +2 -2
  135. package/lib/stdlib/plugin/snapline/index.js +239 -239
  136. package/lib/stdlib/plugin/snapline/index.js.map +1 -1
  137. package/lib/stdlib/plugin/timebar/index.d.ts +4 -4
  138. package/lib/stdlib/plugin/timebar/index.js +4 -4
  139. package/lib/stdlib/plugin/timebar/index.js.map +1 -1
  140. package/lib/stdlib/plugin/tooltip/index.d.ts +1 -1
  141. package/lib/stdlib/plugin/watermaker/index.d.ts +1 -1
  142. package/lib/stdlib/themeSolver/spec.js +20 -5
  143. package/lib/stdlib/themeSolver/spec.js.map +1 -1
  144. package/lib/types/animate.d.ts +2 -2
  145. package/lib/types/behavior.d.ts +1 -1
  146. package/lib/types/combo.d.ts +7 -7
  147. package/lib/types/common.d.ts +5 -5
  148. package/lib/types/data.d.ts +11 -6
  149. package/lib/types/edge.d.ts +9 -9
  150. package/lib/types/event.d.ts +1 -1
  151. package/lib/types/event.js +2 -2
  152. package/lib/types/event.js.map +1 -1
  153. package/lib/types/graph.d.ts +6 -0
  154. package/lib/types/history.d.ts +2 -2
  155. package/lib/types/history.js +1 -1
  156. package/lib/types/history.js.map +1 -1
  157. package/lib/types/hook.d.ts +1 -1
  158. package/lib/types/item.d.ts +13 -13
  159. package/lib/types/item.js +1 -1
  160. package/lib/types/item.js.map +1 -1
  161. package/lib/types/layout.d.ts +6 -6
  162. package/lib/types/loop.d.ts +2 -2
  163. package/lib/types/loop.js +1 -1
  164. package/lib/types/loop.js.map +1 -1
  165. package/lib/types/node.d.ts +7 -7
  166. package/lib/types/render.d.ts +1 -1
  167. package/lib/types/spec.d.ts +4 -3
  168. package/lib/types/stdlib.d.ts +1 -1
  169. package/lib/types/theme.d.ts +8 -8
  170. package/lib/types/view.d.ts +2 -2
  171. package/lib/util/array.d.ts +1 -0
  172. package/lib/util/array.js +5 -1
  173. package/lib/util/array.js.map +1 -1
  174. package/lib/util/data.d.ts +10 -0
  175. package/lib/util/data.js +38 -1
  176. package/lib/util/data.js.map +1 -1
  177. package/lib/util/event.d.ts +2 -2
  178. package/lib/util/layout.d.ts +1 -1
  179. package/lib/util/loop.d.ts +1 -0
  180. package/lib/util/loop.js +11 -1
  181. package/lib/util/loop.js.map +1 -1
  182. package/lib/util/shape.d.ts +2 -1
  183. package/lib/util/shape.js +1 -0
  184. package/lib/util/shape.js.map +1 -1
  185. package/lib/util/shape3d.d.ts +1 -1
  186. package/lib/util/warn.d.ts +1 -1
  187. package/package.json +3 -3
  188. package/src/index.ts +1 -1
  189. package/src/runtime/controller/data.ts +309 -229
  190. package/src/runtime/controller/item.ts +2 -0
  191. package/src/runtime/graph.ts +26 -8
  192. package/src/stdlib/behavior/click-select.ts +3 -1
  193. package/src/stdlib/behavior/drag-canvas.ts +1 -1
  194. package/src/stdlib/behavior/zoom-canvas.ts +1 -1
  195. package/src/stdlib/data/index.ts +1 -0
  196. package/src/stdlib/data/mapNodeSize.ts +20 -1
  197. package/src/stdlib/data/processParallelEdges.ts +203 -0
  198. package/src/stdlib/data/transformV4Data.ts +14 -1
  199. package/src/stdlib/data/validateData.ts +16 -2
  200. package/src/stdlib/index.ts +3 -1
  201. package/src/stdlib/themeSolver/spec.ts +23 -7
  202. package/src/types/data.ts +6 -0
  203. package/src/types/graph.ts +6 -0
  204. package/src/types/item.ts +2 -1
  205. package/src/types/spec.ts +1 -0
  206. package/src/util/array.ts +12 -0
  207. package/src/util/data.ts +46 -1
  208. package/src/util/loop.ts +11 -0
  209. package/src/util/shape.ts +1 -0
@@ -4,7 +4,7 @@
4
4
  * now: 当前用户输入值
5
5
  * scope: 选项所属的模块
6
6
  */
7
- declare type WarnOption = {
7
+ type WarnOption = {
8
8
  optionName: string;
9
9
  shouldBe: any;
10
10
  now: string | number;
package/lib/index.d.ts CHANGED
@@ -6,7 +6,7 @@ export * from './types';
6
6
  /**
7
7
  * Extend the graph class with std lib
8
8
  */
9
- declare const version = "5.0.0-beta.20";
9
+ declare const version = "5.0.0-beta.22";
10
10
  declare const Graph: {
11
11
  new (spec: import("./types").Specification<{
12
12
  'drag-canvas': typeof import("./stdlib/behavior").DragCanvas;
@@ -62,12 +62,12 @@ declare const _default: {
62
62
  Util: typeof Util;
63
63
  stdLib: {
64
64
  transforms: {
65
- 'validate-data': (data: import("./types").GraphData, options?: {}, graphCore?: import("./types").GraphCore) => import("./types").GraphData;
66
- 'transform-v4-data': (data: import("./types").GraphData, options?: {}, graphCore?: import("./types").GraphCore) => import("./types").GraphData;
67
- 'map-node-size': (data: import("./types").GraphData, options?: {
65
+ 'validate-data': (data: import("./types/data").GraphDataChanges, options?: {}, graphCore?: import("./types").GraphCore) => import("./types/data").GraphDataChanges;
66
+ 'transform-v4-data': (data: import("./types/data").GraphDataChanges, options?: {}, graphCore?: import("./types").GraphCore) => import("./types/data").GraphDataChanges;
67
+ 'map-node-size': (data: import("./types/data").GraphDataChanges, options?: {
68
68
  field?: string;
69
69
  range?: [number, number];
70
- }, graphCore?: import("./types").GraphCore) => import("./types").GraphData;
70
+ }, graphCore?: import("./types").GraphCore) => import("./types/data").GraphDataChanges;
71
71
  };
72
72
  themes: {
73
73
  light: import("./types").ThemeSpecification;
@@ -111,12 +111,19 @@ declare const _default: {
111
111
  };
112
112
  };
113
113
  Extensions: {
114
- ValidateData: (data: import("./types").GraphData, options?: {}, graphCore?: import("./types").GraphCore) => import("./types").GraphData;
115
- TransformV4Data: (data: import("./types").GraphData, options?: {}, graphCore?: import("./types").GraphCore) => import("./types").GraphData;
116
- MapNodeSize: (data: import("./types").GraphData, options?: {
114
+ ValidateData: (data: import("./types/data").GraphDataChanges, options?: {}, graphCore?: import("./types").GraphCore) => import("./types/data").GraphDataChanges;
115
+ TransformV4Data: (data: import("./types/data").GraphDataChanges, options?: {}, graphCore?: import("./types").GraphCore) => import("./types/data").GraphDataChanges;
116
+ MapNodeSize: (data: import("./types/data").GraphDataChanges, options?: {
117
117
  field?: string;
118
118
  range?: [number, number];
119
- }, graphCore?: import("./types").GraphCore) => import("./types").GraphData;
119
+ }, graphCore?: import("./types").GraphCore) => import("./types/data").GraphDataChanges;
120
+ ProcessParallelEdges: (data: import("./types/data").GraphDataChanges, options?: {
121
+ edgeIds?: import("@antv/graphlib/lib/types").ID[];
122
+ offsetDiff?: number;
123
+ multiEdgeType?: string;
124
+ singleEdgeType?: string;
125
+ loopEdgeType?: string;
126
+ }, graphCore?: import("./types").GraphCore) => import("./types/data").GraphDataChanges;
120
127
  LightTheme: import("./types").ThemeSpecification;
121
128
  DarkTheme: import("./types").ThemeSpecification;
122
129
  SpecThemeSolver: typeof import("./stdlib/themeSolver").SpecThemeSolver;
package/lib/index.js CHANGED
@@ -44,7 +44,7 @@ g_1.runtime.enableCSSParsing = false;
44
44
  /**
45
45
  * Extend the graph class with std lib
46
46
  */
47
- const version = '5.0.0-beta.20';
47
+ const version = '5.0.0-beta.22';
48
48
  exports.version = version;
49
49
  const Graph = (0, extend_1.extend)(graph_1.Graph, stdlib_1.stdLib);
50
50
  exports.Graph = Graph;
@@ -49,31 +49,48 @@ export declare class DataController {
49
49
  private onModeChange;
50
50
  private establishGraphCoreTree;
51
51
  /**
52
- * Change data by replace, merge repalce, or union.
52
+ * Change data by replace, merge repalce, union, remove or update.
53
53
  * @param data new data
54
- * @param changeType type of data change, 'replace' means discard the old data. 'mergeReplace' means merge the common part. 'union' means merge whole sets of old and new one
54
+ * @param changeType type of data change, 'replace' means discard the old data. 'mergeReplace' means merge the common part. 'union' means merge whole sets of old and new one. 'remove' means remove the common part. 'update' means update the comme part.
55
55
  */
56
56
  private changeData;
57
- private removeNode;
58
57
  /**
59
- * Remove part of old data.
60
- * @param data data to be removed which is part of old one
58
+ * Add new data.
59
+ * @param data data to be added which is not in graphCore
61
60
  */
62
- private removeData;
61
+ private doAdd;
63
62
  /**
64
63
  * Update part of old data.
65
- * @param data data to be updated which is part of old one
64
+ * @param data data to be updated which is part of old one in the graphCore
65
+ */
66
+ private doUpdate;
67
+ /**
68
+ * Remove part of old data.
69
+ * @param data data to be removed which is part of old one in the graphCore
66
70
  */
67
- private updateData;
71
+ private doRemove;
72
+ private removeNode;
68
73
  private formatData;
69
74
  /**
70
- * Relatively move the position of combo a dinstance (dx and dy in combo data).
75
+ * Relatively move the position of combo a distance (dx and dy in combo data).
71
76
  * @param data graph data which contains the combo dx dy info
72
77
  */
73
78
  private moveCombo;
74
79
  private addCombo;
75
80
  /**
76
- * Clone data from graphCore, and run transforms
81
+ * Preprocess the data and divide the data into three categories: Add, Update, Delete according to the operation type.
82
+ * @param data new graph data
83
+ * @param type type of data change
84
+ */
85
+ private preprocessData;
86
+ /**
87
+ * Determine whether to execute transform in the current data life cycle
88
+ * @param config transform plugin's configuration
89
+ * @param changeType type of data change
90
+ */
91
+ private isTransformActive;
92
+ /**
93
+ * run transforms on preprocessed data
77
94
  * @returns transformed data and the id map list
78
95
  */
79
96
  private transformData;
@@ -26,6 +26,22 @@ const behavior_1 = require("../../util/behavior");
26
26
  class DataController {
27
27
  constructor(graph) {
28
28
  this.extensions = [];
29
+ /**
30
+ * Determine whether to execute transform in the current data life cycle
31
+ * @param config transform plugin's configuration
32
+ * @param changeType type of data change
33
+ */
34
+ this.isTransformActive = (config, changeType) => {
35
+ let activeLifecycle = (0, util_1.isObject)(config) && 'activeLifecycle' in config
36
+ ? config.activeLifecycle
37
+ : data_1.DEFAULT_ACTIVE_DATA_LIFECYCLE;
38
+ activeLifecycle = Array.isArray(activeLifecycle)
39
+ ? activeLifecycle
40
+ : activeLifecycle === 'all'
41
+ ? data_1.AVAILABLE_DATA_LIFECYCLE
42
+ : [activeLifecycle];
43
+ return activeLifecycle.includes(data_1.dataLifecycleMap[changeType]);
44
+ };
29
45
  this.graph = graph;
30
46
  this.tap();
31
47
  }
@@ -134,12 +150,6 @@ class DataController {
134
150
  const { graphCore } = this;
135
151
  const change = () => {
136
152
  switch (changeType) {
137
- case 'remove':
138
- this.removeData(data);
139
- break;
140
- case 'update':
141
- this.updateData(data);
142
- break;
143
153
  case 'moveCombo':
144
154
  this.moveCombo(data);
145
155
  break;
@@ -147,7 +157,7 @@ class DataController {
147
157
  this.addCombo(data);
148
158
  break;
149
159
  default:
150
- // changeType is 'replace' | 'mergeReplace' | 'union'
160
+ // changeType is 'replace' | 'mergeReplace' | 'union' | 'remove' | 'update'
151
161
  this.changeData(data, changeType);
152
162
  break;
153
163
  }
@@ -214,9 +224,9 @@ class DataController {
214
224
  this.treeDirtyFlag = false;
215
225
  }
216
226
  /**
217
- * Change data by replace, merge repalce, or union.
227
+ * Change data by replace, merge repalce, union, remove or update.
218
228
  * @param data new data
219
- * @param changeType type of data change, 'replace' means discard the old data. 'mergeReplace' means merge the common part. 'union' means merge whole sets of old and new one
229
+ * @param changeType type of data change, 'replace' means discard the old data. 'mergeReplace' means merge the common part. 'union' means merge whole sets of old and new one. 'remove' means remove the common part. 'update' means update the comme part.
220
230
  */
221
231
  changeData(dataConfig, changeType) {
222
232
  var _a;
@@ -224,8 +234,10 @@ class DataController {
224
234
  if (!dataType)
225
235
  return;
226
236
  this.dataType = dataType;
227
- const { nodes = [], edges = [], combos = [] } = this.transformData(data);
237
+ const preprocessedData = this.preprocessData(data, changeType);
238
+ const { dataAdded, dataRemoved, dataUpdated } = this.transformData(preprocessedData, changeType);
228
239
  if (changeType === 'replace') {
240
+ const { nodes, edges, combos } = dataAdded;
229
241
  this.graphCore = new graphlib_1.Graph((0, util_1.clone)({
230
242
  nodes: nodes.concat((combos === null || combos === void 0 ? void 0 : combos.map((combo) => ({
231
243
  id: combo.id,
@@ -258,76 +270,142 @@ class DataController {
258
270
  }
259
271
  }
260
272
  else {
261
- const { graphCore } = this;
262
- const prevData = (0, data_1.deconstructData)({
263
- nodes: graphCore.getAllNodes(),
264
- edges: [],
273
+ this.doRemove(dataRemoved);
274
+ this.doAdd(dataAdded);
275
+ this.doUpdate(dataUpdated);
276
+ }
277
+ if ((_a = data.edges) === null || _a === void 0 ? void 0 : _a.filter((edge) => edge.hasOwnProperty('source') || edge.hasOwnProperty('target')).length) {
278
+ // convert and store tree structure to graphCore
279
+ this.updateTreeGraph(dataType, {
280
+ nodes: this.graphCore.getAllNodes(),
281
+ edges: this.graphCore.getAllEdges(),
265
282
  });
266
- const nodesAndCombos = nodes.concat(combos.map((combo) => ({
267
- id: combo.id,
268
- data: Object.assign(Object.assign({}, combo.data), { _isCombo: true }),
269
- })));
270
- // =========== node & combos ============
271
- if (!prevData.nodes.length) {
272
- graphCore.addNodes(nodesAndCombos);
283
+ }
284
+ }
285
+ /**
286
+ * Add new data.
287
+ * @param data data to be added which is not in graphCore
288
+ */
289
+ doAdd(data) {
290
+ if ((0, util_1.isEmpty)(data))
291
+ return;
292
+ const { graphCore } = this;
293
+ const { nodes = [], edges = [], combos = [] } = data;
294
+ if (nodes.length || combos.length) {
295
+ const nodesAndCombos = getNodesAndCombos(data);
296
+ graphCore.addNodes(nodesAndCombos);
297
+ }
298
+ if (edges.length) {
299
+ graphCore.addEdges(edges);
300
+ }
301
+ }
302
+ /**
303
+ * Update part of old data.
304
+ * @param data data to be updated which is part of old one in the graphCore
305
+ */
306
+ doUpdate(data) {
307
+ if ((0, util_1.isEmpty)(data))
308
+ return;
309
+ const { graphCore } = this;
310
+ const { nodes = [], edges = [], combos = [] } = data;
311
+ // update node
312
+ nodes.forEach((newModel) => {
313
+ const { id, data } = newModel;
314
+ if (data) {
315
+ const mergedData = mergeOneLevelData(graphCore.getNode(id), newModel);
316
+ graphCore.mergeNodeData(id, mergedData);
273
317
  }
274
- else {
275
- if (changeType === 'mergeReplace') {
276
- // remove the nodes which are not in incoming data but in graphCore
277
- const nodeAndComboIds = nodesAndCombos.map((node) => node.id);
278
- prevData.nodes.forEach((prevNode) => {
279
- if (!nodeAndComboIds.includes(prevNode.id)) {
280
- this.removeNode(prevNode);
281
- }
282
- });
283
- }
284
- // add or update node
285
- nodesAndCombos.forEach((item) => {
286
- if (graphCore.hasNode(item.id)) {
287
- // update node which is in the graphCore
288
- graphCore.mergeNodeData(item.id, item.data);
289
- }
290
- else {
291
- // add node which is in data but not in graphCore
292
- graphCore.addNode(item);
293
- }
294
- });
318
+ if (data.hasOwnProperty('parentId')) {
319
+ graphCore.setParent(id, data.parentId, 'combo');
295
320
  }
296
- // =========== edge ============
297
- prevData.edges = graphCore.getAllEdges();
298
- if (!prevData.edges.length) {
299
- graphCore.addEdges(edges);
321
+ });
322
+ // update edge
323
+ edges.forEach((newModel) => {
324
+ const oldModel = graphCore.getEdge(newModel.id);
325
+ if (!oldModel)
326
+ return;
327
+ const { id, source, target, data } = newModel;
328
+ if (source && oldModel.source !== source)
329
+ graphCore.updateEdgeSource(id, source);
330
+ if (target && oldModel.target !== target)
331
+ graphCore.updateEdgeTarget(id, target);
332
+ if (data) {
333
+ const mergedData = mergeOneLevelData(graphCore.getEdge(id), newModel);
334
+ graphCore.mergeEdgeData(id, mergedData);
300
335
  }
301
- else {
302
- if (changeType === 'mergeReplace') {
303
- // remove the edges which are not in incoming data but in graphCore
304
- const edgeIds = edges.map((edge) => edge.id);
305
- prevData.edges.forEach((prevEdge) => {
306
- if (!edgeIds.includes(prevEdge.id))
307
- graphCore.removeEdge(prevEdge.id);
336
+ });
337
+ // update combos
338
+ const modelsToMove = [];
339
+ combos.forEach((newModel) => {
340
+ const { id, data } = newModel;
341
+ const { x: comboNewX, y: comboNewY } = data, others = __rest(data, ["x", "y"]);
342
+ if (comboNewX !== undefined || comboNewY !== undefined) {
343
+ if (this.graphCore.getChildren(id, 'combo').length) {
344
+ // combo's position is updated, update the succeed nodes' positions
345
+ const oldBounds = this.graph.getRenderBBox(id, true);
346
+ if (!oldBounds)
347
+ return;
348
+ const [comboOldX, comboOldY] = oldBounds.center;
349
+ const dx = comboNewX - comboOldX;
350
+ const dy = comboNewY - comboOldY;
351
+ (0, data_1.graphComboTreeDfs)(this.graph, [newModel], (succeed) => {
352
+ const { x, y, _isCombo } = succeed.data;
353
+ if (!_isCombo) {
354
+ modelsToMove.push({
355
+ id: succeed.id,
356
+ data: {
357
+ x: x + dx,
358
+ y: y + dy,
359
+ },
360
+ });
361
+ }
362
+ }, 'BT');
363
+ }
364
+ else {
365
+ // empty combo, modify the position directly
366
+ modelsToMove.push({
367
+ id,
368
+ data: {
369
+ x: comboNewX,
370
+ y: comboNewY,
371
+ },
308
372
  });
309
373
  }
310
- // add or update edge
311
- edges.forEach((edge) => {
312
- if (graphCore.hasEdge(edge.id)) {
313
- // update edge which is in the graphCore
314
- graphCore.mergeEdgeData(edge.id, edge.data);
315
- }
316
- else {
317
- // add edge which is in data but not in graphCore
318
- graphCore.addEdge(edge);
319
- }
374
+ }
375
+ // update other properties
376
+ if (Object.keys(others).length) {
377
+ const mergedData = mergeOneLevelData(graphCore.getNode(id), {
378
+ id,
379
+ data: others,
320
380
  });
381
+ graphCore.mergeNodeData(id, mergedData);
382
+ if (others.hasOwnProperty('parentId')) {
383
+ graphCore.setParent(id, others.parentId, 'combo');
384
+ }
321
385
  }
386
+ });
387
+ // update succeed nodes
388
+ modelsToMove.forEach((newModel) => {
389
+ const { id, data } = newModel;
390
+ graphCore.mergeNodeData(id, data);
391
+ });
392
+ }
393
+ /**
394
+ * Remove part of old data.
395
+ * @param data data to be removed which is part of old one in the graphCore
396
+ */
397
+ doRemove(data) {
398
+ if ((0, util_1.isEmpty)(data))
399
+ return;
400
+ const { graphCore } = this;
401
+ if (data.edges.length) {
402
+ // add or update edge
403
+ const ids = data.edges.map((edge) => edge.id);
404
+ graphCore.removeEdges(ids);
322
405
  }
323
- if ((_a = data.edges) === null || _a === void 0 ? void 0 : _a.length) {
324
- const { graphCore } = this;
325
- // convert and store tree structure to graphCore
326
- this.updateTreeGraph(dataType, {
327
- nodes: graphCore.getAllNodes(),
328
- edges: graphCore.getAllEdges(),
329
- });
330
- }
406
+ const nodesAndCombos = getNodesAndCombos(data);
407
+ // update combo tree view and tree graph view
408
+ nodesAndCombos === null || nodesAndCombos === void 0 ? void 0 : nodesAndCombos.forEach((item) => this.removeNode(item));
331
409
  }
332
410
  removeNode(nodeModel) {
333
411
  const { id, data } = nodeModel;
@@ -363,143 +441,6 @@ class DataController {
363
441
  }
364
442
  graphCore.removeNode(id);
365
443
  }
366
- /**
367
- * Remove part of old data.
368
- * @param data data to be removed which is part of old one
369
- */
370
- removeData(data) {
371
- const { graphCore } = this;
372
- const { nodes = [], edges = [], combos = [] } = data;
373
- const nodesAndCombos = nodes.concat(combos);
374
- const prevNodesAndCombos = graphCore.getAllNodes();
375
- const prevEdges = graphCore.getAllEdges();
376
- if (prevNodesAndCombos.length && nodesAndCombos.length) {
377
- // update combo tree view and tree graph view
378
- nodesAndCombos.forEach((item) => {
379
- this.removeNode(item);
380
- });
381
- // remove the node
382
- // graphCore.removeNodes(nodesAndCombos.map((node) => node.id));
383
- }
384
- if (prevEdges.length && edges.length) {
385
- // add or update edge
386
- const ids = edges
387
- .map((edge) => edge.id)
388
- .filter((id) => graphCore.hasEdge(id));
389
- graphCore.removeEdges(ids);
390
- }
391
- }
392
- /**
393
- * Update part of old data.
394
- * @param data data to be updated which is part of old one
395
- */
396
- updateData(dataConfig) {
397
- const { graphCore } = this;
398
- const { type: dataType, data } = this.formatData(dataConfig);
399
- if (!dataType)
400
- return;
401
- const { nodes = [], edges = [], combos = [] } = data; //this.transformData(data as GraphData);
402
- const { nodes: prevNodes, edges: prevEdges, combos: prevCombos, } = (0, data_1.deconstructData)({
403
- nodes: graphCore.getAllNodes(),
404
- edges: graphCore.getAllEdges(),
405
- });
406
- if (prevNodes.length) {
407
- // update node
408
- nodes.forEach((newModel) => {
409
- const { id, data } = newModel;
410
- if (data) {
411
- const mergedData = mergeOneLevelData(graphCore.getNode(id), newModel);
412
- graphCore.mergeNodeData(id, mergedData);
413
- }
414
- if (data.hasOwnProperty('parentId')) {
415
- graphCore.setParent(id, data.parentId, 'combo');
416
- }
417
- });
418
- }
419
- if (prevEdges.length) {
420
- // update edge
421
- edges.forEach((newModel) => {
422
- const oldModel = graphCore.getEdge(newModel.id);
423
- if (!oldModel)
424
- return;
425
- const { id, source, target, data } = newModel;
426
- if (source && oldModel.source !== source)
427
- graphCore.updateEdgeSource(id, source);
428
- if (target && oldModel.target !== target)
429
- graphCore.updateEdgeTarget(id, target);
430
- if (data) {
431
- const mergedData = mergeOneLevelData(graphCore.getEdge(id), newModel);
432
- graphCore.mergeEdgeData(id, mergedData);
433
- }
434
- });
435
- }
436
- if (prevCombos.length) {
437
- // update combos
438
- const modelsToMove = [];
439
- combos.forEach((newModel) => {
440
- const { id, data } = newModel;
441
- if (!data)
442
- return;
443
- const { x: comboNewX, y: comboNewY } = data, others = __rest(data, ["x", "y"]);
444
- if (comboNewX !== undefined || comboNewY !== undefined) {
445
- if (this.graphCore.getChildren(id, 'combo').length) {
446
- // combo's position is updated, update the succeed nodes' positions
447
- const oldBounds = this.graph.getRenderBBox(id, true);
448
- if (!oldBounds)
449
- return;
450
- const [comboOldX, comboOldY] = oldBounds.center;
451
- const dx = comboNewX - comboOldX;
452
- const dy = comboNewY - comboOldY;
453
- (0, data_1.graphComboTreeDfs)(this.graph, [newModel], (succeed) => {
454
- const { x, y, _isCombo } = succeed.data;
455
- if (!_isCombo) {
456
- modelsToMove.push({
457
- id: succeed.id,
458
- data: {
459
- x: x + dx,
460
- y: y + dy,
461
- },
462
- });
463
- }
464
- }, 'BT');
465
- }
466
- else {
467
- // empty combo, modify the position directly
468
- modelsToMove.push({
469
- id,
470
- data: {
471
- x: comboNewX,
472
- y: comboNewY,
473
- },
474
- });
475
- }
476
- }
477
- // update other properties
478
- if (Object.keys(others).length) {
479
- const mergedData = mergeOneLevelData(graphCore.getNode(id), {
480
- id,
481
- data: others,
482
- });
483
- graphCore.mergeNodeData(id, mergedData);
484
- if (others.hasOwnProperty('parentId')) {
485
- graphCore.setParent(id, others.parentId, 'combo');
486
- }
487
- }
488
- });
489
- // update succeed nodes
490
- modelsToMove.forEach((newModel) => {
491
- const { id, data } = newModel;
492
- graphCore.mergeNodeData(id, data);
493
- });
494
- }
495
- if (edges === null || edges === void 0 ? void 0 : edges.filter((edge) => edge.hasOwnProperty('source') || edge.hasOwnProperty('target')).length) {
496
- // convert and store tree structure to graphCore
497
- this.updateTreeGraph(dataType, {
498
- nodes: this.graphCore.getAllNodes(),
499
- edges: this.graphCore.getAllEdges(),
500
- });
501
- }
502
- }
503
444
  formatData(dataConfig) {
504
445
  const { type, value } = dataConfig;
505
446
  let data = value;
@@ -522,7 +463,7 @@ class DataController {
522
463
  };
523
464
  }
524
465
  /**
525
- * Relatively move the position of combo a dinstance (dx and dy in combo data).
466
+ * Relatively move the position of combo a distance (dx and dy in combo data).
526
467
  * @param data graph data which contains the combo dx dy info
527
468
  */
528
469
  moveCombo(data) {
@@ -601,16 +542,57 @@ class DataController {
601
542
  });
602
543
  }
603
544
  /**
604
- * Clone data from graphCore, and run transforms
545
+ * Preprocess the data and divide the data into three categories: Add, Update, Delete according to the operation type.
546
+ * @param data new graph data
547
+ * @param type type of data change
548
+ */
549
+ preprocessData(data, type) {
550
+ const { graphCore } = this;
551
+ const dataCloned = (0, util_1.clone)(data);
552
+ const prevData = graphCore
553
+ ? (0, data_1.deconstructData)({
554
+ nodes: graphCore.getAllNodes(),
555
+ edges: graphCore.getAllEdges(),
556
+ })
557
+ : {};
558
+ const { prevMinusNew, newMinusPrev, intersectionOfPrevAndNew } = diffGraphData(prevData, dataCloned);
559
+ const dataChangeMap = {
560
+ replace: { dataAdded: dataCloned, dataUpdated: {}, dataRemoved: {} },
561
+ mergeReplace: {
562
+ dataAdded: newMinusPrev,
563
+ dataUpdated: intersectionOfPrevAndNew,
564
+ dataRemoved: prevMinusNew,
565
+ },
566
+ union: {
567
+ dataAdded: newMinusPrev,
568
+ dataUpdated: intersectionOfPrevAndNew,
569
+ dataRemoved: {},
570
+ },
571
+ remove: {
572
+ dataAdded: {},
573
+ dataUpdated: {},
574
+ dataRemoved: intersectionOfPrevAndNew,
575
+ },
576
+ update: {
577
+ dataAdded: {},
578
+ dataUpdated: intersectionOfPrevAndNew,
579
+ dataRemoved: {},
580
+ },
581
+ };
582
+ return dataChangeMap[type];
583
+ }
584
+ /**
585
+ * run transforms on preprocessed data
605
586
  * @returns transformed data and the id map list
606
587
  */
607
- transformData(data) {
608
- let dataCloned = (0, util_1.clone)(data);
588
+ transformData(data, changeType) {
609
589
  // transform the data with transform extensions, output innerData and idMaps ===
610
590
  this.extensions.forEach(({ func, config }) => {
611
- dataCloned = func(dataCloned, config, this.graphCore);
591
+ if (this.isTransformActive(config, changeType)) {
592
+ data = func(data, config, this.graphCore);
593
+ }
612
594
  });
613
- return dataCloned;
595
+ return data;
614
596
  }
615
597
  /**
616
598
  * convert and store tree structure to graphCore
@@ -723,4 +705,46 @@ const mergeOneLevelData = (prevModel, newModel) => {
723
705
  });
724
706
  return mergedData;
725
707
  };
708
+ /**
709
+ * Generate the difference between two model list
710
+ * @param prevModels
711
+ * @param newModels
712
+ * @returns
713
+ */
714
+ const diffData = (prevModels = [], newModels = []) => {
715
+ const prevIds = new Set(prevModels === null || prevModels === void 0 ? void 0 : prevModels.map((item) => item.id));
716
+ const newIds = new Set(newModels === null || newModels === void 0 ? void 0 : newModels.map((item) => item.id));
717
+ return {
718
+ prevMinusNew: prevModels === null || prevModels === void 0 ? void 0 : prevModels.filter((item) => !newIds.has(item.id)),
719
+ newMinusPrev: newModels === null || newModels === void 0 ? void 0 : newModels.filter((item) => !prevIds.has(item.id)),
720
+ intersectionOfPrevAndNew: newModels === null || newModels === void 0 ? void 0 : newModels.filter((item) => prevIds.has(item.id)),
721
+ };
722
+ };
723
+ /**
724
+ * Compute the difference between two graph data structure.
725
+ * @param prevData
726
+ * @param newData
727
+ * @returns
728
+ */
729
+ const diffGraphData = (prevData, newData) => {
730
+ const result = {
731
+ newMinusPrev: { nodes: [], edges: [], combos: [] },
732
+ prevMinusNew: { nodes: [], edges: [], combos: [] },
733
+ intersectionOfPrevAndNew: { nodes: [], edges: [], combos: [] },
734
+ };
735
+ ['nodes', 'edges', 'combos'].forEach((key) => {
736
+ const diff = diffData(prevData[key], newData[key]);
737
+ result.newMinusPrev[key] = diff.newMinusPrev;
738
+ result.prevMinusNew[key] = diff.prevMinusNew;
739
+ result.intersectionOfPrevAndNew[key] = diff.intersectionOfPrevAndNew;
740
+ });
741
+ return result;
742
+ };
743
+ const getNodesAndCombos = (data) => {
744
+ const { nodes = [], combos = [] } = data;
745
+ return nodes.concat(combos.map((combo) => ({
746
+ id: combo.id,
747
+ data: Object.assign(Object.assign({}, combo.data), { _isCombo: true }),
748
+ })));
749
+ };
726
750
  //# sourceMappingURL=data.js.map