@antv/g6 5.0.27 → 5.0.28

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 (103) hide show
  1. package/dist/g6.min.js +29 -29
  2. package/dist/lib/behaviors/auto-adapt-label.d.ts +6 -1
  3. package/dist/lib/exports.d.ts +4 -3
  4. package/dist/lib/runtime/canvas.d.ts +16 -0
  5. package/dist/lib/runtime/graph.d.ts +8 -2
  6. package/dist/lib/spec/canvas.d.ts +11 -1
  7. package/dist/lib/types/edge.d.ts +6 -2
  8. package/dist/lib/types/node.d.ts +2 -2
  9. package/dist/lib/utils/dom.d.ts +1 -1
  10. package/dist/lib/utils/transform.d.ts +1 -1
  11. package/dist/lib/version.d.ts +1 -1
  12. package/esm/behaviors/auto-adapt-label.d.ts +6 -1
  13. package/esm/behaviors/auto-adapt-label.js +38 -10
  14. package/esm/behaviors/auto-adapt-label.js.map +1 -1
  15. package/esm/behaviors/fix-element-size.js +5 -1
  16. package/esm/behaviors/fix-element-size.js.map +1 -1
  17. package/esm/elements/nodes/base-node.js +2 -2
  18. package/esm/elements/nodes/base-node.js.map +1 -1
  19. package/esm/elements/shapes/base-shape.js +3 -1
  20. package/esm/elements/shapes/base-shape.js.map +1 -1
  21. package/esm/exports.d.ts +4 -3
  22. package/esm/exports.js +2 -1
  23. package/esm/exports.js.map +1 -1
  24. package/esm/plugins/fullscreen/index.js +3 -2
  25. package/esm/plugins/fullscreen/index.js.map +1 -1
  26. package/esm/runtime/canvas.d.ts +16 -0
  27. package/esm/runtime/canvas.js +35 -15
  28. package/esm/runtime/canvas.js.map +1 -1
  29. package/esm/runtime/element.js +15 -5
  30. package/esm/runtime/element.js.map +1 -1
  31. package/esm/runtime/graph.d.ts +8 -2
  32. package/esm/runtime/graph.js +34 -22
  33. package/esm/runtime/graph.js.map +1 -1
  34. package/esm/runtime/layout.js +1 -2
  35. package/esm/runtime/layout.js.map +1 -1
  36. package/esm/spec/canvas.d.ts +11 -1
  37. package/esm/types/edge.d.ts +6 -2
  38. package/esm/types/node.d.ts +2 -2
  39. package/esm/utils/dom.d.ts +1 -1
  40. package/esm/utils/dom.js +2 -0
  41. package/esm/utils/dom.js.map +1 -1
  42. package/esm/utils/shortcut.js +4 -2
  43. package/esm/utils/shortcut.js.map +1 -1
  44. package/esm/utils/transform.d.ts +1 -1
  45. package/esm/utils/transform.js +13 -5
  46. package/esm/utils/transform.js.map +1 -1
  47. package/esm/version.d.ts +1 -1
  48. package/esm/version.js +1 -1
  49. package/lib/behaviors/auto-adapt-label.d.ts +6 -1
  50. package/lib/behaviors/auto-adapt-label.js +38 -10
  51. package/lib/behaviors/auto-adapt-label.js.map +1 -1
  52. package/lib/behaviors/fix-element-size.js +5 -1
  53. package/lib/behaviors/fix-element-size.js.map +1 -1
  54. package/lib/elements/nodes/base-node.js +2 -2
  55. package/lib/elements/nodes/base-node.js.map +1 -1
  56. package/lib/elements/shapes/base-shape.js +3 -1
  57. package/lib/elements/shapes/base-shape.js.map +1 -1
  58. package/lib/exports.d.ts +4 -3
  59. package/lib/exports.js +6 -3
  60. package/lib/exports.js.map +1 -1
  61. package/lib/plugins/fullscreen/index.js +3 -2
  62. package/lib/plugins/fullscreen/index.js.map +1 -1
  63. package/lib/runtime/canvas.d.ts +16 -0
  64. package/lib/runtime/canvas.js +35 -15
  65. package/lib/runtime/canvas.js.map +1 -1
  66. package/lib/runtime/element.js +15 -5
  67. package/lib/runtime/element.js.map +1 -1
  68. package/lib/runtime/graph.d.ts +8 -2
  69. package/lib/runtime/graph.js +34 -22
  70. package/lib/runtime/graph.js.map +1 -1
  71. package/lib/runtime/layout.js +1 -2
  72. package/lib/runtime/layout.js.map +1 -1
  73. package/lib/spec/canvas.d.ts +11 -1
  74. package/lib/types/edge.d.ts +6 -2
  75. package/lib/types/node.d.ts +2 -2
  76. package/lib/utils/dom.d.ts +1 -1
  77. package/lib/utils/dom.js +2 -0
  78. package/lib/utils/dom.js.map +1 -1
  79. package/lib/utils/shortcut.js +4 -2
  80. package/lib/utils/shortcut.js.map +1 -1
  81. package/lib/utils/transform.d.ts +1 -1
  82. package/lib/utils/transform.js +13 -5
  83. package/lib/utils/transform.js.map +1 -1
  84. package/lib/version.d.ts +1 -1
  85. package/lib/version.js +1 -1
  86. package/package.json +11 -11
  87. package/src/behaviors/auto-adapt-label.ts +43 -12
  88. package/src/behaviors/fix-element-size.ts +15 -2
  89. package/src/elements/nodes/base-node.ts +2 -2
  90. package/src/elements/shapes/base-shape.ts +2 -1
  91. package/src/exports.ts +4 -1
  92. package/src/plugins/fullscreen/index.ts +2 -2
  93. package/src/runtime/canvas.ts +52 -17
  94. package/src/runtime/element.ts +15 -7
  95. package/src/runtime/graph.ts +36 -17
  96. package/src/runtime/layout.ts +1 -2
  97. package/src/spec/canvas.ts +11 -1
  98. package/src/types/edge.ts +6 -2
  99. package/src/types/node.ts +2 -2
  100. package/src/utils/dom.ts +3 -1
  101. package/src/utils/shortcut.ts +2 -2
  102. package/src/utils/transform.ts +13 -6
  103. package/src/version.ts +1 -1
