@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
@@ -19,7 +19,9 @@ var __rest = (this && this.__rest) || function (s, e) {
19
19
  return t;
20
20
  };
21
21
  import EventEmitter from '@antv/event-emitter';
22
+ import { Rect, } from '@antv/g';
22
23
  import { clone, groupBy, isArray, isEmpty, isEqual, isNil, isNumber, isObject, isString, map, } from '@antv/util';
24
+ import { createDom } from '@antv/dom-util';
23
25
  import { changeRenderer, createCanvas } from '../util/canvas';
24
26
  import { formatPadding } from '../util/shape';
25
27
  import { DataController, ExtensionController, InteractionController, ItemController, LayoutController, ThemeController, ViewportController, } from './controller';
@@ -193,7 +195,8 @@ export default class Graph extends EventEmitter {
193
195
  }),
194
196
  transientupdate: new Hook({ name: 'transientupdate' }),
195
197
  pluginchange: new Hook({ name: 'pluginchange' }),
196
- themechange: new Hook({ name: 'init' }),
198
+ themechange: new Hook({ name: 'themechange' }),
199
+ mapperchange: new Hook({ name: 'mapperchange' }),
197
200
  treecollapseexpand: new Hook({ name: 'treecollapseexpand' }),
198
201
  destroy: new Hook({ name: 'destroy' }),
199
202
  };
@@ -228,6 +231,28 @@ export default class Graph extends EventEmitter {
228
231
  theme: this.themeController.specification,
229
232
  });
230
233
  }
