@antv/g6 5.0.30 → 5.0.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 (148) hide show
  1. package/dist/g6.min.js +29 -29
  2. package/dist/lib/animations/types.d.ts +7 -1
  3. package/dist/lib/elements/base-element.d.ts +3 -2
  4. package/dist/lib/elements/edges/base-edge.d.ts +3 -3
  5. package/dist/lib/elements/nodes/base-node.d.ts +3 -3
  6. package/dist/lib/elements/shapes/badge.d.ts +1 -1
  7. package/dist/lib/elements/shapes/base-shape.d.ts +1 -1
  8. package/dist/lib/exports.d.ts +1 -1
  9. package/dist/lib/plugins/legend.d.ts +22 -14
  10. package/dist/lib/plugins/minimap/index.d.ts +2 -4
  11. package/dist/lib/plugins/timebar.d.ts +1 -2
  12. package/dist/lib/plugins/utils/canvas.d.ts +31 -0
  13. package/dist/lib/plugins/utils/dom.d.ts +22 -0
  14. package/dist/lib/types/element.d.ts +0 -8
  15. package/dist/lib/utils/dom.d.ts +0 -26
  16. package/dist/lib/utils/style.d.ts +1 -1
  17. package/dist/lib/version.d.ts +1 -1
  18. package/esm/animations/types.d.ts +7 -1
  19. package/esm/elements/base-element.d.ts +3 -2
  20. package/esm/elements/base-element.js +2 -1
  21. package/esm/elements/base-element.js.map +1 -1
  22. package/esm/elements/combos/base-combo.js +6 -6
  23. package/esm/elements/combos/base-combo.js.map +1 -1
  24. package/esm/elements/edges/base-edge.d.ts +3 -3
  25. package/esm/elements/edges/base-edge.js +4 -4
  26. package/esm/elements/edges/base-edge.js.map +1 -1
  27. package/esm/elements/nodes/base-node.d.ts +3 -3
  28. package/esm/elements/nodes/base-node.js.map +1 -1
  29. package/esm/elements/shapes/badge.d.ts +1 -1
  30. package/esm/elements/shapes/base-shape.d.ts +1 -1
  31. package/esm/elements/shapes/base-shape.js.map +1 -1
  32. package/esm/exports.d.ts +1 -1
  33. package/esm/plugins/background/index.js +1 -2
  34. package/esm/plugins/background/index.js.map +1 -1
  35. package/esm/plugins/contextmenu/index.js +1 -1
  36. package/esm/plugins/contextmenu/index.js.map +1 -1
  37. package/esm/plugins/grid-line.js +3 -3
  38. package/esm/plugins/grid-line.js.map +1 -1
  39. package/esm/plugins/legend.d.ts +22 -14
  40. package/esm/plugins/legend.js +39 -51
  41. package/esm/plugins/legend.js.map +1 -1
  42. package/esm/plugins/minimap/index.d.ts +2 -4
  43. package/esm/plugins/minimap/index.js +13 -30
  44. package/esm/plugins/minimap/index.js.map +1 -1
  45. package/esm/plugins/timebar.d.ts +1 -2
  46. package/esm/plugins/timebar.js +15 -47
  47. package/esm/plugins/timebar.js.map +1 -1
  48. package/esm/plugins/toolbar/index.js +1 -1
  49. package/esm/plugins/toolbar/index.js.map +1 -1
  50. package/esm/plugins/utils/canvas.d.ts +31 -0
  51. package/esm/plugins/utils/canvas.js +55 -0
  52. package/esm/plugins/utils/canvas.js.map +1 -0
  53. package/esm/plugins/utils/dom.d.ts +22 -0
  54. package/esm/plugins/utils/dom.js +54 -0
  55. package/esm/plugins/utils/dom.js.map +1 -0
  56. package/esm/plugins/watermark/index.js +1 -1
  57. package/esm/plugins/watermark/index.js.map +1 -1
  58. package/esm/runtime/animation.js +5 -2
  59. package/esm/runtime/animation.js.map +1 -1
  60. package/esm/runtime/element.js +5 -9
  61. package/esm/runtime/element.js.map +1 -1
  62. package/esm/types/element.d.ts +0 -8
  63. package/esm/utils/dom.d.ts +0 -26
  64. package/esm/utils/dom.js +0 -65
  65. package/esm/utils/dom.js.map +1 -1
  66. package/esm/utils/style.d.ts +1 -1
  67. package/esm/utils/style.js +2 -2
  68. package/esm/utils/style.js.map +1 -1
  69. package/esm/version.d.ts +1 -1
  70. package/esm/version.js +1 -1
  71. package/lib/animations/types.d.ts +7 -1
  72. package/lib/elements/base-element.d.ts +3 -2
  73. package/lib/elements/base-element.js +2 -1
  74. package/lib/elements/base-element.js.map +1 -1
  75. package/lib/elements/combos/base-combo.js +6 -6
  76. package/lib/elements/combos/base-combo.js.map +1 -1
  77. package/lib/elements/edges/base-edge.d.ts +3 -3
  78. package/lib/elements/edges/base-edge.js +4 -4
  79. package/lib/elements/edges/base-edge.js.map +1 -1
  80. package/lib/elements/nodes/base-node.d.ts +3 -3
  81. package/lib/elements/nodes/base-node.js.map +1 -1
  82. package/lib/elements/shapes/badge.d.ts +1 -1
  83. package/lib/elements/shapes/base-shape.d.ts +1 -1
  84. package/lib/elements/shapes/base-shape.js.map +1 -1
  85. package/lib/exports.d.ts +1 -1
  86. package/lib/plugins/background/index.js +1 -2
  87. package/lib/plugins/background/index.js.map +1 -1
  88. package/lib/plugins/contextmenu/index.js +1 -1
  89. package/lib/plugins/contextmenu/index.js.map +1 -1
  90. package/lib/plugins/grid-line.js +3 -3
  91. package/lib/plugins/grid-line.js.map +1 -1
  92. package/lib/plugins/legend.d.ts +22 -14
  93. package/lib/plugins/legend.js +38 -50
  94. package/lib/plugins/legend.js.map +1 -1
  95. package/lib/plugins/minimap/index.d.ts +2 -4
  96. package/lib/plugins/minimap/index.js +13 -30
  97. package/lib/plugins/minimap/index.js.map +1 -1
  98. package/lib/plugins/timebar.d.ts +1 -2
  99. package/lib/plugins/timebar.js +15 -47
  100. package/lib/plugins/timebar.js.map +1 -1
  101. package/lib/plugins/toolbar/index.js +1 -1
  102. package/lib/plugins/toolbar/index.js.map +1 -1
  103. package/lib/plugins/utils/canvas.d.ts +31 -0
  104. package/lib/plugins/utils/canvas.js +58 -0
  105. package/lib/plugins/utils/canvas.js.map +1 -0
  106. package/lib/plugins/utils/dom.d.ts +22 -0
  107. package/lib/plugins/utils/dom.js +58 -0
  108. package/lib/plugins/utils/dom.js.map +1 -0
  109. package/lib/plugins/watermark/index.js +1 -1
  110. package/lib/plugins/watermark/index.js.map +1 -1
  111. package/lib/runtime/animation.js +5 -2
  112. package/lib/runtime/animation.js.map +1 -1
  113. package/lib/runtime/element.js +5 -9
  114. package/lib/runtime/element.js.map +1 -1
  115. package/lib/types/element.d.ts +0 -8
  116. package/lib/utils/dom.d.ts +0 -26
  117. package/lib/utils/dom.js +0 -68
  118. package/lib/utils/dom.js.map +1 -1
  119. package/lib/utils/style.d.ts +1 -1
  120. package/lib/utils/style.js +2 -2
  121. package/lib/utils/style.js.map +1 -1
  122. package/lib/version.d.ts +1 -1
  123. package/lib/version.js +1 -1
  124. package/package.json +1 -1
  125. package/src/animations/types.ts +7 -1
  126. package/src/elements/base-element.ts +5 -3
  127. package/src/elements/combos/base-combo.ts +6 -6
  128. package/src/elements/edges/base-edge.ts +6 -7
  129. package/src/elements/nodes/base-node.ts +2 -3
  130. package/src/elements/shapes/base-shape.ts +1 -4
  131. package/src/exports.ts +0 -1
  132. package/src/global.d.ts +5 -0
  133. package/src/plugins/background/index.ts +1 -2
  134. package/src/plugins/contextmenu/index.ts +1 -1
  135. package/src/plugins/grid-line.ts +3 -3
  136. package/src/plugins/legend.ts +67 -55
  137. package/src/plugins/minimap/index.ts +16 -52
  138. package/src/plugins/timebar.ts +16 -47
  139. package/src/plugins/toolbar/index.ts +1 -1
  140. package/src/plugins/utils/canvas.ts +89 -0
  141. package/src/plugins/utils/dom.ts +67 -0
  142. package/src/plugins/watermark/index.ts +1 -1
  143. package/src/runtime/animation.ts +5 -2
  144. package/src/runtime/element.ts +4 -9
  145. package/src/types/element.ts +0 -9
  146. package/src/utils/dom.ts +0 -80
  147. package/src/utils/style.ts +3 -6
  148. package/src/version.ts +1 -1
