@antv/g6 5.0.0-beta.5 → 5.0.0-beta.7

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 (221) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/esm/index.d.ts +15 -7
  3. package/esm/index.js +1 -1
  4. package/esm/item/combo.d.ts +1 -1
  5. package/esm/item/combo.js +16 -9
  6. package/esm/item/combo.js.map +1 -1
  7. package/esm/item/edge.d.ts +22 -2
  8. package/esm/item/edge.js +93 -42
  9. package/esm/item/edge.js.map +1 -1
  10. package/esm/item/item.js +9 -5
  11. package/esm/item/item.js.map +1 -1
  12. package/esm/item/node.d.ts +1 -1
  13. package/esm/item/node.js +22 -15
  14. package/esm/item/node.js.map +1 -1
  15. package/esm/runtime/controller/data.d.ts +7 -10
  16. package/esm/runtime/controller/data.js +123 -281
  17. package/esm/runtime/controller/data.js.map +1 -1
  18. package/esm/runtime/controller/item.d.ts +1 -0
  19. package/esm/runtime/controller/item.js +21 -9
  20. package/esm/runtime/controller/item.js.map +1 -1
  21. package/esm/runtime/graph.d.ts +85 -7
  22. package/esm/runtime/graph.js +396 -61
  23. package/esm/runtime/graph.js.map +1 -1
  24. package/esm/stdlib/behavior/brush-select.js +1 -2
  25. package/esm/stdlib/behavior/brush-select.js.map +1 -1
  26. package/esm/stdlib/behavior/create-edge.d.ts +57 -0
  27. package/esm/stdlib/behavior/create-edge.js +207 -0
  28. package/esm/stdlib/behavior/create-edge.js.map +1 -0
  29. package/esm/stdlib/behavior/drag-canvas.d.ts +2 -1
  30. package/esm/stdlib/behavior/drag-canvas.js +26 -18
  31. package/esm/stdlib/behavior/drag-canvas.js.map +1 -1
  32. package/esm/stdlib/behavior/drag-combo.d.ts +2 -2
  33. package/esm/stdlib/behavior/drag-combo.js +35 -24
  34. package/esm/stdlib/behavior/drag-combo.js.map +1 -1
  35. package/esm/stdlib/behavior/drag-node.d.ts +3 -3
  36. package/esm/stdlib/behavior/drag-node.js +70 -57
  37. package/esm/stdlib/behavior/drag-node.js.map +1 -1
  38. package/esm/stdlib/behavior/index.d.ts +2 -0
  39. package/esm/stdlib/behavior/index.js +2 -0
  40. package/esm/stdlib/behavior/index.js.map +1 -1
  41. package/esm/stdlib/behavior/lasso-select.d.ts +1 -1
  42. package/esm/stdlib/behavior/lasso-select.js +1 -2
  43. package/esm/stdlib/behavior/lasso-select.js.map +1 -1
  44. package/esm/stdlib/behavior/scroll-canvas.d.ts +67 -0
  45. package/esm/stdlib/behavior/scroll-canvas.js +266 -0
  46. package/esm/stdlib/behavior/scroll-canvas.js.map +1 -0
  47. package/esm/stdlib/behavior/zoom-canvas.d.ts +2 -1
  48. package/esm/stdlib/behavior/zoom-canvas.js +30 -22
  49. package/esm/stdlib/behavior/zoom-canvas.js.map +1 -1
  50. package/esm/stdlib/data/mapNodeSize.d.ts +2 -2
  51. package/esm/stdlib/data/mapNodeSize.js +2 -2
  52. package/esm/stdlib/data/mapNodeSize.js.map +1 -1
  53. package/esm/stdlib/data/transformV4Data.d.ts +2 -2
  54. package/esm/stdlib/data/transformV4Data.js +2 -2
  55. package/esm/stdlib/data/transformV4Data.js.map +1 -1
  56. package/esm/stdlib/data/validateData.d.ts +2 -2
  57. package/esm/stdlib/data/validateData.js +9 -9
  58. package/esm/stdlib/data/validateData.js.map +1 -1
  59. package/esm/stdlib/index.d.ts +10 -6
  60. package/esm/stdlib/index.js +5 -1
  61. package/esm/stdlib/index.js.map +1 -1
  62. package/esm/stdlib/item/edge/base.js +4 -9
  63. package/esm/stdlib/item/edge/base.js.map +1 -1
  64. package/esm/stdlib/plugin/hull/hullComponent.d.ts +1 -1
  65. package/esm/stdlib/plugin/hull/hullComponent.js +3 -6
  66. package/esm/stdlib/plugin/hull/hullComponent.js.map +1 -1
  67. package/esm/stdlib/plugin/minimap/index.d.ts +5 -1
  68. package/esm/stdlib/plugin/minimap/index.js +116 -9
  69. package/esm/stdlib/plugin/minimap/index.js.map +1 -1
  70. package/esm/stdlib/plugin/snapline/index.d.ts +5 -1
  71. package/esm/stdlib/plugin/snapline/index.js +23 -12
  72. package/esm/stdlib/plugin/snapline/index.js.map +1 -1
  73. package/esm/types/graph.d.ts +34 -8
  74. package/esm/types/hook.d.ts +16 -2
  75. package/esm/types/spec.d.ts +6 -3
  76. package/esm/types/view.d.ts +5 -0
  77. package/esm/util/event.d.ts +3 -1
  78. package/esm/util/event.js +8 -1
  79. package/esm/util/event.js.map +1 -1
  80. package/esm/util/item.d.ts +16 -1
  81. package/esm/util/item.js +31 -8
  82. package/esm/util/item.js.map +1 -1
  83. package/esm/util/layout.d.ts +11 -0
  84. package/esm/util/layout.js +45 -0
  85. package/esm/util/layout.js.map +1 -1
  86. package/esm/util/point.js +5 -1
  87. package/esm/util/point.js.map +1 -1
  88. package/esm/util/shape.d.ts +1 -1
  89. package/esm/util/shape.js +3 -7
  90. package/esm/util/shape.js.map +1 -1
  91. package/esm/util/warn.d.ts +14 -0
  92. package/esm/util/warn.js +4 -0
  93. package/esm/util/warn.js.map +1 -0
  94. package/lib/index.d.ts +15 -7
  95. package/lib/index.js +1 -1
  96. package/lib/item/combo.d.ts +1 -1
  97. package/lib/item/combo.js +16 -9
  98. package/lib/item/combo.js.map +1 -1
  99. package/lib/item/edge.d.ts +22 -2
  100. package/lib/item/edge.js +92 -41
  101. package/lib/item/edge.js.map +1 -1
  102. package/lib/item/item.js +9 -5
  103. package/lib/item/item.js.map +1 -1
  104. package/lib/item/node.d.ts +1 -1
  105. package/lib/item/node.js +22 -15
  106. package/lib/item/node.js.map +1 -1
  107. package/lib/runtime/controller/data.d.ts +7 -10
  108. package/lib/runtime/controller/data.js +123 -281
  109. package/lib/runtime/controller/data.js.map +1 -1
  110. package/lib/runtime/controller/item.d.ts +1 -0
  111. package/lib/runtime/controller/item.js +21 -9
  112. package/lib/runtime/controller/item.js.map +1 -1
  113. package/lib/runtime/graph.d.ts +85 -7
  114. package/lib/runtime/graph.js +396 -61
  115. package/lib/runtime/graph.js.map +1 -1
  116. package/lib/stdlib/behavior/brush-select.js +1 -2
  117. package/lib/stdlib/behavior/brush-select.js.map +1 -1
  118. package/lib/stdlib/behavior/create-edge.d.ts +57 -0
  119. package/lib/stdlib/behavior/create-edge.js +211 -0
  120. package/lib/stdlib/behavior/create-edge.js.map +1 -0
  121. package/lib/stdlib/behavior/drag-canvas.d.ts +2 -1
  122. package/lib/stdlib/behavior/drag-canvas.js +26 -18
  123. package/lib/stdlib/behavior/drag-canvas.js.map +1 -1
  124. package/lib/stdlib/behavior/drag-combo.d.ts +2 -2
  125. package/lib/stdlib/behavior/drag-combo.js +35 -24
  126. package/lib/stdlib/behavior/drag-combo.js.map +1 -1
  127. package/lib/stdlib/behavior/drag-node.d.ts +3 -3
  128. package/lib/stdlib/behavior/drag-node.js +70 -57
  129. package/lib/stdlib/behavior/drag-node.js.map +1 -1
  130. package/lib/stdlib/behavior/index.d.ts +2 -0
  131. package/lib/stdlib/behavior/index.js +2 -0
  132. package/lib/stdlib/behavior/index.js.map +1 -1
  133. package/lib/stdlib/behavior/lasso-select.d.ts +1 -1
  134. package/lib/stdlib/behavior/lasso-select.js +1 -2
  135. package/lib/stdlib/behavior/lasso-select.js.map +1 -1
  136. package/lib/stdlib/behavior/scroll-canvas.d.ts +67 -0
  137. package/lib/stdlib/behavior/scroll-canvas.js +270 -0
  138. package/lib/stdlib/behavior/scroll-canvas.js.map +1 -0
  139. package/lib/stdlib/behavior/zoom-canvas.d.ts +2 -1
  140. package/lib/stdlib/behavior/zoom-canvas.js +30 -22
  141. package/lib/stdlib/behavior/zoom-canvas.js.map +1 -1
  142. package/lib/stdlib/data/mapNodeSize.d.ts +2 -2
  143. package/lib/stdlib/data/mapNodeSize.js +2 -2
  144. package/lib/stdlib/data/mapNodeSize.js.map +1 -1
  145. package/lib/stdlib/data/transformV4Data.d.ts +2 -2
  146. package/lib/stdlib/data/transformV4Data.js +2 -2
  147. package/lib/stdlib/data/transformV4Data.js.map +1 -1
  148. package/lib/stdlib/data/validateData.d.ts +2 -2
  149. package/lib/stdlib/data/validateData.js +9 -9
  150. package/lib/stdlib/data/validateData.js.map +1 -1
  151. package/lib/stdlib/index.d.ts +10 -6
  152. package/lib/stdlib/index.js +5 -1
  153. package/lib/stdlib/index.js.map +1 -1
  154. package/lib/stdlib/item/edge/base.js +4 -9
  155. package/lib/stdlib/item/edge/base.js.map +1 -1
  156. package/lib/stdlib/plugin/hull/hullComponent.d.ts +1 -1
  157. package/lib/stdlib/plugin/hull/hullComponent.js +3 -6
  158. package/lib/stdlib/plugin/hull/hullComponent.js.map +1 -1
  159. package/lib/stdlib/plugin/minimap/index.d.ts +5 -1
  160. package/lib/stdlib/plugin/minimap/index.js +116 -9
  161. package/lib/stdlib/plugin/minimap/index.js.map +1 -1
  162. package/lib/stdlib/plugin/snapline/index.d.ts +5 -1
  163. package/lib/stdlib/plugin/snapline/index.js +23 -12
  164. package/lib/stdlib/plugin/snapline/index.js.map +1 -1
  165. package/lib/types/graph.d.ts +34 -8
  166. package/lib/types/hook.d.ts +16 -2
  167. package/lib/types/spec.d.ts +6 -3
  168. package/lib/types/view.d.ts +5 -0
  169. package/lib/util/event.d.ts +3 -1
  170. package/lib/util/event.js +8 -1
  171. package/lib/util/event.js.map +1 -1
  172. package/lib/util/item.d.ts +16 -1
  173. package/lib/util/item.js +33 -9
  174. package/lib/util/item.js.map +1 -1
  175. package/lib/util/layout.d.ts +11 -0
  176. package/lib/util/layout.js +47 -1
  177. package/lib/util/layout.js.map +1 -1
  178. package/lib/util/point.js +5 -1
  179. package/lib/util/point.js.map +1 -1
  180. package/lib/util/shape.d.ts +1 -1
  181. package/lib/util/shape.js +3 -7
  182. package/lib/util/shape.js.map +1 -1
  183. package/lib/util/warn.d.ts +14 -0
  184. package/lib/util/warn.js +8 -0
  185. package/lib/util/warn.js.map +1 -0
  186. package/package.json +1 -1
  187. package/src/index.ts +1 -1
  188. package/src/item/combo.ts +14 -9
  189. package/src/item/edge.ts +121 -55
  190. package/src/item/item.ts +13 -9
  191. package/src/item/node.ts +20 -15
  192. package/src/runtime/controller/data.ts +139 -339
  193. package/src/runtime/controller/item.ts +30 -12
  194. package/src/runtime/graph.ts +502 -67
  195. package/src/stdlib/behavior/brush-select.ts +4 -2
  196. package/src/stdlib/behavior/create-edge.ts +296 -0
  197. package/src/stdlib/behavior/drag-canvas.ts +30 -19
  198. package/src/stdlib/behavior/drag-combo.ts +2 -2
  199. package/src/stdlib/behavior/drag-node.ts +8 -8
  200. package/src/stdlib/behavior/index.ts +2 -0
  201. package/src/stdlib/behavior/lasso-select.ts +5 -3
  202. package/src/stdlib/behavior/scroll-canvas.ts +348 -0
  203. package/src/stdlib/behavior/zoom-canvas.ts +34 -23
  204. package/src/stdlib/data/mapNodeSize.ts +2 -2
  205. package/src/stdlib/data/transformV4Data.ts +2 -2
  206. package/src/stdlib/data/validateData.ts +9 -9
  207. package/src/stdlib/index.ts +6 -0
  208. package/src/stdlib/item/edge/base.ts +4 -8
  209. package/src/stdlib/plugin/hull/hullComponent.ts +8 -5
  210. package/src/stdlib/plugin/minimap/index.ts +120 -10
  211. package/src/stdlib/plugin/snapline/index.ts +36 -16
  212. package/src/types/graph.ts +37 -8
  213. package/src/types/hook.ts +16 -2
  214. package/src/types/spec.ts +9 -3
  215. package/src/types/view.ts +8 -3
  216. package/src/util/event.ts +14 -1
  217. package/src/util/item.ts +65 -28
  218. package/src/util/layout.ts +45 -0
  219. package/src/util/point.ts +4 -2
  220. package/src/util/shape.ts +6 -7
  221. package/src/util/warn.ts +20 -0
