@antv/g6 5.0.0-beta.31 → 5.0.0-beta.32

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 (264) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/dist/lib/behaviors/collapse-expand.d.ts +4 -5
  3. package/dist/lib/behaviors/create-edge.d.ts +55 -0
  4. package/dist/lib/behaviors/drag-canvas.d.ts +6 -6
  5. package/dist/lib/behaviors/drag-element.d.ts +5 -3
  6. package/dist/lib/behaviors/focus-element.d.ts +4 -3
  7. package/dist/lib/behaviors/hover-element.d.ts +4 -5
  8. package/dist/lib/behaviors/index.d.ts +2 -0
  9. package/dist/lib/behaviors/zoom-canvas.d.ts +7 -6
  10. package/dist/lib/constants/events/graph.d.ts +5 -13
  11. package/dist/lib/constants/index.d.ts +1 -0
  12. package/dist/lib/constants/registry.d.ts +20 -0
  13. package/dist/lib/elements/combos/base-combo.d.ts +21 -0
  14. package/dist/lib/elements/edges/base-edge.d.ts +21 -0
  15. package/dist/lib/elements/nodes/base-node.d.ts +22 -1
  16. package/dist/lib/exports.d.ts +16 -14
  17. package/dist/lib/plugins/camera-setting.d.ts +95 -0
  18. package/dist/lib/plugins/contextmenu.d.ts +8 -5
  19. package/dist/lib/plugins/index.d.ts +4 -0
  20. package/dist/lib/plugins/legend.d.ts +49 -0
  21. package/dist/lib/plugins/tooltip.d.ts +12 -10
  22. package/dist/lib/registry/extension/index.d.ts +2 -2
  23. package/dist/lib/registry/index.d.ts +6 -4
  24. package/dist/lib/registry/types.d.ts +0 -6
  25. package/dist/lib/runtime/canvas.d.ts +2 -1
  26. package/dist/lib/runtime/data.d.ts +1 -1
  27. package/dist/lib/runtime/element.d.ts +4 -5
  28. package/dist/lib/runtime/graph.d.ts +31 -4
  29. package/dist/lib/runtime/plugin.d.ts +1 -0
  30. package/dist/lib/types/event.d.ts +42 -7
  31. package/dist/lib/types/index.d.ts +0 -1
  32. package/dist/lib/utils/bbox.d.ts +1 -1
  33. package/dist/lib/utils/element.d.ts +19 -11
  34. package/dist/lib/utils/event/events.d.ts +8 -13
  35. package/dist/lib/utils/shortcut.d.ts +10 -0
  36. package/esm/behaviors/collapse-expand.d.ts +4 -5
  37. package/esm/behaviors/collapse-expand.js +8 -4
  38. package/esm/behaviors/collapse-expand.js.map +1 -1
  39. package/esm/behaviors/create-edge.d.ts +55 -0
  40. package/esm/behaviors/create-edge.js +141 -0
  41. package/esm/behaviors/create-edge.js.map +1 -0
  42. package/esm/behaviors/drag-canvas.d.ts +6 -6
  43. package/esm/behaviors/drag-canvas.js +15 -7
  44. package/esm/behaviors/drag-canvas.js.map +1 -1
  45. package/esm/behaviors/drag-element.d.ts +5 -3
  46. package/esm/behaviors/drag-element.js.map +1 -1
  47. package/esm/behaviors/focus-element.d.ts +4 -3
  48. package/esm/behaviors/focus-element.js.map +1 -1
  49. package/esm/behaviors/hover-element.d.ts +4 -5
  50. package/esm/behaviors/hover-element.js.map +1 -1
  51. package/esm/behaviors/index.d.ts +2 -0
  52. package/esm/behaviors/index.js +1 -0
  53. package/esm/behaviors/index.js.map +1 -1
  54. package/esm/behaviors/zoom-canvas.d.ts +7 -6
  55. package/esm/behaviors/zoom-canvas.js +6 -2
  56. package/esm/behaviors/zoom-canvas.js.map +1 -1
  57. package/esm/constants/events/graph.d.ts +5 -13
  58. package/esm/constants/events/graph.js +4 -12
  59. package/esm/constants/events/graph.js.map +1 -1
  60. package/esm/constants/index.d.ts +1 -0
  61. package/esm/constants/index.js +1 -0
  62. package/esm/constants/index.js.map +1 -1
  63. package/esm/constants/registry.d.ts +20 -0
  64. package/esm/constants/registry.js +22 -0
  65. package/esm/constants/registry.js.map +1 -0
  66. package/esm/elements/combos/base-combo.d.ts +21 -0
  67. package/esm/elements/combos/base-combo.js +21 -0
  68. package/esm/elements/combos/base-combo.js.map +1 -1
  69. package/esm/elements/edges/base-edge.d.ts +21 -0
  70. package/esm/elements/edges/base-edge.js +21 -0
  71. package/esm/elements/edges/base-edge.js.map +1 -1
  72. package/esm/elements/nodes/base-node.d.ts +22 -1
  73. package/esm/elements/nodes/base-node.js +21 -0
  74. package/esm/elements/nodes/base-node.js.map +1 -1
  75. package/esm/exports.d.ts +16 -14
  76. package/esm/exports.js +7 -6
  77. package/esm/exports.js.map +1 -1
  78. package/esm/plugins/camera-setting.d.ts +95 -0
  79. package/esm/plugins/camera-setting.js +65 -0
  80. package/esm/plugins/camera-setting.js.map +1 -0
  81. package/esm/plugins/contextmenu.d.ts +8 -5
  82. package/esm/plugins/contextmenu.js.map +1 -1
  83. package/esm/plugins/index.d.ts +4 -0
  84. package/esm/plugins/index.js +2 -0
  85. package/esm/plugins/index.js.map +1 -1
  86. package/esm/plugins/legend.d.ts +49 -0
  87. package/esm/plugins/legend.js +267 -0
  88. package/esm/plugins/legend.js.map +1 -0
  89. package/esm/plugins/tooltip.d.ts +12 -10
  90. package/esm/plugins/tooltip.js +0 -1
  91. package/esm/plugins/tooltip.js.map +1 -1
  92. package/esm/preset.js +2 -5
  93. package/esm/preset.js.map +1 -1
  94. package/esm/registry/build-in.js +4 -2
  95. package/esm/registry/build-in.js.map +1 -1
  96. package/esm/registry/extension/index.d.ts +2 -2
  97. package/esm/registry/extension/index.js.map +1 -1
  98. package/esm/registry/index.d.ts +6 -4
  99. package/esm/registry/index.js.map +1 -1
  100. package/esm/registry/types.d.ts +0 -6
  101. package/esm/runtime/behavior.js +3 -2
  102. package/esm/runtime/behavior.js.map +1 -1
  103. package/esm/runtime/canvas.d.ts +2 -1
  104. package/esm/runtime/canvas.js +68 -30
  105. package/esm/runtime/canvas.js.map +1 -1
  106. package/esm/runtime/data.d.ts +1 -1
  107. package/esm/runtime/element.d.ts +4 -5
  108. package/esm/runtime/element.js +32 -13
  109. package/esm/runtime/element.js.map +1 -1
  110. package/esm/runtime/graph.d.ts +31 -4
  111. package/esm/runtime/graph.js +48 -9
  112. package/esm/runtime/graph.js.map +1 -1
  113. package/esm/runtime/plugin.d.ts +1 -0
  114. package/esm/runtime/plugin.js +3 -0
  115. package/esm/runtime/plugin.js.map +1 -1
  116. package/esm/types/event.d.ts +42 -7
  117. package/esm/types/index.d.ts +0 -1
  118. package/esm/utils/bbox.d.ts +1 -1
  119. package/esm/utils/element.d.ts +19 -11
  120. package/esm/utils/element.js +19 -9
  121. package/esm/utils/element.js.map +1 -1
  122. package/esm/utils/event/events.d.ts +8 -13
  123. package/esm/utils/event/events.js +3 -8
  124. package/esm/utils/event/events.js.map +1 -1
  125. package/esm/utils/shortcut.d.ts +10 -0
  126. package/esm/utils/shortcut.js +24 -3
  127. package/esm/utils/shortcut.js.map +1 -1
  128. package/lib/behaviors/collapse-expand.d.ts +4 -5
  129. package/lib/behaviors/collapse-expand.js +8 -4
  130. package/lib/behaviors/collapse-expand.js.map +1 -1
  131. package/lib/behaviors/create-edge.d.ts +55 -0
  132. package/lib/behaviors/create-edge.js +145 -0
  133. package/lib/behaviors/create-edge.js.map +1 -0
  134. package/lib/behaviors/drag-canvas.d.ts +6 -6
  135. package/lib/behaviors/drag-canvas.js +15 -7
  136. package/lib/behaviors/drag-canvas.js.map +1 -1
  137. package/lib/behaviors/drag-element.d.ts +5 -3
  138. package/lib/behaviors/drag-element.js.map +1 -1
  139. package/lib/behaviors/focus-element.d.ts +4 -3
  140. package/lib/behaviors/focus-element.js.map +1 -1
  141. package/lib/behaviors/hover-element.d.ts +4 -5
  142. package/lib/behaviors/hover-element.js.map +1 -1
  143. package/lib/behaviors/index.d.ts +2 -0
  144. package/lib/behaviors/index.js +3 -1
  145. package/lib/behaviors/index.js.map +1 -1
  146. package/lib/behaviors/zoom-canvas.d.ts +7 -6
  147. package/lib/behaviors/zoom-canvas.js +6 -2
  148. package/lib/behaviors/zoom-canvas.js.map +1 -1
  149. package/lib/constants/events/graph.d.ts +5 -13
  150. package/lib/constants/events/graph.js +4 -12
  151. package/lib/constants/events/graph.js.map +1 -1
  152. package/lib/constants/index.d.ts +1 -0
  153. package/lib/constants/index.js +1 -0
  154. package/lib/constants/index.js.map +1 -1
  155. package/lib/constants/registry.d.ts +20 -0
  156. package/lib/constants/registry.js +25 -0
  157. package/lib/constants/registry.js.map +1 -0
  158. package/lib/elements/combos/base-combo.d.ts +21 -0
  159. package/lib/elements/combos/base-combo.js +21 -0
  160. package/lib/elements/combos/base-combo.js.map +1 -1
  161. package/lib/elements/edges/base-edge.d.ts +21 -0
  162. package/lib/elements/edges/base-edge.js +21 -0
  163. package/lib/elements/edges/base-edge.js.map +1 -1
  164. package/lib/elements/nodes/base-node.d.ts +22 -1
  165. package/lib/elements/nodes/base-node.js +21 -0
  166. package/lib/elements/nodes/base-node.js.map +1 -1
  167. package/lib/exports.d.ts +16 -14
  168. package/lib/exports.js +23 -1
  169. package/lib/exports.js.map +1 -1
  170. package/lib/plugins/camera-setting.d.ts +95 -0
  171. package/lib/plugins/camera-setting.js +69 -0
  172. package/lib/plugins/camera-setting.js.map +1 -0
  173. package/lib/plugins/contextmenu.d.ts +8 -5
  174. package/lib/plugins/contextmenu.js.map +1 -1
  175. package/lib/plugins/index.d.ts +4 -0
  176. package/lib/plugins/index.js +5 -1
  177. package/lib/plugins/index.js.map +1 -1
  178. package/lib/plugins/legend.d.ts +49 -0
  179. package/lib/plugins/legend.js +271 -0
  180. package/lib/plugins/legend.js.map +1 -0
  181. package/lib/plugins/tooltip.d.ts +12 -10
  182. package/lib/plugins/tooltip.js +0 -1
  183. package/lib/plugins/tooltip.js.map +1 -1
  184. package/lib/preset.js +2 -5
  185. package/lib/preset.js.map +1 -1
  186. package/lib/registry/build-in.js +2 -0
  187. package/lib/registry/build-in.js.map +1 -1
  188. package/lib/registry/extension/index.d.ts +2 -2
  189. package/lib/registry/extension/index.js.map +1 -1
  190. package/lib/registry/index.d.ts +6 -4
  191. package/lib/registry/index.js.map +1 -1
  192. package/lib/registry/types.d.ts +0 -6
  193. package/lib/runtime/behavior.js +3 -2
  194. package/lib/runtime/behavior.js.map +1 -1
  195. package/lib/runtime/canvas.d.ts +2 -1
  196. package/lib/runtime/canvas.js +68 -30
  197. package/lib/runtime/canvas.js.map +1 -1
  198. package/lib/runtime/data.d.ts +1 -1
  199. package/lib/runtime/element.d.ts +4 -5
  200. package/lib/runtime/element.js +31 -12
  201. package/lib/runtime/element.js.map +1 -1
  202. package/lib/runtime/graph.d.ts +31 -4
  203. package/lib/runtime/graph.js +47 -8
  204. package/lib/runtime/graph.js.map +1 -1
  205. package/lib/runtime/plugin.d.ts +1 -0
  206. package/lib/runtime/plugin.js +3 -0
  207. package/lib/runtime/plugin.js.map +1 -1
  208. package/lib/types/event.d.ts +42 -7
  209. package/lib/types/index.d.ts +0 -1
  210. package/lib/utils/bbox.d.ts +1 -1
  211. package/lib/utils/element.d.ts +19 -11
  212. package/lib/utils/element.js +21 -10
  213. package/lib/utils/element.js.map +1 -1
  214. package/lib/utils/event/events.d.ts +8 -13
  215. package/lib/utils/event/events.js +4 -10
  216. package/lib/utils/event/events.js.map +1 -1
  217. package/lib/utils/shortcut.d.ts +10 -0
  218. package/lib/utils/shortcut.js +24 -3
  219. package/lib/utils/shortcut.js.map +1 -1
  220. package/package.json +2 -2
  221. package/src/behaviors/collapse-expand.ts +13 -11
  222. package/src/behaviors/create-edge.ts +206 -0
  223. package/src/behaviors/drag-canvas.ts +22 -14
  224. package/src/behaviors/drag-element.ts +8 -11
  225. package/src/behaviors/focus-element.ts +7 -6
  226. package/src/behaviors/hover-element.ts +14 -10
  227. package/src/behaviors/index.ts +2 -0
  228. package/src/behaviors/zoom-canvas.ts +14 -9
  229. package/src/constants/events/graph.ts +4 -12
  230. package/src/constants/index.ts +1 -0
  231. package/src/constants/registry.ts +20 -0
  232. package/src/elements/combos/base-combo.ts +24 -0
  233. package/src/elements/edges/base-edge.ts +25 -1
  234. package/src/elements/nodes/base-node.ts +25 -1
  235. package/src/exports.ts +55 -12
  236. package/src/plugins/camera-setting.ts +153 -0
  237. package/src/plugins/contextmenu.ts +10 -7
  238. package/src/plugins/index.ts +4 -0
  239. package/src/plugins/legend.ts +321 -0
  240. package/src/plugins/tooltip.ts +16 -15
  241. package/src/preset.ts +2 -6
  242. package/src/registry/build-in.ts +12 -2
  243. package/src/registry/extension/index.ts +4 -4
  244. package/src/registry/index.ts +10 -4
  245. package/src/registry/types.ts +0 -7
  246. package/src/runtime/behavior.ts +3 -2
  247. package/src/runtime/canvas.ts +80 -40
  248. package/src/runtime/element.ts +52 -20
  249. package/src/runtime/graph.ts +56 -12
  250. package/src/runtime/plugin.ts +4 -0
  251. package/src/types/event.ts +62 -7
  252. package/src/types/index.ts +0 -1
  253. package/src/utils/bbox.ts +1 -1
  254. package/src/utils/element.ts +22 -11
  255. package/src/utils/event/events.ts +15 -17
  256. package/src/utils/shortcut.ts +28 -3
  257. package/dist/lib/types/behavior.d.ts +0 -5
  258. package/esm/types/behavior.d.ts +0 -5
  259. package/esm/types/behavior.js +0 -2
  260. package/esm/types/behavior.js.map +0 -1
  261. package/lib/types/behavior.d.ts +0 -5
  262. package/lib/types/behavior.js +0 -3
  263. package/lib/types/behavior.js.map +0 -1
  264. package/src/types/behavior.ts +0 -6
