@antv/g6 5.0.6 → 5.0.8

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 (203) 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/behaviors/drag-canvas.d.ts +4 -0
  4. package/dist/lib/elements/base-element.d.ts +5 -4
  5. package/dist/lib/plugins/fullscreen/index.d.ts +2 -0
  6. package/dist/lib/registry/index.d.ts +5 -1
  7. package/dist/lib/runtime/animation.d.ts +7 -0
  8. package/dist/lib/runtime/canvas.d.ts +27 -38
  9. package/dist/lib/runtime/element.d.ts +1 -1
  10. package/dist/lib/runtime/graph.d.ts +1 -17
  11. package/dist/lib/runtime/viewport.d.ts +1 -0
  12. package/dist/lib/types/point.d.ts +5 -0
  13. package/dist/lib/utils/point.d.ts +4 -4
  14. package/dist/lib/utils/print.d.ts +15 -0
  15. package/dist/lib/utils/shortcut.d.ts +1 -0
  16. package/dist/lib/utils/state.d.ts +9 -0
  17. package/dist/lib/version.d.ts +1 -1
  18. package/esm/behaviors/brush-select.js +1 -2
  19. package/esm/behaviors/brush-select.js.map +1 -1
  20. package/esm/behaviors/click-select.d.ts +8 -16
  21. package/esm/behaviors/click-select.js +108 -90
  22. package/esm/behaviors/click-select.js.map +1 -1
  23. package/esm/behaviors/drag-canvas.d.ts +4 -0
  24. package/esm/behaviors/drag-canvas.js +29 -9
  25. package/esm/behaviors/drag-canvas.js.map +1 -1
  26. package/esm/behaviors/drag-element-force.js +2 -1
  27. package/esm/behaviors/drag-element-force.js.map +1 -1
  28. package/esm/behaviors/drag-element.js +1 -2
  29. package/esm/behaviors/drag-element.js.map +1 -1
  30. package/esm/behaviors/zoom-canvas.js.map +1 -1
  31. package/esm/elements/base-element.d.ts +5 -4
  32. package/esm/elements/base-element.js +3 -0
  33. package/esm/elements/base-element.js.map +1 -1
  34. package/esm/elements/combos/base-combo.js +2 -2
  35. package/esm/elements/combos/base-combo.js.map +1 -1
  36. package/esm/elements/nodes/html.js +3 -5
  37. package/esm/elements/nodes/html.js.map +1 -1
  38. package/esm/elements/shapes/base-shape.js +27 -3
  39. package/esm/elements/shapes/base-shape.js.map +1 -1
  40. package/esm/plugins/contextmenu/index.js +5 -2
  41. package/esm/plugins/contextmenu/index.js.map +1 -1
  42. package/esm/plugins/fullscreen/index.d.ts +2 -0
  43. package/esm/plugins/fullscreen/index.js +15 -2
  44. package/esm/plugins/fullscreen/index.js.map +1 -1
  45. package/esm/registry/extension/index.js +2 -1
  46. package/esm/registry/extension/index.js.map +1 -1
  47. package/esm/registry/index.d.ts +5 -1
  48. package/esm/registry/index.js +8 -3
  49. package/esm/registry/index.js.map +1 -1
  50. package/esm/runtime/animation.d.ts +7 -0
  51. package/esm/runtime/animation.js +17 -0
  52. package/esm/runtime/animation.js.map +1 -1
  53. package/esm/runtime/canvas.d.ts +27 -38
  54. package/esm/runtime/canvas.js +128 -175
  55. package/esm/runtime/canvas.js.map +1 -1
  56. package/esm/runtime/data.js +4 -3
  57. package/esm/runtime/data.js.map +1 -1
  58. package/esm/runtime/element.d.ts +1 -1
  59. package/esm/runtime/element.js +2 -1
  60. package/esm/runtime/element.js.map +1 -1
  61. package/esm/runtime/graph.d.ts +1 -17
  62. package/esm/runtime/graph.js +8 -34
  63. package/esm/runtime/graph.js.map +1 -1
  64. package/esm/runtime/layout.js +5 -2
  65. package/esm/runtime/layout.js.map +1 -1
  66. package/esm/runtime/viewport.d.ts +1 -0
  67. package/esm/runtime/viewport.js +13 -3
  68. package/esm/runtime/viewport.js.map +1 -1
  69. package/esm/types/point.d.ts +5 -0
  70. package/esm/utils/animation.js +9 -3
  71. package/esm/utils/animation.js.map +1 -1
  72. package/esm/utils/id.js +2 -1
  73. package/esm/utils/id.js.map +1 -1
  74. package/esm/utils/layout.js +9 -1
  75. package/esm/utils/layout.js.map +1 -1
  76. package/esm/utils/palette.js +2 -1
  77. package/esm/utils/palette.js.map +1 -1
  78. package/esm/utils/point.d.ts +4 -4
  79. package/esm/utils/point.js +2 -2
  80. package/esm/utils/point.js.map +1 -1
  81. package/esm/utils/print.d.ts +15 -0
  82. package/esm/utils/print.js +28 -0
  83. package/esm/utils/print.js.map +1 -0
  84. package/esm/utils/shortcut.d.ts +1 -0
  85. package/esm/utils/shortcut.js +7 -0
  86. package/esm/utils/shortcut.js.map +1 -1
  87. package/esm/utils/state.d.ts +9 -0
  88. package/esm/utils/state.js +11 -0
  89. package/esm/utils/state.js.map +1 -0
  90. package/esm/utils/theme.js +5 -1
  91. package/esm/utils/theme.js.map +1 -1
  92. package/esm/version.d.ts +1 -1
  93. package/esm/version.js +1 -1
  94. package/lib/behaviors/brush-select.js +1 -2
  95. package/lib/behaviors/brush-select.js.map +1 -1
  96. package/lib/behaviors/click-select.d.ts +8 -16
  97. package/lib/behaviors/click-select.js +106 -88
  98. package/lib/behaviors/click-select.js.map +1 -1
  99. package/lib/behaviors/drag-canvas.d.ts +4 -0
  100. package/lib/behaviors/drag-canvas.js +28 -8
  101. package/lib/behaviors/drag-canvas.js.map +1 -1
  102. package/lib/behaviors/drag-element-force.js +2 -1
  103. package/lib/behaviors/drag-element-force.js.map +1 -1
  104. package/lib/behaviors/drag-element.js +1 -2
  105. package/lib/behaviors/drag-element.js.map +1 -1
  106. package/lib/behaviors/zoom-canvas.js.map +1 -1
  107. package/lib/elements/base-element.d.ts +5 -4
  108. package/lib/elements/base-element.js +3 -0
  109. package/lib/elements/base-element.js.map +1 -1
  110. package/lib/elements/combos/base-combo.js +2 -2
  111. package/lib/elements/combos/base-combo.js.map +1 -1
  112. package/lib/elements/nodes/html.js +3 -5
  113. package/lib/elements/nodes/html.js.map +1 -1
  114. package/lib/elements/shapes/base-shape.js +27 -3
  115. package/lib/elements/shapes/base-shape.js.map +1 -1
  116. package/lib/plugins/contextmenu/index.js +5 -2
  117. package/lib/plugins/contextmenu/index.js.map +1 -1
  118. package/lib/plugins/fullscreen/index.d.ts +2 -0
  119. package/lib/plugins/fullscreen/index.js +15 -2
  120. package/lib/plugins/fullscreen/index.js.map +1 -1
  121. package/lib/registry/extension/index.js +2 -1
  122. package/lib/registry/extension/index.js.map +1 -1
  123. package/lib/registry/index.d.ts +5 -1
  124. package/lib/registry/index.js +8 -3
  125. package/lib/registry/index.js.map +1 -1
  126. package/lib/runtime/animation.d.ts +7 -0
  127. package/lib/runtime/animation.js +17 -0
  128. package/lib/runtime/animation.js.map +1 -1
  129. package/lib/runtime/canvas.d.ts +27 -38
  130. package/lib/runtime/canvas.js +127 -174
  131. package/lib/runtime/canvas.js.map +1 -1
  132. package/lib/runtime/data.js +4 -3
  133. package/lib/runtime/data.js.map +1 -1
  134. package/lib/runtime/element.d.ts +1 -1
  135. package/lib/runtime/element.js +2 -1
  136. package/lib/runtime/element.js.map +1 -1
  137. package/lib/runtime/graph.d.ts +1 -17
  138. package/lib/runtime/graph.js +8 -34
  139. package/lib/runtime/graph.js.map +1 -1
  140. package/lib/runtime/layout.js +5 -2
  141. package/lib/runtime/layout.js.map +1 -1
  142. package/lib/runtime/viewport.d.ts +1 -0
  143. package/lib/runtime/viewport.js +13 -3
  144. package/lib/runtime/viewport.js.map +1 -1
  145. package/lib/types/point.d.ts +5 -0
  146. package/lib/utils/animation.js +8 -2
  147. package/lib/utils/animation.js.map +1 -1
  148. package/lib/utils/id.js +2 -1
  149. package/lib/utils/id.js.map +1 -1
  150. package/lib/utils/layout.js +9 -1
  151. package/lib/utils/layout.js.map +1 -1
  152. package/lib/utils/palette.js +2 -1
  153. package/lib/utils/palette.js.map +1 -1
  154. package/lib/utils/point.d.ts +4 -4
  155. package/lib/utils/point.js +2 -2
  156. package/lib/utils/point.js.map +1 -1
  157. package/lib/utils/print.d.ts +15 -0
  158. package/lib/utils/print.js +32 -0
  159. package/lib/utils/print.js.map +1 -0
  160. package/lib/utils/shortcut.d.ts +1 -0
  161. package/lib/utils/shortcut.js +7 -0
  162. package/lib/utils/shortcut.js.map +1 -1
  163. package/lib/utils/state.d.ts +9 -0
  164. package/lib/utils/state.js +14 -0
  165. package/lib/utils/state.js.map +1 -0
  166. package/lib/utils/theme.js +5 -1
  167. package/lib/utils/theme.js.map +1 -1
  168. package/lib/version.d.ts +1 -1
  169. package/lib/version.js +1 -1
  170. package/package.json +8 -8
  171. package/src/behaviors/brush-select.ts +1 -2
  172. package/src/behaviors/click-select.ts +117 -88
  173. package/src/behaviors/drag-canvas.ts +32 -9
  174. package/src/behaviors/drag-element-force.ts +2 -1
  175. package/src/behaviors/drag-element.ts +1 -2
  176. package/src/behaviors/zoom-canvas.ts +2 -3
  177. package/src/elements/base-element.ts +8 -4
  178. package/src/elements/combos/base-combo.ts +2 -2
  179. package/src/elements/nodes/html.ts +3 -7
  180. package/src/elements/shapes/base-shape.ts +30 -3
  181. package/src/global.d.ts +12 -0
  182. package/src/plugins/contextmenu/index.ts +5 -2
  183. package/src/plugins/fullscreen/index.ts +18 -2
  184. package/src/registry/extension/index.ts +2 -1
  185. package/src/registry/index.ts +8 -2
  186. package/src/runtime/animation.ts +21 -0
  187. package/src/runtime/canvas.ts +140 -208
  188. package/src/runtime/data.ts +4 -3
  189. package/src/runtime/element.ts +3 -2
  190. package/src/runtime/graph.ts +8 -38
  191. package/src/runtime/layout.ts +5 -2
  192. package/src/runtime/viewport.ts +18 -5
  193. package/src/types/point.ts +6 -0
  194. package/src/utils/animation.ts +9 -2
  195. package/src/utils/id.ts +2 -1
  196. package/src/utils/layout.ts +7 -1
  197. package/src/utils/palette.ts +2 -1
  198. package/src/utils/point.ts +6 -6
  199. package/src/utils/print.ts +30 -0
  200. package/src/utils/shortcut.ts +9 -0
  201. package/src/utils/state.ts +12 -0
  202. package/src/utils/theme.ts +6 -1
  203. package/src/version.ts +1 -1
