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

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 (147) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/esm/index.d.ts +13 -1
  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 +1 -1
  8. package/esm/item/edge.js +15 -7
  9. package/esm/item/edge.js.map +1 -1
  10. package/esm/item/node.d.ts +1 -1
  11. package/esm/item/node.js +16 -9
  12. package/esm/item/node.js.map +1 -1
  13. package/esm/runtime/controller/item.d.ts +1 -0
  14. package/esm/runtime/controller/item.js +19 -3
  15. package/esm/runtime/controller/item.js.map +1 -1
  16. package/esm/runtime/graph.d.ts +82 -4
  17. package/esm/runtime/graph.js +361 -47
  18. package/esm/runtime/graph.js.map +1 -1
  19. package/esm/stdlib/behavior/brush-select.js +1 -2
  20. package/esm/stdlib/behavior/brush-select.js.map +1 -1
  21. package/esm/stdlib/behavior/create-edge.d.ts +57 -0
  22. package/esm/stdlib/behavior/create-edge.js +206 -0
  23. package/esm/stdlib/behavior/create-edge.js.map +1 -0
  24. package/esm/stdlib/behavior/drag-canvas.d.ts +1 -1
  25. package/esm/stdlib/behavior/drag-canvas.js +7 -7
  26. package/esm/stdlib/behavior/drag-canvas.js.map +1 -1
  27. package/esm/stdlib/behavior/drag-combo.d.ts +2 -2
  28. package/esm/stdlib/behavior/drag-combo.js +35 -24
  29. package/esm/stdlib/behavior/drag-combo.js.map +1 -1
  30. package/esm/stdlib/behavior/drag-node.d.ts +3 -3
  31. package/esm/stdlib/behavior/drag-node.js +70 -57
  32. package/esm/stdlib/behavior/drag-node.js.map +1 -1
  33. package/esm/stdlib/behavior/index.d.ts +2 -0
  34. package/esm/stdlib/behavior/index.js +2 -0
  35. package/esm/stdlib/behavior/index.js.map +1 -1
  36. package/esm/stdlib/behavior/lasso-select.d.ts +1 -1
  37. package/esm/stdlib/behavior/lasso-select.js +1 -2
  38. package/esm/stdlib/behavior/lasso-select.js.map +1 -1
  39. package/esm/stdlib/behavior/scroll-canvas.d.ts +67 -0
  40. package/esm/stdlib/behavior/scroll-canvas.js +266 -0
  41. package/esm/stdlib/behavior/scroll-canvas.js.map +1 -0
  42. package/esm/stdlib/behavior/zoom-canvas.d.ts +1 -1
  43. package/esm/stdlib/behavior/zoom-canvas.js +7 -7
  44. package/esm/stdlib/behavior/zoom-canvas.js.map +1 -1
  45. package/esm/stdlib/index.d.ts +4 -0
  46. package/esm/stdlib/index.js +5 -1
  47. package/esm/stdlib/index.js.map +1 -1
  48. package/esm/stdlib/plugin/hull/hullComponent.d.ts +1 -1
  49. package/esm/stdlib/plugin/hull/hullComponent.js +3 -6
  50. package/esm/stdlib/plugin/hull/hullComponent.js.map +1 -1
  51. package/esm/stdlib/plugin/minimap/index.d.ts +5 -1
  52. package/esm/stdlib/plugin/minimap/index.js +109 -8
  53. package/esm/stdlib/plugin/minimap/index.js.map +1 -1
  54. package/esm/types/graph.d.ts +31 -5
  55. package/esm/types/hook.d.ts +16 -2
  56. package/esm/types/spec.d.ts +6 -3
  57. package/esm/util/item.d.ts +16 -1
  58. package/esm/util/item.js +31 -8
  59. package/esm/util/item.js.map +1 -1
  60. package/esm/util/warn.d.ts +14 -0
  61. package/esm/util/warn.js +4 -0
  62. package/esm/util/warn.js.map +1 -0
  63. package/lib/index.d.ts +13 -1
  64. package/lib/index.js +1 -1
  65. package/lib/item/combo.d.ts +1 -1
  66. package/lib/item/combo.js +16 -9
  67. package/lib/item/combo.js.map +1 -1
  68. package/lib/item/edge.d.ts +1 -1
  69. package/lib/item/edge.js +15 -7
  70. package/lib/item/edge.js.map +1 -1
  71. package/lib/item/node.d.ts +1 -1
  72. package/lib/item/node.js +16 -9
  73. package/lib/item/node.js.map +1 -1
  74. package/lib/runtime/controller/item.d.ts +1 -0
  75. package/lib/runtime/controller/item.js +19 -3
  76. package/lib/runtime/controller/item.js.map +1 -1
  77. package/lib/runtime/graph.d.ts +82 -4
  78. package/lib/runtime/graph.js +361 -47
  79. package/lib/runtime/graph.js.map +1 -1
  80. package/lib/stdlib/behavior/brush-select.js +1 -2
  81. package/lib/stdlib/behavior/brush-select.js.map +1 -1
  82. package/lib/stdlib/behavior/create-edge.d.ts +57 -0
  83. package/lib/stdlib/behavior/create-edge.js +210 -0
  84. package/lib/stdlib/behavior/create-edge.js.map +1 -0
  85. package/lib/stdlib/behavior/drag-canvas.d.ts +1 -1
  86. package/lib/stdlib/behavior/drag-canvas.js +7 -7
  87. package/lib/stdlib/behavior/drag-canvas.js.map +1 -1
  88. package/lib/stdlib/behavior/drag-combo.d.ts +2 -2
  89. package/lib/stdlib/behavior/drag-combo.js +35 -24
  90. package/lib/stdlib/behavior/drag-combo.js.map +1 -1
  91. package/lib/stdlib/behavior/drag-node.d.ts +3 -3
  92. package/lib/stdlib/behavior/drag-node.js +70 -57
  93. package/lib/stdlib/behavior/drag-node.js.map +1 -1
  94. package/lib/stdlib/behavior/index.d.ts +2 -0
  95. package/lib/stdlib/behavior/index.js +2 -0
  96. package/lib/stdlib/behavior/index.js.map +1 -1
  97. package/lib/stdlib/behavior/lasso-select.d.ts +1 -1
  98. package/lib/stdlib/behavior/lasso-select.js +1 -2
  99. package/lib/stdlib/behavior/lasso-select.js.map +1 -1
  100. package/lib/stdlib/behavior/scroll-canvas.d.ts +67 -0
  101. package/lib/stdlib/behavior/scroll-canvas.js +270 -0
  102. package/lib/stdlib/behavior/scroll-canvas.js.map +1 -0
  103. package/lib/stdlib/behavior/zoom-canvas.d.ts +1 -1
  104. package/lib/stdlib/behavior/zoom-canvas.js +7 -7
  105. package/lib/stdlib/behavior/zoom-canvas.js.map +1 -1
  106. package/lib/stdlib/index.d.ts +4 -0
  107. package/lib/stdlib/index.js +5 -1
  108. package/lib/stdlib/index.js.map +1 -1
  109. package/lib/stdlib/plugin/hull/hullComponent.d.ts +1 -1
  110. package/lib/stdlib/plugin/hull/hullComponent.js +3 -6
  111. package/lib/stdlib/plugin/hull/hullComponent.js.map +1 -1
  112. package/lib/stdlib/plugin/minimap/index.d.ts +5 -1
  113. package/lib/stdlib/plugin/minimap/index.js +109 -8
  114. package/lib/stdlib/plugin/minimap/index.js.map +1 -1
  115. package/lib/types/graph.d.ts +31 -5
  116. package/lib/types/hook.d.ts +16 -2
  117. package/lib/types/spec.d.ts +6 -3
  118. package/lib/util/item.d.ts +16 -1
  119. package/lib/util/item.js +33 -9
  120. package/lib/util/item.js.map +1 -1
  121. package/lib/util/warn.d.ts +14 -0
  122. package/lib/util/warn.js +8 -0
  123. package/lib/util/warn.js.map +1 -0
  124. package/package.json +1 -1
  125. package/src/index.ts +1 -1
  126. package/src/item/combo.ts +14 -9
  127. package/src/item/edge.ts +13 -7
  128. package/src/item/node.ts +14 -9
  129. package/src/runtime/controller/item.ts +28 -6
  130. package/src/runtime/graph.ts +445 -51
  131. package/src/stdlib/behavior/brush-select.ts +4 -2
  132. package/src/stdlib/behavior/create-edge.ts +295 -0
  133. package/src/stdlib/behavior/drag-canvas.ts +8 -8
  134. package/src/stdlib/behavior/drag-combo.ts +2 -2
  135. package/src/stdlib/behavior/drag-node.ts +8 -8
  136. package/src/stdlib/behavior/index.ts +2 -0
  137. package/src/stdlib/behavior/lasso-select.ts +5 -3
  138. package/src/stdlib/behavior/scroll-canvas.ts +348 -0
  139. package/src/stdlib/behavior/zoom-canvas.ts +8 -8
  140. package/src/stdlib/index.ts +6 -0
  141. package/src/stdlib/plugin/hull/hullComponent.ts +8 -5
  142. package/src/stdlib/plugin/minimap/index.ts +113 -9
  143. package/src/types/graph.ts +31 -5
  144. package/src/types/hook.ts +16 -2
  145. package/src/types/spec.ts +9 -3
  146. package/src/util/item.ts +65 -28
  147. 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 {
@@ -48,6 +57,7 @@ import { FitViewRules, GraphTransformOptions } from '../types/view';
48
57
  import { changeRenderer, createCanvas } from '../util/canvas';
49
58
  import { formatPadding } from '../util/shape';
50
59
  import { Plugin as PluginBase } from '../types/plugin';
60
+ import { ComboMapper, EdgeMapper, NodeMapper } from '../types/spec';
51
61
  import {
52
62
  DataController,
53
63
  ExtensionController,
@@ -349,7 +359,11 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
349
359
  main: Canvas;
350
360
  transient: Canvas;
351
361
  };
352
- }>({ name: 'init' }),
362
+ }>({ name: 'themechange' }),
363
+ mapperchange: new Hook<{
364
+ type: ITEM_TYPE;
365
+ mapper: NodeMapper | EdgeMapper | ComboMapper;
366
+ }>({ name: 'mapperchange' }),
353
367
  treecollapseexpand: new Hook<{
354
368
  ids: ID[];
355
369
  animate: boolean;
@@ -404,6 +418,32 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
404
418
  });