@@ -1,5 +1,13 @@
1
1
  import EventEmitter from '@antv/event-emitter';
2
- import { AABB, Canvas, DisplayObject, PointLike } from '@antv/g';
2
+ import {
3
+ AABB,
4
+ Canvas,
5
+ DataURLType,
6
+ DisplayObject,
7
+ PointLike,
8
+ Rect,
9
+ Cursor,
10
+ } from '@antv/g';
3
11
  import { GraphChange, ID } from '@antv/graphlib';
4
12
  import {
5
13
  clone,
@@ -13,6 +21,7 @@ import {
13
21
  isString,
14
22
  map,
15
23
  } from '@antv/util';
24
+ import { createDom } from '@antv/dom-util';
16
25
  import { History } from '../stdlib/plugin/history';
17
26
  import { Command } from '../stdlib/plugin/history/command';
18
27
  import type {
@@ -47,7 +56,9 @@ import type {
47
56
  import { FitViewRules, GraphTransformOptions } from '../types/view';
48
57
  import { changeRenderer, createCanvas } from '../util/canvas';
49
58
  import { formatPadding } from '../util/shape';
59
+ import { getLayoutBounds } from '../util/layout';
50
60
  import { Plugin as PluginBase } from '../types/plugin';
61
+ import { ComboMapper, EdgeMapper, NodeMapper } from '../types/spec';
51
62
  import {
52
63
  DataController,
53
64
  ExtensionController,
@@ -349,7 +360,11 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
349
360
  main: Canvas;
350
361
  transient: Canvas;
351
362
  };
352
- }>({ name: 'init' }),
363
+ }>({ name: 'themechange' }),
364
+ mapperchange: new Hook<{
365
+ type: ITEM_TYPE;
366
+ mapper: NodeMapper | EdgeMapper | ComboMapper;
367
+ }>({ name: 'mapperchange' }),
353
368
  treecollapseexpand: new Hook<{
354
369
  ids: ID[];
355
370
  animate: boolean;
@@ -362,12 +377,14 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
362
377
 
363
378
  private formatSpecification(spec: Specification<B, T>) {
364
379
  return {
380
+ ...this.specification,
365
381
  ...spec,
366
382
  optimize: {
367
383
  tileBehavior: 2000,
368
384
  tileBehaviorSize: 1000,
369
385
  tileFirstRender: 10000,
370
386
  tileFirstRenderSize: 1000,
387
+ ...this.specification?.optimize,
371
388
  ...spec.optimize,
372
389
  },
373
390
  };
@@ -404,6 +421,32 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
404
421
  });
405
422
  }