@@ -73,6 +73,13 @@ export class Graph extends EventEmitter {
73
73
  zoomRange: [0.01, 10],
74
74
  };
75
75
 
76
+ /**
77
+ * <zh/> 当前图实例是否已经渲染
78
+ *
79
+ * <en/> Whether the current graph instance has been rendered
80
+ */
81
+ public rendered = false;
82
+
76
83
  /**
77
84
  * <zh/> 当前图实例是否已经被销毁
78
85
  *
@@ -91,8 +98,8 @@ export class Graph extends EventEmitter {
91
98
  this._setOptions(this.options, true);
92
99
  this.context.graph = this;
93
100
 
94
- // Listening window.resize to autoResize.
95
- this.options.autoResize && window.addEventListener('resize', this.onResize);
101
+ // Listening resize to autoResize.
102
+ this.options.autoResize && globalThis.addEventListener?.('resize', this.onResize);
96
103
  }
97
104
 
98
105
  /**
@@ -155,16 +162,18 @@ export class Graph extends EventEmitter {
155
162
  }
156
163
 
157
164
  /**
158
- * <zh/> 获取当前画布容器的尺寸
165
+ * <zh/> 设置当前画布容器的尺寸
159
166
  *
160
- * <en/> Get the size of the current canvas container
167
+ * <en/> Set the size of the current canvas container
161
168
  * @param width - <zh/> 画布宽度 | <en/> canvas width
162
169
  * @param height - <zh/> 画布高度 | <en/> canvas height
163
170
  * @apiCategory canvas
164
171
  */
