@antv/g6 5.0.48 → 5.0.50

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 (189) hide show
  1. package/README.md +1 -1
  2. package/README.zh-CN.md +1 -1
  3. package/dist/g6.min.js +32 -30
  4. package/dist/lib/behaviors/brush-select.d.ts +1 -1
  5. package/dist/lib/behaviors/drag-canvas.d.ts +1 -0
  6. package/dist/lib/behaviors/drag-element.d.ts +3 -2
  7. package/dist/lib/exports.d.ts +1 -1
  8. package/dist/lib/layouts/types.d.ts +30 -3
  9. package/dist/lib/plugins/hull/index.d.ts +1 -0
  10. package/dist/lib/plugins/index.d.ts +2 -0
  11. package/dist/lib/plugins/minimap/index.d.ts +7 -4
  12. package/dist/lib/plugins/title/index.d.ts +85 -0
  13. package/dist/lib/plugins/tooltip.d.ts +1 -1
  14. package/dist/lib/registry/extension/index.d.ts +1 -0
  15. package/dist/lib/runtime/graph.d.ts +27 -0
  16. package/dist/lib/types/utility.d.ts +1 -0
  17. package/dist/lib/utils/element.d.ts +1 -1
  18. package/dist/lib/utils/vector.d.ts +9 -0
  19. package/dist/lib/version.d.ts +1 -1
  20. package/esm/behaviors/brush-select.d.ts +1 -1
  21. package/esm/behaviors/brush-select.js +12 -3
  22. package/esm/behaviors/brush-select.js.map +1 -1
  23. package/esm/behaviors/drag-canvas.d.ts +1 -0
  24. package/esm/behaviors/drag-canvas.js +6 -1
  25. package/esm/behaviors/drag-canvas.js.map +1 -1
  26. package/esm/behaviors/drag-element-force.js +1 -1
  27. package/esm/behaviors/drag-element-force.js.map +1 -1
  28. package/esm/behaviors/drag-element.d.ts +3 -2
  29. package/esm/behaviors/drag-element.js +12 -3
  30. package/esm/behaviors/drag-element.js.map +1 -1
  31. package/esm/behaviors/hover-activate.js +1 -1
  32. package/esm/behaviors/hover-activate.js.map +1 -1
  33. package/esm/behaviors/lasso-select.js +3 -3
  34. package/esm/behaviors/lasso-select.js.map +1 -1
  35. package/esm/exports.d.ts +1 -1
  36. package/esm/exports.js +1 -1
  37. package/esm/exports.js.map +1 -1
  38. package/esm/layouts/types.d.ts +30 -3
  39. package/esm/plugins/grid-line.js +1 -1
  40. package/esm/plugins/grid-line.js.map +1 -1
  41. package/esm/plugins/hull/index.d.ts +1 -0
  42. package/esm/plugins/hull/index.js +5 -1
  43. package/esm/plugins/hull/index.js.map +1 -1
  44. package/esm/plugins/index.d.ts +2 -0
  45. package/esm/plugins/index.js +1 -0
  46. package/esm/plugins/index.js.map +1 -1
  47. package/esm/plugins/minimap/index.d.ts +7 -4
  48. package/esm/plugins/minimap/index.js +68 -61
  49. package/esm/plugins/minimap/index.js.map +1 -1
  50. package/esm/plugins/title/index.d.ts +85 -0
  51. package/esm/plugins/title/index.js +172 -0
  52. package/esm/plugins/title/index.js.map +1 -0
  53. package/esm/plugins/tooltip.d.ts +1 -1
  54. package/esm/plugins/tooltip.js +4 -4
  55. package/esm/plugins/tooltip.js.map +1 -1
  56. package/esm/registry/build-in.js +2 -1
  57. package/esm/registry/build-in.js.map +1 -1
  58. package/esm/registry/extension/index.d.ts +1 -0
  59. package/esm/registry/extension/index.js +7 -3
  60. package/esm/registry/extension/index.js.map +1 -1
  61. package/esm/runtime/canvas.js +2 -0
  62. package/esm/runtime/canvas.js.map +1 -1
  63. package/esm/runtime/element.js +6 -26
  64. package/esm/runtime/element.js.map +1 -1
  65. package/esm/runtime/graph.d.ts +27 -0
  66. package/esm/runtime/graph.js +49 -2
  67. package/esm/runtime/graph.js.map +1 -1
  68. package/esm/runtime/layout.js +4 -3
  69. package/esm/runtime/layout.js.map +1 -1
  70. package/esm/runtime/viewport.js +3 -0
  71. package/esm/runtime/viewport.js.map +1 -1
  72. package/esm/themes/base.js +1 -0
  73. package/esm/themes/base.js.map +1 -1
  74. package/esm/transforms/collapse-expand-node.js +1 -1
  75. package/esm/transforms/collapse-expand-node.js.map +1 -1
  76. package/esm/transforms/process-parallel-edges.js +16 -3
  77. package/esm/transforms/process-parallel-edges.js.map +1 -1
  78. package/esm/types/utility.d.ts +1 -0
  79. package/esm/types/utility.js +2 -0
  80. package/esm/types/utility.js.map +1 -0
  81. package/esm/utils/element.d.ts +1 -1
  82. package/esm/utils/element.js.map +1 -1
  83. package/esm/utils/layout.js +2 -2
  84. package/esm/utils/layout.js.map +1 -1
  85. package/esm/utils/vector.d.ts +9 -0
  86. package/esm/utils/vector.js +17 -0
  87. package/esm/utils/vector.js.map +1 -1
  88. package/esm/version.d.ts +1 -1
  89. package/esm/version.js +1 -1
  90. package/lib/behaviors/brush-select.d.ts +1 -1
  91. package/lib/behaviors/brush-select.js +12 -3
  92. package/lib/behaviors/brush-select.js.map +1 -1
  93. package/lib/behaviors/drag-canvas.d.ts +1 -0
  94. package/lib/behaviors/drag-canvas.js +5 -0
  95. package/lib/behaviors/drag-canvas.js.map +1 -1
  96. package/lib/behaviors/drag-element-force.js +1 -1
  97. package/lib/behaviors/drag-element-force.js.map +1 -1
  98. package/lib/behaviors/drag-element.d.ts +3 -2
  99. package/lib/behaviors/drag-element.js +11 -2
  100. package/lib/behaviors/drag-element.js.map +1 -1
  101. package/lib/behaviors/hover-activate.js +1 -1
  102. package/lib/behaviors/hover-activate.js.map +1 -1
  103. package/lib/behaviors/lasso-select.js +3 -3
  104. package/lib/behaviors/lasso-select.js.map +1 -1
  105. package/lib/exports.d.ts +1 -1
  106. package/lib/exports.js +3 -2
  107. package/lib/exports.js.map +1 -1
  108. package/lib/layouts/types.d.ts +30 -3
  109. package/lib/plugins/grid-line.js +1 -1
  110. package/lib/plugins/grid-line.js.map +1 -1
  111. package/lib/plugins/hull/index.d.ts +1 -0
  112. package/lib/plugins/hull/index.js +5 -1
  113. package/lib/plugins/hull/index.js.map +1 -1
  114. package/lib/plugins/index.d.ts +2 -0
  115. package/lib/plugins/index.js +3 -1
  116. package/lib/plugins/index.js.map +1 -1
  117. package/lib/plugins/minimap/index.d.ts +7 -4
  118. package/lib/plugins/minimap/index.js +68 -61
  119. package/lib/plugins/minimap/index.js.map +1 -1
  120. package/lib/plugins/title/index.d.ts +85 -0
  121. package/lib/plugins/title/index.js +176 -0
  122. package/lib/plugins/title/index.js.map +1 -0
  123. package/lib/plugins/tooltip.d.ts +1 -1
  124. package/lib/plugins/tooltip.js +4 -4
  125. package/lib/plugins/tooltip.js.map +1 -1
  126. package/lib/registry/build-in.js +1 -0
  127. package/lib/registry/build-in.js.map +1 -1
  128. package/lib/registry/extension/index.d.ts +1 -0
  129. package/lib/registry/extension/index.js +7 -3
  130. package/lib/registry/extension/index.js.map +1 -1
  131. package/lib/runtime/canvas.js +2 -0
  132. package/lib/runtime/canvas.js.map +1 -1
  133. package/lib/runtime/element.js +6 -26
  134. package/lib/runtime/element.js.map +1 -1
  135. package/lib/runtime/graph.d.ts +27 -0
  136. package/lib/runtime/graph.js +49 -2
  137. package/lib/runtime/graph.js.map +1 -1
  138. package/lib/runtime/layout.js +4 -3
  139. package/lib/runtime/layout.js.map +1 -1
  140. package/lib/runtime/viewport.js +3 -0
  141. package/lib/runtime/viewport.js.map +1 -1
  142. package/lib/themes/base.js +1 -0
  143. package/lib/themes/base.js.map +1 -1
  144. package/lib/transforms/collapse-expand-node.js +1 -1
  145. package/lib/transforms/collapse-expand-node.js.map +1 -1
  146. package/lib/transforms/process-parallel-edges.js +16 -3
  147. package/lib/transforms/process-parallel-edges.js.map +1 -1
  148. package/lib/types/utility.d.ts +1 -0
  149. package/lib/types/utility.js +3 -0
  150. package/lib/types/utility.js.map +1 -0
  151. package/lib/utils/element.d.ts +1 -1
  152. package/lib/utils/element.js.map +1 -1
  153. package/lib/utils/layout.js +2 -2
  154. package/lib/utils/layout.js.map +1 -1
  155. package/lib/utils/vector.d.ts +9 -0
  156. package/lib/utils/vector.js +18 -0
  157. package/lib/utils/vector.js.map +1 -1
  158. package/lib/version.d.ts +1 -1
  159. package/lib/version.js +1 -1
  160. package/package.json +8 -8
  161. package/src/behaviors/brush-select.ts +14 -3
  162. package/src/behaviors/drag-canvas.ts +7 -1
  163. package/src/behaviors/drag-element-force.ts +2 -1
  164. package/src/behaviors/drag-element.ts +17 -4
  165. package/src/behaviors/hover-activate.ts +1 -1
  166. package/src/behaviors/lasso-select.ts +3 -3
  167. package/src/exports.ts +1 -0
  168. package/src/layouts/types.ts +33 -4
  169. package/src/plugins/grid-line.ts +1 -1
  170. package/src/plugins/hull/index.ts +6 -1
  171. package/src/plugins/index.ts +2 -0
  172. package/src/plugins/minimap/index.ts +75 -68
  173. package/src/plugins/title/index.ts +300 -0
  174. package/src/plugins/tooltip.ts +4 -4
  175. package/src/registry/build-in.ts +2 -0
  176. package/src/registry/extension/index.ts +9 -3
  177. package/src/runtime/canvas.ts +2 -0
  178. package/src/runtime/element.ts +6 -32
  179. package/src/runtime/graph.ts +58 -2
  180. package/src/runtime/layout.ts +9 -3
  181. package/src/runtime/viewport.ts +14 -3
  182. package/src/themes/base.ts +1 -0
  183. package/src/transforms/collapse-expand-node.ts +1 -1
  184. package/src/transforms/process-parallel-edges.ts +17 -3
  185. package/src/types/utility.ts +1 -0
  186. package/src/utils/element.ts +5 -5
  187. package/src/utils/layout.ts +2 -3
  188. package/src/utils/vector.ts +17 -0
  189. package/src/version.ts +1 -1