@@ -1,5 +1,6 @@
1
1
  import { ExtensionCategory } from '../constants';
2
2
  import type { Loosen } from '../types';
3
+ import { print } from '../utils/print';
3
4
  import { BUILT_IN_EXTENSIONS } from './build-in';
4
5
  import type { ExtensionRegistry } from './types';
5
6
 
@@ -37,6 +38,10 @@ const EXTENSION_REGISTRY: ExtensionRegistry = {
37
38
  * <zh/> 要注册的扩展类,在使用时创建实例
38
39
  *
39
40
  * <en/> The extension class to be registered. An instance will be created upon use
41
+ * @param override
42
+ * <zh/> 是否覆盖已注册的扩展
43
+ *
44
+ * <en/> Whether to override the registered extension
40
45
  * @remarks
41
46
  * <zh/> 内置扩展在项目导入时会自动注册。对于非内置扩展,可以通过 `register` 方法手动注册。扩展只需要注册一次,即可在项目的任何位置使用。
42
47
  *
@@ -55,10 +60,11 @@ export function register<T extends ExtensionCategory>(
55
60
  category: Loosen<T>,
56
61
  type: string,
57
62
  Ctor: ExtensionRegistry[T][string],
63
+ override = false,
58
64
  ) {
59
65
  const ext = EXTENSION_REGISTRY[category][type];
60
- if (ext) {
61
- if (ext !== Ctor) console.error(`The extension ${type} of ${category} has been registered before.`);
66
+ if (!override && ext) {
67
+ if (ext !== Ctor) print.warn(`The extension ${type} of ${category} has been registered before.`);
62
68
  } else Object.assign(EXTENSION_REGISTRY[category]!, { [type]: Ctor });
63
69
  }
64
70
 
@@ -46,6 +46,7 @@ export class Animation {
46
46
  animation.finished.then(() => {
47
47
  cb?.afterAnimate?.(animation);
48
48
  cb?.after?.();
49
+ this.animations.delete(animation);
49
50
  });
50
51
  } else cb?.after?.();
51
52
 
@@ -63,6 +64,7 @@ export class Animation {
63
64
  animation.finished.then(() => {
64
65
  callbacks?.afterAnimate?.(animation);
65
66
  callbacks?.after?.();
67
+ this.release();
66
68
  });
67
69
  } else callbacks?.after?.();
68
70
 
@@ -147,6 +149,25 @@ export class Animation {
147
149
  this.tasks = [];
148
150
  }
149
151
 
152
+ /**
153
+ * <zh/> 释放存量动画对象
154
+ *
155
+ * <en/> Release stock animation objects
156
+ * @description see: https://github.com/antvis/G/issues/1731
157
+ */
158
+ private release() {
159
+ const { canvas } = this.context;
160
+
161
+ // @ts-expect-error private property
162
+ const animationsWithPromises = canvas.document?.timeline?.animationsWithPromises;
163
+ if (animationsWithPromises) {
164
+ // @ts-expect-error private property
165
+ canvas.document.timeline.animationsWithPromises = animationsWithPromises.filter(
166
+ (animation: IAnimation) => animation.playState !== 'finished',
167
+ );
168
+ }
169
+ }
170
+
150
171
  public destroy() {
151
172
  this.stop();
152
173
  this.animations.clear();
@@ -1,16 +1,15 @@
1
- import type { Cursor, DisplayObject, CanvasConfig as GCanvasConfig, IAnimation, IRenderer, PointLike } 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 { CanvasLayer } from '../types/canvas';
7
+ import type { CanvasLayer } from '../types';
8
8
  import { getBBoxSize, getCombinedBBox } from '../utils/bbox';
9
9
 
10
10
  export interface CanvasConfig
11
- extends Pick<GCanvasConfig, 'container' | 'devicePixelRatio' | 'width' | 'height' | 'cursor'> {
11
+ extends Pick<GCanvasConfig, 'container' | 'devicePixelRatio' | 'width' | 'height' | 'cursor' | 'background'> {
12
12
  renderer?: CanvasOptions['renderer'];
13
- background?: string;
14
13
  }
15
14
 
16
15
  export interface DataURLOptions {
@@ -39,153 +38,93 @@ export interface DataURLOptions {
39
38
  encoderOptions: number;
40
39
  }
41
40
 
42
- /**
43
- * @deprecated this canvas will be replace by layered canvas
44
- */
45
- export class Canvas {
46
- protected config: CanvasConfig;
47
-
48
- public background!: GCanvas;
49
- public main!: GCanvas;
50
- public label!: GCanvas;
51
- public transient!: GCanvas;
52
-
53
- public get canvas() {
54
- return {
55
- main: this.main,
56
- label: this.label,
57
- transient: this.transient,
58
- background: this.background,
59
- };
60
- }
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
+ };
61
50
 
62
- public get document() {
63
- return this.main.document;
51
+ public getLayer(layer: CanvasLayer) {
52
+ return this.extends.layers[layer];
64
53
  }
65
54
 
66
- 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
+ }
67
64
 
68
- 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
+ }
69
75
 
70
76
  constructor(config: CanvasConfig) {
71
- this.config = config;
72
- }
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 });
73
82
 
74
- public async init() {
75
- if (this.initialized) return;
76
-
77
- const { renderer: getRenderer, background, ...restConfig } = this.config;
78
- const names: CanvasLayer[] = ['main', 'label', 'transient', 'background'];
79
-
80
- const { renderers, canvas } = names.reduce(
81
- (acc, name) => {
82
- const renderer = isFunction(getRenderer) ? getRenderer?.(name) : new CanvasRenderer();
83
-
84
- if (name === 'main') {
85
- renderer.registerPlugin(
86
- new DragNDropPlugin({
87
- isDocumentDraggable: true,
88
- isDocumentDroppable: true,
89
- dragstartDistanceThreshold: 10,
90
- dragstartTimeThreshold: 100,
91
- }),
92
- );
93
- } else {
94
- renderer.unregisterPlugin(renderer.getPlugin('dom-interaction'));
95
- }
83
+ const layers = Object.fromEntries(
84
+ layersName.map((layer) => {
85
+ if (layer === 'main') return [layer, this];
96
86
 
97
87
  const canvas = new GCanvas({
98
- renderer,
99
- supportsMutipleCanvasesInOneContainer: true,
100
88
  ...restConfig,
89
+ supportsMutipleCanvasesInOneContainer: true,
90
+ renderer: renderers[layer],
91
+ background: layer === 'background' ? background : undefined,
101
92
  });
102
93
 
103
- acc.renderers[name] = renderer;
104
- acc.canvas[name] = canvas;
105
- this[name] = canvas;
94
+ return [layer, canvas];
95
+ }),
96
+ ) as Record<CanvasLayer, GCanvas>;
106
97
 
107
- return acc;
108
- },
109
- { renderers: {}, canvas: {} } as {
110
- renderers: Record<CanvasLayer, IRenderer>;
111
- canvas: Record<CanvasLayer, GCanvas>;
112
- },
113
- );
114
-
115
- this.renderers = renderers;
116
-
117
- Object.entries(canvas).forEach(([name, canvas]) => {
118
- const domElement = canvas.getContextService().getDomElement() as unknown as HTMLElement;
119
-
120
- domElement.style.position = 'absolute';
121
- domElement.style.outline = 'none';
122
- domElement.tabIndex = 1;
123
-
124
- if (name !== 'main') domElement.style.pointerEvents = 'none';
125
- });
126
-
127
- this.setBackground();
128
-
129
- await Promise.all(Object.values(this.canvas).map((canvas) => canvas.ready));
130
-
131
- this.initialized = true;
132
- }
133
-
134
- public getRendererType(layer: CanvasLayer = 'main') {
135
- const plugins = this.renderers[layer].getPlugins();
136
-
137
- for (const plugin of plugins) {
138
- if (plugin.name === 'canvas-renderer') return 'canvas';
139
- if (plugin.name === 'svg-renderer') return 'svg';
140
- if (plugin.name === 'device-renderer') return 'gpu';
141
- }
142
-
143
- return 'unknown';
144
- }
145
-
146
- public get context() {
147
- return this.main.context;
148
- }
98
+ configCanvasDom(layers);
149
99
 
150
- public getDevice() {
151
- // @ts-expect-error deviceRendererPlugin is private
152
- return this.main.context?.deviceRendererPlugin?.getDevice();
153
- }
154
-
155
- public getConfig() {
156
- return this.config;
157
- }
158
-
159
- public setBackground(background = this.config.background) {
160
- this.config.background = background;
161
- }
162
-
163
- public setCursor(cursor: Cursor) {
164
- this.config.cursor = cursor;
165
- Object.values(this.canvas).forEach((canvas) => {
166
- canvas.setCursor(cursor);
167
- });
100
+ this.extends = {
101
+ config,
102
+ renderer,
103
+ renderers,
104
+ layers,
105
+ };
168
106
  }
169
107
 
170
- public getSize(): [number, number] {
171
- 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
+ ]);
172
115
  }