165
172
  public setSize(width: number, height: number): void {
166
- Object.assign(this.options, { width, height });
167
- this.context.canvas?.resize(width, height);
173
+ if (width) this.options.width = width;
174
+ if (height) this.options.height = height;
175
+
176
+ this.resize(width, height);
168
177
  }
169
178
 
170
179
  /**
@@ -1031,7 +1040,8 @@ export class Graph extends EventEmitter {
1031
1040
  renderer,
1032
1041
  cursor,
1033
1042
  background,
1034
- devicePixelRatio = window.devicePixelRatio ?? 1,
1043
+ canvas: canvasOptions,
1044
+ devicePixelRatio = globalThis.devicePixelRatio ?? 1,
1035
1045
  } = this.options;
1036
1046
  if (container instanceof Canvas) {
1037
1047
  this.context.canvas = container;
@@ -1043,8 +1053,8 @@ export class Graph extends EventEmitter {
1043
1053
  const containerSize = sizeOf($container!);
1044
1054
 
1045
1055
  this.emit(GraphEvent.BEFORE_CANVAS_INIT, { container: $container, width, height });
1046
-
1047
- const canvas = new Canvas({
1056
+ const options = {
1057
+ ...canvasOptions,
1048
1058
  container: $container!,
1049
1059
  width: width || containerSize[0],
1050
1060
  height: height || containerSize[1],
@@ -1052,7 +1062,9 @@ export class Graph extends EventEmitter {
1052
1062
  renderer,
1053
1063
  cursor,
1054
1064
  devicePixelRatio,
1055
- });
1065
+ };
1066
+
1067
+ const canvas = new Canvas(options);
1056
1068
 
1057
1069
  this.context.canvas = canvas;
1058
1070
  await canvas.ready;
@@ -1120,6 +1132,7 @@ export class Graph extends EventEmitter {
1120
1132
  const animation = this.context.element!.draw({ type: 'render' });
1121
1133
  await Promise.all([animation?.finished, this.context.layout!.layout()]);
1122
1134
  await this.autoFit();
1135
+ this.rendered = true;
1123
1136
  emit(this, new GraphLifeCycleEvent(GraphEvent.AFTER_RENDER));
1124
1137
  }
1125
1138
 
@@ -1204,7 +1217,7 @@ export class Graph extends EventEmitter {
1204
1217
  this.context = {};
1205
1218
 
1206
1219
  this.off();
1207
- window.removeEventListener('resize', this.onResize);
1220
+ globalThis.removeEventListener?.('resize', this.onResize);
1208
1221
 
1209
1222
  this.destroyed = true;
1210
1223
 
@@ -1239,11 +1252,17 @@ export class Graph extends EventEmitter {
1239
1252
  */
1240
1253
  public resize(width: number, height: number): void;
1241
1254
  public resize(width?: number, height?: number): void {
1242
- const size: Vector2 = !width || !height ? sizeOf(this.context.canvas!.getContainer()!) : [width, height];
1243
- if (isEqual(size, this.getSize())) return;
1244
- emit(this, new GraphLifeCycleEvent(GraphEvent.BEFORE_SIZE_CHANGE, { size }));
1245
- this.context.canvas.resize(...size);
1246
- emit(this, new GraphLifeCycleEvent(GraphEvent.AFTER_SIZE_CHANGE, { size }));
1255
+ const containerSize = sizeOf(this.context.canvas?.getContainer());
1256
+ const specificSize: Vector2 = [width || containerSize[0], height || containerSize[1]];
1257
+
1258
+ if (!this.context.canvas) return;
1259
+
1260
+ const canvasSize = this.context.canvas!.getSize();
1261
+ if (isEqual(specificSize, canvasSize)) return;
1262
+
1263
+ emit(this, new GraphLifeCycleEvent(GraphEvent.BEFORE_SIZE_CHANGE, { size: specificSize }));
1264
+ this.context.canvas.resize(...specificSize);
1265
+ emit(this, new GraphLifeCycleEvent(GraphEvent.AFTER_SIZE_CHANGE, { size: specificSize }));
1247
1266
  }