@@ -14,6 +14,7 @@ export { Legend } from './legend';
14
14
  export { Minimap } from './minimap';
15
15
  export { Snapline } from './snapline';
16
16
  export { Timebar } from './timebar';
17
+ export { Title } from './title';
17
18
  export { Toolbar } from './toolbar';
18
19
  export { Tooltip } from './tooltip';
19
20
  export { Watermark } from './watermark';
@@ -34,6 +35,7 @@ export type { LegendOptions } from './legend';
34
35
  export type { MinimapOptions } from './minimap';
35
36
  export type { SnaplineOptions } from './snapline';
36
37
  export type { TimebarOptions } from './timebar';
38
+ export type { SubTitleStyle, TitleOptions, TitleStyle } from './title';
37
39
  export type { ToolbarOptions } from './toolbar';
38
40
  export type { TooltipOptions } from './tooltip';
39
41
  export type { WatermarkOptions } from './watermark';
@@ -3,7 +3,8 @@ import { debounce, throttle } from '@antv/util';
3
3
  import { GraphEvent } from '../../constants';
4
4
  import type { RuntimeContext } from '../../runtime/types';
5
5
  import { GraphData } from '../../spec';
6
- import type { ElementDatum, ElementType, ID, IGraphLifeCycleEvent, Padding, Placement, Vector3 } from '../../types';
6
+ import type { ElementDatum, ElementType, IGraphLifeCycleEvent, Padding, Placement, Vector3 } from '../../types';
7
+ import { isVisible } from '../../utils/element';
7
8
  import { idOf } from '../../utils/id';
