@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
package/esm/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/esm/index.js CHANGED
@@ -11,7 +11,7 @@ runtime.enableCSSParsing = false;
11
11
  /**
12
12
  * Extend the graph class with std lib
13
13
  */
14
- const version = '5.0.0-beta.20';
14
+ const version = '5.0.0-beta.22';
15
15
  const Graph = extend(EmptyGraph, stdLib);
16
16
  export { Extensions, Graph, Util, extend, stdLib, version };
17
17
  export default { Graph, Util, stdLib, Extensions, extend, version };
@@ -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;
@@ -10,9 +10,9 @@ var __rest = (this && this.__rest) || function (s, e) {
10
10
  return t;
11
11
  };
12
12
  import { Graph as GraphLib } from '@antv/graphlib';
13
- import { clone, isArray, isObject } from '@antv/util';
13
+ import { clone, isArray, isEmpty, isObject } from '@antv/util';
14
14
  import { registery as registry } from '../../stdlib';
15
- import { deconstructData, graphComboTreeDfs, graphData2TreeData, traverse, treeData2GraphData, validateComboStrucutre, } from '../../util/data';
15
+ import { AVAILABLE_DATA_LIFECYCLE, DEFAULT_ACTIVE_DATA_LIFECYCLE, dataLifecycleMap, deconstructData, graphComboTreeDfs, graphData2TreeData, traverse, treeData2GraphData, validateComboStrucutre, } from '../../util/data';
16
16
  import { getExtension } from '../../util/extension';
17
17
  import { isTreeLayout } from '../../util/layout';
18
18
  import { hasTreeBehaviors } from '../../util/behavior';
