@antv/g6 5.0.7 → 5.0.9

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 (177) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/dist/lib/behaviors/click-select.d.ts +8 -16
  3. package/dist/lib/elements/base-element.d.ts +5 -4
  4. package/dist/lib/plugins/fullscreen/index.d.ts +2 -0
  5. package/dist/lib/registry/index.d.ts +5 -1
  6. package/dist/lib/runtime/animation.d.ts +7 -0
  7. package/dist/lib/runtime/canvas.d.ts +27 -39
  8. package/dist/lib/runtime/element.d.ts +1 -1
  9. package/dist/lib/runtime/graph.d.ts +1 -17
  10. package/dist/lib/utils/print.d.ts +15 -0
  11. package/dist/lib/utils/shortcut.d.ts +1 -0
  12. package/dist/lib/utils/state.d.ts +9 -0
  13. package/dist/lib/version.d.ts +1 -1
  14. package/esm/behaviors/brush-select.js +1 -2
  15. package/esm/behaviors/brush-select.js.map +1 -1
  16. package/esm/behaviors/click-select.d.ts +8 -16
  17. package/esm/behaviors/click-select.js +108 -90
  18. package/esm/behaviors/click-select.js.map +1 -1
  19. package/esm/behaviors/drag-element-force.js +2 -1
  20. package/esm/behaviors/drag-element-force.js.map +1 -1
  21. package/esm/behaviors/drag-element.js +1 -2
  22. package/esm/behaviors/drag-element.js.map +1 -1
  23. package/esm/elements/base-element.d.ts +5 -4
  24. package/esm/elements/base-element.js +3 -0
  25. package/esm/elements/base-element.js.map +1 -1
  26. package/esm/elements/combos/base-combo.js +2 -2
  27. package/esm/elements/combos/base-combo.js.map +1 -1
  28. package/esm/elements/nodes/html.js +3 -5
  29. package/esm/elements/nodes/html.js.map +1 -1
  30. package/esm/elements/shapes/base-shape.js +27 -3
  31. package/esm/elements/shapes/base-shape.js.map +1 -1
  32. package/esm/plugins/contextmenu/index.js +1 -0
  33. package/esm/plugins/contextmenu/index.js.map +1 -1
  34. package/esm/plugins/fullscreen/index.d.ts +2 -0
  35. package/esm/plugins/fullscreen/index.js +15 -2
  36. package/esm/plugins/fullscreen/index.js.map +1 -1
  37. package/esm/registry/extension/index.js +2 -1
  38. package/esm/registry/extension/index.js.map +1 -1
  39. package/esm/registry/index.d.ts +5 -1
  40. package/esm/registry/index.js +8 -3
  41. package/esm/registry/index.js.map +1 -1
  42. package/esm/runtime/animation.d.ts +7 -0
  43. package/esm/runtime/animation.js +17 -0
  44. package/esm/runtime/animation.js.map +1 -1
  45. package/esm/runtime/canvas.d.ts +27 -39
  46. package/esm/runtime/canvas.js +128 -175
  47. package/esm/runtime/canvas.js.map +1 -1
  48. package/esm/runtime/data.js +4 -3
  49. package/esm/runtime/data.js.map +1 -1
  50. package/esm/runtime/element.d.ts +1 -1
  51. package/esm/runtime/element.js +2 -1
  52. package/esm/runtime/element.js.map +1 -1
  53. package/esm/runtime/graph.d.ts +1 -17
  54. package/esm/runtime/graph.js +8 -34
  55. package/esm/runtime/graph.js.map +1 -1
  56. package/esm/runtime/layout.js +5 -2
  57. package/esm/runtime/layout.js.map +1 -1
  58. package/esm/runtime/viewport.js +1 -1
  59. package/esm/runtime/viewport.js.map +1 -1
  60. package/esm/utils/animation.js +9 -3
  61. package/esm/utils/animation.js.map +1 -1
  62. package/esm/utils/id.js +2 -1
  63. package/esm/utils/id.js.map +1 -1
  64. package/esm/utils/layout.js +9 -1
  65. package/esm/utils/layout.js.map +1 -1
  66. package/esm/utils/palette.js +2 -1
  67. package/esm/utils/palette.js.map +1 -1
  68. package/esm/utils/print.d.ts +15 -0
  69. package/esm/utils/print.js +28 -0
  70. package/esm/utils/print.js.map +1 -0
  71. package/esm/utils/shortcut.d.ts +1 -0
  72. package/esm/utils/shortcut.js +7 -0
  73. package/esm/utils/shortcut.js.map +1 -1
  74. package/esm/utils/state.d.ts +9 -0
  75. package/esm/utils/state.js +11 -0
  76. package/esm/utils/state.js.map +1 -0
  77. package/esm/utils/theme.js +5 -1
  78. package/esm/utils/theme.js.map +1 -1
  79. package/esm/version.d.ts +1 -1
  80. package/esm/version.js +1 -1
  81. package/lib/behaviors/brush-select.js +1 -2
  82. package/lib/behaviors/brush-select.js.map +1 -1
  83. package/lib/behaviors/click-select.d.ts +8 -16
  84. package/lib/behaviors/click-select.js +106 -88
  85. package/lib/behaviors/click-select.js.map +1 -1
  86. package/lib/behaviors/drag-element-force.js +2 -1
  87. package/lib/behaviors/drag-element-force.js.map +1 -1
  88. package/lib/behaviors/drag-element.js +1 -2
  89. package/lib/behaviors/drag-element.js.map +1 -1
  90. package/lib/elements/base-element.d.ts +5 -4
  91. package/lib/elements/base-element.js +3 -0
  92. package/lib/elements/base-element.js.map +1 -1
  93. package/lib/elements/combos/base-combo.js +2 -2
  94. package/lib/elements/combos/base-combo.js.map +1 -1
  95. package/lib/elements/nodes/html.js +3 -5
  96. package/lib/elements/nodes/html.js.map +1 -1
  97. package/lib/elements/shapes/base-shape.js +27 -3
  98. package/lib/elements/shapes/base-shape.js.map +1 -1
  99. package/lib/plugins/contextmenu/index.js +1 -0
  100. package/lib/plugins/contextmenu/index.js.map +1 -1
  101. package/lib/plugins/fullscreen/index.d.ts +2 -0
  102. package/lib/plugins/fullscreen/index.js +15 -2
  103. package/lib/plugins/fullscreen/index.js.map +1 -1
  104. package/lib/registry/extension/index.js +2 -1
  105. package/lib/registry/extension/index.js.map +1 -1
  106. package/lib/registry/index.d.ts +5 -1
  107. package/lib/registry/index.js +8 -3
  108. package/lib/registry/index.js.map +1 -1
  109. package/lib/runtime/animation.d.ts +7 -0
  110. package/lib/runtime/animation.js +17 -0
  111. package/lib/runtime/animation.js.map +1 -1
  112. package/lib/runtime/canvas.d.ts +27 -39
  113. package/lib/runtime/canvas.js +127 -174
  114. package/lib/runtime/canvas.js.map +1 -1
  115. package/lib/runtime/data.js +4 -3
  116. package/lib/runtime/data.js.map +1 -1
  117. package/lib/runtime/element.d.ts +1 -1
  118. package/lib/runtime/element.js +2 -1
  119. package/lib/runtime/element.js.map +1 -1
  120. package/lib/runtime/graph.d.ts +1 -17
  121. package/lib/runtime/graph.js +8 -34
  122. package/lib/runtime/graph.js.map +1 -1
  123. package/lib/runtime/layout.js +5 -2
  124. package/lib/runtime/layout.js.map +1 -1
  125. package/lib/runtime/viewport.js +1 -1
  126. package/lib/runtime/viewport.js.map +1 -1
  127. package/lib/utils/animation.js +8 -2
  128. package/lib/utils/animation.js.map +1 -1
  129. package/lib/utils/id.js +2 -1
  130. package/lib/utils/id.js.map +1 -1
  131. package/lib/utils/layout.js +9 -1
  132. package/lib/utils/layout.js.map +1 -1
  133. package/lib/utils/palette.js +2 -1
  134. package/lib/utils/palette.js.map +1 -1
  135. package/lib/utils/print.d.ts +15 -0
  136. package/lib/utils/print.js +32 -0
  137. package/lib/utils/print.js.map +1 -0
  138. package/lib/utils/shortcut.d.ts +1 -0
  139. package/lib/utils/shortcut.js +7 -0
  140. package/lib/utils/shortcut.js.map +1 -1
  141. package/lib/utils/state.d.ts +9 -0
  142. package/lib/utils/state.js +14 -0
  143. package/lib/utils/state.js.map +1 -0
  144. package/lib/utils/theme.js +5 -1
  145. package/lib/utils/theme.js.map +1 -1
  146. package/lib/version.d.ts +1 -1
  147. package/lib/version.js +1 -1
  148. package/package.json +8 -8
  149. package/src/behaviors/brush-select.ts +1 -2
  150. package/src/behaviors/click-select.ts +117 -88
  151. package/src/behaviors/drag-element-force.ts +2 -1
  152. package/src/behaviors/drag-element.ts +1 -2
  153. package/src/elements/base-element.ts +8 -4
  154. package/src/elements/combos/base-combo.ts +2 -2
  155. package/src/elements/nodes/html.ts +3 -7
  156. package/src/elements/shapes/base-shape.ts +30 -3
  157. package/src/global.d.ts +12 -0
  158. package/src/plugins/contextmenu/index.ts +1 -0
  159. package/src/plugins/fullscreen/index.ts +18 -2
  160. package/src/registry/extension/index.ts +2 -1
  161. package/src/registry/index.ts +8 -2
  162. package/src/runtime/animation.ts +21 -0
  163. package/src/runtime/canvas.ts +140 -209
  164. package/src/runtime/data.ts +4 -3
  165. package/src/runtime/element.ts +3 -2
  166. package/src/runtime/graph.ts +8 -38
  167. package/src/runtime/layout.ts +5 -2
  168. package/src/runtime/viewport.ts +2 -2
  169. package/src/utils/animation.ts +9 -2
  170. package/src/utils/id.ts +2 -1
  171. package/src/utils/layout.ts +7 -1
  172. package/src/utils/palette.ts +2 -1
  173. package/src/utils/print.ts +30 -0
  174. package/src/utils/shortcut.ts +9 -0
  175. package/src/utils/state.ts +12 -0
  176. package/src/utils/theme.ts +6 -1
  177. package/src/version.ts +1 -1