1248
1267
 
1249
1268
  /**
@@ -1733,7 +1752,7 @@ export class Graph extends EventEmitter {
1733
1752
  });
1734
1753
  this.updateData(dataToUpdate);
1735
1754
 
1736
- await this.context.element!.draw({ animation })?.finished;
1755
+ await this.context.element!.draw({ animation, stage: 'state' })?.finished;
1737
1756
  }
1738
1757
 
1739
1758
  /**
@@ -16,7 +16,6 @@ import { createTreeStructure } from '../utils/graphlib';
16
16
  import { idOf } from '../utils/id';
17
17
  import { isTreeLayout, layoutAdapter, layoutMapping2GraphData } from '../utils/layout';
18
18
  import { print } from '../utils/print';
19
- import { parseSize } from '../utils/size';
20
19
  import { dfs } from '../utils/traverse';
21
20
  import type { RuntimeContext } from './types';
22
21
 
@@ -299,7 +298,7 @@ export class LayoutController {
299
298
  const nodeElement = element?.getElement<Node | Combo>(node.id as string);
300
299
  const { size } = nodeElement?.attributes || {};
301
300
 
302
- return Math.max(...parseSize(size));
301
+ return size;
303
302
  });
304
303
 
305
304
  const Ctor = getExtension('layout', type);
@@ -1,5 +1,5 @@
1
1
  import type { Cursor, IRenderer } from '@antv/g';
2
- import type { Canvas } from '../runtime/canvas';
2
+ import type { Canvas, CanvasConfig } from '../runtime/canvas';
3
3
 
4
4
  /**
5
5
  * <zh/> 画布配置项
@@ -57,4 +57,14 @@ export interface CanvasOptions {
57
57
  * <en/> cursor style
58
58
  */
59
59
  cursor?: Cursor;
60
+ /**
61
+ * <zh/> 画布配置
62
+ *
63
+ * <en/> canvas config
64
+ * @remarks
65
+ * <zh/> GraphOptions 下相关配置项为快捷配置项,会被转换为 canvas 配置项
66
+ *
67
+ * <en/> The related configuration items under GraphOptions are shortcut configuration items, which will be converted to canvas configuration items
68
+ */
69
+ canvas?: CanvasConfig;
60
70
  }