@@ -23,6 +23,22 @@ import { hasTreeBehaviors } from '../../util/behavior';
23
23
  export class DataController {
24
24
  constructor(graph) {
25
25
  this.extensions = [];
26
+ /**
27
+ * Determine whether to execute transform in the current data life cycle
28
+ * @param config transform plugin's configuration
29
+ * @param changeType type of data change
30
+ */
31
+ this.isTransformActive = (config, changeType) => {
32
+ let activeLifecycle = isObject(config) && 'activeLifecycle' in config
33
+ ? config.activeLifecycle
34
+ : DEFAULT_ACTIVE_DATA_LIFECYCLE;
35
+ activeLifecycle = Array.isArray(activeLifecycle)
36
+ ? activeLifecycle
37
+ : activeLifecycle === 'all'
38
+ ? AVAILABLE_DATA_LIFECYCLE
39
+ : [activeLifecycle];
40
+ return activeLifecycle.includes(dataLifecycleMap[changeType]);
41
+ };
26
42
  this.graph = graph;
27
43
  this.tap();
28
44
  }
@@ -131,12 +147,6 @@ export class DataController {
131
147
  const { graphCore } = this;
132
148
  const change = () => {
133
149
  switch (changeType) {
134
- case 'remove':
135
- this.removeData(data);
136
- break;
137
- case 'update':
138
- this.updateData(data);
139
- break;
140
150
  case 'moveCombo':
141
151
  this.moveCombo(data);
142
152
  break;
@@ -144,7 +154,7 @@ export class DataController {
144
154
  this.addCombo(data);
145
155
  break;
146
156
  default:
147
- // changeType is 'replace' | 'mergeReplace' | 'union'
157
+ // changeType is 'replace' | 'mergeReplace' | 'union' | 'remove' | 'update'
148
158
  this.changeData(data, changeType);
149
159
  break;
150
160
  }
@@ -211,9 +221,9 @@ export class DataController {
211
221
  this.treeDirtyFlag = false;
212
222
  }
213
223
  /**
214
- * Change data by replace, merge repalce, or union.
224
+ * Change data by replace, merge repalce, union, remove or update.
215
225
  * @param data new data
216
- * @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
226
+ * @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.
217
227
  */
218
228
  changeData(dataConfig, changeType) {
219
229
  var _a;
@@ -221,8 +231,10 @@ export class DataController {
221
231
  if (!dataType)
222
232
  return;
223
233
  this.dataType = dataType;
224
- const { nodes = [], edges = [], combos = [] } = this.transformData(data);
234
+ const preprocessedData = this.preprocessData(data, changeType);
235
+ const { dataAdded, dataRemoved, dataUpdated } = this.transformData(preprocessedData, changeType);
225
236
  if (changeType === 'replace') {
237
+ const { nodes, edges, combos } = dataAdded;
226
238
  this.graphCore = new GraphLib(clone({
227
239
  nodes: nodes.concat((combos === null || combos === void 0 ? void 0 : combos.map((combo) => ({
228
240
  id: combo.id,
@@ -255,76 +267,142 @@ export class DataController {
255
267
  }
256
268
  }
257
269
  else {
258
- const { graphCore } = this;
259
- const prevData = deconstructData({
260
- nodes: graphCore.getAllNodes(),
261
- edges: [],
270
+ this.doRemove(dataRemoved);
271
+ this.doAdd(dataAdded);
272
+ this.doUpdate(dataUpdated);
273
+ }
274
+ if ((_a = data.edges) === null || _a === void 0 ? void 0 : _a.filter((edge) => edge.hasOwnProperty('source') || edge.hasOwnProperty('target')).length) {
275
+ // convert and store tree structure to graphCore
276
+ this.updateTreeGraph(dataType, {
277
+ nodes: this.graphCore.getAllNodes(),
278
+ edges: this.graphCore.getAllEdges(),
262
279
  });
263
- const nodesAndCombos = nodes.concat(combos.map((combo) => ({
264
- id: combo.id,
265
- data: Object.assign(Object.assign({}, combo.data), { _isCombo: true }),
266
- })));
267
- // =========== node & combos ============
268
- if (!prevData.nodes.length) {
269
- graphCore.addNodes(nodesAndCombos);
280
+ }
281
+ }
282
+ /**
283
+ * Add new data.
284
+ * @param data data to be added which is not in graphCore
285
+ */
286
+ doAdd(data) {
287
+ if (isEmpty(data))
288
+ return;
289
+ const { graphCore } = this;
290
+ const { nodes = [], edges = [], combos = [] } = data;
291
+ if (nodes.length || combos.length) {
292
+ const nodesAndCombos = getNodesAndCombos(data);
293
+ graphCore.addNodes(nodesAndCombos);
294
+ }
295
+ if (edges.length) {
296
+ graphCore.addEdges(edges);
297
+ }
298
+ }
299
+ /**
300
+ * Update part of old data.
301
+ * @param data data to be updated which is part of old one in the graphCore
302
+ */
303
+ doUpdate(data) {
304
+ if (isEmpty(data))
305
+ return;
306
+ const { graphCore } = this;
307
+ const { nodes = [], edges = [], combos = [] } = data;
308
+ // update node
309
+ nodes.forEach((newModel) => {
310
+ const { id, data } = newModel;
311
+ if (data) {
312
+ const mergedData = mergeOneLevelData(graphCore.getNode(id), newModel);
313
+ graphCore.mergeNodeData(id, mergedData);
270
314
  }
271
- else {
272
- if (changeType === 'mergeReplace') {
273
- // remove the nodes which are not in incoming data but in graphCore
274
- const nodeAndComboIds = nodesAndCombos.map((node) => node.id);
275
- prevData.nodes.forEach((prevNode) => {
276
- if (!nodeAndComboIds.includes(prevNode.id)) {
277
- this.removeNode(prevNode);
278
- }
279
- });
280
- }
281
- // add or update node
282
- nodesAndCombos.forEach((item) => {
283
- if (graphCore.hasNode(item.id)) {
284
- // update node which is in the graphCore
285
- graphCore.mergeNodeData(item.id, item.data);
286
- }
287
- else {
288
- // add node which is in data but not in graphCore
289
- graphCore.addNode(item);
290
- }
291
- });
315
+ if (data.hasOwnProperty('parentId')) {
316
+ graphCore.setParent(id, data.parentId, 'combo');
292
317
  }
293
- // =========== edge ============
294
- prevData.edges = graphCore.getAllEdges();
295
- if (!prevData.edges.length) {
296
- graphCore.addEdges(edges);
318
+ });
319
+ // update edge
320
+ edges.forEach((newModel) => {
321
+ const oldModel = graphCore.getEdge(newModel.id);
322
+ if (!oldModel)
323
+ return;
324
+ const { id, source, target, data } = newModel;
325
+ if (source && oldModel.source !== source)
326
+ graphCore.updateEdgeSource(id, source);
327
+ if (target && oldModel.target !== target)
328
+ graphCore.updateEdgeTarget(id, target);
329
+ if (data) {
330
+ const mergedData = mergeOneLevelData(graphCore.getEdge(id), newModel);
331
+ graphCore.mergeEdgeData(id, mergedData);
297
332
  }
298
- else {
299
- if (changeType === 'mergeReplace') {
300
- // remove the edges which are not in incoming data but in graphCore
301
- const edgeIds = edges.map((edge) => edge.id);
302
- prevData.edges.forEach((prevEdge) => {
303
- if (!edgeIds.includes(prevEdge.id))
304
- graphCore.removeEdge(prevEdge.id);
333
+ });
334
+ // update combos
335
+ const modelsToMove = [];
336
+ combos.forEach((newModel) => {
337
+ const { id, data } = newModel;
338
+ const { x: comboNewX, y: comboNewY } = data, others = __rest(data, ["x", "y"]);
339
+ if (comboNewX !== undefined || comboNewY !== undefined) {
340
+ if (this.graphCore.getChildren(id, 'combo').length) {
341
+ // combo's position is updated, update the succeed nodes' positions
342
+ const oldBounds = this.graph.getRenderBBox(id, true);
343
+ if (!oldBounds)
344
+ return;
345
+ const [comboOldX, comboOldY] = oldBounds.center;
346
+ const dx = comboNewX - comboOldX;
347
+ const dy = comboNewY - comboOldY;
348
+ graphComboTreeDfs(this.graph, [newModel], (succeed) => {
349
+ const { x, y, _isCombo } = succeed.data;
350
+ if (!_isCombo) {
351
+ modelsToMove.push({
352
+ id: succeed.id,
353
+ data: {
354
+ x: x + dx,
355
+ y: y + dy,
356
+ },
357
+ });
358
+ }
359
+ }, 'BT');
360
+ }
361
+ else {
362
+ // empty combo, modify the position directly
363
+ modelsToMove.push({
364
+ id,
365
+ data: {
366
+ x: comboNewX,
367
+ y: comboNewY,
368
+ },
305
369
  });
306
370
  }
307
- // add or update edge
308
- edges.forEach((edge) => {
309
- if (graphCore.hasEdge(edge.id)) {
310
- // update edge which is in the graphCore
311
- graphCore.mergeEdgeData(edge.id, edge.data);
312
- }
313
- else {
314
- // add edge which is in data but not in graphCore
315
- graphCore.addEdge(edge);
316
- }
371
+ }
372
+ // update other properties
373
+ if (Object.keys(others).length) {
374
+ const mergedData = mergeOneLevelData(graphCore.getNode(id), {
375
+ id,
376
+ data: others,
317
377
  });
378
+ graphCore.mergeNodeData(id, mergedData);
379
+ if (others.hasOwnProperty('parentId')) {
380
+ graphCore.setParent(id, others.parentId, 'combo');
381
+ }
318
382
  }
383
+ });
384
+ // update succeed nodes
385
+ modelsToMove.forEach((newModel) => {
386
+ const { id, data } = newModel;
387
+ graphCore.mergeNodeData(id, data);
388
+ });
389
+ }
390
+ /**
391
+ * Remove part of old data.
392
+ * @param data data to be removed which is part of old one in the graphCore
393
+ */
394
+ doRemove(data) {
395
+ if (isEmpty(data))
396
+ return;
397
+ const { graphCore } = this;
398
+ if (data.edges.length) {
399
+ // add or update edge
400
+ const ids = data.edges.map((edge) => edge.id);
401
+ graphCore.removeEdges(ids);
319
402
  }
320
- if ((_a = data.edges) === null || _a === void 0 ? void 0 : _a.length) {
321
- const { graphCore } = this;
322
- // convert and store tree structure to graphCore
323
- this.updateTreeGraph(dataType, {
324
- nodes: graphCore.getAllNodes(),
325
- edges: graphCore.getAllEdges(),
326
- });
327
- }
403
+ const nodesAndCombos = getNodesAndCombos(data);
404
+ // update combo tree view and tree graph view
405
+ nodesAndCombos === null || nodesAndCombos === void 0 ? void 0 : nodesAndCombos.forEach((item) => this.removeNode(item));
328
406
  }
329
407
  removeNode(nodeModel) {
330
408
  const { id, data } = nodeModel;
@@ -360,143 +438,6 @@ export class DataController {
360
438
  }
361
439
  graphCore.removeNode(id);
362
440
  }
363
- /**
364
- * Remove part of old data.
365
- * @param data data to be removed which is part of old one
366
- */
367
- removeData(data) {
368
- const { graphCore } = this;
369
- const { nodes = [], edges = [], combos = [] } = data;
370
- const nodesAndCombos = nodes.concat(combos);
371
- const prevNodesAndCombos = graphCore.getAllNodes();
372
- const prevEdges = graphCore.getAllEdges();
373
- if (prevNodesAndCombos.length && nodesAndCombos.length) {
374
- // update combo tree view and tree graph view
375
- nodesAndCombos.forEach((item) => {
376
- this.removeNode(item);
377
- });
378
- // remove the node
379
- // graphCore.removeNodes(nodesAndCombos.map((node) => node.id));
380
- }
381
- if (prevEdges.length && edges.length) {
382
- // add or update edge
383
- const ids = edges
384
- .map((edge) => edge.id)
385
- .filter((id) => graphCore.hasEdge(id));
386
- graphCore.removeEdges(ids);
387
- }
388
- }
389
- /**
390
- * Update part of old data.
391
- * @param data data to be updated which is part of old one
392
- */
393
- updateData(dataConfig) {
394
- const { graphCore } = this;
395
- const { type: dataType, data } = this.formatData(dataConfig);
396
- if (!dataType)
397
- return;
398
- const { nodes = [], edges = [], combos = [] } = data; //this.transformData(data as GraphData);
399
- const { nodes: prevNodes, edges: prevEdges, combos: prevCombos, } = deconstructData({
400
- nodes: graphCore.getAllNodes(),
401
- edges: graphCore.getAllEdges(),
402
- });
403
- if (prevNodes.length) {
404
- // update node
405
- nodes.forEach((newModel) => {
406
- const { id, data } = newModel;
407
- if (data) {
408
- const mergedData = mergeOneLevelData(graphCore.getNode(id), newModel);
409
- graphCore.mergeNodeData(id, mergedData);
410
- }
411
- if (data.hasOwnProperty('parentId')) {
412
- graphCore.setParent(id, data.parentId, 'combo');
413
- }
414
- });
415
- }
416
- if (prevEdges.length) {
417
- // update edge
418
- edges.forEach((newModel) => {
419
- const oldModel = graphCore.getEdge(newModel.id);
420
- if (!oldModel)
421
- return;
422
- const { id, source, target, data } = newModel;
423
- if (source && oldModel.source !== source)
424
- graphCore.updateEdgeSource(id, source);
425
- if (target && oldModel.target !== target)
426
- graphCore.updateEdgeTarget(id, target);
427
- if (data) {
428
- const mergedData = mergeOneLevelData(graphCore.getEdge(id), newModel);
429
- graphCore.mergeEdgeData(id, mergedData);
430
- }
431
- });
432
- }
433
- if (prevCombos.length) {
434
- // update combos
435
- const modelsToMove = [];
436
- combos.forEach((newModel) => {
437
- const { id, data } = newModel;
438
- if (!data)
439
- return;
440
- const { x: comboNewX, y: comboNewY } = data, others = __rest(data, ["x", "y"]);
441
- if (comboNewX !== undefined || comboNewY !== undefined) {
442
- if (this.graphCore.getChildren(id, 'combo').length) {
443
- // combo's position is updated, update the succeed nodes' positions
444
- const oldBounds = this.graph.getRenderBBox(id, true);
445
- if (!oldBounds)
446
- return;
447
- const [comboOldX, comboOldY] = oldBounds.center;
448
- const dx = comboNewX - comboOldX;
449
- const dy = comboNewY - comboOldY;
450
- graphComboTreeDfs(this.graph, [newModel], (succeed) => {
451
- const { x, y, _isCombo } = succeed.data;
452
- if (!_isCombo) {
453
- modelsToMove.push({
454
- id: succeed.id,
455
- data: {
456
- x: x + dx,
457
- y: y + dy,
458
- },
459
- });
460
- }
461
- }, 'BT');
462
- }
463
- else {
464
- // empty combo, modify the position directly
465
- modelsToMove.push({
466
- id,
467
- data: {
468
- x: comboNewX,
469
- y: comboNewY,
470
- },
471
- });
472
- }
473
- }
474
- // update other properties
475
- if (Object.keys(others).length) {
476
- const mergedData = mergeOneLevelData(graphCore.getNode(id), {
477
- id,
478
- data: others,
479
- });
480
- graphCore.mergeNodeData(id, mergedData);
481
- if (others.hasOwnProperty('parentId')) {
482
- graphCore.setParent(id, others.parentId, 'combo');
483
- }
484
- }
485
- });
486
- // update succeed nodes
487
- modelsToMove.forEach((newModel) => {
488
- const { id, data } = newModel;
489
- graphCore.mergeNodeData(id, data);
490
- });
491
- }
492
- if (edges === null || edges === void 0 ? void 0 : edges.filter((edge) => edge.hasOwnProperty('source') || edge.hasOwnProperty('target')).length) {
493
- // convert and store tree structure to graphCore
494
- this.updateTreeGraph(dataType, {
495
- nodes: this.graphCore.getAllNodes(),
496
- edges: this.graphCore.getAllEdges(),
497
- });
498
- }
499
- }
500
441
  formatData(dataConfig) {
501
442
  const { type, value } = dataConfig;
502
443
  let data = value;
@@ -519,7 +460,7 @@ export class DataController {
519
460
  };
520
461
  }
521
462
  /**
522
- * Relatively move the position of combo a dinstance (dx and dy in combo data).
463
+ * Relatively move the position of combo a distance (dx and dy in combo data).
523
464
  * @param data graph data which contains the combo dx dy info
524
465
  */
525
466
  moveCombo(data) {
@@ -598,16 +539,57 @@ export class DataController {
598
539
  });
599
540
  }
600
541
  /**
601
- * Clone data from graphCore, and run transforms
542
+ * Preprocess the data and divide the data into three categories: Add, Update, Delete according to the operation type.
543
+ * @param data new graph data
544
+ * @param type type of data change
545
+ */
546
+ preprocessData(data, type) {
547
+ const { graphCore } = this;
548
+ const dataCloned = clone(data);
549
+ const prevData = graphCore
550
+ ? deconstructData({
551
+ nodes: graphCore.getAllNodes(),
552
+ edges: graphCore.getAllEdges(),
553
+ })
554
+ : {};
555
+ const { prevMinusNew, newMinusPrev, intersectionOfPrevAndNew } = diffGraphData(prevData, dataCloned);
556
+ const dataChangeMap = {
557
+ replace: { dataAdded: dataCloned, dataUpdated: {}, dataRemoved: {} },
558
+ mergeReplace: {
559
+ dataAdded: newMinusPrev,
560
+ dataUpdated: intersectionOfPrevAndNew,
561
+ dataRemoved: prevMinusNew,
562
+ },
563
+ union: {
564
+ dataAdded: newMinusPrev,
565
+ dataUpdated: intersectionOfPrevAndNew,
566
+ dataRemoved: {},
567
+ },
568
+ remove: {
569
+ dataAdded: {},
570
+ dataUpdated: {},
571
+ dataRemoved: intersectionOfPrevAndNew,
572
+ },
573
+ update: {
574
+ dataAdded: {},
575
+ dataUpdated: intersectionOfPrevAndNew,
576
+ dataRemoved: {},
577
+ },
578
+ };
579
+ return dataChangeMap[type];
580
+ }
581
+ /**
582
+ * run transforms on preprocessed data
602
583
  * @returns transformed data and the id map list
603
584
  */
604
- transformData(data) {
605
- let dataCloned = clone(data);
585
+ transformData(data, changeType) {
606
586
  // transform the data with transform extensions, output innerData and idMaps ===
607
587
  this.extensions.forEach(({ func, config }) => {
608
- dataCloned = func(dataCloned, config, this.graphCore);
588
+ if (this.isTransformActive(config, changeType)) {
589
+ data = func(data, config, this.graphCore);
590
+ }
609
591
  });
610
- return dataCloned;
592
+ return data;
611
593
  }
612
594
  /**
613
595
  * convert and store tree structure to graphCore
@@ -719,4 +701,46 @@ const mergeOneLevelData = (prevModel, newModel) => {
719
701
  });
720
702
  return mergedData;
721
703
  };
704
+ /**
705
+ * Generate the difference between two model list
706
+ * @param prevModels
707
+ * @param newModels
708
+ * @returns
709
+ */
710
+ const diffData = (prevModels = [], newModels = []) => {
711
+ const prevIds = new Set(prevModels === null || prevModels === void 0 ? void 0 : prevModels.map((item) => item.id));
712
+ const newIds = new Set(newModels === null || newModels === void 0 ? void 0 : newModels.map((item) => item.id));
713
+ return {
714
+ prevMinusNew: prevModels === null || prevModels === void 0 ? void 0 : prevModels.filter((item) => !newIds.has(item.id)),
715
+ newMinusPrev: newModels === null || newModels === void 0 ? void 0 : newModels.filter((item) => !prevIds.has(item.id)),
716
+ intersectionOfPrevAndNew: newModels === null || newModels === void 0 ? void 0 : newModels.filter((item) => prevIds.has(item.id)),
717
+ };
718
+ };
719
+ /**
720
+ * Compute the difference between two graph data structure.
721
+ * @param prevData
722
+ * @param newData
723
+ * @returns
724
+ */
725
+ const diffGraphData = (prevData, newData) => {
726
+ const result = {
727
+ newMinusPrev: { nodes: [], edges: [], combos: [] },
728
+ prevMinusNew: { nodes: [], edges: [], combos: [] },
729
+ intersectionOfPrevAndNew: { nodes: [], edges: [], combos: [] },
730
+ };
731
+ ['nodes', 'edges', 'combos'].forEach((key) => {
732
+ const diff = diffData(prevData[key], newData[key]);
733
+ result.newMinusPrev[key] = diff.newMinusPrev;
734
+ result.prevMinusNew[key] = diff.prevMinusNew;
735
+ result.intersectionOfPrevAndNew[key] = diff.intersectionOfPrevAndNew;
736
+ });
737
+ return result;
738
+ };
739
+ const getNodesAndCombos = (data) => {
740
+ const { nodes = [], combos = [] } = data;
741
+ return nodes.concat(combos.map((combo) => ({
742
+ id: combo.id,
743
+ data: Object.assign(Object.assign({}, combo.data), { _isCombo: true }),
744
+ })));
745
+ };
722
746
  //# sourceMappingURL=data.js.map