406
423
 
424
+ /**
425
+ * Update the item display mapper for a specific item type.
426
+ * @param {ITEM_TYPE} type - The type of item (node, edge, or combo).
427
+ * @param {NodeMapper | EdgeMapper | ComboMapper} mapper - The mapper to be updated.
428
+ * */
429
+ public updateMapper(
430
+ type: ITEM_TYPE,
431
+ mapper: NodeMapper | EdgeMapper | ComboMapper,
432
+ ) {
433
+ switch (type) {
434
+ case 'node':
435
+ this.specification.node = mapper as NodeMapper;
436
+ break;
437
+ case 'edge':
438
+ this.specification.edge = mapper as EdgeMapper;
439
+ break;
440
+ case 'combo':
441
+ this.specification.combo = mapper as ComboMapper;
442
+ break;
443
+ }
444
+ this.hooks.mapperchange.emit({
445
+ type,
446
+ mapper,
447
+ });
448
+ }
449
+
407
450
  /**
408
451
  * Get the copy of specs(configurations).
409
452
  * @returns graph specs
@@ -439,15 +482,28 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
439
482
  const { autoFit } = this.specification;
440
483
  if (autoFit) {
441
484
  if (autoFit === 'view') {
442
- await this.fitView();
485
+ await this.fitView({ rules: { boundsType: 'layout' } });
443
486
  } else if (autoFit === 'center') {
444
- await this.fitCenter();
487
+ await this.fitCenter('layout');
445
488
  } else {
446
489
  const { type, effectTiming, ...others } = autoFit;
447
490
  if (type === 'view') {
448
- await this.fitView(others as any, effectTiming);
491
+ const { padding, rules } = others as {
492
+ padding: Padding;
493
+ rules: FitViewRules;
494
+ };
495
+ await this.fitView(
496
+ {
497
+ padding,
498
+ rules: {
499
+ ...rules,
500
+ boundsType: 'layout',
501
+ },
502
+ },
503
+ effectTiming,
504
+ );
449
505
  } else if (type === 'center') {
450
- await this.fitCenter(effectTiming);
506
+ await this.fitCenter('layout', effectTiming);
451
507
  } else if (type === 'position') {
452
508
  // TODO: align
453
509
  await this.translateTo((others as any).position, effectTiming);
@@ -673,8 +729,8 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
673
729
  */