8
9
  import { parsePadding } from '../../utils/padding';
9
10
  import { toPointObject } from '../../utils/point';
@@ -55,13 +56,17 @@ export interface MinimapOptions extends BasePluginOptions {
55
56
  * @remarks
56
57
  * <zh/>
57
58
  * - 'key' 使用元素的主图形作为缩略图形
58
- * - 也可以传入一个函数,接收元素的 id 和类型,返回一个图形
59
+ * - 'icon' 使用元素中心的 icon 作为缩略图形
60
+ * - 更多图形名称可查阅 https://g6.antv.antgroup.com/manual/element/node/base-node#style
61
+ * - 也可以传入一个函数,接收元素的 [id, 类型, 元素节点],返回一个自定义样式的图形
59
62
  *
60
63
  * <en/>
61
64
  * - 'key' uses the key shape of the element as the thumbnail shape
62
- * - You can also pass in a function that receives the id and type of the element and returns a shape
65
+ * - 'icon' uses the icon shape of the element as the thumbnail shape
66
+ * - more shape name see https://g6.antv.antgroup.com/manual/element/node/base-node#style
67
+ * - You can also pass in a function that receives the [id, type of the element, element] and returns a custom shape
63
68
  */
64
- shape?: 'key' | ((id: string, elementType: ElementType) => DisplayObject);
69
+ shape?: string | 'key' | 'icon' | ((id: string, elementType: ElementType, element: DisplayObject) => DisplayObject);
65
70
  /**
66
71
  * <zh/> 缩略图画布类名,传入外置容器时不生效
67
72
  *
@@ -149,6 +154,7 @@ export class Minimap extends BasePlugin<MinimapOptions> {
149
154
  const { graph } = this.context;
150
155
  graph.on(GraphEvent.AFTER_DRAW, this.onDraw);
151
156
  graph.on(GraphEvent.AFTER_RENDER, this.onRender);
157
+ graph.on(GraphEvent.AFTER_ANIMATE, this.onRender);
152
158
  graph.on(GraphEvent.AFTER_TRANSFORM, this.onTransform);
153
159
  }
154
160
 
@@ -156,6 +162,7 @@ export class Minimap extends BasePlugin<MinimapOptions> {
156
162
  const { graph } = this.context;
157
163
  graph.off(GraphEvent.AFTER_DRAW, this.onDraw);
158
164
  graph.off(GraphEvent.AFTER_RENDER, this.onRender);
165
+ graph.off(GraphEvent.AFTER_ANIMATE, this.onRender);
159
166
  graph.off(GraphEvent.AFTER_TRANSFORM, this.onTransform);
160
167
  }
161
168
 
@@ -166,8 +173,6 @@ export class Minimap extends BasePlugin<MinimapOptions> {
166
173
 
167
174
  private onRender!: () => void;
168
175
 
169
- private shapes = new Map<ID, DisplayObject>();
170
-
171
176
  /**
172
177
  * <zh/> 创建或更新缩略图
173
178
  *
@@ -181,8 +186,18 @@ export class Minimap extends BasePlugin<MinimapOptions> {
181
186
 
182
187
  private getElements(): Required<GraphData> {
183
188
  const { filter } = this.options;
184
- const { model } = this.context;
185
- const data = model.getData();
189
+ const { model, element } = this.context;
190
+ const originData = model.getData();
191
+ //过滤那些不存在于elementMap中的数据
192
+ const data = {
193
+ nodes: originData.nodes.filter((node) => element?.getElement(idOf(node))),
194
+ edges: originData.edges.filter((edge) => {
195
+ const edgeElement = element?.getElement(idOf(edge));
196
+ // 边数据存在且可见时才保留
197
+ return edgeElement && isVisible(edgeElement);
198
+ }),
199
+ combos: originData.combos.filter((combo) => element?.getElement(idOf(combo))),
200
+ };
186
201
 
187
202
  if (!filter) return data;
188
203
 
@@ -201,65 +216,46 @@ export class Minimap extends BasePlugin<MinimapOptions> {
201
216
  const { shape } = this.options;
202
217
  const { element } = this.context;
203
218
 
204
- if (shape === 'key') {
205
- const ids = new Set<ID>();
206
-
207
- const iterate = (datum: ElementDatum) => {
208
- const id = idOf(datum);
209
- ids.add(id);
210
-
211
- const target = element!.getElement(id);
212
- if (!target) return;
213
-
214
- const shape = target.getShape('key');
215
- const cloneShape = this.shapes.get(id) || shape.cloneNode();
216
-
217
- cloneShape.setPosition(shape.getPosition());
218
- // keep zIndex / id
219
- if (target.style.zIndex) cloneShape.style.zIndex = target.style.zIndex;
220
- cloneShape.id = target.id;
221
-
222
- if (!this.shapes.has(id)) {
223
- canvas.appendChild(cloneShape);
224
- this.shapes.set(id, cloneShape);
219
+ const iterate = (datum: ElementDatum, elType: ElementType) => {
220
+ const id = idOf(datum);
221
+ const target = element?.getElement(id);
222
+ if (!target) return;
223
+
224
+ const keyShape = target.getShape('key');
225
+ let cloneShape: DisplayObject;
226
+
227
+ if (typeof shape === 'string') {
228
+ const shapeName = shape;
229
+ const miniShape = target.getShape(shapeName);
230
+ cloneShape = miniShape.cloneNode();
231
+ } else {
232
+ const miniShape = shape(id, elType, target);
233
+ if (miniShape === target) {
234
+ cloneShape = miniShape.cloneNode(true);
225
235
  } else {
226
- Object.entries(shape.attributes).forEach(([key, value]) => {
227
- if (cloneShape.style[key] !== value) cloneShape.style[key] = value;
228
- });
229
- }
230
- };
231
-
232
- // 注意执行顺序 / Note the execution order
233
- edges.forEach(iterate);
234
- combos.forEach(iterate);
235
- nodes.forEach(iterate);
236
-
237
- this.shapes.forEach((shape, id) => {
238
- if (!ids.has(id)) {
239
- canvas.removeChild(shape);
240
- this.shapes.delete(id);
236
+ cloneShape = miniShape;
241
237
  }
242
- });
243
-
244
- return;
245
- }
246
-
247
- const setPosition = (id: ID, shape: DisplayObject) => {
248
- const target = element!.getElement(id)!;
249
- const position = target.getPosition();
250
- shape.setPosition(position);
251
- return shape;
238
+ }
239
+
240
+ /**
241
+ * 这里使用的是 keyShape 的位置
242
+ * 对于整个元素的位置而言,使用 keyShape 位置会比较准确
243
+ * 也比较合理
244
+ */
245
+ cloneShape.setPosition(keyShape.getPosition());
246
+ // keep zIndex / id
247
+ if (target.style.zIndex) cloneShape.style.zIndex = target.style.zIndex;
248
+ cloneShape.id = target.id;
249
+
250
+ canvas.appendChild(cloneShape);
252
251
  };