@@ -68,7 +68,13 @@ export interface AnimationContext {
68
68
  *
69
69
  * <en/> For example, before the element is destroyed, the final state opacity of the element needs to be set to 0
70
70
  */
71
- modifiedStyle: Record<string, unknown>;
71
+ modifiedStyle?: Record<string, unknown>;
72
+ /**
73
+ * <zh/> 变更样式
74
+ *
75
+ * <en/> Updated style
76
+ */
77
+ updatedStyle?: Record<string, unknown>;
72
78
  }
73
79
 
74
80
  /**
@@ -1,11 +1,13 @@
1
1
  import type { IAnimation } from '@antv/g';
2
2
  import type { RuntimeContext } from '../runtime/types';
3
- import type { BaseElementStyleProps, Keyframe } from '../types';
3
+ import type { Keyframe } from '../types';
4
+ import type { BaseShapeStyleProps } from './shapes';
4
5
  import { BaseShape } from './shapes';
5
6
 
6
- export abstract class BaseElement<T extends BaseElementStyleProps> extends BaseShape<T> {
7
+ export abstract class BaseElement<T extends BaseShapeStyleProps> extends BaseShape<T> {
7
8
  protected get context(): RuntimeContext {
8
- return this.attributes.context!;
9
+ // @ts-expect-error skip type-check
10
+ return this.config.context;
9
11
  }
10
12
 
11
13
  protected get parsedAttributes() {
@@ -136,8 +136,8 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
136
136
  }
137
137
 
138
138
  protected getContentBBox(attributes: Required<S>): AABB {
139
- const { context, childrenNode = [], padding } = attributes;
140
- const children = childrenNode.map((id) => context!.element!.getElement(id)).filter(Boolean);
139
+ const { childrenNode = [], padding } = attributes;
140
+ const children = childrenNode.map((id) => this.context!.element!.getElement(id)).filter(Boolean);
141
141
  if (children.length === 0) {
142
142
  const bbox = new AABB();
143
143
  const { x = 0, y = 0, size } = attributes;
@@ -181,8 +181,8 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
181
181
  }
182
182
 
183
183
  protected getCollapsedMarkerText(type: CollapsedMarkerStyleProps['type'], attributes: Required<S>): string {
184
- const { context, childrenData = [] } = attributes;
185
- const { model } = context!;
184
+ const { childrenData = [] } = attributes;
185
+ const { model } = this.context;
186
186
 
187
187
  if (type === 'descendant-count') return model.getDescendantsData(this.id).length.toString();
188
188
  if (type === 'child-count') return childrenData.length.toString();
@@ -196,12 +196,12 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
196
196
  }
197
197
 
198
198
  public getComboPosition(attributes: Required<S>): Point {
199
- const { x = 0, y = 0, collapsed, context, childrenData = [] } = attributes;
199
+ const { x = 0, y = 0, collapsed, childrenData = [] } = attributes;
200
200
 
201
201
  if (childrenData.length === 0) return [+x, +y, 0];
202
202
 
203
203
  if (collapsed) {
204
- const { model } = context!;
204
+ const { model } = this.context;
205
205
  const descendants = model.getDescendantsData(this.id).filter((datum) => !model.isCombo(idOf(datum)));
206
206
 
207
207
  if (descendants.length > 0 && descendants.some(hasPosition)) {
@@ -3,7 +3,6 @@ import { Image, Path } from '@antv/g';
3
3
  import type { PathArray } from '@antv/util';
4
4
  import { isFunction, pick } from '@antv/util';
5
5
  import type {
6
- BaseElementStyleProps,
7
6
  Edge,
8
7
  EdgeArrowStyleProps,
9
8
  EdgeBadgeStyleProps,
@@ -26,7 +25,7 @@ import * as Symbol from '../../utils/symbol';
26
25
  import { getWordWrapWidthByEnds } from '../../utils/text';
27
26
  import { BaseElement } from '../base-element';
28
27
  import { effect } from '../effect';
29
- import type { BadgeStyleProps, LabelStyleProps } from '../shapes';
28
+ import type { BadgeStyleProps, BaseShapeStyleProps, LabelStyleProps } from '../shapes';
30
29
  import { Badge, Label } from '../shapes';
31
30
 
32
31
  /**
@@ -35,7 +34,7 @@ import { Badge, Label } from '../shapes';
35
34
  * <en/> Base style properties of the edge
36
35
  */