173
116
 
174
117
  public resize(width: number, height: number) {
175
- this.config.width = width;
176
- this.config.height = height;
177
- Object.values(this.canvas).forEach((canvas) => {
178
- 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);
179
122
  });
180
123
  }
181
124
 
182
- public getCamera() {
183
- return this.main.getCamera();
184
- }
185
-
186
125
  public getBounds() {
187
126
  return getCombinedBBox(
188
- Object.values(this.canvas)
127
+ Object.values(this.getLayers())
189
128
  .map((canvas) => canvas.document.documentElement)
190
129
  .filter((el) => el.childNodes.length > 0)
191
130
  .map((el) => el.getBounds()),
@@ -193,48 +132,38 @@ export class Canvas {
193
132
  }
194
133
 
195
134
  public getContainer() {
196
- const container = this.config.container!;
197
-
198
- return isString(container) ? document.getElementById(container!) : container;
135
+ const container = this.extends.config.container!;
136
+ return typeof container === 'string' ? document.getElementById(container!) : container;
199
137
  }
200
138
 
201
- public appendChild<T extends DisplayObject>(child: T): T {
202
- const layer = (child.style?.$layer || 'main') as CanvasLayer;
203
-
204
- return this[layer].appendChild(child);
205
- }
206
-
207
- public getContextService() {
208
- return this.main.getContextService();
209
- }
210
-
211
- public viewport2Client(viewport: PointLike) {
212
- return this.main.viewport2Client(viewport);
139
+ public getSize(): [number, number] {
140
+ return [this.extends.config.width || 0, this.extends.config.height || 0];
213
141
  }
214
142
 
215
- public viewport2Canvas(viewport: PointLike) {
216
- 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);
217
147
  }
218
148
 
219
- public client2Viewport(client: PointLike) {
220
- return this.main.client2Viewport(client);
221
- }
149
+ public setRenderer(renderer: any) {
150
+ if (renderer === this.extends.renderer) return;
151
+ const renderers = createRenderers(renderer);
152
+ this.extends.renderers = renderers;
222
153
 
223
- public canvas2Viewport(canvas: PointLike) {
224
- 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
+ });
225
158
  }
226
159
 
227
160
  public async toDataURL(options: Partial<DataURLOptions> = {}) {
228
161
  const devicePixelRatio = window.devicePixelRatio || 1;
229
162
  const { mode = 'viewport', ...restOptions } = options;
230
-
231
- let startX = 0;
232
- let startY = 0;
233
- let width = 0;
234
- let height = 0;
163
+ let [startX, startY, width, height] = [0, 0, 0, 0];
235
164
 
236
165
  if (mode === 'viewport') {
237
- [width, height] = [this.config.width || 0, this.config.height || 0];
166
+ [width, height] = this.getSize();
238
167
  } else if (mode === 'overall') {
239
168
  const bounds = this.getBounds();
240
169
  const size = getBBoxSize(bounds);
@@ -250,28 +179,28 @@ export class Canvas {
250
179
  renderer: new CanvasRenderer(),
251
180
  devicePixelRatio,
252
181
  container,
253
- background: this.config.background,
182
+ background: this.extends.config.background,
254
183
  });
255
184
 
256
185
  await offscreenCanvas.ready;
257
186
 
258
- offscreenCanvas.appendChild(this.background.getRoot().cloneNode(true));
259
- offscreenCanvas.appendChild(this.main.getRoot().cloneNode(true));
187
+ offscreenCanvas.appendChild(this.getLayer('background').getRoot().cloneNode(true));
188
+ offscreenCanvas.appendChild(this.getRoot().cloneNode(true));
260
189
 
261
190
  // Handle label canvas
262
- const label = this.label.getRoot().cloneNode(true);
191
+ const label = this.getLayer('label').getRoot().cloneNode(true);
263
192
  const originCanvasPosition = offscreenCanvas.viewport2Canvas({ x: 0, y: 0 });
264
- const currentCanvasPosition = this.main.viewport2Canvas({ x: 0, y: 0 });
193
+ const currentCanvasPosition = this.viewport2Canvas({ x: 0, y: 0 });
265
194
  label.translate([
266
195
  currentCanvasPosition.x - originCanvasPosition.x,
267
196
  currentCanvasPosition.y - originCanvasPosition.y,
268
197
  ]);
269
- label.scale(1 / this.main.getCamera().getZoom());
198
+ label.scale(1 / this.getCamera().getZoom());
270
199
  offscreenCanvas.appendChild(label);
271
200
 
272
- offscreenCanvas.appendChild(this.transient.getRoot().cloneNode(true));
201
+ offscreenCanvas.appendChild(this.getLayer('transient').getRoot().cloneNode(true));
273
202
 
274
- const camera = this.main.getCamera();
203
+ const camera = this.getCamera();
275
204
  const offscreenCamera = offscreenCanvas.getCamera();
276
205
 
277
206
  if (mode === 'viewport') {
@@ -288,7 +217,7 @@ export class Canvas {
288
217
  const contextService = offscreenCanvas.getContextService();
289
218
 
290
219
  return new Promise<string>((resolve) => {
291
- offscreenCanvas.on(CanvasEvent.RERENDER, async () => {
220
+ offscreenCanvas.addEventListener(CanvasEvent.RERENDER, async () => {
292
221
  // 等待图片渲染完成 / Wait for the image to render
293
222
  await new Promise((r) => setTimeout(r, 300));
294
223
  const url = await contextService.toDataURL(restOptions);
@@ -297,57 +226,60 @@ export class Canvas {
297
226
  });
298
227
  }
299
228
 
300
- public destroy() {
301
- this.config = {};
302
- // @ts-expect-error force delete
303
- this.renderers = {};
304
- Object.entries(this.canvas).forEach(([name, canvas]) => {
229
+ public destroy(cleanUp?: boolean, skipTriggerEvent?: boolean) {
230
+ Object.values(this.getLayers()).forEach((canvas) => {
305
231
  const camera = canvas.getCamera();
306
- // @ts-expect-error landmark is private
307
- if (camera.landmarks?.length) {
308
- camera.cancelLandmarkAnimation();
309
- }
310
-
311
- destroyCanvas(canvas);
312
- // @ts-expect-error force delete
313
- this[name] = undefined;
232
+ camera.cancelLandmarkAnimation();
233
+ if (canvas === this) super.destroy(cleanUp, skipTriggerEvent);
234
+ else canvas.destroy(cleanUp, skipTriggerEvent);
314
235
  });
315
236
  }
316
237
  }
317
238
 
318
239
  /**
319
- * <zh/> G Canvas destroy 未处理动画对象,导致内存泄漏
240
+ * <zh/> 创建渲染器
320
241
  *
321
- * <en/> G Canvas destroy does not handle animation objects, causing memory leaks
322
- * @param canvas GCanvas
323
- * @remarks
324
- * <zh/> 这些操作都应该在 G 中完成,这里只是一个临时的解决方案
325
- * 此操作大概能在测试环节降低 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
326
271
  *
327
- * <en/> These operations should be completed in G, this is just a temporary solution
328
- * 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
329
274
  */
330
- function destroyCanvas(canvas: GCanvas) {
331
- canvas.destroy();
332
-
333
- // 移除相机事件 / Remove camera events
334
- const camera = canvas.getCamera();
335
- camera.eventEmitter.removeAllListeners();
336
-
337
- canvas.document.timeline.destroy();
338
- // @ts-expect-error private property
339
- const { animationsWithPromises } = canvas.document.timeline;
340
- // 释放 target 对象图形 / Release target object graphics
341
- animationsWithPromises.forEach((animation: IAnimation) => {
342
- if (animation.effect.target) animation.effect.target = null;
343
- // @ts-expect-error private property
344
- if (animation.effect.computedTiming) animation.effect.computedTiming = null;
345
- });
275
+ function configCanvasDom(layers: Record<CanvasLayer, GCanvas>) {
276
+ Object.entries(layers).forEach(([layer, canvas]) => {
277
+ const domElement = canvas.getContextService().getDomElement() as unknown as HTMLElement;
346
278
 
347
- // @ts-expect-error private property
348
- canvas.document.timeline.animationsWithPromises = [];
349
- // @ts-expect-error private property
350
- canvas.document.timeline.rafCallbacks = [];
351
- // @ts-expect-error private property
352
- 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
+ });
353
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;