@@ -1,5 +1,13 @@
1
1
  import { comboCollapseExpand, fade, translate } from '../animations';
2
- import { CollapseExpand, DragCanvas, DragElement, FocusElement, HoverElement, ZoomCanvas } from '../behaviors';
2
+ import {
3
+ CollapseExpand,
4
+ CreateEdge,
5
+ DragCanvas,
6
+ DragElement,
7
+ FocusElement,
8
+ HoverElement,
9
+ ZoomCanvas,
10
+ } from '../behaviors';
3
11
  import {
4
12
  Circle,
5
13
  CircleCombo,
@@ -38,7 +46,7 @@ import {
38
46
  mindmap,
39
47
  } from '../layouts';
40
48
  import { blues, greens, oranges, spectral } from '../palettes';
41
- import { Contextmenu, GridLine, Toolbar, Tooltip, Watermark } from '../plugins';
49
+ import { Contextmenu, GridLine, Legend, Toolbar, Tooltip, Watermark } from '../plugins';
42
50
  import { dark, light } from '../themes';
43
51
  import type { ExtensionRegistry } from './types';
44
52
 
@@ -59,6 +67,7 @@ export const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
59
67
  'collapse-expand': CollapseExpand,
60
68
  'hover-element': HoverElement,
61
69
  'focus-element': FocusElement,
70
+ 'create-edge': CreateEdge,
62
71
  },