37
36
  export interface BaseEdgeStyleProps
38
- extends BaseElementStyleProps,
37
+ extends BaseShapeStyleProps,
39
38
  Prefix<'label', EdgeLabelStyleProps>,
40
39
  Prefix<'halo', PathStyleProps>,
41
40
  Prefix<'badge', EdgeBadgeStyleProps>,
@@ -219,13 +218,13 @@ export abstract class BaseEdge extends BaseElement<BaseEdgeStyleProps> implement
219
218
  }
220
219
 
221
220
  protected get sourceNode() {
222
- const { context, sourceNode: source } = this.parsedAttributes;
223
- return context.element!.getElement<Node>(source)!;
221
+ const { sourceNode: source } = this.parsedAttributes;
222
+ return this.context.element!.getElement<Node>(source)!;
224
223
  }
225
224
 
226
225
  protected get targetNode() {
227
- const { context, targetNode: target } = this.parsedAttributes;
228
- return context.element!.getElement<Node>(target)!;
226
+ const { targetNode: target } = this.parsedAttributes;
227
+ return this.context.element!.getElement<Node>(target)!;
229
228
  }
230
229
 
231
230
  protected getKeyStyle(attributes: ParsedBaseEdgeStyleProps): PathStyleProps {
@@ -4,7 +4,6 @@ import type { CategoricalPalette } from '../../palettes/types';
4
4
  import type { RuntimeContext } from '../../runtime/types';
5
5
  import type { NodeData } from '../../spec';
6
6
  import type {
7
- BaseElementStyleProps,
8
7
  ID,
9
8
  Node,
10
9
  NodeBadgeStyleProps,
@@ -28,7 +27,7 @@ import { getWordWrapWidthByBox } from '../../utils/text';
28
27
  import { setVisibility } from '../../utils/visibility';
29
28
  import { BaseElement } from '../base-element';
30
29
  import { effect } from '../effect';
31
- import type { BadgeStyleProps, IconStyleProps, LabelStyleProps } from '../shapes';
30
+ import type { BadgeStyleProps, BaseShapeStyleProps, IconStyleProps, LabelStyleProps } from '../shapes';
32
31
  import { Badge, Icon, Label } from '../shapes';
33
32
  import { connectImage, dispatchPositionChange } from '../shapes/image';
34
33
 
@@ -38,7 +37,7 @@ import { connectImage, dispatchPositionChange } from '../shapes/image';
38
37
  * <en/> Base node style props
39
38
  */
40
39
  export interface BaseNodeStyleProps
41
- extends BaseElementStyleProps,
40
+ extends BaseShapeStyleProps,
42
41
  Prefix<'label', NodeLabelStyleProps>,
43
42
  Prefix<'halo', BaseStyleProps>,
44
43
  Prefix<'icon', IconStyleProps>,
@@ -160,10 +160,7 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
160
160
  */
161
161
  public getGraphicStyle<T extends Record<string, any>>(
162
162
  style: T,
163
- ): Omit<
164
- T,
165
- 'x' | 'y' | 'z' | 'transform' | 'transformOrigin' | 'className' | 'class' | 'context' | 'zIndex' | 'visibility'
166
- > {
163
+ ): Omit<T, 'x' | 'y' | 'z' | 'transform' | 'transformOrigin' | 'className' | 'class' | 'zIndex' | 'visibility'> {
167
164
  return getSubShapeStyle(style);
168
165
  }
169
166
 
package/src/exports.ts CHANGED
@@ -234,7 +234,6 @@ export type {
234
234
  } from './transforms';
235
235
  export type { DrawData } from './transforms/types';
236
236
  export type {
237
- BaseElementStyleProps,
238
237
  CardinalPlacement,
239
238
  CollapsedMarkerStyleProps,
240
239
  Combo,
package/src/global.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  import '@antv/g';
2
+ import type { RuntimeContext } from './runtime/types';
2
3
 
3
4
  declare module '@antv/g' {
4
5
  interface BaseStyleProps {
@@ -9,4 +10,8 @@ declare module '@antv/g' {
9
10
  */
10
11
  $layer?: string;
11
12
  }
13
+
14
+ interface DisplayObjectConfig {
15
+ context?: RuntimeContext;
16
+ }
12
17
  }
@@ -1,8 +1,8 @@
1
1
  import { omit } from '@antv/util';
2
2
  import type { RuntimeContext } from '../../runtime/types';
3
- import { createPluginContainer } from '../../utils/dom';
4
3
  import type { BasePluginOptions } from '../base-plugin';
5
4
  import { BasePlugin } from '../base-plugin';
5
+ import { createPluginContainer } from '../utils/dom';
6
6
 
7
7
  /**
8
8
  * <zh/> 背景配置项
@@ -24,7 +24,6 @@ export class Background extends BasePlugin<BackgroundOptions> {
24
24
  static defaultOptions: Partial<BackgroundOptions> = {
25
25
  transition: 'background 0.5s',
26
26
  backgroundSize: 'cover',
27
- zIndex: '-1',
28
27
  };
29
28
 
30
29
  private $element: HTMLElement = createPluginContainer('background');
@@ -1,9 +1,9 @@
1
1
  import type { RuntimeContext } from '../../runtime/types';
2
2
  import type { Element } from '../../types';
3
3
  import type { IElementEvent } from '../../types/event';
4
- import { createPluginContainer, insertDOM } from '../../utils/dom';
5
4
  import type { BasePluginOptions } from '../base-plugin';
6
5
  import { BasePlugin } from '../base-plugin';
6
+ import { createPluginContainer, insertDOM } from '../utils/dom';
7
7
  import type { Item } from './util';
8
8
  import { CONTEXTMENU_CSS, getContentFromItems } from './util';
9
9
  /**
@@ -1,10 +1,10 @@
1
1
  import { GraphEvent } from '../constants';
2
2
  import type { RuntimeContext } from '../runtime/types';
3
3
  import type { Point } from '../types';
4
- import { createPluginContainer, insertBefore } from '../utils/dom';
5
4
  import { ViewportEvent } from '../utils/event';
6
5
  import { add, mod } from '../utils/vector';
7
6
  import { BasePlugin, BasePluginOptions } from './base-plugin';
7
+ import { createPluginContainer } from './utils/dom';
8
8
 
9
9
  /**
10
10
  * <zh/> 网格线配置项
@@ -98,7 +98,7 @@ export class GridLine extends BasePlugin<GridLineOptions> {
98
98
  stroke: '#eee',
99
99
  };
100
100
 
101
- private $element: HTMLElement = createPluginContainer('grid-line');
101
+ private $element: HTMLElement = createPluginContainer('grid-line', true);
102
102
 
103
103
  private offset: Point = [0, 0];
104
104
 
@@ -106,7 +106,7 @@ export class GridLine extends BasePlugin<GridLineOptions> {
106
106
  super(context, Object.assign({}, GridLine.defaultOptions, options));
107
107
 
108
108
  const $container = this.context.canvas.getContainer()!;
109
- insertBefore($container, this.$element);
109
+ $container.prepend(this.$element);
110
110
 
111
111
  this.updateStyle();
112
112
  this.bindEvents();
@@ -1,5 +1,6 @@
1
- import { Category, Layout, Selection } from '@antv/component';
1
+ import { Category, Selection } from '@antv/component';
2
2
  import { CategoryStyleProps } from '@antv/component/lib/ui/legend/types';
3
+ import { Canvas } from '@antv/g';
3
4
  import { get, isFunction } from '@antv/util';
4
5
  import { GraphEvent } from '../constants';
5
6
  import type { RuntimeContext } from '../runtime/types';
@@ -7,6 +8,7 @@ import type { ElementDatum, ElementType, ID, State } from '../types';
7
8
  import type { CardinalPlacement } from '../types/placement';
8
9
  import type { BasePluginOptions } from './base-plugin';
9
10
  import { BasePlugin } from './base-plugin';
11
+ import { createPluginCanvas } from './utils/canvas';
10
12
 
11
13
  interface Datum extends Record<string, any> {
12
14
  id?: string;
@@ -40,6 +42,24 @@ export interface LegendOptions extends BasePluginOptions, Omit<CategoryStyleProp
40
42
  * @defaultValue 'bottom'
41
43
  */
42
44
  position?: CardinalPlacement;
45
+ /**
46
+ * <zh/> 图例挂载的容器,无则挂载到 Graph 所在容器
47
+ *
48
+ * <en/> The container where the legend is mounted, if not, it will be mounted to the container where the Graph is located
49
+ */
50
+ container?: HTMLElement | string;
51
+ /**
52
+ * <zh/> 图例画布类名,传入外置容器时不生效
53
+ *
54
+ * <en/> The class name of the legend canvas, which does not take effect when an external container is passed in
55
+ */
56
+ className?: string;
57
+ /**
58
+ * <zh/> 图例的容器样式,传入外置容器时不生效
59
+ *
60
+ * <en/> The style of the legend container, which does not take effect when an external container is passed in
61
+ */
62
+ containerStyle?: Partial<CSSStyleDeclaration>;
43
63
  /**
44
64
  * <zh/> 节点分类标识
45
65
  *
@@ -80,9 +100,10 @@ export class Legend extends BasePlugin<LegendOptions> {
80
100
  colPadding: 10,
81
101
  itemMarkerSize: 16,
82
102
  itemLabelFontSize: 16,
103
+ width: 240,
104
+ height: 160,
83
105
  };
84
106
  private typePrefix = '__data__';
85
- private element: Layout | null = null;
86
107
  private draw = false;
87
108
  private fieldMap = {
88
109
  node: new Map<string, ID[]>(),
@@ -90,6 +111,9 @@ export class Legend extends BasePlugin<LegendOptions> {
90
111
  combo: new Map<string, ID[]>(),
91
112
  };
92
113
  private selectedItems: string[] = [];
114
+ private category?: Category;
115
+ private container?: HTMLElement;
116
+ private canvas?: Canvas;
93
117
 
94
118
  constructor(context: RuntimeContext, options: LegendOptions) {
95
119
  super(context, Object.assign({}, Legend.defaultOptions, options));
@@ -110,8 +134,11 @@ export class Legend extends BasePlugin<LegendOptions> {
110
134
  }
111
135
 
112
136
  private clear() {
113
- this.element?.destroy();
114
- this.element = null;
137
+ this.canvas?.destroy();
138
+ this.container?.remove();
139
+ this.canvas = undefined;
140
+ this.container = undefined;
141
+
115
142
  this.draw = false;
116
143
  }
117
144
 
@@ -184,10 +211,9 @@ export class Legend extends BasePlugin<LegendOptions> {
184
211
  * <en/> Refresh the status of the legend element
185
212
  */
186
213
  public updateElement() {
187
- if (!this.element) return;
188
- const category = this.element.getChildByIndex(0) as Category;
214
+ if (!this.category) return;
189
215
 
190
- category.update({
216
+ this.category.update({
191
217
  itemMarkerOpacity: ({ id }) => {
192
218
  if (!this.selectedItems.length || this.selectedItems.includes(id)) return 1;
193
219
  return 0.5;
@@ -224,7 +250,7 @@ export class Legend extends BasePlugin<LegendOptions> {
224
250
 
225
251
  private getMarkerData = (field: string | ((item: ElementDatum) => string), elementType: ElementType) => {
226
252
  if (!field) return [];
227
- const { model, element, graph } = this.context;
253
+ const { model, element } = this.context;
228
254
  const { nodes, edges, combos } = model.getData();
229
255
  const items: { [key: string]: Datum } = {};
230
256
 
@@ -239,7 +265,8 @@ export class Legend extends BasePlugin<LegendOptions> {
239
265
  combo: 'rect',
240
266
  };
241
267
 
242
- /** 用于将 G6 element 转换为 componets 支持的类型 */
268
+ // 用于将 G6 element 转换为 components 支持的类型
269
+ // Used to convert G6 element to types supported by components
243
270
  const markerMapping: { [key: string]: string } = {
244
271
  circle: 'circle',
245
272
  ellipse: 'circle', // 待 components 支持 ellipse
@@ -303,69 +330,52 @@ export class Legend extends BasePlugin<LegendOptions> {
303
330
  return Object.values(items);
304
331
  };
305
332
 
306
- /**
307
- * <zh/> 图例布局
308
- *
309
- * <en/> Legend layout
310
- * @param position - <zh/> 图例位置| <en/> Legend position
311
- * @returns <zh/> 图例布局样式| <en/> Legend layout style
312
- */
313
- public layout = (position: CardinalPlacement) => {
314
- const preset = {
315
- flexDirection: 'row',
316
- alignItems: 'flex-end',
317
- justifyContent: 'center',
318
- };
319
- let { flexDirection, alignItems, justifyContent } = preset;
333
+ private upsertCanvas() {
334
+ if (this.canvas) return this.canvas;
320
335
 
321
- const layout = {
322
- top: ['row', 'flex-start', 'center'],
323
- bottom: ['row', 'flex-end', 'center'],
324
- left: ['column', 'flex-start', 'center'],
325
- right: ['column', 'flex-end', 'center'],
326
- };
336
+ const graphCanvas = this.context.canvas;
337
+ const [canvasWidth, canvasHeight] = graphCanvas.getSize();
327
338
 
328
- if (position in layout) {
329
- [flexDirection, alignItems, justifyContent] = layout[position];
330
- }
331
- return {
332
- display: 'flex',
333
- flexDirection,
334
- justifyContent,
335
- alignItems,
336
- };
337
- };
339
+ const { width = canvasWidth, height = canvasHeight, position, container, containerStyle, className } = this.options;
340
+ const [$container, canvas] = createPluginCanvas({
341
+ width,
342
+ height,
343
+ graphCanvas,
344
+ container,
345
+ containerStyle,
346
+ placement: position,
347
+ className: 'legend',
348
+ });
349
+
350
+ this.container = $container;
351
+ if (className) $container.classList.add(className);
352
+ this.canvas = canvas;
353
+
354
+ return this.canvas;
355
+ }
338
356
 
339
357
  private createElement = () => {
340
358
  if (this.draw) {
341
359
  this.updateElement();
342
360
  return;
343
361
  }
344
- const { canvas } = this.context;
345
- const [canvasWidth, canvasHeight] = canvas.getSize();
346
362
  const {
347
- width = canvasWidth,
348
- height = canvasHeight,
363
+ width,
364
+ height,
349
365
  nodeField,
350
366
  edgeField,
351
367
  comboField,
352
368
  trigger,
353
369
  position,
370
+ container,
371
+ containerStyle,
372
+ className,
354
373
  ...rest
355
374
  } = this.options;
356
375
  const nodeItems = this.getMarkerData(nodeField, 'node');
357
376
  const edgeItems = this.getMarkerData(edgeField, 'edge');
358
377
  const comboItems = this.getMarkerData(comboField, 'combo');
359
378
  const items = [...nodeItems, ...comboItems, ...edgeItems];
360
- const layout = this.layout(position);
361
-
362
- const layoutWrapper = new Layout({
363
- style: {
364
- width,
365
- height,
366
- ...layout,
367
- },
368
- });
369
379
 
370
380
  const categoryStyle = Object.assign(
371
381
  {
@@ -386,9 +396,11 @@ export class Legend extends BasePlugin<LegendOptions> {
386
396
  className: 'legend',
387
397
  style: categoryStyle,
388
398
  });
389
- layoutWrapper.appendChild(category);
390
- canvas.appendChild(layoutWrapper as any);
391
- this.element = layoutWrapper;
399
+ this.category = category;
400
+
401
+ const canvas = this.upsertCanvas();
402
+ canvas.appendChild(category);
403
+
392
404
  this.draw = true;
393
405
  };
394
406
 
@@ -1,5 +1,4 @@
1
1
  import { Canvas, DisplayObject, IRenderer, Landmark } from '@antv/g';
2
- import { Renderer } from '@antv/g-canvas';
3
2
  import { debounce, throttle } from '@antv/util';
4
3
  import { GraphEvent } from '../../constants';
5
4
  import type { RuntimeContext } from '../../runtime/types';
@@ -7,10 +6,10 @@ import { GraphData } from '../../spec';
7
6
  import type { ElementDatum, ElementType, ID, IGraphLifeCycleEvent, Padding, Placement, Vector3 } from '../../types';
8
7
  import { idOf } from '../../utils/id';
9
8
  import { parsePadding } from '../../utils/padding';
10
- import { parsePlacement } from '../../utils/placement';
11
9
  import { toPointObject } from '../../utils/point';
12
10
  import type { BasePluginOptions } from '../base-plugin';
13
11
  import { BasePlugin } from '../base-plugin';
12
+ import { createPluginCanvas } from '../utils/canvas';
14
13
 
15
14
  /**
16
15
  * <zh/> 缩略图插件配置项
@@ -64,9 +63,9 @@ export interface MinimapOptions extends BasePluginOptions {
64
63
  */
65
64
  shape?: 'key' | ((id: string, elementType: ElementType) => DisplayObject);
66
65
  /**
67
- * <zh/> 缩略图画布类名
66
+ * <zh/> 缩略图画布类名,传入外置容器时不生效
68
67
  *
69
- * <en/> The class name of the minimap canvas
68
+ * <en/> The class name of the minimap canvas, which does not take effect when an external container is passed in
70
69
  */
71
70
  className?: string;
72
71
  /**
@@ -269,46 +268,6 @@ export class Minimap extends BasePlugin<MinimapOptions> {
269
268
 
270
269
  private container!: HTMLElement;
271
270
 
272
- private calculatePosition(): [number, number] {
273
- const {
274
- position,
275
- size: [w, h],
276
- } = this.options;
277
-
278
- const { canvas } = this.context;
279
- const [width, height] = canvas.getSize();
280
- const [x, y] = parsePlacement(position);
281
- return [x * (width - w), y * (height - h)];
282
- }
283
-
284
- private createContainer(): HTMLElement {
285
- const {
286
- container,
287
- className,
288
- size: [width, height],
289
- containerStyle,
290
- } = this.options;
291
- if (container) {
292
- return typeof container === 'string' ? document.querySelector(container)! : container;
293
- }
294
-
295
- const $container = document.createElement('div');
296
- $container.classList.add('g6-minimap');
297
- if (className) $container.classList.add(className);
298
-
299
- const [x, y] = this.calculatePosition();
300
- Object.assign($container.style, {
301
- position: 'absolute',
302
- left: x + 'px',
303
- top: y + 'px',
304
- width: width + 'px',
305
- height: height + 'px',
306
- ...containerStyle,
307
- });
308
-
309
- return this.context.canvas.getContainer()!.appendChild($container);
310
- }
311
-
312
271
  private initCanvas() {
313
272
  const {
314
273
  renderer,
@@ -320,18 +279,23 @@ export class Minimap extends BasePlugin<MinimapOptions> {
320
279
  if (width !== w || height !== h) this.canvas.resize(width, height);
321
280
  if (renderer) this.canvas.setRenderer(renderer);
322
281
  } else {
323
- const dom = document.createElement('div');
282
+ const { className, position, container, containerStyle } = this.options;
324
283
 
325
- const container = this.createContainer();
326
- this.container = container;
327
- container.appendChild(dom);
328
-
329
- this.canvas = new Canvas({
284
+ const [$container, canvas] = createPluginCanvas({
285
+ renderer,
330
286
  width,
331
287
  height,
332
- container: dom,
333
- renderer: renderer || new Renderer(),
288
+ placement: position,
289
+ className: 'minimap',
290
+ container,
291
+ containerStyle,
292
+ graphCanvas: this.context.canvas,
334
293
  });
294
+
295
+ if (className) $container.classList.add(className);
296
+
297
+ this.container = $container;
298
+ this.canvas = canvas;
335
299
  }
336
300
 
337
301
  this.setCamera();
@@ -1,6 +1,5 @@
1
1
  import { Timebar as TimebarComponent } from '@antv/component';
2
2
  import { Canvas } from '@antv/g';
3
- import { Renderer as CanvasRenderer } from '@antv/g-canvas';
4
3
  import { isArray, isDate, isNumber } from '@antv/util';
5
4
  import { idOf } from '../utils/id';
6
5
  import { parsePadding } from '../utils/padding';
@@ -11,6 +10,7 @@ import type { RuntimeContext } from '../runtime/types';
11
10
  import type { GraphData } from '../spec';
12
11
  import type { ElementDatum, ElementType, ID, Padding } from '../types';
13
12
  import type { BasePluginOptions } from './base-plugin';
13
+ import { createPluginCanvas } from './utils/canvas';
14
14
 
15
15
  const prospectiveTimeKeys = ['timestamp', 'time', 'date', 'datetime'];
16
16
 
@@ -205,7 +205,7 @@ export class Timebar extends BasePlugin<TimebarOptions> {
205
205
 
206
206
  private canvas?: Canvas;
207
207
 
208
- private wrapper?: HTMLElement;
208
+ private container?: HTMLElement;
209
209
 
210
210
  private originalData?: GraphData;
211
211
 
@@ -275,16 +275,7 @@ export class Timebar extends BasePlugin<TimebarOptions> {
275
275
  super.update(options);
276
276
  this.backup();
277
277
 
278
- Object.keys(options).forEach((key) => {
279
- switch (key) {
280
- case 'position':
281
- this.upsertWrapper();
282
- break;
283
- default:
284
- this.upsertTimebar();
285
- break;
286
- }
287
- });
278
+ this.upsertTimebar();
288
279
  }
289
280
 
290
281
  /**
@@ -332,48 +323,26 @@ export class Timebar extends BasePlugin<TimebarOptions> {
332
323
  });
333
324
  }
334
325
 
335
- private upsertWrapper() {
336
- if (!this.wrapper) {
337
- const wrapper = document.createElement('div');
338
- wrapper.style.position = 'absolute';
339
- this.wrapper = wrapper;
340
- }
341
- const { x, y, className, position } = this.options;
342
- if (className) this.wrapper.className = className;
343
-
344
- if (isNumber(x) || isNumber(y)) {
345
- Object.assign(this.wrapper.style, {
346
- left: `${x ?? 0}px`,
347
- top: `${y ?? 0}px`,
348
- });
349
- } else {
350
- Object.assign(this.wrapper.style, {
351
- [position === 'top' ? 'bottom' : 'top']: 'unset',
352
- [position === 'top' ? 'top' : 'bottom']: '0px',
353
- });
354
- }
355
-
356
- this.context.canvas.getContainer()?.appendChild(this.wrapper);
357
-
358
- return this.wrapper;
359
- }
360
-
361
326
  private upsertCanvas() {
362
- const wrapper = this.upsertWrapper();
363
327
  if (this.canvas) return this.canvas;
364
328
 
365
- const { height } = this.options;
366
- const [width] = this.context.canvas.getSize();
329
+ const { className, height, position } = this.options;
330
+ const graphCanvas = this.context.canvas;
331
+ const [width] = graphCanvas.getSize();
367
332
  const [top, , bottom] = this.padding;
368
333
 
369
- this.canvas = new Canvas({
370
- container: wrapper,
334
+ const [$container, canvas] = createPluginCanvas({
371
335
  width,
372
336
  height: height + top + bottom,
373
- renderer: this.context.options.renderer?.('main') || new CanvasRenderer(),
374
- supportsMutipleCanvasesInOneContainer: true,
337
+ graphCanvas,
338
+ className: 'timebar',
339
+ placement: position,
375
340
  });
376
341
 
342
+ this.container = $container;
343
+ if (className) $container.classList.add(className);
344
+ this.canvas = canvas;
345
+
377
346
  return this.canvas;
378
347
  }
379
348
 
@@ -437,9 +406,9 @@ export class Timebar extends BasePlugin<TimebarOptions> {
437
406
  this.originalData && graph.setData({ ...this.originalData });
438
407
  this.timebar?.destroy();
439
408
  this.canvas?.destroy();
440
- this.wrapper?.remove();
409
+ this.container?.remove();
441
410
  this.originalData = undefined;
442
- this.wrapper = undefined;
411
+ this.container = undefined;
443
412
  this.timebar = undefined;
444
413
  this.canvas = undefined;
445
414