405
419
  }
406
420
 
421
+ /**
422
+ * Update the item display mapper for a specific item type.
423
+ * @param {ITEM_TYPE} type - The type of item (node, edge, or combo).
424
+ * @param {NodeMapper | EdgeMapper | ComboMapper} mapper - The mapper to be updated.
425
+ * */
426
+ public updateMapper(
427
+ type: ITEM_TYPE,
428
+ mapper: NodeMapper | EdgeMapper | ComboMapper,
429
+ ) {
430
+ switch (type) {
431
+ case 'node':
432
+ this.specification.node = mapper as NodeMapper;
433
+ break;
434
+ case 'edge':
435
+ this.specification.edge = mapper as EdgeMapper;
436
+ break;
437
+ case 'combo':
438
+ this.specification.combo = mapper as ComboMapper;
439
+ break;
440
+ }
441
+ this.hooks.mapperchange.emit({
442
+ type,
443
+ mapper,
444
+ });
445
+ }
446
+
407
447
  /**
408
448
  * Get the copy of specs(configurations).
409
449
  * @returns graph specs
@@ -1043,22 +1083,25 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1043
1083
 
1044
1084
  const { graphCore } = this.dataController;
1045
1085
  const { specification } = this.themeController;
1086
+ // debugger;
1046
1087
  graphCore.once('changed', (event) => {
1047
1088
  if (!event.changes.length) return;
1048
1089
  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', {
1090
+ const timingParameters = {
1056
1091
  type: itemType,
1057
1092
  action: 'add',
1058
1093
  models,
1059
1094
  apiName: 'addData',
1060
1095
  changes,
1096
+ };
1097
+ this.emit('beforeitemchange', timingParameters);
1098
+ this.hooks.itemchange.emit({
1099
+ type: itemType,
1100
+ changes: graphCore.reduceChanges(event.changes),
1101
+ graphCore,
1102
+ theme: specification,
1061
1103
  });
1104
+ this.emit('afteritemchange', timingParameters);
1062
1105
  });
1063
1106
 
1064
1107
  const modelArr = isArray(models) ? models : [models];
@@ -1091,19 +1134,21 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1091
1134
  graphCore.once('changed', (event) => {
1092
1135
  if (!event.changes.length) return;
1093
1136
  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', {
1137
+ const timingParameters = {
1101
1138
  type: itemType,
1102
1139
  action: 'remove',
1103
1140
  ids: idArr,
1104
1141
  apiName: 'removeData',
1105
1142
  changes,
1143
+ };
1144
+ this.emit('beforeitemchange', timingParameters);
1145
+ this.hooks.itemchange.emit({
1146
+ type: itemType,
1147
+ changes: event.changes,
1148
+ graphCore,
1149
+ theme: specification,
1106
1150
  });
1151
+ this.emit('afteritemchange', timingParameters);
1107
1152
  });
1108
1153
  this.hooks.datachange.emit({
1109
1154
  data,
@@ -1196,19 +1241,21 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1196
1241
  const { specification } = this.themeController;
1197
1242
  graphCore.once('changed', (event) => {
1198
1243
  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', {
1244
+ const timingParameters = {
1206
1245
  type: itemType,
1207
1246
  action: 'update',
1208
1247
  models,
1209
1248
  apiName: 'updateData',
1210
1249
  changes,
1250
+ };
1251
+ this.emit('beforeitemchange', timingParameters);
1252
+ this.hooks.itemchange.emit({
1253
+ type: itemType,
1254
+ changes: event.changes,
1255
+ graphCore,
1256
+ theme: specification,
1211
1257
  });
1258
+ this.emit('afteritemchange', timingParameters);
1212
1259
  });
1213
1260
 
1214
1261
  this.hooks.datachange.emit({
@@ -1317,6 +1364,15 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1317
1364
  const changes = event.changes.filter(
1318
1365
  (change) => !isEqual(change.newValue, change.oldValue),
1319
1366
  );
1367
+ const timingParameters = {
1368
+ type,
1369
+ action: 'updatePosition',
1370
+ upsertAncestors,
1371
+ models,
1372
+ apiName: 'updatePosition',
1373
+ changes,
1374
+ };
1375
+ this.emit('beforeitemchange', timingParameters);
1320
1376
  this.hooks.itemchange.emit({
1321
1377
  type,
1322
1378
  changes: event.changes,
@@ -1327,14 +1383,7 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1327
1383
  animate: !disableAnimate,
1328
1384
  callback,
1329
1385
  });
1330
- this.emit('afteritemchange', {
1331
- type,
1332
- action: 'updatePosition',
1333
- upsertAncestors,
1334
- models,
1335
- apiName: 'updatePosition',
1336
- changes,
1337
- });
1386
+ this.emit('afteritemchange', timingParameters);
1338
1387
  });
1339
1388
 
1340
1389
  this.hooks.datachange.emit({
@@ -1609,19 +1658,21 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1609
1658
  graphCore.once('changed', (event) => {
1610
1659
  if (!event.changes.length) return;
1611
1660
  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', {
1661
+ const timingParameters = {
1619
1662
  type: 'combo',
1620
1663
  action: 'add',
1621
1664
  models: [model],
1622
1665
  apiName: 'addCombo',
1623
1666
  changes,
1667
+ };
1668
+ this.emit('beforeitemchange', timingParameters);
1669
+ this.hooks.itemchange.emit({
1670
+ type: 'combo',
1671
+ changes: graphCore.reduceChanges(event.changes),
1672
+ graphCore,
1673
+ theme: specification,
1624
1674
  });
1675
+ this.emit('afteritemchange', timingParameters);
1625
1676
  });
1626
1677
 
1627
1678
  const data = {
@@ -1707,16 +1758,7 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1707
1758
  graphCore.once('changed', (event) => {
1708
1759
  if (!event.changes.length) return;
1709
1760
  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', {
1761
+ const timingParameters = {
1720
1762
  type: 'combo',
1721
1763
  ids: idArr,
1722
1764
  dx,
@@ -1725,7 +1767,18 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1725
1767
  upsertAncestors,
1726
1768
  apiName: 'moveCombo',
1727
1769
  changes,
1770
+ };
1771
+ this.emit('beforeitemchange', timingParameters);
1772
+ this.hooks.itemchange.emit({
1773
+ type: 'combo',
1774
+ changes: event.changes,
1775
+ graphCore,
1776
+ theme: specification,
1777
+ upsertAncestors,
1778
+ action: 'updatePosition',
1779
+ callback,
1728
1780
  });
1781
+ this.emit('afteritemchange', timingParameters);
1729
1782
  });
1730
1783
 
1731
1784
  this.hooks.datachange.emit({
@@ -1824,6 +1877,15 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
1824
1877
  return this.interactionController.getMode();
1825
1878
  }
1826
1879
 
1880
+ /**
1881
+ * Set the cursor. But the cursor in item's style has higher priority.
1882
+ * @param cursor
1883
+ */
1884
+ public setCursor(cursor: Cursor) {
1885
+ this.canvas.setCursor(cursor);
1886
+ this.transientCanvas.setCursor(cursor);
1887
+ }
1888
+
1827
1889
  /**
1828
1890
  * Add behavior(s) to mode(s).
1829
1891
  * @param behaviors behavior names or configs
@@ -2041,9 +2103,19 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
2041
2103
  type: ITEM_TYPE | SHAPE_TYPE,
2042
2104
  id: ID,
2043
2105
  config: {
2044
- action: 'remove' | 'add' | 'update' | undefined;
2045
- style: ShapeStyle;
2046
- onlyDrawKeyShape?: boolean;
2106
+ action?: 'remove' | 'add' | 'update' | undefined;
2107
+ /** Data to be merged into the transient item. */
2108
+ data?: Record<string, any>;
2109
+ /** Style to be merged into the transient shape. */
2110
+ style?: ShapeStyle;
2111
+ /** For type: 'edge' */
2112
+ drawSource?: boolean;
2113
+ /** For type: 'edge' */
2114
+ drawTarget?: boolean;
2115
+ /** 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. */
2116
+ shapeIds?: string[];
2117
+ /** Whether show the shapes in shapeIds. True by default. */
2118
+ visible?: boolean;
2047
2119
  upsertAncestors?: boolean;