234
+ /**
235
+ * Update the item display mapper for a specific item type.
236
+ * @param {ITEM_TYPE} type - The type of item (node, edge, or combo).
237
+ * @param {NodeMapper | EdgeMapper | ComboMapper} mapper - The mapper to be updated.
238
+ * */
239
+ updateMapper(type, mapper) {
240
+ switch (type) {
241
+ case 'node':
242
+ this.specification.node = mapper;
243
+ break;
244
+ case 'edge':
245
+ this.specification.edge = mapper;
246
+ break;
247
+ case 'combo':
248
+ this.specification.combo = mapper;
249
+ break;
250
+ }
251
+ this.hooks.mapperchange.emit({
252
+ type,
253
+ mapper,
254
+ });
255
+ }
231
256
  /**
232
257
  * Get the copy of specs(configurations).
233
258
  * @returns graph specs
@@ -749,23 +774,26 @@ export default class Graph extends EventEmitter {
749
774
  // data controller and item controller subscribe additem in order
750
775
  const { graphCore } = this.dataController;
751
776
  const { specification } = this.themeController;
777
+ // debugger;
752
778
  graphCore.once('changed', (event) => {
753
779
  if (!event.changes.length)
754
780
  return;
755
781
  const changes = event.changes;
756
- this.hooks.itemchange.emit({
757
- type: itemType,
758
- changes: graphCore.reduceChanges(event.changes),
759
- graphCore,
760
- theme: specification,
761
- });
762
- this.emit('afteritemchange', {
782
+ const timingParameters = {
763
783
  type: itemType,
764
784
  action: 'add',
765
785
  models,
766
786
  apiName: 'addData',
767
787
  changes,
788
+ };
789
+ this.emit('beforeitemchange', timingParameters);
790
+ this.hooks.itemchange.emit({
791
+ type: itemType,
792
+ changes: graphCore.reduceChanges(event.changes),
793
+ graphCore,
794
+ theme: specification,
768
795
  });
796
+ this.emit('afteritemchange', timingParameters);
769
797
  });
770
798
  const modelArr = isArray(models) ? models : [models];
771
799
  const data = { nodes: [], edges: [], combos: [] };
@@ -794,19 +822,21 @@ export default class Graph extends EventEmitter {
794
822
  if (!event.changes.length)
795
823
  return;
796
824
  const changes = event.changes;
797
- this.hooks.itemchange.emit({
798
- type: itemType,
799
- changes: event.changes,
800
- graphCore,
801
- theme: specification,
802
- });
803
- this.emit('afteritemchange', {
825
+ const timingParameters = {
804
826
  type: itemType,
805
827
  action: 'remove',
806
828
  ids: idArr,
807
829
  apiName: 'removeData',
808
830
  changes,
831
+ };
832
+ this.emit('beforeitemchange', timingParameters);
833
+ this.hooks.itemchange.emit({
834
+ type: itemType,
835
+ changes: event.changes,
836
+ graphCore,
837
+ theme: specification,
809
838
  });
839
+ this.emit('afteritemchange', timingParameters);
810
840
  });
811
841
  this.hooks.datachange.emit({
812
842
  data,
@@ -874,19 +904,21 @@ export default class Graph extends EventEmitter {
874
904
  const { specification } = this.themeController;
875
905
  graphCore.once('changed', (event) => {
876
906
  const changes = this.extendChanges(clone(event.changes));
877
- this.hooks.itemchange.emit({
878
- type: itemType,
879
- changes: event.changes,
880
- graphCore,
881
- theme: specification,
882
- });
883
- this.emit('afteritemchange', {
907
+ const timingParameters = {
884
908
  type: itemType,
885
909
  action: 'update',
886
910
  models,
887
911
  apiName: 'updateData',
888
912
  changes,
913
+ };
914
+ this.emit('beforeitemchange', timingParameters);
915
+ this.hooks.itemchange.emit({
916
+ type: itemType,
917
+ changes: event.changes,
918
+ graphCore,
919
+ theme: specification,
889
920
  });
921
+ this.emit('afteritemchange', timingParameters);
890
922
  });
891
923
  this.hooks.datachange.emit({
892
924
  data,
@@ -933,6 +965,15 @@ export default class Graph extends EventEmitter {
933
965
  if (!event.changes.length)
934
966
  return;
935
967
  const changes = event.changes.filter((change) => !isEqual(change.newValue, change.oldValue));
968
+ const timingParameters = {
969
+ type,
970
+ action: 'updatePosition',
971
+ upsertAncestors,
972
+ models,
973
+ apiName: 'updatePosition',
974
+ changes,
975
+ };
976
+ this.emit('beforeitemchange', timingParameters);
936
977
  this.hooks.itemchange.emit({
937
978
  type,
938
979
  changes: event.changes,
@@ -943,14 +984,7 @@ export default class Graph extends EventEmitter {
943
984
  animate: !disableAnimate,
944
985
  callback,
945
986
  });
946
- this.emit('afteritemchange', {
947
- type,
948
- action: 'updatePosition',
949
- upsertAncestors,
950
- models,
951
- apiName: 'updatePosition',
952
- changes,
953
- });
987
+ this.emit('afteritemchange', timingParameters);
954
988
  });
955
989
  this.hooks.datachange.emit({
956
990
  data: {
@@ -1199,19 +1233,21 @@ export default class Graph extends EventEmitter {
1199
1233
  if (!event.changes.length)
1200
1234
  return;
1201
1235
  const changes = event.changes;
1202
- this.hooks.itemchange.emit({
1203
- type: 'combo',
1204
- changes: graphCore.reduceChanges(event.changes),
1205
- graphCore,
1206
- theme: specification,
1207
- });
1208
- this.emit('afteritemchange', {
1236
+ const timingParameters = {
1209
1237
  type: 'combo',
1210
1238
  action: 'add',
1211
1239
  models: [model],
1212
1240
  apiName: 'addCombo',
1213
1241
  changes,
1242
+ };
1243
+ this.emit('beforeitemchange', timingParameters);
1244
+ this.hooks.itemchange.emit({
1245
+ type: 'combo',
1246
+ changes: graphCore.reduceChanges(event.changes),
1247
+ graphCore,
1248
+ theme: specification,
1214
1249
  });
1250
+ this.emit('afteritemchange', timingParameters);
1215
1251
  });
1216
1252
  const data = {
1217
1253
  nodes: [],
@@ -1275,16 +1311,7 @@ export default class Graph extends EventEmitter {
1275
1311
  if (!event.changes.length)
1276
1312
  return;
1277
1313
  const changes = this.extendChanges(clone(event.changes));
1278
- this.hooks.itemchange.emit({
1279
- type: 'combo',
1280
- changes: event.changes,
1281
- graphCore,
1282
- theme: specification,
1283
- upsertAncestors,
1284
- action: 'updatePosition',
1285
- callback,
1286
- });
1287
- this.emit('afteritemchange', {
1314
+ const timingParameters = {
1288
1315
  type: 'combo',
1289
1316
  ids: idArr,
1290
1317
  dx,
@@ -1293,7 +1320,18 @@ export default class Graph extends EventEmitter {
1293
1320
  upsertAncestors,
1294
1321
  apiName: 'moveCombo',
1295
1322
  changes,
1323
+ };
1324
+ this.emit('beforeitemchange', timingParameters);
1325
+ this.hooks.itemchange.emit({
1326
+ type: 'combo',
1327
+ changes: event.changes,
1328
+ graphCore,
1329
+ theme: specification,
1330
+ upsertAncestors,
1331
+ action: 'updatePosition',
1332
+ callback,
1296
1333
  });
1334
+ this.emit('afteritemchange', timingParameters);
1297
1335
  });
1298
1336
  this.hooks.datachange.emit({
1299
1337
  data: {
@@ -1378,6 +1416,14 @@ export default class Graph extends EventEmitter {
1378
1416
  getMode() {
1379
1417
  return this.interactionController.getMode();
1380
1418
  }
1419
+ /**
1420
+ * Set the cursor. But the cursor in item's style has higher priority.
1421
+ * @param cursor
1422
+ */
1423
+ setCursor(cursor) {
1424
+ this.canvas.setCursor(cursor);
1425
+ this.transientCanvas.setCursor(cursor);
1426
+ }
1381
1427
  /**
1382
1428
  * Add behavior(s) to mode(s).
1383
1429
  * @param behaviors behavior names or configs
@@ -1548,6 +1594,274 @@ export default class Graph extends EventEmitter {
1548
1594
  });
1549
1595
  return this.itemController.getTransient(String(id));
1550
1596
  }
1597
+ // ===== download operations =====
1598
+ /**
1599
+ * Asynchronously generates a Data URL representation of the canvas content, including
1600
+ * background, main content, and transient canvas.
1601
+ * @param The type of the Data URL (e.g., 'image/png', 'image/jpeg').
1602
+ * @returns A Promise that resolves to the Data URL string.
1603
+ */
1604
+ toDataURL(type) {
1605
+ return __awaiter(this, void 0, void 0, function* () {
1606
+ const backgroundCanvas = this.backgroundCanvas;
1607
+ const canvas = this.canvas;
1608
+ const transientCanvas = this.transientCanvas;
1609
+ const rendererType = this.rendererType;
1610
+ const pixelRatio = typeof window !== 'undefined' ? window.devicePixelRatio : 1;
1611
+ const width = this.getSize()[0];
1612
+ const height = this.getSize()[1];
1613
+ const vContainerDOM = createDom('<div id="virtual-image"></div>');
1614
+ const vCanvas = createCanvas(rendererType, vContainerDOM, width, height, pixelRatio);
1615
+ const vCanvasContextService = vCanvas.getContextService();
1616
+ let bgRect;
1617
+ if (rendererType !== 'svg') {
1618
+ bgRect = new Rect({
1619
+ style: {
1620
+ x: 0,
1621
+ y: 0,
1622
+ z: -1,
1623
+ width: vCanvasContextService.getDomElement().width,
1624
+ height: vCanvasContextService.getDomElement().height,
1625
+ //@ts-ignore
1626
+ fill: backgroundCanvas.getContextService().getDomElement().style['background-color'],
1627
+ },
1628
+ });
1629
+ vCanvas.appendChild(bgRect);
1630
+ }
1631
+ const backgroundClonedGroup = backgroundCanvas.getRoot().cloneNode(true);
1632
+ const clonedGroup = canvas.getRoot().cloneNode(true);
1633
+ const transientClonedGroup = transientCanvas.getRoot().cloneNode(true);
1634
+ vCanvas.appendChild(backgroundClonedGroup);
1635
+ vCanvas.appendChild(clonedGroup);
1636
+ vCanvas.appendChild(transientClonedGroup);
1637
+ vCanvas.render();
1638
+ if (!type)
1639
+ type = 'image/png';
1640
+ let dataURL = '';
1641
+ yield vCanvasContextService.toDataURL({ type }).then((url) => {
1642
+ dataURL = url;
1643
+ });
1644
+ return dataURL;
1645
+ });
1646
+ }
1647
+ /**
1648
+ * Asynchronously generates a Data URL representation of the full canvas content,
1649
+ * including background, main content, and transient canvas, with optional padding.
1650
+ * @param type The type of the Data URL (e.g., 'image/png', 'image/jpeg').
1651
+ * @param imageConfig Configuration options for the image (optional).
1652
+ * @returns A Promise that resolves to the Data URL string.
1653
+ */
1654
+ toFullDataURL(type, imageConfig) {
1655
+ return __awaiter(this, void 0, void 0, function* () {
1656
+ const backgroundCanvas = this.backgroundCanvas;
1657
+ const canvas = this.canvas;
1658
+ const transientCanvas = this.transientCanvas;
1659
+ const backgroundRoot = canvas.getRoot();
1660
+ const root = canvas.getRoot();
1661
+ const transientRoot = transientCanvas.getRoot();
1662
+ const rendererType = this.rendererType;
1663
+ const backgroundBBox = backgroundRoot.getBBox();
1664
+ const BBox = root.getBBox();
1665
+ const transientBBox = transientRoot.getBBox();
1666
+ let padding = imageConfig ? imageConfig.padding : undefined;
1667
+ if (!padding) {
1668
+ padding = [0, 0, 0, 0];
1669
+ }
1670
+ else if (isNumber(padding)) {
1671
+ padding = [padding, padding, padding, padding];
1672
+ }
1673
+ const left = (transientBBox.left
1674
+ ? backgroundBBox.left
1675
+ ? Math.min(backgroundBBox.left, BBox.left, transientBBox.left)
1676
+ : Math.min(BBox.left, transientBBox.left)
1677
+ : BBox.left) - padding[3];
1678
+ const right = (transientBBox.right
1679
+ ? backgroundBBox.right
1680
+ ? Math.max(backgroundBBox.right, BBox.right, transientBBox.right)
1681
+ : Math.max(BBox.right, transientBBox.right)
1682
+ : BBox.right) + padding[1];
1683
+ const top = (transientBBox.top
1684
+ ? backgroundBBox.top
1685
+ ? Math.min(backgroundBBox.top, BBox.top, transientBBox.top)
1686
+ : Math.min(BBox.top, transientBBox.top)
1687
+ : BBox.top) - padding[0];
1688
+ const bottom = (transientBBox.bottom
1689
+ ? backgroundBBox.bottom
1690
+ ? Math.max(backgroundBBox.bottom, BBox.bottom, transientBBox.bottom)
1691
+ : Math.max(BBox.bottom, transientBBox.bottom)
1692
+ : BBox.bottom) + padding[2];
1693
+ const graphCenterX = (left + right) / 2;
1694
+ const graphCenterY = (top + bottom) / 2;
1695
+ const halfX = (right - left) / 2;
1696
+ const halfY = (bottom - top) / 2;
1697
+ const pixelRatio = typeof window !== 'undefined' ? window.devicePixelRatio : 1;
1698
+ const vWidth = halfX * 2;
1699
+ const vHeight = halfY * 2;
1700
+ const vContainerDOM = createDom('<div id="virtual-image"></div>');
1701
+ const vCanvas = createCanvas(rendererType, vContainerDOM, vWidth, vHeight, pixelRatio);
1702
+ const vCanvasContextService = vCanvas.getContextService();
1703
+ if (rendererType !== 'svg') {
1704
+ const bgRect = new Rect({
1705
+ style: {
1706
+ x: 0,
1707
+ y: 0,
1708
+ z: -1,
1709
+ width: vCanvasContextService.getDomElement().width,
1710
+ height: vCanvasContextService.getDomElement().height,
1711
+ //@ts-ignore
1712
+ fill: backgroundCanvas.getContextService().getDomElement().style['background-color'],
1713
+ },
1714
+ });
1715
+ vCanvas.appendChild(bgRect);
1716
+ }
1717
+ const backgroundClonedGroup = backgroundRoot.cloneNode(true);
1718
+ const clonedGroup = root.cloneNode(true);
1719
+ const transientClonedGroup = transientRoot.cloneNode(true);
1720
+ const transPosition = [
1721
+ -graphCenterX + halfX,
1722
+ -graphCenterY + halfY,
1723
+ ];
1724
+ backgroundClonedGroup.setPosition(transPosition);
1725
+ clonedGroup.setPosition(transPosition);
1726
+ transientClonedGroup.setPosition(transPosition);
1727
+ vCanvas.appendChild(backgroundClonedGroup);
1728
+ vCanvas.appendChild(clonedGroup);
1729
+ vCanvas.appendChild(transientClonedGroup);
1730
+ vCanvas.render();
1731
+ if (!type)
1732
+ type = 'image/png';
1733
+ let dataURL = '';
1734
+ yield vCanvasContextService.toDataURL({ type }).then((url) => {
1735
+ dataURL = url;
1736
+ });
1737
+ return dataURL;
1738
+ });
1739
+ }
1740
+ /**
1741
+ * Converts a Data URL to an image file and handles the download of the image.
1742
+ * @param dataURL The Data URL of the image to be converted and downloaded.
1743
+ * @param renderer The renderer type ('svg' or other) used for the canvas.
1744
+ * @param link The HTML link element used for image download.
1745
+ * @param fileName The desired name for the downloaded image file.
1746
+ */
1747
+ dataURLToImage(dataURL, renderer, link, fileName) {
1748
+ if (!dataURL || dataURL === 'data:') {
1749
+ console.error('Download image failed. The graph is too large or there is invalid attribute values in graph items');
1750
+ return;
1751
+ }
1752
+ if (typeof window !== 'undefined') {
1753
+ if (window.Blob && window.URL && renderer !== 'svg') {
1754
+ const arr = dataURL.split(',');
1755
+ let mime = '';
1756
+ if (arr && arr.length > 0) {
1757
+ const match = arr[0].match(/:(.*?);/);
1758
+ // eslint-disable-next-line prefer-destructuring
1759
+ if (match && match.length >= 2)
1760
+ mime = match[1];
1761
+ }
1762
+ const bstr = atob(arr[1]);
1763
+ let n = bstr.length;
1764
+ const u8arr = new Uint8Array(n);
1765
+ while (n--) {
1766
+ u8arr[n] = bstr.charCodeAt(n);
1767
+ }
1768
+ const blobObj = new Blob([u8arr], { type: mime });
1769
+ if (window.navigator.msSaveBlob) {
1770
+ window.navigator.msSaveBlob(blobObj, fileName);
1771
+ }
1772
+ else {
1773
+ link.addEventListener('click', () => {
1774
+ link.download = fileName;
1775
+ link.href = window.URL.createObjectURL(blobObj);
1776
+ });
1777
+ }
1778
+ }
1779
+ else {
1780
+ link.addEventListener('click', () => {
1781
+ link.download = fileName;
1782
+ link.href = dataURL;
1783
+ });
1784
+ }
1785
+ }
1786
+ }
1787
+ /**
1788
+ * Initiates the download of the graph as an image with an optional name and type.
1789
+ * @param name The desired name for the downloaded image file (optional).
1790
+ * @param type The type of the image to download (optional, defaults to 'image/png').
1791
+ */
1792
+ downloadImage(name, type) {
1793
+ const self = this;
1794
+ // self.stopAnimate();
1795
+ const rendererType = this.rendererType;
1796
+ if (!type)
1797
+ type = 'image/png';
1798
+ const fileName = (name || 'graph') +
1799
+ (rendererType === 'svg' ? '.svg' : type.split('/')[1]);
1800
+ const link = document.createElement('a');
1801
+ self.asyncToDataUrl(type, (dataURL) => {
1802
+ this.dataURLToImage(dataURL, rendererType, link, fileName);
1803
+ const e = document.createEvent('MouseEvents');
1804
+ e.initEvent('click', false, false);
1805
+ link.dispatchEvent(e);
1806
+ });
1807
+ }
1808
+ /**
1809
+ * Initiates the download of the entire graph as an image with optional name, type, and padding configuration.
1810
+ * @param name The desired name for the downloaded image file (optional).
1811
+ * @param type The type of the image to download (optional, defaults to 'image/png').
1812
+ * @param imageConfig Configuration options for the image (optional).
1813
+ */
1814
+ downloadFullImage(name, type, imageConfig) {
1815
+ const self = this;
1816
+ const rendererType = this.rendererType;
1817
+ if (!type)
1818
+ type = 'image/png';
1819
+ const fileName = (name || 'graph') +
1820
+ (rendererType === 'svg' ? '.svg' : type.split('/')[1]);
1821
+ const link = document.createElement('a');
1822
+ self.asyncToFullDataUrl(type, imageConfig, (dataURL) => {
1823
+ this.dataURLToImage(dataURL, rendererType, link, fileName);
1824
+ const e = document.createEvent('MouseEvents');
1825
+ e.initEvent('click', false, false);
1826
+ link.dispatchEvent(e);
1827
+ });
1828
+ }
1829
+ /**
1830
+ * Asynchronously converts the canvas content to a Data URL of the specified type and invokes the provided callback.
1831
+ * @param type The type of the Data URL (optional, defaults to 'image/png').
1832
+ * @param callback A callback function to handle the Data URL (optional).
1833
+ */
1834
+ asyncToDataUrl(type, callback) {
1835
+ let dataURL = '';
1836
+ if (!type)
1837
+ type = 'image/png';
1838
+ setTimeout(() => __awaiter(this, void 0, void 0, function* () {
1839
+ yield this.toDataURL(type).then((url) => {
1840
+ dataURL = url;
1841
+ });
1842
+ if (callback)
1843
+ callback(dataURL);
1844
+ }), 16);
1845
+ }
1846
+ /**
1847
+ * Asynchronously converts the entire canvas content to a Data URL of the specified type
1848
+ * with optional padding, and invokes the provided callback.
1849
+ * @param type The type of the Data URL (optional, defaults to 'image/png').
1850
+ * @param imageConfig Configuration options for the image (optional).
1851
+ * @param callback A callback function to handle the Data URL (optional).
1852
+ */
1853
+ asyncToFullDataUrl(type, imageConfig, callback) {
1854
+ let dataURL = '';
1855
+ if (!type)
1856
+ type = 'image/png';
1857
+ setTimeout(() => __awaiter(this, void 0, void 0, function* () {
1858
+ yield this.toFullDataURL(type, imageConfig).then((url) => {
1859
+ dataURL = url;
1860
+ });
1861
+ if (callback)
1862
+ callback(dataURL);
1863
+ }), 16);
1864
+ }
1551
1865
  // ===== history operations =====
1552
1866
  /**
1553
1867
  * Determine if history (redo/undo) is enabled.