253
252
 
254
253
  canvas.removeChildren();
255
254
 
256
- edges.forEach((datum) => canvas.appendChild(shape(idOf(datum), 'edge')));
257
- combos.forEach((datum) => {
258
- canvas.appendChild(setPosition(idOf(datum), shape(idOf(datum), 'combo')));
259
- });
260
- nodes.forEach((datum) => {
261
- canvas.appendChild(setPosition(idOf(datum), shape(idOf(datum), 'node')));
262
- });
255
+ // 注意执行顺序 / Note the execution order
256
+ edges.forEach((datum) => iterate(datum, 'edge'));
257
+ combos.forEach((datum) => iterate(datum, 'combo'));
258
+ nodes.forEach((datum) => iterate(datum, 'node'));
263
259
  }
264
260
 
265
261
  private container!: HTMLElement;
@@ -373,8 +369,14 @@ export class Minimap extends BasePlugin<MinimapOptions> {
373
369
  let [x, y, width, height] = this.maskBBox;
374
370
 
375
371
  // clamp x, y, width, height
376
- if (x < 0) (width = upper(width + x, minimapWidth)), (x = 0);
377
- if (y < 0) (height = upper(height + y, minimapHeight)), (y = 0);
372
+ if (x < 0) {
373
+ width = upper(width + x, minimapWidth);
374
+ x = 0;
375
+ }
376
+ if (y < 0) {
377
+ height = upper(height + y, minimapHeight);
378
+ y = 0;
379
+ }
378
380
  if (x + width > minimapWidth) width = lower(minimapWidth - x, 0);
379
381
  if (y + height > minimapHeight) height = lower(minimapHeight - y, 0);
380
382
 
@@ -444,12 +446,16 @@ export class Minimap extends BasePlugin<MinimapOptions> {
444
446
  // 当拖拽画布导致 mask 缩小时,拖拽 mask 时,能够恢复到实际大小
445
447
  // When dragging the canvas causes the mask to shrink, dragging the mask will restore it to its actual size
446
448
  if (width < fullWidth) {
447
- if (movementX > 0) (x = lower(x - movementX, 0)), (width = upper(width + movementX, minimapWidth));
448
- else if (movementX < 0) width = upper(width - movementX, minimapWidth);
449
+ if (movementX > 0) {
450
+ x = lower(x - movementX, 0);
451
+ width = upper(width + movementX, minimapWidth);
452
+ } else if (movementX < 0) width = upper(width - movementX, minimapWidth);
449
453
  }
450
454
  if (height < fullHeight) {
451
- if (movementY > 0) (y = lower(y - movementY, 0)), (height = upper(height + movementY, minimapHeight));
452
- else if (movementY < 0) height = upper(height - movementY, minimapHeight);
455
+ if (movementY > 0) {
456
+ y = lower(y - movementY, 0);
457
+ height = upper(height + movementY, minimapHeight);
458
+ } else if (movementY < 0) height = upper(height - movementY, minimapHeight);
453
459
  }
454
460
 
455
461
  Object.assign(this.mask.style, {
@@ -505,8 +511,9 @@ export class Minimap extends BasePlugin<MinimapOptions> {
505
511
 
506
512
  public destroy(): void {
507
513
  this.unbindEvents();
508
- this.canvas.destroy();
514
+ this.canvas?.destroy();
509
515
  this.mask?.remove();
516
+ this.container?.remove();
510
517
  super.destroy();
511
518
  }
512
519
  }
@@ -0,0 +1,300 @@
1
+ import { Canvas } from '@antv/g';
2
+ import { GraphEvent } from '../../constants';
3
+ import type { LabelStyleProps } from '../../elements/shapes/label';
4
+ import { Label } from '../../elements/shapes/label';
5
+ import type { RuntimeContext } from '../../runtime/types';
6
+ import type { Prefix } from '../../types';
7
+ import { parsePadding } from '../../utils/padding';
8
+ import { subStyleProps } from '../../utils/prefix';
9
+ import type { BasePluginOptions } from '../base-plugin';
10
+ import { BasePlugin } from '../base-plugin';
11
+ import { createPluginCanvas } from '../utils/canvas';
12
+
13
+ const commonStyle: Partial<LabelStyleProps> = {
14
+ fill: '#1D2129',
15
+ wordWrap: true, // 自动换行
16
+ maxLines: 1, // 最大行数
17
+ textOverflow: 'ellipsis', // 溢出隐藏省略号
18
+ textBaseline: 'top',
19
+
20
+ /**
21
+ * textAlign 需要和 x 结合使用
22
+ * 举例: 前提条件: 画布 width = 600
23
+ * - textAlign: 'start' | 'left
24
+ * 需要设 x = 0
25
+ * - textAlign: 'end' | 'right'
26
+ * 需要设 x = 600 (即画布的宽度)
27
+ * - textAlign: 'center'
28
+ * 需要设 x = 300 (即画布的宽度 / 2)
29
+ */
30
+ textAlign: 'start',
31
+ x: 0,
32
+ };
33
+ const defaultTitleStyle: Partial<LabelStyleProps> = {
34
+ ...commonStyle,
35
+ fillOpacity: 0.9,
36
+ fontSize: 16,
37
+ fontWeight: 'bold',
38
+ };
39
+ const defaultSubTitleStyle: Partial<LabelStyleProps> = {
40
+ ...commonStyle,
41
+ fillOpacity: 0.65,
42
+ fontSize: 12,
43
+ fontWeight: 'normal',
44
+ };
45
+ const defaultOptions: Partial<TitleOptions> = {
46
+ align: 'left',
47
+ spacing: 8,
48
+ size: 44,
49
+ padding: [16, 24, 0, 24],
50
+ };
51
+
52
+ const titleKey = 'title';
53
+ const subtitleKey = 'subtitle';
54
+
55
+ /**
56
+ * <zh/> 标题的样式
57
+ *
58
+ * <en/> Title styles
59
+ */
60
+ export type TitleStyle = Prefix<typeof titleKey, Omit<LabelStyleProps, 'x' | 'y' | 'text'>>;
61
+ /**
62
+ * <zh/> 副标题的样式
63
+ *
64
+ * <en/> Subtitle styles
65
+ */
66
+ export type SubTitleStyle = Prefix<typeof subtitleKey, Omit<LabelStyleProps, 'x' | 'y' | 'text'>>;
67
+
68
+ /**
69
+ * <zh/> 标题插件配置项
70
+ *
71
+ * <en/> Title plugin options
72
+ */
73
+ export interface TitleOptions extends BasePluginOptions, TitleStyle, SubTitleStyle {
74
+ /**
75
+ * <zh/> 整个标题的高度
76
+ *
77
+ * <en/> whole title height
78
+ * @defaultValue 44
79
+ */
80
+ size?: number;
81
+ /**
82
+ * <zh/> 整个标题位于图的位置
83
+ *
84
+ * <en/> The entire title is located at the position of the graph
85
+ * @defaultValue 'left'
86
+ */
87
+ align?: 'left' | 'center' | 'right';
88
+ /**
89
+ * <zh/> 主标题、副标题之间的上下间距
90
+ *
91
+ * <en/> The y spacing between the title and subtitle
92
+ * @defaultValue 8
93
+ */
94
+ spacing?: number;
95
+ /**
96
+ * <zh/> 标题内边距
97
+ *
98
+ * <en/> whole title padding
99
+ * @defaultValue [16, 24, 0, 24]
100
+ */
101
+ padding?: number | number[];
102
+ /**
103
+ * <zh/> 标题内容
104
+ *
105
+ * <en/> title text
106
+ */
107
+ [titleKey]: string;
108
+ /**
109
+ * <zh/> 副标题内容
110
+ *
111
+ * <en/> subtitle text
112
+ */
113
+ [subtitleKey]?: string | null;
114
+ /**
115
+ * <zh/> 标题画布类名,传入外置容器时不生效
116
+ *
117
+ * <en/> The class name of the title canvas, which does not take effect when an external container is passed in
118
+ */
119
+ className?: string;
120
+ }
121
+
122
+ export class Title extends BasePlugin<TitleOptions> {
123
+ private canvas!: Canvas;
124
+ private container!: HTMLElement;
125
+
126
+ private get padding() {
127
+ return parsePadding(this.options.padding);
128
+ }
129
+
130
+ constructor(context: RuntimeContext, options: TitleOptions) {
131
+ const combineOption = Object.assign({}, defaultOptions, options);
132
+ super(context, combineOption);
133
+
134
+ this.bindEvents();
135
+ }
136
+
137
+ private onRender = () => {
138
+ const canvas = this.updateCanvas();
139
+ this.renderTitle(canvas);
140
+ };
141
+
142
+ private bindEvents() {
143
+ const { graph } = this.context;
144
+ graph.on(GraphEvent.AFTER_RENDER, this.onRender);
145
+ graph.on(GraphEvent.AFTER_ANIMATE, this.onRender);
146
+ }
147
+
148
+ private unbindEvents() {
149
+ const { graph } = this.context;
150
+ graph.off(GraphEvent.AFTER_RENDER, this.onRender);
151
+ graph.off(GraphEvent.AFTER_ANIMATE, this.onRender);
152
+ }
153
+
154
+ public destroy(): void {
155
+ this.unbindEvents();
156
+ this.canvas?.destroy();
157
+ this.container?.remove();
158
+ super.destroy();
159
+ }
160
+
161
+ private updateCanvas() {
162
+ const { size, className, align } = this.options;
163
+ const [width] = this.context.canvas.getSize();
164
+ const [pt = 0, , pb = 0] = this.padding;
165
+ const height = size + pt + pb;
166
+
167
+ if (this.canvas) {
168
+ const { width: w, height: h } = this.canvas.getConfig();
169
+ if (width !== w || height !== h) this.canvas.resize(width, height);
170
+ } else {
171
+ const positions = {
172
+ left: 'left-top',
173
+ center: 'top',
174
+ right: 'right-top',
175
+ } as const;
176
+
177
+ const [$container, canvas] = createPluginCanvas({
178
+ width,
179
+ height,
180
+ placement: positions[align] || positions.left,
181
+ className: 'title-canvas',
182
+ graphCanvas: this.context.canvas,
183
+ });
184
+
185
+ if (className) $container.classList.add(className);
186
+
187
+ this.container = $container;
188
+ this.canvas = canvas;
189
+ }
190
+
191
+ return this.canvas;
192
+ }
193
+
194
+ private renderTitle(canvas: Canvas) {
195
+ const titles = new TitleComponent({
196
+ options: this.options,
197
+ ctx: this.context,
198
+ });
199
+
200
+ canvas.removeChildren();
201
+ titles.getTitle().forEach((label) => {
202
+ if (label) canvas.appendChild(label);
203
+ });
204
+ }
205
+ }
206
+
207
+ class TitleComponent {
208
+ private options: TitleOptions;
209
+
210
+ private context: RuntimeContext;
211
+ private get padding() {
212
+ return parsePadding(this.options.padding);
213
+ }
214
+
215
+ constructor(props: { ctx: RuntimeContext; options: TitleOptions }) {
216
+ const { options, ctx } = props;
217
+
218
+ this.options = options;
219
+
220
+ this.context = ctx;
221
+ }
222
+
223
+ public getTitle() {
224
+ const {
225
+ [titleKey]: propsTitle,
226
+ [subtitleKey]: propsSubtitle,
227
+ spacing = 44,
228
+ padding,
229
+ align,
230
+ ...style
231
+ } = this.options;
232
+
233
+ const titleText = propsTitle;
234
+ const subTitleText = propsSubtitle;
235
+
236
+ const titleStyle = subStyleProps(style, titleKey) as LabelStyleProps;
237
+ const subtitleStyle = subStyleProps(style, subtitleKey) as LabelStyleProps;
238
+
239
+ const [topGraphWidth] = this.context.graph.getSize();
240
+ const [pt = 0, pr = 0, , pl = 0] = this.padding;
241
+ const canvasWidth = topGraphWidth;
242
+ const textWidth = canvasWidth - pl - pr;
243
+
244
+ let subTitle: Label | null = null;
245
+
246
+ let alignX = pl;
247
+ let textAlign = 'left' as 'left' | 'center' | 'right';
248
+
249
+ switch (align) {
250
+ case 'left':
251
+ alignX = pl;
252
+ textAlign = 'left';
253
+ break;
254
+ case 'center':
255
+ alignX = canvasWidth / 2;
256
+ textAlign = 'center';
257
+ break;
258
+
259
+ case 'right':
260
+ alignX = canvasWidth - pr;
261
+ textAlign = 'right';
262
+ break;
263
+ default:
264
+ alignX = pl;
265
+ textAlign = 'left';
266
+ }
267
+
268
+ const title = new Label({
269
+ className: titleKey,
270
+ style: {
271
+ ...defaultTitleStyle,
272
+ wordWrapWidth: textWidth - 5,
273
+ x: alignX,
274
+ y: pt,
275
+ textAlign,
276
+ ...titleStyle,
277
+ text: titleText,
278
+ },
279
+ });
280
+
281
+ const titleBBox = title.getBBox();
282
+
283
+ if (subTitleText) {
284
+ subTitle = new Label({
285
+ className: 'subTitle',
286
+ style: {
287
+ ...defaultSubTitleStyle,
288
+ wordWrapWidth: textWidth - 5,
289
+ x: alignX,
290
+ y: titleBBox.height + spacing + pt,
291
+ textAlign,
292
+ ...subtitleStyle,
293
+ text: subTitleText,
294
+ },
295
+ });
296
+ }
297
+
298
+ return [title, subTitle];
299
+ }
300
+ }
@@ -94,12 +94,12 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
94
94
  }
95
95
 
96
96
  return {
97
- 'node:pointerenter': this.onPointerEnter,
97
+ 'node:pointerover': this.onPointerOver,
98
98
  'node:pointermove': this.onPointerMove,
99
99
  'canvas:pointermove': this.onCanvasMove,
100
- 'edge:pointerenter': this.onPointerEnter,
100
+ 'edge:pointerover': this.onPointerOver,
101
101
  'edge:pointermove': this.onPointerMove,
102
- 'combo:pointerenter': this.onPointerEnter,
102
+ 'combo:pointerover': this.onPointerOver,
103
103
  'combo:pointermove': this.onPointerMove,
104
104
  contextmenu: this.onPointerLeave,
105
105
  'node:drag': this.onPointerLeave,
@@ -205,7 +205,7 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
205
205
  this.hide(event);
206
206
  };
207
207
 
208
- private onPointerEnter = (event: IElementEvent) => {
208
+ private onPointerOver = (event: IElementEvent) => {
209
209
  this.show(event);
210
210
  };
211
211
 
@@ -87,6 +87,7 @@ import {
87
87
  Minimap,
88
88
  Snapline,
89
89
  Timebar,
90
+ Title,
90
91
  Toolbar,
91
92
  Tooltip,
92
93
  Watermark,
@@ -208,6 +209,7 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
208
209
  minimap: Minimap,
209
210
  snapline: Snapline,
210
211
  timebar: Timebar,
212
+ title: Title,
211
213
  toolbar: Toolbar,
212
214
  tooltip: Tooltip,
213
215
  watermark: Watermark,
@@ -46,6 +46,7 @@ export abstract class ExtensionController<E extends BaseExtension<any>> {
46
46
  if (!Ctor) return print.warn(`The extension ${type} of ${category} is not registered.`);
47
47
 
48
48
  const instance = new Ctor(this.context, extension);
49
+ instance.initialized = true;
49
50
  this.extensionMap[key] = instance as E;
50
51
  }
51
52
 
@@ -67,10 +68,13 @@ export abstract class ExtensionController<E extends BaseExtension<any>> {
67
68
 
68
69
  protected destroyExtension(key: string) {
69
70
  const instance = this.extensionMap[key];
70
- if (instance) {
71
+
72
+ if (!instance) return;
73
+ if (instance.initialized && !instance.destroyed) {
71
74
  instance.destroy();
72
- delete this.extensionMap[key];
73
75
  }
76
+
77
+ delete this.extensionMap[key];
74
78
  }
75
79
 
76
80
  protected destroyExtensions(extensions: STDExtensionOption[]) {
@@ -78,7 +82,7 @@ export abstract class ExtensionController<E extends BaseExtension<any>> {
78
82
  }
79
83
 
80
84
  public destroy() {
81
- Object.values(this.extensionMap).forEach((extension) => extension.destroy());
85
+ this.destroyExtensions(this.extensions);
82
86
  // @ts-expect-error force delete
83
87
  this.context = {};
84
88
  this.extensions = [];
@@ -98,6 +102,8 @@ export class BaseExtension<T extends { type: string; key?: string; [key: string]
98
102
 
99
103
  protected events: [EventEmitter | HTMLElement, string, (event: IEvent) => void][] = [];
100
104
 
105
+ public initialized = false;
106
+
101
107
  public destroyed = false;
102
108
 
103
109
  constructor(context: RuntimeContext, options: Partial<T>) {
@@ -404,6 +404,8 @@ function configCanvasDom(layers: Record<CanvasLayer, GCanvas>) {
404
404
 
405
405
  if (domElement?.parentElement) {
406
406
  domElement.parentElement.style.display = 'grid';
407
+ // 给父元素设置独立的层叠上下文,避免外部元素影响内部的层叠逻辑
408
+ domElement.parentElement.style.isolation = 'isolate';
407
409
  }
408
410
  });
409
411
  }