2048
2120
  },
2049
2121
  canvas?: Canvas,
@@ -2058,6 +2130,328 @@ export default class Graph<B extends BehaviorRegistry, T extends ThemeRegistry>
2058
2130
  return this.itemController.getTransient(String(id));
2059
2131
  }
2060
2132
 
2133
+ // ===== download operations =====
2134
+ /**
2135
+ * Asynchronously generates a Data URL representation of the canvas content, including
2136
+ * background, main content, and transient canvas.
2137
+ * @param The type of the Data URL (e.g., 'image/png', 'image/jpeg').
2138
+ * @returns A Promise that resolves to the Data URL string.
2139
+ */
2140
+ public async toDataURL(type?: DataURLType): Promise<string> {
2141
+ const backgroundCanvas = this.backgroundCanvas;
2142
+ const canvas = this.canvas;
2143
+ const transientCanvas = this.transientCanvas;
2144
+ const rendererType = this.rendererType;
2145
+
2146
+ const pixelRatio =
2147
+ typeof window !== 'undefined' ? window.devicePixelRatio : 1;
2148
+ const width = this.getSize()[0];
2149
+ const height = this.getSize()[1];
2150
+
2151
+ const vContainerDOM: HTMLDivElement = createDom(
2152
+ '<div id="virtual-image"></div>',
2153
+ );
2154
+ const vCanvas = createCanvas(
2155
+ rendererType,
2156
+ vContainerDOM,
2157
+ width,
2158
+ height,
2159
+ pixelRatio,
2160
+ );
2161
+ const vCanvasContextService = vCanvas.getContextService();
2162
+ let bgRect;
2163
+ if (rendererType !== 'svg') {
2164
+ bgRect = new Rect({
2165
+ style: {
2166
+ x: 0,
2167
+ y: 0,
2168
+ z: -1,
2169
+ width: vCanvasContextService.getDomElement().width,
2170
+ height: vCanvasContextService.getDomElement().height,
2171
+ //@ts-ignore
2172
+ fill: backgroundCanvas.getContextService().getDomElement().style[
2173
+ 'background-color'
2174
+ ],
2175
+ },
2176
+ });
2177
+ vCanvas.appendChild(bgRect);
2178
+ }
2179
+ const backgroundClonedGroup = backgroundCanvas.getRoot().cloneNode(true);
2180
+ const clonedGroup = canvas.getRoot().cloneNode(true);
2181
+ const transientClonedGroup = transientCanvas.getRoot().cloneNode(true);
2182
+ vCanvas.appendChild(backgroundClonedGroup);
2183
+ vCanvas.appendChild(clonedGroup);
2184
+ vCanvas.appendChild(transientClonedGroup);
2185
+ vCanvas.render();
2186
+
2187
+ if (!type) type = 'image/png';
2188
+ let dataURL = '';
2189
+ await vCanvasContextService.toDataURL({ type }).then((url) => {
2190
+ dataURL = url;
2191
+ });
2192
+ return dataURL;
2193
+ }
2194
+
2195
+ /**
2196
+ * Asynchronously generates a Data URL representation of the full canvas content,
2197
+ * including background, main content, and transient canvas, with optional padding.
2198
+ * @param type The type of the Data URL (e.g., 'image/png', 'image/jpeg').
2199
+ * @param imageConfig Configuration options for the image (optional).
2200
+ * @returns A Promise that resolves to the Data URL string.
2201
+ */
2202
+ public async toFullDataURL(
2203
+ type?: DataURLType,
2204
+ imageConfig?: { padding?: number | number[] },
2205
+ ) {
2206
+ const backgroundCanvas = this.backgroundCanvas;
2207
+ const canvas = this.canvas;
2208
+ const transientCanvas = this.transientCanvas;
2209
+ const backgroundRoot = canvas.getRoot();
2210
+ const root = canvas.getRoot();
2211
+ const transientRoot = transientCanvas.getRoot();
2212
+ const rendererType = this.rendererType;
2213
+
2214
+ const backgroundBBox = backgroundRoot.getBBox();
2215
+ const BBox = root.getBBox();
2216
+ const transientBBox = transientRoot.getBBox();
2217
+
2218
+ let padding = imageConfig ? imageConfig.padding : undefined;
2219
+ if (!padding) {
2220
+ padding = [0, 0, 0, 0];
2221
+ } else if (isNumber(padding)) {
2222
+ padding = [padding, padding, padding, padding];
2223
+ }
2224
+
2225
+ const left =
2226
+ (transientBBox.left
2227
+ ? backgroundBBox.left
2228
+ ? Math.min(backgroundBBox.left, BBox.left, transientBBox.left)
2229
+ : Math.min(BBox.left, transientBBox.left)
2230
+ : BBox.left) - padding[3];
2231
+ const right =
2232
+ (transientBBox.right
2233
+ ? backgroundBBox.right
2234
+ ? Math.max(backgroundBBox.right, BBox.right, transientBBox.right)
2235
+ : Math.max(BBox.right, transientBBox.right)
2236
+ : BBox.right) + padding[1];
2237
+ const top =
2238
+ (transientBBox.top
2239
+ ? backgroundBBox.top
2240
+ ? Math.min(backgroundBBox.top, BBox.top, transientBBox.top)
2241
+ : Math.min(BBox.top, transientBBox.top)
2242
+ : BBox.top) - padding[0];
2243
+ const bottom =
2244
+ (transientBBox.bottom
2245
+ ? backgroundBBox.bottom
2246
+ ? Math.max(backgroundBBox.bottom, BBox.bottom, transientBBox.bottom)
2247
+ : Math.max(BBox.bottom, transientBBox.bottom)
2248
+ : BBox.bottom) + padding[2];
2249
+
2250
+ const graphCenterX = (left + right) / 2;
2251
+ const graphCenterY = (top + bottom) / 2;
2252
+ const halfX = (right - left) / 2;
2253
+ const halfY = (bottom - top) / 2;
2254
+
2255
+ const pixelRatio =
2256
+ typeof window !== 'undefined' ? window.devicePixelRatio : 1;
2257
+ const vWidth = halfX * 2;
2258
+ const vHeight = halfY * 2;
2259
+ const vContainerDOM: HTMLDivElement = createDom(
2260
+ '<div id="virtual-image"></div>',
2261
+ );
2262
+ const vCanvas = createCanvas(
2263
+ rendererType,
2264
+ vContainerDOM,
2265
+ vWidth,
2266
+ vHeight,
2267
+ pixelRatio,
2268
+ );
2269
+ const vCanvasContextService = vCanvas.getContextService();
2270
+ if (rendererType !== 'svg') {
2271
+ const bgRect = new Rect({
2272
+ style: {
2273
+ x: 0,
2274
+ y: 0,
2275
+ z: -1,
2276
+ width: vCanvasContextService.getDomElement().width,
2277
+ height: vCanvasContextService.getDomElement().height,
2278
+ //@ts-ignore
2279
+ fill: backgroundCanvas.getContextService().getDomElement().style[
2280
+ 'background-color'
2281
+ ],
2282
+ },
2283
+ });
2284
+ vCanvas.appendChild(bgRect);
2285
+ }
2286
+ const backgroundClonedGroup = backgroundRoot.cloneNode(true);
2287
+ const clonedGroup = root.cloneNode(true);
2288
+ const transientClonedGroup = transientRoot.cloneNode(true);
2289
+ const transPosition: [number, number] = [
2290
+ -graphCenterX + halfX,
2291
+ -graphCenterY + halfY,
2292
+ ];
2293
+ backgroundClonedGroup.setPosition(transPosition);
2294
+ clonedGroup.setPosition(transPosition);
2295
+ transientClonedGroup.setPosition(transPosition);
2296
+ vCanvas.appendChild(backgroundClonedGroup);
2297
+ vCanvas.appendChild(clonedGroup);
2298
+ vCanvas.appendChild(transientClonedGroup);
2299
+ vCanvas.render();
2300
+
2301
+ if (!type) type = 'image/png';
2302
+ let dataURL = '';
2303
+ await vCanvasContextService.toDataURL({ type }).then((url) => {
2304
+ dataURL = url;
2305
+ });
2306
+ return dataURL;
2307
+ }
2308
+
2309
+ /**
2310
+ * Converts a Data URL to an image file and handles the download of the image.
2311
+ * @param dataURL The Data URL of the image to be converted and downloaded.
2312
+ * @param renderer The renderer type ('svg' or other) used for the canvas.
2313
+ * @param link The HTML link element used for image download.
2314
+ * @param fileName The desired name for the downloaded image file.
2315
+ */
2316
+ private dataURLToImage(dataURL: string, renderer: string, link, fileName) {
2317
+ if (!dataURL || dataURL === 'data:') {
2318
+ console.error(
2319
+ 'Download image failed. The graph is too large or there is invalid attribute values in graph items',
2320
+ );
2321
+ return;
2322
+ }
2323
+
2324
+ if (typeof window !== 'undefined') {
2325
+ if (window.Blob && window.URL && renderer !== 'svg') {
2326
+ const arr = dataURL.split(',');
2327
+ let mime = '';
2328
+ if (arr && arr.length > 0) {
2329
+ const match = arr[0].match(/:(.*?);/);
2330
+ // eslint-disable-next-line prefer-destructuring
2331
+ if (match && match.length >= 2) mime = match[1];
2332
+ }
2333
+
2334
+ const bstr = atob(arr[1]);
2335
+ let n = bstr.length;
2336
+ const u8arr = new Uint8Array(n);
2337
+
2338
+ while (n--) {
2339
+ u8arr[n] = bstr.charCodeAt(n);
2340
+ }
2341
+
2342
+ const blobObj = new Blob([u8arr], { type: mime });
2343
+
2344
+ if ((window.navigator as any).msSaveBlob) {
2345
+ (window.navigator as any).msSaveBlob(blobObj, fileName);
2346
+ } else {
2347
+ link.addEventListener('click', () => {
2348
+ link.download = fileName;
2349
+ link.href = window.URL.createObjectURL(blobObj);
2350
+ });
2351
+ }
2352
+ } else {
2353
+ link.addEventListener('click', () => {
2354
+ link.download = fileName;
2355
+ link.href = dataURL;
2356
+ });
2357
+ }
2358
+ }
2359
+ }
2360
+
2361
+ /**
2362
+ * Initiates the download of the graph as an image with an optional name and type.
2363
+ * @param name The desired name for the downloaded image file (optional).
2364
+ * @param type The type of the image to download (optional, defaults to 'image/png').
2365
+ */
2366
+ public downloadImage(name?: string, type?: DataURLType): void {
2367
+ const self = this;
2368
+ // self.stopAnimate();
2369
+
2370
+ const rendererType = this.rendererType;
2371
+ if (!type) type = 'image/png';
2372
+
2373
+ const fileName: string =
2374
+ (name || 'graph') +
2375
+ (rendererType === 'svg' ? '.svg' : type.split('/')[1]);
2376
+
2377
+ const link: HTMLAnchorElement = document.createElement('a');
2378
+
2379
+ self.asyncToDataUrl(type, (dataURL) => {
2380
+ this.dataURLToImage(dataURL, rendererType, link, fileName);
2381
+ const e = document.createEvent('MouseEvents');
2382
+ e.initEvent('click', false, false);
2383
+ link.dispatchEvent(e);
2384
+ });
2385
+ }
2386
+
2387
+ /**
2388
+ * Initiates the download of the entire graph as an image with optional name, type, and padding configuration.
2389
+ * @param name The desired name for the downloaded image file (optional).
2390
+ * @param type The type of the image to download (optional, defaults to 'image/png').
2391
+ * @param imageConfig Configuration options for the image (optional).
2392
+ */
2393
+ public downloadFullImage(
2394
+ name?: string,
2395
+ type?: DataURLType,
2396
+ imageConfig?: { padding?: number | number[] },
2397
+ ): void {
2398
+ const self = this;
2399
+
2400
+ const rendererType = this.rendererType;
2401
+ if (!type) type = 'image/png';
2402
+ const fileName: string =
2403
+ (name || 'graph') +
2404
+ (rendererType === 'svg' ? '.svg' : type.split('/')[1]);
2405
+ const link: HTMLAnchorElement = document.createElement('a');
2406
+
2407
+ self.asyncToFullDataUrl(type, imageConfig, (dataURL) => {
2408
+ this.dataURLToImage(dataURL, rendererType, link, fileName);
2409
+ const e = document.createEvent('MouseEvents');
2410
+ e.initEvent('click', false, false);
2411
+ link.dispatchEvent(e);
2412
+ });
2413
+ }
2414
+
2415
+ /**
2416
+ * Asynchronously converts the canvas content to a Data URL of the specified type and invokes the provided callback.
2417
+ * @param type The type of the Data URL (optional, defaults to 'image/png').
2418
+ * @param callback A callback function to handle the Data URL (optional).
2419
+ */
2420
+ protected asyncToDataUrl(type?: DataURLType, callback?: Function): void {
2421
+ let dataURL = '';
2422
+ if (!type) type = 'image/png';
2423
+
2424
+ setTimeout(async () => {
2425
+ await this.toDataURL(type).then((url) => {
2426
+ dataURL = url;
2427
+ });
2428
+ if (callback) callback(dataURL);
2429
+ }, 16);
2430
+ }
2431
+
2432
+ /**
2433
+ * Asynchronously converts the entire canvas content to a Data URL of the specified type
2434
+ * with optional padding, and invokes the provided callback.
2435
+ * @param type The type of the Data URL (optional, defaults to 'image/png').
2436
+ * @param imageConfig Configuration options for the image (optional).
2437
+ * @param callback A callback function to handle the Data URL (optional).
2438
+ */
2439
+ protected asyncToFullDataUrl(
2440
+ type?: DataURLType,
2441
+ imageConfig?: { padding?: number | number[] },
2442
+ callback?: (dataUrl: string) => void,
2443
+ ): void {
2444
+ let dataURL = '';
2445
+ if (!type) type = 'image/png';
2446
+
2447
+ setTimeout(async () => {
2448
+ await this.toFullDataURL(type, imageConfig).then((url) => {
2449
+ dataURL = url;
2450
+ });
2451
+ if (callback) callback(dataURL);
2452
+ }, 16);
2453
+ }
2454
+
2061
2455
  // ===== history operations =====
2062
2456
 
2063
2457
  /**
@@ -345,8 +345,10 @@ export class BrushSelect extends Behavior {
345
345
  const { graph, options } = this;
346
346
  const { brushStyle } = options;
347
347
  const brush = graph.drawTransient('rect', BRUSH_SHAPE_ID, {
348
- style: brushStyle,
349
- capture: false,
348
+ style: {
349
+ ...brushStyle,
350
+ pointerEvents: 'none',
351
+ },
350
352
  });
351
353
  return brush;
352
354
  }