63
72
  combo: {
64
73
  circle: CircleCombo,
@@ -117,5 +126,6 @@ export const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
117
126
  tooltip: Tooltip,
118
127
  contextmenu: Contextmenu,
119
128
  toolbar: Toolbar,
129
+ legend: Legend,
120
130
  },
121
131
  };
@@ -1,7 +1,7 @@
1
1
  import type EventEmitter from '@antv/event-emitter';
2
2
  import { getExtension } from '..';
3
3
  import type { RuntimeContext } from '../../runtime/types';
4
- import type { Listener } from '../../types';
4
+ import type { IEvent } from '../../types';
5
5
  import { arrayDiff } from '../../utils/diff';
6
6
  import { parseExtensions } from '../../utils/extension';
7
7
  import type { ExtensionOptions, LooselyExtensionOption, STDExtensionOption } from './types';
@@ -34,11 +34,11 @@ export abstract class ExtensionController<Extension extends BaseExtension<Loosel
34
34
  const { category } = this;
35
35
 
36
36
  const { key, type } = extension;
37
- const Ctor = getExtension(category as any, type);
37
+ const Ctor = getExtension(category, type);
38
38
  if (!Ctor) return;
39
39
 
40
40
  const instance = new Ctor(this.context, extension);
41
- this.extensionMap[key] = instance;
41
+ this.extensionMap[key] = instance as Extension;
42
42
  }