package/src/types/edge.ts CHANGED
@@ -54,9 +54,13 @@ export interface EdgeLabelStyleProps extends LabelStyleProps {
54
54
  */
55
55
  autoRotate?: boolean;
56
56
  /**
57
- * <zh/> 文本的最大宽度,超出部分会用省略号代替
57
+ * <zh/> 标签最大宽度(需要 [prefix]WordWrap 为 true)
58
+ * - string: 表示以相对于边长度的百分比形式定义最大宽度。例如 `"50%"` 表示标签宽度不超过边长度的一半
59
+ * - number: 表示以像素值为单位定义最大宽度。例如 `100` 表示标签的最大宽度为 100 像素
58
60
  *
59
- * <en/> maxWidth of label text, the overflow part will be replaced by an ellipsis
61
+ * <en/> The maximum width of the label(need [prefix]WordWrap to be true)
62
+ * - string: When set to a string, it defines the maximum width as a percentage of the edge length. For example, `"50%"` means the label width does not exceed half of the edge length
63
+ * - number: When set to a number, it defines the maximum width in pixels. For example, `100` means the maximum width of the label is 100 pixels
60
64
  * @defaultValue '80%'
61
65
  */
62
66
  maxWidth?: string | number;
package/src/types/node.ts CHANGED
@@ -34,11 +34,11 @@ export interface NodeLabelStyleProps extends LabelStyleProps {
34
34
  */
35
35
  placement?: DirectionalPlacement;
36
36
  /**
37
- * <zh/> 标签最大宽度
37
+ * <zh/> 标签最大宽度(需要 [prefix]WordWrap 为 true)
38
38
  * - string: 表示以相对于节点宽度的百分比形式定义最大宽度。例如 `"50%"` 表示标签宽度不超过节点宽度的一半
39
39
  * - number: 表示以像素值为单位定义最大宽度。例如 `100` 表示标签的最大宽度为 100 像素
40
40
  *
41
- * <en/> The maximum width of the label
41
+ * <en/> The maximum width of the label(need [prefix]WordWrap to be true)
42
42
  * - string: When set to a string, it defines the maximum width as a percentage of the node width. For example, `"50%"` means the label width does not exceed half of the node width
43
43
  * - number: When set to a number, it defines the maximum width in pixels. For example, `100` means the maximum width of the label is 100 pixels
44
44
  * @defaultValue '200%'
package/src/utils/dom.ts CHANGED
@@ -24,7 +24,9 @@ function getContainerSize(container: HTMLElement): [number, number] {
24
24
  * @param container container of Graph
25
25
  * @returns Size of Graph
26
26
  */
27
- export function sizeOf(container: HTMLElement): [number, number] {
27
+ export function sizeOf(container: HTMLElement | null): [number, number] {
28
+ if (!container) return [0, 0];
29
+
28
30
  let effectiveWidth = 640;
29
31
  let effectiveHeight = 480;
30
32
 
@@ -57,7 +57,7 @@ export class Shortcut {
57
57
 
58
58
  // 窗口重新获得焦点后清空按键,避免按键状态异常
59
59
  // Clear the keys when the window regains focus to avoid abnormal key states
60
- window.addEventListener('focus', this.onFocus);
60
+ globalThis.addEventListener?.('focus', this.onFocus);
61
61
  }
62
62
 
63
63
  private onKeyDown = (event: KeyboardEvent) => {
@@ -117,6 +117,6 @@ export class Shortcut {
117
117
  this.emitter.off(CommonEvent.KEY_UP, this.onKeyUp);
118
118
  this.emitter.off(CommonEvent.WHEEL, this.onWheel);
119
119
  this.emitter.off(CommonEvent.DRAG, this.onDrag);
120
- window.removeEventListener('blur', this.onFocus);
120
+ globalThis.removeEventListener?.('blur', this.onFocus);
121
121
  }
122
122
  }
@@ -15,19 +15,26 @@ export function replaceTranslateInTransform(
15
15
  y: number,
16
16
  z: number,
17
17
  transform: string | TransformArray,
18
- ): string | TransformArray {
18
+ ): string | TransformArray | null {
19
19
  if (Array.isArray(transform)) {
20
20
  let hasTranslate = false;
21
- const newTransform: TransformArray = transform.map((t) => {
21
+ const newTransform: TransformArray = [];
22
+
23
+ for (let i = 0; i < transform.length; i++) {
24
+ const t = transform[i];
22
25
  if (t[0] === 'translate') {
26
+ if (t[1] === x && t[2] === y) return null;
23
27
  hasTranslate = true;
24
- return ['translate', x, y];
28
+ newTransform.push(['translate', x, y]);
25
29
  } else if (t[0] === 'translate3d') {
30
+ if (t[1] === x && t[2] === y && t[3] === z) return null;
26
31
  hasTranslate = true;
27
- return ['translate3d', x, y, z];
32
+ newTransform.push(['translate3d', x, y, z]);
33
+ } else {
34
+ newTransform.push(t);
28
35
  }
29
- return t;
30
- });
36
+ }
37
+
31
38
  if (!hasTranslate) {
32
39
  newTransform.splice(0, 0, z === 0 ? ['translate', x, y] : ['translate3d', x, y, z]);
33
40
  }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '5.0.27';
1
+ export const version = '5.0.28';