674
730
  public async fitView(
675
731
  options?: {
676
- padding: Padding;
677
- rules: FitViewRules;
732
+ padding?: Padding;
733
+ rules?: FitViewRules;
678
734
  },
679
735
  effectTiming?: CameraAnimationOptions,
680
736
  ) {
@@ -682,13 +738,21 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
682
738
  const [top, right, bottom, left] = padding
683
739
  ? formatPadding(padding)
684
740
  : [0, 0, 0, 0];
685
- const { direction = 'both', ratioRule = 'min' } = rules || {};
741
+ const {
742
+ direction = 'both',
743
+ ratioRule = 'min',
744
+ boundsType = 'render',
745
+ } = rules || {};
686
746
 
687
- // Get the bounds of the whole graph.
688
747
  const {
689
748
  center: [graphCenterX, graphCenterY],
690
749
  halfExtents,
691
- } = this.canvas.document.documentElement.getBounds();
750
+ } =
751
+ boundsType === 'render'
752
+ ? // Get the bounds of the whole graph content.
753
+ this.canvas.document.documentElement.getBounds()
754
+ : // Get the bounds of the nodes positions while the graph content is not ready.
755
+ getLayoutBounds(this);
692
756
  const origin = this.canvas.canvas2Viewport({
693
757
  x: graphCenterX,
694
758
  y: graphCenterY,
@@ -739,11 +803,18 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
739
803
  * Fit the graph center to the view center.
740
804
  * @param effectTiming animation configurations
741
805
  */
742
- public async fitCenter(effectTiming?: CameraAnimationOptions) {
743
- // Get the bounds of the whole graph.
806
+ public async fitCenter(
807
+ boundsType: 'render' | 'layout' = 'render',
808
+ effectTiming?: CameraAnimationOptions,
809
+ ) {
744
810
  const {
745
811
  center: [graphCenterX, graphCenterY],
746
- } = this.canvas.document.documentElement.getBounds();
812
+ } =
813
+ boundsType === 'render'
814
+ ? // Get the bounds of the whole graph content.
815
+ this.canvas.document.documentElement.getBounds()
816
+ : // Get the bounds of the nodes positions while the graph content is not ready.
817
+ getLayoutBounds(this);
747
818
  await this.translateTo(
748
819
  this.canvas.canvas2Viewport({ x: graphCenterX, y: graphCenterY }),
749
820
  effectTiming,
@@ -1043,22 +1114,25 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1043
1114
 
1044
1115
  const { graphCore } = this.dataController;
1045
1116
  const { specification } = this.themeController;
1117
+ // debugger;
1046
1118
  graphCore.once('changed', (event) => {
1047
1119
  if (!event.changes.length) return;
1048
1120
  const changes = event.changes;
1049
- this.hooks.itemchange.emit({
1050
- type: itemType,
1051
- changes: graphCore.reduceChanges(event.changes),
1052
- graphCore,
1053
- theme: specification,
1054
- });
1055
- this.emit('afteritemchange', {
1121
+ const timingParameters = {
1056
1122
  type: itemType,
1057
1123
  action: 'add',
1058
1124
  models,
1059
1125
  apiName: 'addData',
1060
1126
  changes,
1127
+ };
1128
+ this.emit('beforeitemchange', timingParameters);
1129
+ this.hooks.itemchange.emit({
1130
+ type: itemType,
1131
+ changes: graphCore.reduceChanges(event.changes),
1132
+ graphCore,
1133
+ theme: specification,
1061
1134
  });
1135
+ this.emit('afteritemchange', timingParameters);
1062
1136
  });
1063
1137
 
1064
1138
  const modelArr = isArray(models) ? models : [models];
@@ -1083,27 +1157,39 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1083
1157
  public removeData(itemType: ITEM_TYPE, ids: ID | ID[]) {
1084
1158
  const idArr = isArray(ids) ? ids : [ids];
1085
1159
  const data = { nodes: [], edges: [], combos: [] };
1086
- const { userGraphCore, graphCore } = this.dataController;
1160
+ const { graphCore } = this.dataController;
1087
1161
  const { specification } = this.themeController;
1088
- const getItem =
1089
- itemType === 'edge' ? userGraphCore.getEdge : userGraphCore.getNode;
1090
- data[`${itemType}s`] = idArr.map((id) => getItem.bind(userGraphCore)(id));
1162
+ const getItem = itemType === 'edge' ? graphCore.getEdge : graphCore.getNode;
1163
+ const hasItem = itemType === 'edge' ? graphCore.hasEdge : graphCore.hasNode;
1164
+ data[`${itemType}s`] = idArr
1165
+ .map((id) => {
1166
+ if (!hasItem.bind(graphCore)(id)) {
1167
+ console.warn(
1168
+ `The ${itemType} data with id ${id} does not exist. It will be ignored`,
1169
+ );
1170
+ return;
1171
+ }
1172
+ return getItem.bind(graphCore)(id);
1173
+ })
1174
+ .filter(Boolean);
1091
1175
  graphCore.once('changed', (event) => {
1092
1176
  if (!event.changes.length) return;
1093
1177
  const changes = event.changes;
1094
- this.hooks.itemchange.emit({
1095
- type: itemType,
1096
- changes: event.changes,
1097
- graphCore,
1098
- theme: specification,
1099
- });
1100
- this.emit('afteritemchange', {
1178
+ const timingParameters = {
1101
1179
  type: itemType,
1102
1180
  action: 'remove',
1103
1181
  ids: idArr,
1104
1182
  apiName: 'removeData',
1105
1183
  changes,
1184
+ };
1185
+ this.emit('beforeitemchange', timingParameters);
1186
+ this.hooks.itemchange.emit({
1187
+ type: itemType,
1188
+ changes: event.changes,
1189
+ graphCore,
1190
+ theme: specification,
1106
1191
  });
1192
+ this.emit('afteritemchange', timingParameters);
1107
1193
  });
1108
1194
  this.hooks.datachange.emit({
1109
1195
  data,
@@ -1196,19 +1282,21 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1196
1282
  const { specification } = this.themeController;
1197
1283
  graphCore.once('changed', (event) => {
1198
1284
  const changes = this.extendChanges(clone(event.changes));
1199
- this.hooks.itemchange.emit({
1200
- type: itemType,
1201
- changes: event.changes,
1202
- graphCore,
1203
- theme: specification,
1204
- });
1205
- this.emit('afteritemchange', {
1285
+ const timingParameters = {
1206
1286
  type: itemType,
1207
1287
  action: 'update',
1208
1288
  models,
1209
1289
  apiName: 'updateData',
1210
1290
  changes,
1291
+ };
1292
+ this.emit('beforeitemchange', timingParameters);
1293
+ this.hooks.itemchange.emit({
1294
+ type: itemType,
1295
+ changes: event.changes,
1296
+ graphCore,
1297
+ theme: specification,
1211
1298
  });
1299
+ this.emit('afteritemchange', timingParameters);
1212
1300
  });
1213
1301
 
1214
1302
  this.hooks.datachange.emit({
@@ -1317,6 +1405,15 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1317
1405
  const changes = event.changes.filter(
1318
1406
  (change) => !isEqual(change.newValue, change.oldValue),
1319
1407
  );
1408
+ const timingParameters = {
1409
+ type,
1410
+ action: 'updatePosition',
1411
+ upsertAncestors,
1412
+ models,
1413
+ apiName: 'updatePosition',
1414
+ changes,
1415
+ };
1416
+ this.emit('beforeitemchange', timingParameters);
1320
1417
  this.hooks.itemchange.emit({
1321
1418
  type,
1322
1419
  changes: event.changes,
@@ -1327,14 +1424,7 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1327
1424
  animate: !disableAnimate,
1328
1425
  callback,
1329
1426
  });
1330
- this.emit('afteritemchange', {
1331
- type,
1332
- action: 'updatePosition',
1333
- upsertAncestors,
1334
- models,
1335
- apiName: 'updatePosition',
1336
- changes,
1337
- });
1427
+ this.emit('afteritemchange', timingParameters);
1338
1428
  });
1339
1429
 
1340
1430
  this.hooks.datachange.emit({
@@ -1609,19 +1699,21 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1609
1699
  graphCore.once('changed', (event) => {
1610
1700
  if (!event.changes.length) return;
1611
1701
  const changes = event.changes;
1612
- this.hooks.itemchange.emit({
1613
- type: 'combo',
1614
- changes: graphCore.reduceChanges(event.changes),
1615
- graphCore,
1616
- theme: specification,
1617
- });
1618
- this.emit('afteritemchange', {
1702
+ const timingParameters = {
1619
1703
  type: 'combo',
1620
1704
  action: 'add',
1621
1705
  models: [model],
1622
1706
  apiName: 'addCombo',
1623
1707
  changes,
1708
+ };
1709
+ this.emit('beforeitemchange', timingParameters);
1710
+ this.hooks.itemchange.emit({
1711
+ type: 'combo',
1712
+ changes: graphCore.reduceChanges(event.changes),
1713
+ graphCore,
1714
+ theme: specification,
1624
1715
  });
1716
+ this.emit('afteritemchange', timingParameters);
1625
1717
  });
1626
1718
 
1627
1719
  const data = {
@@ -1707,16 +1799,7 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1707
1799
  graphCore.once('changed', (event) => {
1708
1800
  if (!event.changes.length) return;
1709
1801
  const changes = this.extendChanges(clone(event.changes));
1710
- this.hooks.itemchange.emit({
1711
- type: 'combo',
1712
- changes: event.changes,
1713
- graphCore,
1714
- theme: specification,
1715
- upsertAncestors,
1716
- action: 'updatePosition',
1717
- callback,
1718
- });
1719
- this.emit('afteritemchange', {
1802
+ const timingParameters = {
1720
1803
  type: 'combo',
1721
1804
  ids: idArr,
1722
1805
  dx,
@@ -1725,7 +1808,18 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1725
1808
  upsertAncestors,
1726
1809
  apiName: 'moveCombo',
1727
1810
  changes,
1811
+ };
1812
+ this.emit('beforeitemchange', timingParameters);
1813
+ this.hooks.itemchange.emit({
1814
+ type: 'combo',
1815
+ changes: event.changes,
1816
+ graphCore,
1817
+ theme: specification,
1818
+ upsertAncestors,
1819
+ action: 'updatePosition',
1820
+ callback,
1728
1821
  });
1822
+ this.emit('afteritemchange', timingParameters);
1729
1823
  });
1730
1824
 
1731
1825
  this.hooks.datachange.emit({
@@ -1824,6 +1918,15 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1824
1918
  return this.interactionController.getMode();
1825
1919
  }
1826
1920
 
1921
+ /**
1922
+ * Set the cursor. But the cursor in item's style has higher priority.
1923
+ * @param cursor
1924
+ */
1925
+ public setCursor(cursor: Cursor) {
1926
+ this.canvas.setCursor(cursor);
1927
+ this.transientCanvas.setCursor(cursor);
1928
+ }
1929
+
1827
1930
  /**
1828
1931
  * Add behavior(s) to mode(s).
1829
1932
  * @param behaviors behavior names or configs
@@ -2041,9 +2144,19 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
2041
2144
  type: ITEM_TYPE | SHAPE_TYPE,
2042
2145
  id: ID,
2043
2146
  config: {
2044
- action: 'remove' | 'add' | 'update' | undefined;
2045
- style: ShapeStyle;
2046
- onlyDrawKeyShape?: boolean;
2147
+ action?: 'remove' | 'add' | 'update' | undefined;
2148
+ /** Data to be merged into the transient item. */
2149
+ data?: Record<string, any>;
2150
+ /** Style to be merged into the transient shape. */
2151
+ style?: ShapeStyle;
2152
+ /** For type: 'edge' */
2153
+ drawSource?: boolean;
2154
+ /** For type: 'edge' */
2155
+ drawTarget?: boolean;
2156
+ /** Only shape with id in shapeIds will be cloned while type is ITEM_TYPE. If shapeIds is not assigned, the whole item will be cloned. */
2157
+ shapeIds?: string[];
2158
+ /** Whether show the shapes in shapeIds. True by default. */
2159
+ visible?: boolean;
2047
2160
  upsertAncestors?: boolean;
2048
2161
  },
2049
2162
  canvas?: Canvas,
@@ -2058,6 +2171,328 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
2058
2171
  return this.itemController.getTransient(String(id));
2059
2172
  }
2060
2173
 
2174
+ // ===== download operations =====
2175
+ /**
2176
+ * Asynchronously generates a Data URL representation of the canvas content, including
2177
+ * background, main content, and transient canvas.
2178
+ * @param The type of the Data URL (e.g., 'image/png', 'image/jpeg').
2179
+ * @returns A Promise that resolves to the Data URL string.
2180
+ */
2181
+ public async toDataURL(type?: DataURLType): Promise<string> {
2182
+ const backgroundCanvas = this.backgroundCanvas;
2183
+ const canvas = this.canvas;
2184
+ const transientCanvas = this.transientCanvas;
2185
+ const rendererType = this.rendererType;
2186
+
2187
+ const pixelRatio =
2188
+ typeof window !== 'undefined' ? window.devicePixelRatio : 1;
2189
+ const width = this.getSize()[0];
2190
+ const height = this.getSize()[1];
2191
+
2192
+ const vContainerDOM: HTMLDivElement = createDom(
2193
+ '<div id="virtual-image"></div>',
2194
+ );
2195
+ const vCanvas = createCanvas(
2196
+ rendererType,
2197
+ vContainerDOM,
2198
+ width,
2199
+ height,
2200
+ pixelRatio,
2201
+ );
2202
+ const vCanvasContextService = vCanvas.getContextService();
2203
+ let bgRect;
2204
+ if (rendererType !== 'svg') {
2205
+ bgRect = new Rect({
2206
+ style: {
2207
+ x: 0,
2208
+ y: 0,
2209
+ z: -1,
2210
+ width: vCanvasContextService.getDomElement().width,
2211
+ height: vCanvasContextService.getDomElement().height,
2212
+ //@ts-ignore
2213
+ fill: backgroundCanvas.getContextService().getDomElement().style[
2214
+ 'background-color'
2215
+ ],
2216
+ },
2217
+ });
2218
+ vCanvas.appendChild(bgRect);
2219
+ }
2220
+ const backgroundClonedGroup = backgroundCanvas.getRoot().cloneNode(true);
2221
+ const clonedGroup = canvas.getRoot().cloneNode(true);
2222
+ const transientClonedGroup = transientCanvas.getRoot().cloneNode(true);
2223
+ vCanvas.appendChild(backgroundClonedGroup);
2224
+ vCanvas.appendChild(clonedGroup);
2225
+ vCanvas.appendChild(transientClonedGroup);
2226
+ vCanvas.render();
2227
+
2228
+ if (!type) type = 'image/png';
2229
+ let dataURL = '';
2230
+ await vCanvasContextService.toDataURL({ type }).then((url) => {
2231
+ dataURL = url;
2232
+ });
2233
+ return dataURL;
2234
+ }
2235
+
2236
+ /**
2237
+ * Asynchronously generates a Data URL representation of the full canvas content,
2238
+ * including background, main content, and transient canvas, with optional padding.
2239
+ * @param type The type of the Data URL (e.g., 'image/png', 'image/jpeg').
2240
+ * @param imageConfig Configuration options for the image (optional).
2241
+ * @returns A Promise that resolves to the Data URL string.
2242
+ */
2243
+ public async toFullDataURL(
2244
+ type?: DataURLType,
2245
+ imageConfig?: { padding?: number | number[] },
2246
+ ) {
2247
+ const backgroundCanvas = this.backgroundCanvas;
2248
+ const canvas = this.canvas;
2249
+ const transientCanvas = this.transientCanvas;
2250
+ const backgroundRoot = canvas.getRoot();
2251
+ const root = canvas.getRoot();
2252
+ const transientRoot = transientCanvas.getRoot();
2253
+ const rendererType = this.rendererType;
2254
+
2255
+ const backgroundBBox = backgroundRoot.getBBox();
2256
+ const BBox = root.getBBox();
2257
+ const transientBBox = transientRoot.getBBox();
2258
+
2259
+ let padding = imageConfig ? imageConfig.padding : undefined;
2260
+ if (!padding) {
2261
+ padding = [0, 0, 0, 0];
2262
+ } else if (isNumber(padding)) {
2263
+ padding = [padding, padding, padding, padding];
2264
+ }
2265
+
2266
+ const left =
2267
+ (transientBBox.left
2268
+ ? backgroundBBox.left
2269
+ ? Math.min(backgroundBBox.left, BBox.left, transientBBox.left)
2270
+ : Math.min(BBox.left, transientBBox.left)
2271
+ : BBox.left) - padding[3];
2272
+ const right =
2273
+ (transientBBox.right
2274
+ ? backgroundBBox.right
2275
+ ? Math.max(backgroundBBox.right, BBox.right, transientBBox.right)
2276
+ : Math.max(BBox.right, transientBBox.right)
2277
+ : BBox.right) + padding[1];
2278
+ const top =
2279
+ (transientBBox.top
2280
+ ? backgroundBBox.top
2281
+ ? Math.min(backgroundBBox.top, BBox.top, transientBBox.top)
2282
+ : Math.min(BBox.top, transientBBox.top)
2283
+ : BBox.top) - padding[0];
2284
+ const bottom =
2285
+ (transientBBox.bottom
2286
+ ? backgroundBBox.bottom
2287
+ ? Math.max(backgroundBBox.bottom, BBox.bottom, transientBBox.bottom)
2288
+ : Math.max(BBox.bottom, transientBBox.bottom)
2289
+ : BBox.bottom) + padding[2];
2290
+
2291
+ const graphCenterX = (left + right) / 2;
2292
+ const graphCenterY = (top + bottom) / 2;
2293
+ const halfX = (right - left) / 2;
2294
+ const halfY = (bottom - top) / 2;
2295
+
2296
+ const pixelRatio =
2297
+ typeof window !== 'undefined' ? window.devicePixelRatio : 1;
2298
+ const vWidth = halfX * 2;
2299
+ const vHeight = halfY * 2;
2300
+ const vContainerDOM: HTMLDivElement = createDom(
2301
+ '<div id="virtual-image"></div>',
2302
+ );
2303
+ const vCanvas = createCanvas(
2304
+ rendererType,
2305
+ vContainerDOM,
2306
+ vWidth,
2307
+ vHeight,
2308
+ pixelRatio,
2309
+ );
2310
+ const vCanvasContextService = vCanvas.getContextService();
2311
+ if (rendererType !== 'svg') {
2312
+ const bgRect = new Rect({
2313
+ style: {
2314
+ x: 0,
2315
+ y: 0,
2316
+ z: -1,
2317
+ width: vCanvasContextService.getDomElement().width,
2318
+ height: vCanvasContextService.getDomElement().height,
2319
+ //@ts-ignore
2320
+ fill: backgroundCanvas.getContextService().getDomElement().style[
2321
+ 'background-color'
2322
+ ],
2323
+ },
2324
+ });
2325
+ vCanvas.appendChild(bgRect);
2326
+ }
2327
+ const backgroundClonedGroup = backgroundRoot.cloneNode(true);
2328
+ const clonedGroup = root.cloneNode(true);
2329
+ const transientClonedGroup = transientRoot.cloneNode(true);
2330
+ const transPosition: [number, number] = [
2331
+ -graphCenterX + halfX,
2332
+ -graphCenterY + halfY,
2333
+ ];
2334
+ backgroundClonedGroup.setPosition(transPosition);
2335
+ clonedGroup.setPosition(transPosition);
2336
+ transientClonedGroup.setPosition(transPosition);
2337
+ vCanvas.appendChild(backgroundClonedGroup);
2338
+ vCanvas.appendChild(clonedGroup);
2339
+ vCanvas.appendChild(transientClonedGroup);
2340
+ vCanvas.render();
2341
+
2342
+ if (!type) type = 'image/png';
2343
+ let dataURL = '';
2344
+ await vCanvasContextService.toDataURL({ type }).then((url) => {
2345
+ dataURL = url;
2346
+ });
2347
+ return dataURL;
2348
+ }
2349
+
2350
+ /**
2351
+ * Converts a Data URL to an image file and handles the download of the image.
2352
+ * @param dataURL The Data URL of the image to be converted and downloaded.
2353
+ * @param renderer The renderer type ('svg' or other) used for the canvas.
2354
+ * @param link The HTML link element used for image download.
2355
+ * @param fileName The desired name for the downloaded image file.
2356
+ */
2357
+ private dataURLToImage(dataURL: string, renderer: string, link, fileName) {
2358
+ if (!dataURL || dataURL === 'data:') {
2359
+ console.error(
2360
+ 'Download image failed. The graph is too large or there is invalid attribute values in graph items',
2361
+ );
2362
+ return;
2363
+ }
2364
+
2365
+ if (typeof window !== 'undefined') {
2366
+ if (window.Blob && window.URL && renderer !== 'svg') {
2367
+ const arr = dataURL.split(',');
2368
+ let mime = '';
2369
+ if (arr && arr.length > 0) {
2370
+ const match = arr[0].match(/:(.*?);/);
2371
+ // eslint-disable-next-line prefer-destructuring
2372
+ if (match && match.length >= 2) mime = match[1];
2373
+ }
2374
+
2375
+ const bstr = atob(arr[1]);
2376
+ let n = bstr.length;
2377
+ const u8arr = new Uint8Array(n);
2378
+
2379
+ while (n--) {
2380
+ u8arr[n] = bstr.charCodeAt(n);
2381
+ }
2382
+
2383
+ const blobObj = new Blob([u8arr], { type: mime });
2384
+
2385
+ if ((window.navigator as any).msSaveBlob) {
2386
+ (window.navigator as any).msSaveBlob(blobObj, fileName);
2387
+ } else {
2388
+ link.addEventListener('click', () => {
2389
+ link.download = fileName;
2390
+ link.href = window.URL.createObjectURL(blobObj);
2391
+ });
2392
+ }
2393
+ } else {
2394
+ link.addEventListener('click', () => {
2395
+ link.download = fileName;
2396
+ link.href = dataURL;
2397
+ });
2398
+ }
2399
+ }
2400
+ }
2401
+
2402
+ /**
2403
+ * Initiates the download of the graph as an image with an optional name and type.
2404
+ * @param name The desired name for the downloaded image file (optional).
2405
+ * @param type The type of the image to download (optional, defaults to 'image/png').
2406
+ */
2407
+ public downloadImage(name?: string, type?: DataURLType): void {
2408
+ const self = this;
2409
+ // self.stopAnimate();
2410
+
2411
+ const rendererType = this.rendererType;
2412
+ if (!type) type = 'image/png';
2413
+
2414
+ const fileName: string =
2415
+ (name || 'graph') +
2416
+ (rendererType === 'svg' ? '.svg' : type.split('/')[1]);
2417
+
2418
+ const link: HTMLAnchorElement = document.createElement('a');
2419
+
2420
+ self.asyncToDataUrl(type, (dataURL) => {
2421
+ this.dataURLToImage(dataURL, rendererType, link, fileName);
2422
+ const e = document.createEvent('MouseEvents');
2423
+ e.initEvent('click', false, false);
2424
+ link.dispatchEvent(e);
2425
+ });
2426
+ }
2427
+
2428
+ /**
2429
+ * Initiates the download of the entire graph as an image with optional name, type, and padding configuration.
2430
+ * @param name The desired name for the downloaded image file (optional).
2431
+ * @param type The type of the image to download (optional, defaults to 'image/png').
2432
+ * @param imageConfig Configuration options for the image (optional).
2433
+ */
2434
+ public downloadFullImage(
2435
+ name?: string,
2436
+ type?: DataURLType,
2437
+ imageConfig?: { padding?: number | number[] },
2438
+ ): void {
2439
+ const self = this;
2440
+
2441
+ const rendererType = this.rendererType;
2442
+ if (!type) type = 'image/png';
2443
+ const fileName: string =
2444
+ (name || 'graph') +
2445
+ (rendererType === 'svg' ? '.svg' : type.split('/')[1]);
2446
+ const link: HTMLAnchorElement = document.createElement('a');
2447
+
2448
+ self.asyncToFullDataUrl(type, imageConfig, (dataURL) => {
2449
+ this.dataURLToImage(dataURL, rendererType, link, fileName);
2450
+ const e = document.createEvent('MouseEvents');
2451
+ e.initEvent('click', false, false);
2452
+ link.dispatchEvent(e);
2453
+ });
2454
+ }
2455
+
2456
+ /**
2457
+ * Asynchronously converts the canvas content to a Data URL of the specified type and invokes the provided callback.
2458
+ * @param type The type of the Data URL (optional, defaults to 'image/png').
2459
+ * @param callback A callback function to handle the Data URL (optional).
2460
+ */
2461
+ protected asyncToDataUrl(type?: DataURLType, callback?: Function): void {
2462
+ let dataURL = '';
2463
+ if (!type) type = 'image/png';
2464
+
2465
+ setTimeout(async () => {
2466
+ await this.toDataURL(type).then((url) => {
2467
+ dataURL = url;
2468
+ });
2469
+ if (callback) callback(dataURL);
2470
+ }, 16);
2471
+ }
2472
+
2473
+ /**
2474
+ * Asynchronously converts the entire canvas content to a Data URL of the specified type
2475
+ * with optional padding, and invokes the provided callback.
2476
+ * @param type The type of the Data URL (optional, defaults to 'image/png').
2477
+ * @param imageConfig Configuration options for the image (optional).
2478
+ * @param callback A callback function to handle the Data URL (optional).
2479
+ */
2480
+ protected asyncToFullDataUrl(
2481
+ type?: DataURLType,
2482
+ imageConfig?: { padding?: number | number[] },
2483
+ callback?: (dataUrl: string) => void,
2484
+ ): void {
2485
+ let dataURL = '';
2486
+ if (!type) type = 'image/png';
2487
+
2488
+ setTimeout(async () => {
2489
+ await this.toFullDataURL(type, imageConfig).then((url) => {
2490
+ dataURL = url;
2491
+ });
2492
+ if (callback) callback(dataURL);
2493
+ }, 16);
2494
+ }
2495
+
2061
2496
  // ===== history operations =====
2062
2497
 
2063
2498
  /**