@@ -1,17 +1,15 @@
1
- import type { Cursor, DisplayObject, CanvasConfig as GCanvasConfig, IAnimation, IRenderer } from '@antv/g';
1
+ import type { DisplayObject, CanvasConfig as GCanvasConfig, IChildNode } from '@antv/g';
2
2
  import { CanvasEvent, Canvas as GCanvas } from '@antv/g';
3
3
  import { Renderer as CanvasRenderer } from '@antv/g-canvas';
4
4
  import { Plugin as DragNDropPlugin } from '@antv/g-plugin-dragndrop';
5
- import { createDOM, isFunction, isString } from '@antv/util';
5
+ import { createDOM } from '@antv/util';
6
6
  import type { CanvasOptions } from '../spec/canvas';
7
- import type { PointObject } from '../types';
8
- import type { CanvasLayer } from '../types/canvas';
7
+ import type { CanvasLayer } from '../types';
9
8
  import { getBBoxSize, getCombinedBBox } from '../utils/bbox';
10
9
 
11
10
  export interface CanvasConfig
12
- extends Pick<GCanvasConfig, 'container' | 'devicePixelRatio' | 'width' | 'height' | 'cursor'> {
11
+ extends Pick<GCanvasConfig, 'container' | 'devicePixelRatio' | 'width' | 'height' | 'cursor' | 'background'> {
13
12
  renderer?: CanvasOptions['renderer'];
14
- background?: string;
15
13
  }
16
14
 
17
15
  export interface DataURLOptions {
@@ -40,153 +38,93 @@ export interface DataURLOptions {
40
38
  encoderOptions: number;
41
39
  }
42
40
 
43
- /**
44
- * @deprecated this canvas will be replace by layered canvas
45
- */
46
- export class Canvas {
47
- protected config: CanvasConfig;
48
-
49
- public background!: GCanvas;
50
- public main!: GCanvas;
51
- public label!: GCanvas;
52
- public transient!: GCanvas;
53
-
54
- public get canvas() {
55
- return {
56
- main: this.main,
57
- label: this.label,
58
- transient: this.transient,
59
- background: this.background,
60
- };
61
- }
41
+ const layersName: CanvasLayer[] = ['background', 'main', 'label', 'transient'];
42
+
43
+ export class Canvas extends GCanvas {
44
+ private extends: {
45
+ config: CanvasConfig;
46
+ renderer: CanvasOptions['renderer'];
47
+ renderers: Record<CanvasLayer, CanvasRenderer>;
48
+ layers: Record<CanvasLayer, GCanvas>;
49
+ };
62
50
 
63
- public get document() {
64
- return this.main.document;
51
+ public getLayer(layer: CanvasLayer) {
52
+ return this.extends.layers[layer];
65
53
  }
66
54
 
67
- public renderers!: Record<CanvasLayer, IRenderer>;
55
+ /**
56
+ * <zh/> 获取所有图层
57
+ *
58
+ * <en/> Get all layers
59
+ * @returns <zh/> 图层 <en/> Layer
60
+ */
61
+ public getLayers() {
62
+ return this.extends.layers;
63
+ }
68
64
 
69
- private initialized = false;
65
+ /**
66
+ * <zh/> 获取渲染器
67
+ *
68
+ * <en/> Get renderer
69
+ * @param layer - <zh/> 图层 <en/> Layer
70
+ * @returns <zh/> 渲染器 <en/> Renderer
71
+ */
72
+ public getRenderer(layer: CanvasLayer) {
73
+ return this.extends.renderers[layer];
74
+ }
70
75
 
71
76
  constructor(config: CanvasConfig) {
72
- this.config = config;
73
- }
77
+ const { renderer, background, cursor, ...restConfig } = config;
78
+ const renderers = createRenderers(renderer);
79
+
80
+ // init main canvas
81
+ super({ ...restConfig, supportsMutipleCanvasesInOneContainer: true, cursor, renderer: renderers.main });
74
82
 
75
- public async init() {
76
- if (this.initialized) return;
77
-
78
- const { renderer: getRenderer, background, ...restConfig } = this.config;
79
- const names: CanvasLayer[] = ['main', 'label', 'transient', 'background'];
80
-
81
- const { renderers, canvas } = names.reduce(
82
- (acc, name) => {
83
- const renderer = isFunction(getRenderer) ? getRenderer?.(name) : new CanvasRenderer();
84
-
85
- if (name === 'main') {
86
- renderer.registerPlugin(
87
- new DragNDropPlugin({
88
- isDocumentDraggable: true,
89
- isDocumentDroppable: true,
90
- dragstartDistanceThreshold: 10,
91
- dragstartTimeThreshold: 100,
92
- }),
93
- );
94
- } else {
95
- renderer.unregisterPlugin(renderer.getPlugin('dom-interaction'));
96
- }
83
+ const layers = Object.fromEntries(
84
+ layersName.map((layer) => {
85
+ if (layer === 'main') return [layer, this];
97
86
 
98
87
  const canvas = new GCanvas({
99
- renderer,
100
- supportsMutipleCanvasesInOneContainer: true,
101
88
  ...restConfig,
89
+ supportsMutipleCanvasesInOneContainer: true,
90
+ renderer: renderers[layer],
91
+ background: layer === 'background' ? background : undefined,
102
92
  });
103
93
 
104
- acc.renderers[name] = renderer;
105
- acc.canvas[name] = canvas;
106
- this[name] = canvas;
94
+ return [layer, canvas];
95
+ }),
96
+ ) as Record<CanvasLayer, GCanvas>;
107
97
 
108
- return acc;
109
- },
110
- { renderers: {}, canvas: {} } as {
111
- renderers: Record<CanvasLayer, IRenderer>;
112
- canvas: Record<CanvasLayer, GCanvas>;
113
- },
114
- );
115
-
116
- this.renderers = renderers;
117
-
118
- Object.entries(canvas).forEach(([name, canvas]) => {
119
- const domElement = canvas.getContextService().getDomElement() as unknown as HTMLElement;
120
-
121
- domElement.style.position = 'absolute';
122
- domElement.style.outline = 'none';
123
- domElement.tabIndex = 1;
124
-
125
- if (name !== 'main') domElement.style.pointerEvents = 'none';
126
- });
127
-
128
- this.setBackground();
129
-
130
- await Promise.all(Object.values(this.canvas).map((canvas) => canvas.ready));
131
-
132
- this.initialized = true;
133
- }
134
-
135
- public getRendererType(layer: CanvasLayer = 'main') {
136
- const plugins = this.renderers[layer].getPlugins();
137
-
138
- for (const plugin of plugins) {
139
- if (plugin.name === 'canvas-renderer') return 'canvas';
140
- if (plugin.name === 'svg-renderer') return 'svg';
141
- if (plugin.name === 'device-renderer') return 'gpu';
142
- }
143
-
144
- return 'unknown';
145
- }
146
-
147
- public get context() {
148
- return this.main.context;
149
- }
98
+ configCanvasDom(layers);
150
99
 
151
- public getDevice() {
152
- // @ts-expect-error deviceRendererPlugin is private
153
- return this.main.context?.deviceRendererPlugin?.getDevice();
154
- }
155
-
156
- public getConfig() {
157
- return this.config;
158
- }
159
-
160
- public setBackground(background = this.config.background) {
161
- this.config.background = background;
162
- }
163
-
164
- public setCursor(cursor: Cursor) {
165
- this.config.cursor = cursor;
166
- Object.values(this.canvas).forEach((canvas) => {
167
- canvas.setCursor(cursor);
168
- });
100
+ this.extends = {
101
+ config,
102
+ renderer,
103
+ renderers,
104
+ layers,
105
+ };
169
106
  }
170
107
 
171
- public getSize(): [number, number] {
172
- return [this.config.width || 0, this.config.height || 0];
108
+ public get ready() {
109
+ return Promise.all([
110
+ super.ready,
111
+ ...Object.entries(this.getLayers()).map(([layer, canvas]) =>
112
+ layer === 'main' ? Promise.resolve() : canvas.ready,
113
+ ),
114
+ ]);
173
115
  }
174
116
 
175
117
  public resize(width: number, height: number) {
176
- this.config.width = width;
177
- this.config.height = height;
178
- Object.values(this.canvas).forEach((canvas) => {
179
- canvas.resize(width, height);
118
+ Object.assign(this.extends.config, { width, height });
119
+ Object.values(this.getLayers()).forEach((canvas) => {
120
+ if (canvas === this) super.resize(width, height);
121
+ else canvas.resize(width, height);
180
122
  });
181
123
  }
182
124
 
183
- public getCamera() {
184
- return this.main.getCamera();
185
- }
186
-
187
125
  public getBounds() {
188
126
  return getCombinedBBox(
189
- Object.values(this.canvas)
127
+ Object.values(this.getLayers())
190
128
  .map((canvas) => canvas.document.documentElement)
191
129
  .filter((el) => el.childNodes.length > 0)
192
130
  .map((el) => el.getBounds()),
@@ -194,48 +132,38 @@ export class Canvas {
194
132
  }
195
133
 
196
134
  public getContainer() {
197
- const container = this.config.container!;
198
-
199
- return isString(container) ? document.getElementById(container!) : container;
135
+ const container = this.extends.config.container!;
136
+ return typeof container === 'string' ? document.getElementById(container!) : container;
200
137
  }
201
138
 
202
- public appendChild<T extends DisplayObject>(child: T): T {
203
- const layer = (child.style?.$layer || 'main') as CanvasLayer;
204
-
205
- return this[layer].appendChild(child);
206
- }
207
-
208
- public getContextService() {
209
- return this.main.getContextService();
210
- }
211
-
212
- public viewport2Client(viewport: PointObject) {
213
- return this.main.viewport2Client(viewport);
139
+ public getSize(): [number, number] {
140
+ return [this.extends.config.width || 0, this.extends.config.height || 0];
214
141
  }
215
142
 
216
- public viewport2Canvas(viewport: PointObject) {
217
- return this.main.viewport2Canvas(viewport);
143
+ public appendChild<T extends IChildNode>(child: T, index?: number): T {
144
+ const layer = ((child as unknown as DisplayObject).style?.$layer || 'main') as CanvasLayer;
145
+ if (layer === 'main') return super.appendChild(child, index);
146
+ return this.getLayer(layer).appendChild(child, index);
218
147
  }
219
148
 
220
- public client2Viewport(client: PointObject) {
221
- return this.main.client2Viewport(client);
222
- }
149
+ public setRenderer(renderer: any) {
150
+ if (renderer === this.extends.renderer) return;
151
+ const renderers = createRenderers(renderer);
152
+ this.extends.renderers = renderers;
223
153
 
224
- public canvas2Viewport(canvas: PointObject) {
225
- return this.main.canvas2Viewport(canvas);
154
+ Object.entries(renderers).forEach(([layer, instance]) => {
155
+ if (layer === 'main') super.setRenderer(instance);
156
+ else this.getLayer(layer as CanvasLayer).setRenderer(instance);
157
+ });
226
158
  }
227
159
 
228
160
  public async toDataURL(options: Partial<DataURLOptions> = {}) {
229
161
  const devicePixelRatio = window.devicePixelRatio || 1;
230
162
  const { mode = 'viewport', ...restOptions } = options;
231
-
232
- let startX = 0;
233
- let startY = 0;
234
- let width = 0;
235
- let height = 0;
163
+ let [startX, startY, width, height] = [0, 0, 0, 0];
236
164
 
237
165
  if (mode === 'viewport') {
238
- [width, height] = [this.config.width || 0, this.config.height || 0];
166
+ [width, height] = this.getSize();
239
167
  } else if (mode === 'overall') {
240
168
  const bounds = this.getBounds();
241
169
  const size = getBBoxSize(bounds);
@@ -251,28 +179,28 @@ export class Canvas {
251
179
  renderer: new CanvasRenderer(),
252
180
  devicePixelRatio,
253
181
  container,
254
- background: this.config.background,
182
+ background: this.extends.config.background,
255
183
  });
256
184
 
257
185
  await offscreenCanvas.ready;
258
186
 
259
- offscreenCanvas.appendChild(this.background.getRoot().cloneNode(true));
260
- offscreenCanvas.appendChild(this.main.getRoot().cloneNode(true));
187
+ offscreenCanvas.appendChild(this.getLayer('background').getRoot().cloneNode(true));
188
+ offscreenCanvas.appendChild(this.getRoot().cloneNode(true));
261
189
 
262
190
  // Handle label canvas
263
- const label = this.label.getRoot().cloneNode(true);
191
+ const label = this.getLayer('label').getRoot().cloneNode(true);
264
192
  const originCanvasPosition = offscreenCanvas.viewport2Canvas({ x: 0, y: 0 });
265
- const currentCanvasPosition = this.main.viewport2Canvas({ x: 0, y: 0 });
193
+ const currentCanvasPosition = this.viewport2Canvas({ x: 0, y: 0 });
266
194
  label.translate([
267
195
  currentCanvasPosition.x - originCanvasPosition.x,
268
196
  currentCanvasPosition.y - originCanvasPosition.y,
269
197
  ]);
270
- label.scale(1 / this.main.getCamera().getZoom());
198
+ label.scale(1 / this.getCamera().getZoom());
271
199
  offscreenCanvas.appendChild(label);
272
200
 
273
- offscreenCanvas.appendChild(this.transient.getRoot().cloneNode(true));
201
+ offscreenCanvas.appendChild(this.getLayer('transient').getRoot().cloneNode(true));
274
202
 
275
- const camera = this.main.getCamera();
203
+ const camera = this.getCamera();
276
204
  const offscreenCamera = offscreenCanvas.getCamera();
277
205
 
278
206
  if (mode === 'viewport') {
@@ -289,7 +217,7 @@ export class Canvas {
289
217
  const contextService = offscreenCanvas.getContextService();
290
218
 
291
219
  return new Promise<string>((resolve) => {
292
- offscreenCanvas.on(CanvasEvent.RERENDER, async () => {
220
+ offscreenCanvas.addEventListener(CanvasEvent.RERENDER, async () => {
293
221
  // 等待图片渲染完成 / Wait for the image to render
294
222
  await new Promise((r) => setTimeout(r, 300));
295
223
  const url = await contextService.toDataURL(restOptions);
@@ -298,57 +226,60 @@ export class Canvas {
298
226
  });
299
227
  }
300
228
 
301
- public destroy() {
302
- this.config = {};
303
- // @ts-expect-error force delete
304
- this.renderers = {};
305
- Object.entries(this.canvas).forEach(([name, canvas]) => {
229
+ public destroy(cleanUp?: boolean, skipTriggerEvent?: boolean) {
230
+ Object.values(this.getLayers()).forEach((canvas) => {
306
231
  const camera = canvas.getCamera();
307
- // @ts-expect-error landmark is private
308
- if (camera.landmarks?.length) {
309
- camera.cancelLandmarkAnimation();
310
- }
311
-
312
- destroyCanvas(canvas);
313
- // @ts-expect-error force delete
314
- this[name] = undefined;
232
+ camera.cancelLandmarkAnimation();
233
+ if (canvas === this) super.destroy(cleanUp, skipTriggerEvent);
234
+ else canvas.destroy(cleanUp, skipTriggerEvent);
315
235
  });
316
236
  }
317
237
  }
318
238
 
319
239
  /**
320
- * <zh/> G Canvas destroy 未处理动画对象,导致内存泄漏
240
+ * <zh/> 创建渲染器
321
241
  *
322
- * <en/> G Canvas destroy does not handle animation objects, causing memory leaks
323
- * @param canvas GCanvas
324
- * @remarks
325
- * <zh/> 这些操作都应该在 G 中完成,这里只是一个临时的解决方案
326
- * 此操作大概能在测试环节降低 10~20% 的内存占用(从 2800MB 降低到 2200MB)
242
+ * <en/> Create renderers
243
+ * @param renderer - <zh/> 渲染器创建器 <en/> Renderer creator
244
+ * @returns <zh/> 渲染器 <en/> Renderer
245
+ */
246
+ function createRenderers(renderer: CanvasConfig['renderer']) {
247
+ return Object.fromEntries(
248
+ layersName.map((layer) => {
249
+ const instance = renderer?.(layer) || new CanvasRenderer();
250
+
251
+ if (layer === 'main') {
252
+ instance.registerPlugin(
253
+ new DragNDropPlugin({
254
+ isDocumentDraggable: true,
255
+ isDocumentDroppable: true,
256
+ dragstartDistanceThreshold: 10,
257
+ dragstartTimeThreshold: 100,
258
+ }),
259
+ );
260
+ } else {
261
+ instance.unregisterPlugin(instance.getPlugin('dom-interaction'));
262
+ }
263
+
264
+ return [layer, instance];
265
+ }),
266
+ ) as Record<CanvasLayer, CanvasRenderer>;
267
+ }
268
+
269
+ /**
270
+ * <zh/> 配置画布 DOM
327
271
  *
328
- * <en/> These operations should be completed in G, this is just a temporary solution
329
- * This operation can reduce memory usage by 10-20% in the test environment (from 2800MB to 2200MB)
272
+ * <en/> Configure canvas DOM
273
+ * @param layers - <zh/> 画布 <en/> Canvas
330
274
  */
331
- function destroyCanvas(canvas: GCanvas) {
332
- canvas.destroy();
333
-
334
- // 移除相机事件 / Remove camera events
335
- const camera = canvas.getCamera();
336
- camera.eventEmitter.removeAllListeners();
337
-
338
- canvas.document.timeline.destroy();
339
- // @ts-expect-error private property
340
- const { animationsWithPromises } = canvas.document.timeline;
341
- // 释放 target 对象图形 / Release target object graphics
342
- animationsWithPromises.forEach((animation: IAnimation) => {
343
- if (animation.effect.target) animation.effect.target = null;
344
- // @ts-expect-error private property
345
- if (animation.effect.computedTiming) animation.effect.computedTiming = null;
346
- });
275
+ function configCanvasDom(layers: Record<CanvasLayer, GCanvas>) {
276
+ Object.entries(layers).forEach(([layer, canvas]) => {
277
+ const domElement = canvas.getContextService().getDomElement() as unknown as HTMLElement;
347
278
 
348
- // @ts-expect-error private property
349
- canvas.document.timeline.animationsWithPromises = [];
350
- // @ts-expect-error private property
351
- canvas.document.timeline.rafCallbacks = [];
352
- // @ts-expect-error private property
353
- canvas.document.timeline = null;
279
+ domElement.style.position = 'absolute';
280
+ domElement.style.outline = 'none';
281
+ domElement.tabIndex = 1;
282
+
283
+ if (layer !== 'main') domElement.style.pointerEvents = 'none';
284
+ });
354
285
  }
@@ -25,6 +25,7 @@ import { arrayDiff } from '../utils/diff';
25
25
  import { toG6Data, toGraphlibData } from '../utils/graphlib';
26
26
  import { idOf, parentIdOf } from '../utils/id';
27
27
  import { positionOf } from '../utils/position';
28
+ import { format, print } from '../utils/print';
28
29
  import { dfs } from '../utils/traverse';
29
30
  import { add } from '../utils/vector';
30
31
 
@@ -188,7 +189,7 @@ export class DataController {
188
189
  public getParentData(id: ID, hierarchyKey: HierarchyKey): NodeLikeData | undefined {
189
190
  const { model } = this;
190
191
  if (!hierarchyKey) {
191
- console.error('The hierarchy structure key is not specified');
192
+ print.warn('The hierarchy structure key is not specified');
192
193
  return undefined;
193
194
  }
194
195
  if (!model.hasNode(id) || !model.hasTreeStructure(hierarchyKey)) return undefined;
@@ -289,7 +290,7 @@ export class DataController {
289
290
 
290
291
  public setData(data: GraphData) {
291
292
  const { nodes: modifiedNodes = [], edges: modifiedEdges = [], combos: modifiedCombos = [] } = data;
292
- const { nodes: originalNodes = [], edges: originalEdges = [], combos: originalCombos = [] } = this.getData();
293
+ const { nodes: originalNodes, edges: originalEdges, combos: originalCombos } = this.getData();
293
294
 
294
295
  const nodeDiff = arrayDiff(originalNodes, modifiedNodes, (node) => idOf(node));
295
296
  const edgeDiff = arrayDiff(originalEdges, modifiedEdges, (edge) => idOf(edge));
@@ -761,7 +762,7 @@ export class DataController {
761
762
 
762
763
  if (this.model.hasEdge(id)) return 'edge';
763
764
 
764
- throw new Error(`Unknown element type of id: ${id}`);
765
+ throw new Error(format(`Unknown element type of id: ${id}`));
765
766
  }
766
767
 
767
768
  public destroy() {
@@ -31,6 +31,7 @@ import { AnimateEvent, ElementLifeCycleEvent, GraphLifeCycleEvent, emit } from '
31
31
  import { idOf } from '../utils/id';
32
32
  import { assignColorByPalette, parsePalette } from '../utils/palette';
33
33
  import { positionOf } from '../utils/position';
34
+ import { print } from '../utils/print';
34
35
  import { computeElementCallbackStyle } from '../utils/style';
35
36
  import { themeOf } from '../utils/theme';
36
37
  import type { RuntimeContext } from './types';
@@ -366,7 +367,7 @@ export class ElementController {
366
367
 
367
368
  // get shape constructor
368
369
  const Ctor = getExtension(elementType, type);
369
- if (!Ctor) return;
370
+ if (!Ctor) return print.warn(`The element ${type} of ${elementType} is not registered.`);
370
371
 
371
372
  this.emit(new ElementLifeCycleEvent(GraphEvent.BEFORE_ELEMENT_CREATE, elementType, datum), context);
372
373
 
@@ -791,7 +792,7 @@ export interface DrawContext {
791
792
  animation: boolean;
792
793
  /** <zh/> 当前绘制阶段 | <en/> Current draw stage */
793
794
  stage?: AnimationStage;
794
- /** <zh/> 是否不抛出事件 | <en/> Whether not to throw events */
795
+ /** <zh/> 是否不抛出事件 | <en/> Whether not to dispatch events */
795
796
  silence?: boolean;
796
797
  /** <zh/> 收起/展开的对象 ID | <en/> ID of the object to collapse/expand */
797
798
  collapseExpandTarget?: ID;
@@ -3,7 +3,6 @@ import type { AABB, BaseStyleProps } from '@antv/g';
3
3
  import { debounce, isEqual, isFunction, isNumber, isObject, isString, omit } from '@antv/util';
4
4
  import { COMBO_KEY, GraphEvent } from '../constants';
5
5
  import type { Plugin } from '../plugins/types';
6
- import { getExtension } from '../registry';
7
6
  import type {
8
7
  BehaviorOptions,
9
8
  ComboData,
@@ -45,6 +44,7 @@ import { sizeOf } from '../utils/dom';
45
44
  import { GraphLifeCycleEvent, emit } from '../utils/event';
46
45
  import { idOf } from '../utils/id';
47
46
  import { parsePoint, toPointObject } from '../utils/point';
47
+ import { format } from '../utils/print';
48
48
  import { subtract } from '../utils/vector';
49
49
  import { Animation } from './animation';
50
50
  import { BatchController } from './batch';
@@ -118,12 +118,10 @@ export class Graph extends EventEmitter {
118
118
  * @apiCategory option
119
119
  */
120
120
  public setOptions(options: GraphOptions): void {
121
- const { background, behaviors, combo, data, edge, height, layout, node, plugins, theme, transforms, width } =
122
- options;
121
+ const { behaviors, combo, data, edge, height, layout, node, plugins, theme, transforms, width, renderer } = options;
123
122
 
124
123
  Object.assign(this.options, options);
125
124
 
126
- if (background) this.setBackground(background);
127
125
  if (behaviors) this.setBehaviors(behaviors);
128
126
  if (combo) this.setCombo(combo);
129
127
  if (data) this.setData(data);
@@ -135,29 +133,7 @@ export class Graph extends EventEmitter {
135
133
  if (transforms) this.setTransforms(transforms);
136
134
  if (isNumber(width) || isNumber(height))
137
135
  this.setSize(width ?? this.options.width ?? 0, height ?? this.options.height ?? 0);
138
- }
139
-
140
- /**
141
- * <zh/> 设置画布背景色
142
- *
143
- * <en/> Set canvas background color
144
- * @param background - <zh/> 背景色 | <en/> background color
145
- * @apiCategory canvas
146
- */
147
- public setBackground(background: GraphOptions['background']): void {
148
- this.options.background = background;
149
- this.context.canvas?.setBackground(background);
150
- }
151
-
152
- /**
153
- * <zh/> 获取画布背景色
154
- *
155
- * <en/> Get canvas background color
156
- * @returns <zh/> 背景色 | <en/> background color
157
- * @apiCategory canvas
158
- */
159
- public getBackground(): GraphOptions['background'] {
160
- return this.options.background;
136
+ if (renderer) this.context.canvas?.setRenderer(renderer);
161
137
  }
162
138
 
163
139
  /**
@@ -279,11 +255,6 @@ export class Graph extends EventEmitter {
279
255
  */
280
256
  public setTheme(theme: ThemeOptions | ((prev: ThemeOptions) => ThemeOptions)): void {
281
257
  this.options.theme = isFunction(theme) ? theme(this.getTheme()) : theme;
282
-
283
- const { background } = getExtension('theme', this.options.theme) || {};
284
- if (background && !this.options.background) {
285
- this.setBackground(background);
286
- }
287
258
  }
288
259
 
289
260
  /**
@@ -512,7 +483,7 @@ export class Graph extends EventEmitter {
512
483
  * <en/> Get the data of the current graph, including node, edge, and combo data
513
484
  * @apiCategory data
514
485
  */
515
- public getData(): GraphData {
486
+ public getData(): Required<GraphData> {
516
487
  return this.context.model.getData();
517
488
  }
518
489
 
@@ -1033,14 +1004,13 @@ export class Graph extends EventEmitter {
1033
1004
  }
1034
1005
 
1035
1006
  private async initCanvas() {
1036
- if (this.context.canvas) return await this.context.canvas.init();
1007
+ if (this.context.canvas) return await this.context.canvas.ready;
1037
1008
 
1038
1009
  const { container = 'container', width, height, renderer, background } = this.options;
1039
1010
 
1040
1011
  if (container instanceof Canvas) {
1041
1012
  this.context.canvas = container;
1042
- container.setBackground(background);
1043
- await container.init();
1013
+ await container.ready;
1044
1014
  } else {
1045
1015
  const $container = isString(container) ? document.getElementById(container!) : container;
1046
1016
  const containerSize = sizeOf($container!);
@@ -1056,7 +1026,7 @@ export class Graph extends EventEmitter {
1056
1026
  });
1057
1027
 
1058
1028
  this.context.canvas = canvas;
1059
- await canvas.init();
1029
+ await canvas.ready;
1060
1030
  this.emit(GraphEvent.AFTER_CANVAS_INIT, { canvas });
1061
1031
  }
1062
1032
  }
@@ -1078,7 +1048,7 @@ export class Graph extends EventEmitter {
1078
1048
  // Wait for synchronous tasks to complete, to avoid problems caused by calling destroy immediately after render
1079
1049
  await Promise.resolve();
1080
1050
 
1081
- if (this.destroyed) throw new Error('Graph has been destroyed');
1051
+ if (this.destroyed) throw new Error(format('The graph instance has been destroyed'));
1082
1052
 
1083
1053
  await this.initCanvas();
1084
1054
  this.initRuntime();
@@ -15,6 +15,7 @@ import { GraphLifeCycleEvent, emit } from '../utils/event';
15
15
  import { createTreeStructure } from '../utils/graphlib';
16
16
  import { idOf } from '../utils/id';
17
17
  import { isTreeLayout, layoutAdapter, layoutMapping2GraphData } from '../utils/layout';
18
+ import { print } from '../utils/print';
18
19
  import { parseSize } from '../utils/size';
19
20
  import { dfs } from '../utils/traverse';
20
21
  import type { RuntimeContext } from './types';
@@ -103,6 +104,8 @@ export class LayoutController {
103
104
  const { animation, enableWorker, iterations = 300 } = options;
104
105
 
105
106
  const layout = this.initGraphLayout(options);
107
+ if (!layout) return {};
108
+
106
109
  this.instances[index] = layout;
107
110
  this.instance = layout;
108
111
 
@@ -142,7 +145,7 @@ export class LayoutController {
142
145
  // @ts-expect-error @antv/hierarchy 布局格式与 @antv/layout 不一致,其导出的是一个方法,而非 class
143
146
  // The layout format of @antv/hierarchy is inconsistent with @antv/layout, it exports a method instead of a class
144
147
  const layout = getExtension('layout', type) as (tree: TreeData, options: STDLayoutOptions) => TreeData;
145
- if (!layout) throw new Error(`The layout type ${type} is not found`);
148
+ if (!layout) return {};
146
149
 
147
150
  const { nodes = [], edges = [] } = data;
148
151
 
@@ -256,7 +259,7 @@ export class LayoutController {
256
259
  });
257
260
 
258
261
  const Ctor = getExtension('layout', type);
259
- if (!Ctor) throw new Error(`The layout type ${type} is not found`);
262
+ if (!Ctor) return print.warn(`The layout of ${type} is not registered.`);
260
263
 
261
264
  const STDCtor =
262
265
  Object.getPrototypeOf(Ctor.prototype) === BaseLayout.prototype