43
43
 
44
44
  protected createExtensions(extensions: STDExtensionOption[]) {
@@ -88,7 +88,7 @@ export class BaseExtension<T extends LooselyExtensionOption> {
88
88
 
89
89
  protected options: Required<T>;
90
90
 
91
- protected events: [EventEmitter | HTMLElement, string, Listener][] = [];
91
+ protected events: [EventEmitter | HTMLElement, string, (event: IEvent) => void][] = [];
92
92
 
93
93
  public destroyed = false;
94
94
 
@@ -1,5 +1,7 @@
1
+ import { ExtensionCategory } from '../constants';
2
+ import type { Loosen } from '../types';
1
3
  import { BUILT_IN_EXTENSIONS } from './build-in';
2
- import type { ExtensionCategory, ExtensionRegistry } from './types';
4
+ import type { ExtensionRegistry } from './types';
3
5
 
4
6
  /**
5
7
  * <zh/> 扩展注册表
@@ -48,7 +50,11 @@ const EXTENSION_REGISTRY: ExtensionRegistry = {
48
50
  * ```
49
51
  * @public
50
52
  */
51
- export function register<T extends ExtensionCategory>(category: T, type: string, Ctor: ExtensionRegistry[T][string]) {
53
+ export function register<T extends ExtensionCategory>(
54
+ category: Loosen<T>,
55
+ type: string,
56
+ Ctor: ExtensionRegistry[T][string],
57
+ ) {
52
58
  if (EXTENSION_REGISTRY[category]![type]) {
53
59
  console.error(`The extension ${type} of ${category} has been registered before.`);
54
60
  return;
@@ -66,7 +72,7 @@ export function register<T extends ExtensionCategory>(category: T, type: string,
66
72
  * @internal
67
73
  */
68
74
  export function getExtension<T extends ExtensionCategory>(
69
- category: T,
75
+ category: Loosen<T>,
70
76
  type: string,
71
77
  ): ExtensionRegistry[T][string] | undefined {
72
78
  const extension = EXTENSION_REGISTRY[category]?.[type];
@@ -85,7 +91,7 @@ export function getExtension<T extends ExtensionCategory>(
85
91
  * @returns <zh/> 注册的扩展 | <en/> Registered extension
86
92
  * @internal
87
93
  */
88
- export function getExtensions<T extends ExtensionCategory>(category: T): ExtensionRegistry[T] {
94
+ export function getExtensions<T extends Loosen<ExtensionCategory>>(category: T): ExtensionRegistry[T] {
89
95
  return EXTENSION_REGISTRY[category];
90
96
  }
91
97
 
@@ -22,10 +22,3 @@ export interface ExtensionRegistry {
22
22
  plugin: Record<string, { new (...args: any[]): Plugin }>;
23
23
  animation: Record<string, STDAnimation>; // animation spec
24
24
  }
25
-
26
- /**
27
- * <zh/> 扩展分类
28
- *
29
- * <en/> Extension category
30
- */
31
- export type ExtensionCategory = keyof ExtensionRegistry;
@@ -59,12 +59,13 @@ export class BehaviorController extends ExtensionController<BaseBehavior<CustomB
59
59
  }
60
60
 
61
61
  private forwardCanvasEvents = (event: FederatedPointerEvent | FederatedWheelEvent) => {
62
- const target = eventTargetOf(event.target as DisplayObject);
62
+ const { target: originalTarget } = event;
63
+ const target = eventTargetOf(originalTarget as DisplayObject);
63
64
  if (!target) return;
64
65
  const { graph, canvas } = this.context;
65
66
  const { type: targetType, element: targetElement } = target;
66
67
  const { type, detail, button } = event;
67
- const stdEvent = { ...event, target: targetElement, targetType };
68
+ const stdEvent = { ...event, target: targetElement, targetType, originalTarget };
68
69
 
69
70
  if (type === CanvasEvent.POINTER_MOVE) {
70
71
  if (this.currentTarget !== targetElement) {
@@ -3,6 +3,7 @@ import type {
3
3
  DataURLOptions,
4
4
  DisplayObject,
5
5
  CanvasConfig as GCanvasConfig,
6
+ IAnimation,
6
7
  IRenderer,
7
8
  PointLike,
8
9
  } from '@antv/g';
@@ -48,21 +49,23 @@ export class Canvas {
48
49
 
49
50
  public renderers!: Record<CanvasLayer, IRenderer>;
50
51
 
52
+ private initialized = false;
53
+
51
54
  constructor(config: CanvasConfig) {
52
55
  this.config = config;
53
56
  }
54
57
 
55
58
  public async init() {
56
- const allCanvas = Object.entries(this.canvas);
59
+ if (this.initialized) return;
57
60
 
58
- if (allCanvas.every(([, canvas]) => !canvas)) {
59
- const { renderer: getRenderer, background, ...restConfig } = this.config;
60
- const names: CanvasLayer[] = ['main', 'label', 'transient', 'transientLabel', 'background'];
61
+ const { renderer: getRenderer, background, ...restConfig } = this.config;
62
+ const names: CanvasLayer[] = ['main', 'label', 'transient', 'transientLabel', 'background'];
61
63
 
62
- const { renderers, canvas } = names.reduce(
63
- (acc, name) => {
64
- const renderer = isFunction(getRenderer) ? getRenderer?.(name) : new CanvasRenderer();
64
+ const { renderers, canvas } = names.reduce(
65
+ (acc, name) => {
66
+ const renderer = isFunction(getRenderer) ? getRenderer?.(name) : new CanvasRenderer();
65
67
 
68
+ if (name === 'main') {
66
69
  renderer.registerPlugin(
67
70
  new DragNDropPlugin({
68
71
  isDocumentDraggable: true,
@@ -71,45 +74,45 @@ export class Canvas {
71
74
  dragstartTimeThreshold: 100,
72
75
  }),
73
76
  );
77
+ } else {
78
+ renderer.unregisterPlugin(renderer.getPlugin('dom-interaction'));
79
+ }
80
+
81
+ const canvas = new GCanvas({
82
+ renderer,
83
+ supportsMutipleCanvasesInOneContainer: true,
84
+ ...restConfig,
85
+ });
86
+
87
+ acc.renderers[name] = renderer;
88
+ acc.canvas[name] = canvas;
89
+ this[name] = canvas;
90
+
91
+ return acc;
92
+ },
93
+ { renderers: {}, canvas: {} } as {
94
+ renderers: Record<CanvasLayer, IRenderer>;
95
+ canvas: Record<CanvasLayer, GCanvas>;
96
+ },
97
+ );
74
98
 
75
- if (name !== 'main') {
76
- renderer.unregisterPlugin(renderer.getPlugin('dom-interaction'));
77
- }
78
-
79
- const canvas = new GCanvas({
80
- renderer,
81
- supportsMutipleCanvasesInOneContainer: true,
82
- ...restConfig,
83
- });
84
-
85
- acc.renderers[name] = renderer;
86
- acc.canvas[name] = canvas;
87
- this[name] = canvas;
88
-
89
- return acc;
90
- },
91
- { renderers: {}, canvas: {} } as {
92
- renderers: Record<CanvasLayer, IRenderer>;
93
- canvas: Record<CanvasLayer, GCanvas>;
94
- },
95
- );
96
-
97
- this.renderers = renderers;
99
+ this.renderers = renderers;
98
100
 
99
- Object.entries(canvas).forEach(([name, canvas]) => {
100
- const domElement = canvas.getContextService().getDomElement() as unknown as HTMLElement;
101
+ Object.entries(canvas).forEach(([name, canvas]) => {
102
+ const domElement = canvas.getContextService().getDomElement() as unknown as HTMLElement;
101
103
 
102
- domElement.style.position = 'absolute';
103
- domElement.style.outline = 'none';
104
- domElement.tabIndex = 1;
104
+ domElement.style.position = 'absolute';
105
+ domElement.style.outline = 'none';
106
+ domElement.tabIndex = 1;
105
107
 
106
- if (name !== 'main') domElement.style.pointerEvents = 'none';
107
- });
108
- }
108
+ if (name !== 'main') domElement.style.pointerEvents = 'none';
109
+ });
109
110
 
110
111
  this.setBackground();
111
112
 
112
- return Promise.all(Object.values(this.canvas).map((canvas) => canvas.ready));
113
+ await Promise.all(Object.values(this.canvas).map((canvas) => canvas.ready));
114
+
115
+ this.initialized = true;
113
116
  }
114
117
 
115
118
  public getRendererType(layer: CanvasLayer = 'main') {
@@ -263,9 +266,46 @@ export class Canvas {
263
266
  camera.cancelLandmarkAnimation();
264
267
  }
265
268
 
266
- canvas.destroy();
269
+ destroyCanvas(canvas);
267
270
  // @ts-expect-error force delete
268
271
  this[name] = undefined;
269
272
  });
270
273
  }
271
274
  }
275
+
276
+ /**
277
+ * <zh/> G Canvas destroy 未处理动画对象,导致内存泄漏
278
+ *
279
+ * <en/> G Canvas destroy does not handle animation objects, causing memory leaks
280
+ * @param canvas GCanvas
281
+ * @description
282
+ * <zh/> 这些操作都应该在 G 中完成,这里只是一个临时的解决方案
283
+ * 此操作大概能在测试环节降低 10~20% 的内存占用(从 2800MB 降低到 2200MB)
284
+ *
285
+ * <en/> These operations should be completed in G, this is just a temporary solution
286
+ * This operation can reduce memory usage by 10-20% in the test environment (from 2800MB to 2200MB)
287
+ */
288
+ function destroyCanvas(canvas: GCanvas) {
289
+ canvas.destroy();
290
+
291
+ // 移除相机事件 / Remove camera events
292
+ const camera = canvas.getCamera();
293
+ camera.eventEmitter.removeAllListeners();
294
+
295
+ canvas.document.timeline.destroy();
296
+ // @ts-expect-error private property
297
+ const { animationsWithPromises } = canvas.document.timeline;
298
+ // 释放 target 对象图形 / Release target object graphics
299
+ animationsWithPromises.forEach((animation: IAnimation) => {
300
+ if (animation.effect.target) animation.effect.target = null;
301
+ // @ts-expect-error private property
302
+ if (animation.effect.computedTiming) animation.effect.computedTiming = null;
303
+ });
304
+
305
+ // @ts-expect-error private property
306
+ canvas.document.timeline.animationsWithPromises = [];
307
+ // @ts-expect-error private property
308
+ canvas.document.timeline.rafCallbacks = [];
309
+ // @ts-expect-error private property
310
+ canvas.document.timeline = null;
311
+ }
@@ -8,8 +8,6 @@ import { executor as animationExecutor } from '../animations';
8
8
  import type { AnimationContext } from '../animations/types';
9
9
  import { AnimationType, ChangeTypeEnum, GraphEvent } from '../constants';
10
10
  import { ELEMENT_TYPES } from '../constants/element';
11
- import type { BaseNode } from '../elements/nodes';
12
- import type { BaseShape } from '../elements/shapes';
13
11
  import { getExtension } from '../registry';
14
12
  import type { ComboData, EdgeData, NodeData } from '../spec';
15
13
  import type { AnimationStage } from '../spec/element/animation';
@@ -18,6 +16,7 @@ import type {
18
16
  Combo,
19
17
  DataChange,
20
18
  Edge,
19
+ Element,
21
20
  ElementData,
22
21
  ElementDatum,
23
22
  ElementType,
@@ -33,7 +32,7 @@ import { reduceDataChanges } from '../utils/change';
33
32
  import { getSubgraphRelatedEdges } from '../utils/edge';
34
33
  import { updateStyle } from '../utils/element';
35
34
  import type { BaseEvent } from '../utils/event';
36
- import { AnimateEvent, GraphLifeCycleEvent } from '../utils/event';
35
+ import { AnimateEvent, ElementLifeCycleEvent, GraphLifeCycleEvent, emit } from '../utils/event';
37
36
  import { idOf } from '../utils/id';
38
37
  import { assignColorByPalette, parsePalette } from '../utils/palette';
39
38
  import { computeElementCallbackStyle } from '../utils/style';
@@ -48,7 +47,7 @@ export class ElementController {
48
47
  combo: Group;
49
48
  };
50
49
 
51
- private elementMap: Record<ID, DisplayObject> = {};
50
+ private elementMap: Record<ID, Element> = {};
52
51
 
53
52
  private shapeTypeMap: Record<ID, string> = {};
54
53
 
@@ -57,8 +56,8 @@ export class ElementController {
57
56
  }
58
57
 
59
58
  public init() {
60
- const { canvas } = this.context;
61
59
  if (!this.container) {
60
+ const { canvas } = this.context;
62
61
  this.container = {
63
62
  node: canvas.appendChild(new Group({ style: { zIndex: 2 } })),
64
63
  edge: canvas.appendChild(new Group({ style: { zIndex: 1 } })),
@@ -68,8 +67,7 @@ export class ElementController {
68
67
  }
69
68
 
70
69
  private emit(event: BaseEvent) {
71
- const { graph } = this.context;
72
- graph.emit(event.type, event);
70
+ emit(this.context.graph, event);
73
71
  }
74
72
 
75
73
  private forEachElementData(callback: (elementType: ElementType, elementData: ElementData) => void) {
@@ -210,7 +208,7 @@ export class ElementController {
210
208
  this.computeElementsStatesStyle(ids);
211
209
  }
212
210
 
213
- public getElement<T extends DisplayObject = BaseShape<any>>(id: ID): T | undefined {
211
+ public getElement<T extends Element>(id: ID): T | undefined {
214
212
  return this.elementMap[id] as T;
215
213
  }
216
214
 
@@ -284,8 +282,8 @@ export class ElementController {
284
282
  */
285
283
  private getEdgeEndsContext(datum: EdgeData) {
286
284
  const { source, target } = datum;
287
- const sourceNode = this.getElement<BaseNode>(source);
288
- const targetNode = this.getElement<BaseNode>(target);
285
+ const sourceNode = this.getElement<Node>(source);
286
+ const targetNode = this.getElement<Node>(target);
289
287
 
290
288
  return { sourceNode, targetNode };
291
289
  }
@@ -326,9 +324,10 @@ export class ElementController {
326
324
  * <en/> start render process
327
325
  */
328
326
  public async draw(drawContext: DrawContext = { animation: true }) {
327
+ this.init();
328
+
329
329
  const drawData = this.computeDrawData();
330
330
  if (!drawData) return;
331
- this.init();
332
331
 
333
332
  // 计算样式 / Calculate style
334
333
  this.computeStyle();
@@ -545,6 +544,9 @@ export class ElementController {
545
544
  // get shape constructor
546
545
  const Ctor = getExtension(elementType, type);
547
546
  if (!Ctor) return () => null;
547
+
548
+ this.emit(new ElementLifeCycleEvent(GraphEvent.BEFORE_ELEMENT_CREATE, elementType, datum));
549
+
548
550
  const shape = this.container[elementType].appendChild(
549
551
  new Ctor({
550
552
  id,
@@ -553,12 +555,18 @@ export class ElementController {
553
555
  ...style,
554
556
  },
555
557
  }),
556
- ) as DisplayObject;
558
+ ) as Element;
557
559
 
558
560
  this.shapeTypeMap[id] = type;
559
561
  this.elementMap[id] = shape;
560
562
 
561
- return () => animator?.(id, shape, { ...shape.attributes, opacity: 0 }) || null;
563
+ return () =>
564
+ withAnimationCallbacks(animator?.(id, shape, { ...shape.attributes, opacity: 0 }) || null, {
565
+ after: () => {
566
+ this.emit(new ElementLifeCycleEvent(GraphEvent.AFTER_ELEMENT_CREATE, elementType, datum));
567
+ shape.onCreate();
568
+ },
569
+ });
562
570
  }
563
571
 
564
572
  private getCreateTasks(data: ProcedureData, context: Omit<DrawContext, 'animator'>): AnimatableTask[] {
@@ -572,7 +580,7 @@ export class ElementController {
572
580
  const tasks: AnimatableTask[] = [];
573
581
  iteration.forEach(([elementType, elementData]) => {
574
582
  if (elementData.size === 0) return;
575
- const animator = this.getAnimationExecutor(elementType, 'enter');
583
+ const animator = this.getAnimationExecutor(elementType, 'enter', context.animation);
576
584
  elementData.forEach((datum) =>
577
585
  tasks.push(() => this.createElement(elementType, datum, { ...context, animator })),
578
586
  );
@@ -606,14 +614,22 @@ export class ElementController {
606
614
  const id = idOf(datum);
607
615
  const shape = this.getElement(id);
608
616
  if (!shape) return () => null;
617
+
618
+ this.emit(new ElementLifeCycleEvent(GraphEvent.BEFORE_ELEMENT_UPDATE, elementType, datum));
619
+ const afterUpdate = () => {
620
+ this.emit(new ElementLifeCycleEvent(GraphEvent.AFTER_ELEMENT_UPDATE, elementType, datum));
621
+ shape.onUpdate();
622
+ };
623
+
609
624
  const { type, ...style } = this.getElementComputedStyle(elementType, datum);
610
625
 
611
626
  // 如果类型不同,需要先销毁原有元素,再创建新元素
612
627
  // If the type is different, you need to destroy the original element first, and then create a new element
613
628
  if (this.shapeTypeMap[id] !== type) {
614
629
  return () => {
615
- this.destroyElement(datum, { ...context, animation: false })();
630
+ this.destroyElement(elementType, datum, { ...context, animation: false })();
616
631
  this.createElement(elementType, datum, { ...context, animation: false })();
632
+ afterUpdate();
617
633
  return null;
618
634
  };
619
635
  }
@@ -629,7 +645,11 @@ export class ElementController {
629
645
  // show
630
646
  if (style.visibility !== 'hidden') {
631
647
  updateStyle(shape, { visibility: 'visible' });
632
- return () => animator?.(id, shape, { ...shape.attributes, opacity: 0 }, { opacity: originalOpacity }) || null;
648
+ return () =>
649
+ withAnimationCallbacks(
650
+ animator?.(id, shape, { ...shape.attributes, opacity: 0 }, { opacity: originalOpacity }) || null,
651
+ { after: afterUpdate },
652
+ );
633
653
  }
634
654
  // hide
635
655
  else if (style.visibility === 'hidden') {
@@ -637,7 +657,10 @@ export class ElementController {
637
657
  withAnimationCallbacks(
638
658
  animator?.(id, shape, { ...shape.attributes, opacity: originalOpacity }, { opacity: 0 }) || null,
639
659
  {
640
- after: () => updateStyle(shape, { visibility: this.latestElementVisibilityMap.get(shape) }),
660
+ after: () => {
661
+ updateStyle(shape, { visibility: this.latestElementVisibilityMap.get(shape) });
662
+ afterUpdate();
663
+ },
641
664
  },
642
665
  );
643
666
  }
@@ -646,7 +669,10 @@ export class ElementController {
646
669
  const originalStyle = { ...shape.attributes };
647
670
  updateStyle(shape, style);
648
671
 
649
- return () => animator?.(id, shape, originalStyle) || null;
672
+ return () =>
673
+ withAnimationCallbacks(animator?.(id, shape, originalStyle) || null, {
674
+ after: afterUpdate,
675
+ });
650
676
  }
651
677
 
652
678
  private getUpdateTasks(data: ProcedureData, context: Omit<DrawContext, 'animator'>): AnimatableTask[] {
@@ -670,18 +696,22 @@ export class ElementController {
670
696
  return tasks;
671
697
  }
672
698
 
673
- private destroyElement(datum: ElementDatum, context: DrawContext) {
699
+ private destroyElement(elementType: ElementType, datum: ElementDatum, context: DrawContext) {
674
700
  const { animator } = context;
675
701
  const id = idOf(datum);
676
702
  const element = this.elementMap[id];
677
703
  if (!element) return () => null;
678
704
 
705
+ this.emit(new ElementLifeCycleEvent(GraphEvent.BEFORE_ELEMENT_DESTROY, elementType, datum));
706
+
679
707
  return () => {
680
708
  const result = animator?.(id, element, { ...element.attributes }, { opacity: 0 }) || null;
681
709
  withAnimationCallbacks(result, {
682
710
  after: () => {
683
711
  this.clearElement(id);
684
712
  element.destroy();
713
+ element.onDestroy();
714
+ this.emit(new ElementLifeCycleEvent(GraphEvent.AFTER_ELEMENT_DESTROY, elementType, datum));
685
715
  },
686
716
  });
687
717
  return result;
@@ -701,7 +731,9 @@ export class ElementController {
701
731
  iteration.forEach(([elementType, elementData]) => {
702
732
  if (elementData.size === 0) return [];
703
733
  const animator = this.getAnimationExecutor(elementType, stage || 'exit', animation);
704
- elementData.forEach((datum) => tasks.push(() => this.destroyElement(datum, { ...context, animator })));
734
+ elementData.forEach((datum) =>
735
+ tasks.push(() => this.destroyElement(elementType, datum, { ...context, animator })),
736
+ );
705
737
  });
706
738
 
707
739
  // TODO 重新计算色板样式,如果是分组色板,则不需要重新计算
@@ -3,6 +3,7 @@ import type { AABB, BaseStyleProps, DataURLOptions } from '@antv/g';
3
3
  import type { ID } from '@antv/graphlib';
4
4
  import { debounce, isEqual, isFunction, isNumber, isObject, isString, omit } from '@antv/util';
5
5
  import { COMBO_KEY, GraphEvent } from '../constants';
6
+ import type { Plugin } from '../plugins/types';
6
7
  import { getExtension } from '../registry';
7
8
  import type {
8
9
  BehaviorOptions,
@@ -18,8 +19,8 @@ import type {
18
19
  PluginOptions,
19
20
  ThemeOptions,
20
21
  } from '../spec';
21
- import { UpdateBehaviorOption } from '../spec/behavior';
22
- import { UpdatePluginOption } from '../spec/plugin';
22
+ import type { UpdateBehaviorOption } from '../spec/behavior';
23
+ import type { UpdatePluginOption } from '../spec/plugin';
23
24
  import type {
24
25
  CallableValue,
25
26
  DataID,
@@ -27,6 +28,7 @@ import type {
27
28
  ElementDatum,
28
29
  ElementType,
29
30
  FitViewOptions,
31
+ IEvent,
30
32
  NodeLikeData,
31
33
  PartialEdgeData,
32
34
  PartialGraphData,
@@ -38,7 +40,7 @@ import type {
38
40
  ViewportAnimationEffectTiming,
39
41
  } from '../types';
40
42
  import { sizeOf } from '../utils/dom';
41
- import { ElementStateChangeEvent, GraphLifeCycleEvent, emit } from '../utils/event';
43
+ import { GraphLifeCycleEvent, emit } from '../utils/event';
42
44
  import { idOf } from '../utils/id';
43
45
  import { parsePoint, toPointObject } from '../utils/point';
44
46
  import { zIndexOf } from '../utils/style';
@@ -276,6 +278,17 @@ export class Graph extends EventEmitter {
276
278
  return this.options.plugins || [];
277
279
  }
278
280
 
281
+ /**
282
+ * <zh/> 获取插件实例
283
+ *
284
+ * <en/> Get plugin instance
285
+ * @param key - <zh/> 插件 key | <en/> plugin key
286
+ * @returns <zh/> 插件实例 | <en/> plugin instance
287
+ */
288
+ public getPluginInstance<T extends Plugin>(key: string) {
289
+ return this.context.plugin!.getPluginInstance(key) as unknown as T;
290
+ }
291
+
279
292
  public getData(): GraphData {
280
293
  return this.context.model.getData();
281
294
  }
@@ -417,26 +430,31 @@ export class Graph extends EventEmitter {
417
430
  return this.context.model.getElementDataByState(elementType, state);
418
431
  }
419
432
 
420
- private createCanvas() {
421
- if (this.context.canvas) return this.context.canvas;
433
+ private async initCanvas() {
434
+ if (this.context.canvas) return await this.context.canvas.init();
422
435
 
423
436
  const { container = 'container', width, height, renderer, background } = this.options;
424
437
 
425
438
  if (container instanceof Canvas) {
426
439
  this.context.canvas = container;
427
440
  container.setBackground(background);
441
+ await container.init();
428
442
  } else {
429
443
  const $container = isString(container) ? document.getElementById(container!) : container;
430
444
 
431
- this.context.canvas = new Canvas({
445
+ this.emit(GraphEvent.BEFORE_CANVAS_INIT, { container: $container, width, height });
446
+
447
+ const canvas = new Canvas({
432
448
  container: $container!,
433
449
  width,
434
450
  height,
435
451
  renderer,
436
452
  });
437
- }
438
453
 
439
- return this.context.canvas;
454
+ this.context.canvas = canvas;
455
+ await canvas.init();
456
+ this.emit(GraphEvent.AFTER_CANVAS_INIT, { canvas });
457
+ }
440
458
  }
441
459
 
442
460
  private createRuntime() {
@@ -451,7 +469,7 @@ export class Graph extends EventEmitter {
451
469
  private async prepare(): Promise<void> {
452
470
  if (this.destroyed) throw new Error('Graph has been destroyed');
453
471
 
454
- await this.createCanvas().init();
472
+ await this.initCanvas();
455
473
  this.createRuntime();
456
474
  }
457
475
 
@@ -941,17 +959,19 @@ export class Graph extends EventEmitter {
941
959
  ? [args1, (args2 as boolean) ?? true]
942
960
  : [{ [args1]: args2 as State | State[] }, args3];
943
961
 
944
- emit(this, new ElementStateChangeEvent(GraphEvent.BEFORE_ELEMENT_STATE_CHANGE, config));
962
+ const parseState = (state: State | State[]) => {
963
+ if (!state) return [];
964
+ return Array.isArray(state) ? state : [state];
965
+ };
945
966
 
946
967
  const dataToUpdate: Required<PartialGraphData> = { nodes: [], edges: [], combos: [] };
947
968
  Object.entries(config).forEach(([id, value]) => {
948
969
  const elementType = this.getElementType(id);
949
- dataToUpdate[`${elementType}s`].push({ id, style: { states: Array.isArray(value) ? value : [value] } });
970
+ dataToUpdate[`${elementType}s`].push({ id, style: { states: parseState(value) } });
950
971
  });
951
972
  this.updateData(dataToUpdate);
952
973
 
953
974
  await this.context.element!.draw({ animation });
954
- emit(this, new ElementStateChangeEvent(GraphEvent.AFTER_ELEMENT_STATE_CHANGE, config));
955
975
  }
956
976
 
957
977
  /**
@@ -1024,4 +1044,28 @@ export class Graph extends EventEmitter {
1024
1044
  private onResize = debounce(() => {
1025
1045
  this.resize();
1026
1046
  }, 300);
1047
+
1048
+ /**
1049
+ * <zh/> 监听事件
1050
+ *
1051
+ * <en/> Listen to events
1052
+ * @param eventName - <zh/> 事件名称 | <en/> event name
1053
+ * @param callback - <zh/> 回调函数 | <en/> callback function
1054
+ * @returns <zh/> Graph 实例 | <en/> Graph instance
1055
+ */
1056
+ public on<T extends IEvent = IEvent>(eventName: string, callback: (event: T) => void) {
1057
+ return super.on(eventName, callback);
1058
+ }
1059
+
1060
+ /**
1061
+ * <zh/> 一次性监听事件
1062
+ *
1063
+ * <en/> Listen to events once
1064
+ * @param eventName - <zh/> 事件名称 | <en/> event name
1065
+ * @param callback - <zh/> 回调函数 | <en/> callback function
1066
+ * @returns <zh/> Graph 实例 | <en/> Graph instance
1067
+ */
1068
+ public once<T extends IEvent = IEvent>(eventName: string, callback: (event: T) => void) {
1069
+ return super.once(eventName, callback);
1070
+ }
1027
1071
  }
@@ -14,4 +14,8 @@ export class PluginController extends ExtensionController<BasePlugin<CustomPlugi
14
14
  public setPlugins(plugins: PluginOptions) {
15
15
  this.setExtensions(plugins);
16
16
  }
17
+
18
+ public getPluginInstance(key: string) {
19
+ return this.extensionMap[key];
20
+ }
17
21
  }