@antv/g6 5.0.15 → 5.0.17

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 (196) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/dist/lib/behaviors/drag-canvas.d.ts +13 -0
  3. package/dist/lib/behaviors/optimize-viewport-transform.d.ts +0 -1
  4. package/dist/lib/behaviors/scroll-canvas.d.ts +7 -0
  5. package/dist/lib/behaviors/zoom-canvas.d.ts +7 -0
  6. package/dist/lib/constants/registry.d.ts +7 -1
  7. package/dist/lib/elements/edges/base-edge.d.ts +2 -2
  8. package/dist/lib/elements/edges/polyline.d.ts +4 -20
  9. package/dist/lib/elements/shapes/base-shape.d.ts +51 -2
  10. package/dist/lib/exports.d.ts +3 -2
  11. package/dist/lib/plugins/edge-filter-lens/index.d.ts +125 -0
  12. package/dist/lib/plugins/index.d.ts +4 -0
  13. package/dist/lib/plugins/snapline/index.d.ts +95 -0
  14. package/dist/lib/registry/types.d.ts +4 -0
  15. package/dist/lib/runtime/element.d.ts +1 -0
  16. package/dist/lib/runtime/plugin.d.ts +1 -1
  17. package/dist/lib/runtime/viewport.d.ts +8 -0
  18. package/dist/lib/types/index.d.ts +1 -0
  19. package/dist/lib/types/router.d.ts +103 -0
  20. package/dist/lib/utils/bbox.d.ts +8 -0
  21. package/dist/lib/utils/router/orth.d.ts +8 -8
  22. package/dist/lib/utils/router/shortest-path.d.ts +57 -0
  23. package/dist/lib/utils/text.d.ts +1 -1
  24. package/dist/lib/utils/visibility.d.ts +2 -1
  25. package/dist/lib/version.d.ts +1 -1
  26. package/esm/behaviors/brush-select.js +11 -6
  27. package/esm/behaviors/brush-select.js.map +1 -1
  28. package/esm/behaviors/drag-canvas.d.ts +13 -0
  29. package/esm/behaviors/drag-canvas.js +10 -1
  30. package/esm/behaviors/drag-canvas.js.map +1 -1
  31. package/esm/behaviors/optimize-viewport-transform.d.ts +0 -1
  32. package/esm/behaviors/optimize-viewport-transform.js +5 -4
  33. package/esm/behaviors/optimize-viewport-transform.js.map +1 -1
  34. package/esm/behaviors/scroll-canvas.d.ts +7 -0
  35. package/esm/behaviors/scroll-canvas.js +3 -1
  36. package/esm/behaviors/scroll-canvas.js.map +1 -1
  37. package/esm/behaviors/zoom-canvas.d.ts +7 -0
  38. package/esm/behaviors/zoom-canvas.js +3 -1
  39. package/esm/behaviors/zoom-canvas.js.map +1 -1
  40. package/esm/constants/registry.d.ts +7 -1
  41. package/esm/constants/registry.js +6 -0
  42. package/esm/constants/registry.js.map +1 -1
  43. package/esm/elements/edges/base-edge.d.ts +2 -2
  44. package/esm/elements/edges/base-edge.js +10 -4
  45. package/esm/elements/edges/base-edge.js.map +1 -1
  46. package/esm/elements/edges/polyline.d.ts +4 -20
  47. package/esm/elements/edges/polyline.js +32 -33
  48. package/esm/elements/edges/polyline.js.map +1 -1
  49. package/esm/elements/shapes/base-shape.d.ts +51 -2
  50. package/esm/elements/shapes/base-shape.js +23 -5
  51. package/esm/elements/shapes/base-shape.js.map +1 -1
  52. package/esm/exports.d.ts +3 -2
  53. package/esm/exports.js +1 -1
  54. package/esm/exports.js.map +1 -1
  55. package/esm/plugins/edge-filter-lens/index.d.ts +125 -0
  56. package/esm/plugins/edge-filter-lens/index.js +236 -0
  57. package/esm/plugins/edge-filter-lens/index.js.map +1 -0
  58. package/esm/plugins/index.d.ts +4 -0
  59. package/esm/plugins/index.js +2 -0
  60. package/esm/plugins/index.js.map +1 -1
  61. package/esm/plugins/snapline/index.d.ts +95 -0
  62. package/esm/plugins/snapline/index.js +268 -0
  63. package/esm/plugins/snapline/index.js.map +1 -0
  64. package/esm/registry/build-in.js +21 -2
  65. package/esm/registry/build-in.js.map +1 -1
  66. package/esm/registry/store.js +1 -0
  67. package/esm/registry/store.js.map +1 -1
  68. package/esm/registry/types.d.ts +4 -0
  69. package/esm/runtime/element.d.ts +1 -0
  70. package/esm/runtime/element.js +10 -4
  71. package/esm/runtime/element.js.map +1 -1
  72. package/esm/runtime/plugin.d.ts +1 -1
  73. package/esm/runtime/plugin.js +8 -1
  74. package/esm/runtime/plugin.js.map +1 -1
  75. package/esm/runtime/viewport.d.ts +8 -0
  76. package/esm/runtime/viewport.js +35 -2
  77. package/esm/runtime/viewport.js.map +1 -1
  78. package/esm/types/index.d.ts +1 -0
  79. package/esm/types/router.d.ts +103 -0
  80. package/esm/types/router.js +2 -0
  81. package/esm/types/router.js.map +1 -0
  82. package/esm/utils/bbox.d.ts +8 -0
  83. package/esm/utils/bbox.js +19 -0
  84. package/esm/utils/bbox.js.map +1 -1
  85. package/esm/utils/router/orth.d.ts +8 -8
  86. package/esm/utils/router/orth.js +13 -7
  87. package/esm/utils/router/orth.js.map +1 -1
  88. package/esm/utils/router/shortest-path.d.ts +57 -0
  89. package/esm/utils/router/shortest-path.js +387 -0
  90. package/esm/utils/router/shortest-path.js.map +1 -0
  91. package/esm/utils/text.d.ts +1 -1
  92. package/esm/utils/visibility.d.ts +2 -1
  93. package/esm/utils/visibility.js +3 -2
  94. package/esm/utils/visibility.js.map +1 -1
  95. package/esm/version.d.ts +1 -1
  96. package/esm/version.js +1 -1
  97. package/lib/behaviors/brush-select.js +11 -6
  98. package/lib/behaviors/brush-select.js.map +1 -1
  99. package/lib/behaviors/drag-canvas.d.ts +13 -0
  100. package/lib/behaviors/drag-canvas.js +10 -1
  101. package/lib/behaviors/drag-canvas.js.map +1 -1
  102. package/lib/behaviors/optimize-viewport-transform.d.ts +0 -1
  103. package/lib/behaviors/optimize-viewport-transform.js +5 -4
  104. package/lib/behaviors/optimize-viewport-transform.js.map +1 -1
  105. package/lib/behaviors/scroll-canvas.d.ts +7 -0
  106. package/lib/behaviors/scroll-canvas.js +3 -1
  107. package/lib/behaviors/scroll-canvas.js.map +1 -1
  108. package/lib/behaviors/zoom-canvas.d.ts +7 -0
  109. package/lib/behaviors/zoom-canvas.js +3 -1
  110. package/lib/behaviors/zoom-canvas.js.map +1 -1
  111. package/lib/constants/registry.d.ts +7 -1
  112. package/lib/constants/registry.js +6 -0
  113. package/lib/constants/registry.js.map +1 -1
  114. package/lib/elements/edges/base-edge.d.ts +2 -2
  115. package/lib/elements/edges/base-edge.js +9 -3
  116. package/lib/elements/edges/base-edge.js.map +1 -1
  117. package/lib/elements/edges/polyline.d.ts +4 -20
  118. package/lib/elements/edges/polyline.js +32 -33
  119. package/lib/elements/edges/polyline.js.map +1 -1
  120. package/lib/elements/shapes/base-shape.d.ts +51 -2
  121. package/lib/elements/shapes/base-shape.js +23 -5
  122. package/lib/elements/shapes/base-shape.js.map +1 -1
  123. package/lib/exports.d.ts +3 -2
  124. package/lib/exports.js +3 -1
  125. package/lib/exports.js.map +1 -1
  126. package/lib/plugins/edge-filter-lens/index.d.ts +125 -0
  127. package/lib/plugins/edge-filter-lens/index.js +240 -0
  128. package/lib/plugins/edge-filter-lens/index.js.map +1 -0
  129. package/lib/plugins/index.d.ts +4 -0
  130. package/lib/plugins/index.js +5 -1
  131. package/lib/plugins/index.js.map +1 -1
  132. package/lib/plugins/snapline/index.d.ts +95 -0
  133. package/lib/plugins/snapline/index.js +272 -0
  134. package/lib/plugins/snapline/index.js.map +1 -0
  135. package/lib/registry/build-in.js +20 -1
  136. package/lib/registry/build-in.js.map +1 -1
  137. package/lib/registry/store.js +1 -0
  138. package/lib/registry/store.js.map +1 -1
  139. package/lib/registry/types.d.ts +4 -0
  140. package/lib/runtime/element.d.ts +1 -0
  141. package/lib/runtime/element.js +9 -3
  142. package/lib/runtime/element.js.map +1 -1
  143. package/lib/runtime/plugin.d.ts +1 -1
  144. package/lib/runtime/plugin.js +8 -1
  145. package/lib/runtime/plugin.js.map +1 -1
  146. package/lib/runtime/viewport.d.ts +8 -0
  147. package/lib/runtime/viewport.js +34 -1
  148. package/lib/runtime/viewport.js.map +1 -1
  149. package/lib/types/index.d.ts +1 -0
  150. package/lib/types/router.d.ts +103 -0
  151. package/lib/types/router.js +3 -0
  152. package/lib/types/router.js.map +1 -0
  153. package/lib/utils/bbox.d.ts +8 -0
  154. package/lib/utils/bbox.js +20 -0
  155. package/lib/utils/bbox.js.map +1 -1
  156. package/lib/utils/router/orth.d.ts +8 -8
  157. package/lib/utils/router/orth.js +13 -7
  158. package/lib/utils/router/orth.js.map +1 -1
  159. package/lib/utils/router/shortest-path.d.ts +57 -0
  160. package/lib/utils/router/shortest-path.js +394 -0
  161. package/lib/utils/router/shortest-path.js.map +1 -0
  162. package/lib/utils/text.d.ts +1 -1
  163. package/lib/utils/visibility.d.ts +2 -1
  164. package/lib/utils/visibility.js +3 -2
  165. package/lib/utils/visibility.js.map +1 -1
  166. package/lib/version.d.ts +1 -1
  167. package/lib/version.js +1 -1
  168. package/package.json +1 -1
  169. package/src/behaviors/brush-select.ts +12 -5
  170. package/src/behaviors/drag-canvas.ts +24 -1
  171. package/src/behaviors/optimize-viewport-transform.ts +4 -9
  172. package/src/behaviors/scroll-canvas.ts +9 -1
  173. package/src/behaviors/zoom-canvas.ts +9 -1
  174. package/src/constants/registry.ts +6 -0
  175. package/src/elements/edges/base-edge.ts +19 -5
  176. package/src/elements/edges/polyline.ts +36 -65
  177. package/src/elements/shapes/base-shape.ts +76 -6
  178. package/src/exports.ts +5 -0
  179. package/src/plugins/edge-filter-lens/index.ts +391 -0
  180. package/src/plugins/grid-line.ts +1 -1
  181. package/src/plugins/index.ts +4 -0
  182. package/src/plugins/snapline/index.ts +383 -0
  183. package/src/registry/build-in.ts +33 -1
  184. package/src/registry/store.ts +1 -0
  185. package/src/registry/types.ts +2 -0
  186. package/src/runtime/element.ts +11 -4
  187. package/src/runtime/plugin.ts +8 -1
  188. package/src/runtime/viewport.ts +42 -3
  189. package/src/types/index.ts +1 -0
  190. package/src/types/router.ts +107 -0
  191. package/src/utils/bbox.ts +24 -0
  192. package/src/utils/router/orth.ts +19 -10
  193. package/src/utils/router/shortest-path.ts +470 -0
  194. package/src/utils/text.ts +1 -1
  195. package/src/utils/visibility.ts +3 -1
  196. package/src/version.ts +1 -1
@@ -57,6 +57,13 @@ export interface ZoomCanvasOptions extends BaseBehaviorOptions {
57
57
  * <en/> Callback when zooming is completed
58
58
  */
59
59
  onFinish?: () => void;
60
+ /**
61
+ * <zh/> 是否阻止默认事件
62
+ *
63
+ * <en/> Whether to prevent the default event
64
+ * @defaultValue true
65
+ */
66
+ preventDefault?: boolean;
60
67
  }
61
68
 
62
69
  /**
@@ -70,6 +77,7 @@ export class ZoomCanvas extends BaseBehavior<ZoomCanvasOptions> {
70
77
  enable: true,
71
78
  sensitivity: 1,
72
79
  trigger: [],
80
+ preventDefault: true,
73
81
  };
74
82
 
75
83
  private shortcut: Shortcut;
@@ -171,7 +179,7 @@ export class ZoomCanvas extends BaseBehavior<ZoomCanvasOptions> {
171
179
  }
172
180
 
173
181
  private preventDefault = (event: Event) => {
174
- event.preventDefault();
182
+ if (this.options.preventDefault) event.preventDefault();
175
183
  };
176
184
 
177
185
  /**
@@ -59,4 +59,10 @@ export enum ExtensionCategory {
59
59
  * <en/> Data transform
60
60
  */
61
61
  TRANSFORM = 'transform',
62
+ /**
63
+ * <zh/> 图形
64
+ *
65
+ * <en/> Shape
66
+ */
67
+ SHAPE = 'shape',
62
68
  }
@@ -18,7 +18,7 @@ import type {
18
18
  } from '../../types';
19
19
  import { getBBoxHeight, getBBoxWidth, getNodeBBox } from '../../utils/bbox';
20
20
  import { getArrowSize, getBadgePositionStyle, getCubicLoopPath, getLabelPositionStyle } from '../../utils/edge';
21
- import { findPorts, getConnectionPoint, isSameNode } from '../../utils/element';
21
+ import { findPorts, getConnectionPoint, getPortPosition, isSameNode } from '../../utils/element';
22
22
  import { omitStyleProps, subStyleProps } from '../../utils/prefix';
23
23
  import { parseSize } from '../../utils/size';
24
24
  import { mergeOptions } from '../../utils/style';
@@ -153,7 +153,7 @@ export interface BaseEdgeStyleProps
153
153
  /**
154
154
  * <zh/> 在路径除了 “起点” 和 “终点” 之外的每一个顶点上放置标记图形。在内部实现中,由于我们会把路径中部分命令转换成 C 命令,因此这些顶点实际是三阶贝塞尔曲线的控制点
155
155
  *
156
- * <en/> Place a marker graphic on each vertex of the path except for the "start point" and "end point". In the internal implementation, because we will convert some commands in the path to C commands, these vertices are actually the control points of the cubic Bezier curve
156
+ * <en/> Place a marker graphic on each vertex of the path except for the "start point" and "end point". In the internal implementation, because we will convert some commands in the path to C commands, these controlPoints are actually the control points of the cubic Bezier curve
157
157
  */
158
158
  markerMid?: DisplayObject | null;
159
159
  /**
@@ -258,14 +258,28 @@ export abstract class BaseEdge extends BaseElement<BaseEdgeStyleProps> implement
258
258
  return getCubicLoopPath(node, placement, clockwise, dist, sourcePort, targetPort);
259
259
  }
260
260
 
261
- protected getEndpoints(attributes: ParsedBaseEdgeStyleProps): [Point, Point] {
261
+ protected getEndpoints(
262
+ attributes: ParsedBaseEdgeStyleProps,
263
+ optimize = true,
264
+ controlPoints: Point[] | (() => Point[]) = [],
265
+ ): [Point, Point] {
262
266
  const { sourcePort: sourcePortKey, targetPort: targetPortKey } = attributes;
263
267
  const { sourceNode, targetNode } = this;
264
268
 
265
269
  const [sourcePort, targetPort] = findPorts(sourceNode, targetNode, sourcePortKey, targetPortKey);
266
270
 
267
- const sourcePoint = getConnectionPoint(sourcePort || sourceNode, targetPort || targetNode);
268
- const targetPoint = getConnectionPoint(targetPort || targetNode, sourcePort || sourceNode);
271
+ if (!optimize) {
272
+ const sourcePoint = sourcePort ? getPortPosition(sourcePort) : sourceNode.getCenter();
273
+ const targetPoint = targetPort ? getPortPosition(targetPort) : targetNode.getCenter();
274
+ return [sourcePoint, targetPoint];
275
+ }
276
+
277
+ const _controlPoints = typeof controlPoints === 'function' ? controlPoints() : controlPoints;
278
+ const sourcePoint = getConnectionPoint(sourcePort || sourceNode, _controlPoints[0] || targetPort || targetNode);
279
+ const targetPoint = getConnectionPoint(
280
+ targetPort || targetNode,
281
+ _controlPoints[_controlPoints.length - 1] || sourcePort || sourceNode,
282
+ );
269
283
 
270
284
  return [sourcePoint, targetPoint];
271
285
  }
@@ -1,11 +1,11 @@
1
1
  import type { DisplayObjectConfig } from '@antv/g';
2
2
  import type { PathArray } from '@antv/util';
3
- import type { LoopStyleProps, Padding, Point, Port } from '../../types';
3
+ import type { LoopStyleProps, Point, PolylineRouter } from '../../types';
4
4
  import { getBBoxHeight, getBBoxWidth, getNodeBBox } from '../../utils/bbox';
5
5
  import { getPolylineLoopPath, getPolylinePath } from '../../utils/edge';
6
- import { findPorts, getConnectionPoint, getPortPosition } from '../../utils/element';
7
6
  import { subStyleProps } from '../../utils/prefix';
8
7
  import { orth } from '../../utils/router/orth';
8
+ import { aStarSearch } from '../../utils/router/shortest-path';
9
9
  import { mergeOptions } from '../../utils/style';
10
10
  import type { BaseEdgeStyleProps } from './base-edge';
11
11
  import { BaseEdge } from './base-edge';
@@ -35,24 +35,9 @@ export interface PolylineStyleProps extends BaseEdgeStyleProps {
35
35
  * <en/> Whether to enable routing, it is enabled by default and controlPoints will be automatically included
36
36
  * @defaultValue false
37
37
  */
38
- router?: boolean;
39
- /**
40
- * <zh/> 路由名称,目前支持 'orth'
41
- *
42
- * <en/> Routing name, currently supports 'orth'
43
- * @defaultValue 'orth'
44
- * @remarks
45
- * <img width="300" src="https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*8Ia2RJFFQSoAAAAAAAAAAAAADmJ7AQ/original" />
46
- */
47
- routerName?: 'orth';
48
- /**
49
- * <zh/> 节点边距
50
- *
51
- * <en/> Padding for routing calculation
52
- * @defaultValue 10
53
- */
54
- routerPadding?: Padding;
38
+ router?: PolylineRouter;
55
39
  }
40
+
56
41
  type ParsedPolylineStyleProps = Required<PolylineStyleProps>;
57
42
 
58
43
  /**
@@ -65,63 +50,48 @@ export class Polyline extends BaseEdge {
65
50
  radius: 0,
66
51
  controlPoints: [],
67
52
  router: false,
68
- routerName: 'orth',
69
- routerPadding: 10,
70
53
  };
71
54
 
72
55
  constructor(options: DisplayObjectConfig<PolylineStyleProps>) {
73
56
  super(mergeOptions({ style: Polyline.defaultStyleProps }, options));
74
57
  }
75
58
 
76
- protected getKeyPath(attributes: ParsedPolylineStyleProps): PathArray {
77
- const { radius } = attributes;
59
+ protected getControlPoints(attributes: ParsedPolylineStyleProps): Point[] {
60
+ const { router } = attributes;
78
61
  const { sourceNode, targetNode } = this;
79
- // 1. 获取连接点(若有连接桩,取连接桩中心;反之,取节点中心)和连接桩 | Get connection points (if port, take port center; otherwise, take node center) and ports
80
- const { sourcePoint, targetPoint, sourcePort, targetPort } = this.getEndpointsAndPorts(attributes);
81
-
82
- // 2. 计算控制点 | Calculate control points
83
- const controlPoints = this.getControlPoints(attributes, sourcePoint, targetPoint);
84
-
85
- // 3. 计算实际的连接点 | Calculate the actual connection points
86
- const newSourcePoint = getConnectionPoint(sourcePort || sourceNode, controlPoints[0] || targetPort || targetNode);
87
- const newTargetPoint = getConnectionPoint(
88
- targetPort || targetNode,
89
- controlPoints[controlPoints.length - 1] || sourcePort || sourceNode,
90
- );
91
-
92
- // 4. 获取路径 | Get the path
93
- return getPolylinePath([newSourcePoint, ...controlPoints, newTargetPoint], radius);
62
+ const [sourcePoint, targetPoint] = this.getEndpoints(attributes, false);
63
+
64
+ let controlPoints: Point[] = [];
65
+
66
+ if (!router) {
67
+ controlPoints = attributes.controlPoints;
68
+ } else {
69
+ if (router.type === 'shortest-path') {
70
+ const nodes = this.context.element!.getNodes();
71
+ controlPoints = aStarSearch(sourceNode, targetNode, nodes, router);
72
+ if (!controlPoints.length) {
73
+ controlPoints = orth(sourcePoint, targetPoint, sourceNode, targetNode, attributes.controlPoints, {
74
+ padding: router.offset,
75
+ });
76
+ }
77
+ } else if (router.type === 'orth') {
78
+ controlPoints = orth(sourcePoint, targetPoint, sourceNode, targetNode, attributes.controlPoints, router);
79
+ }
80
+ }
81
+
82
+ return controlPoints;
94
83
  }
95
84
 
96
- private getEndpointsAndPorts(attributes: ParsedPolylineStyleProps): {
97
- sourcePoint: Point;
98
- targetPoint: Point;
99
- sourcePort: Port | undefined;
100
- targetPort: Port | undefined;
101
- } {
102
- const { sourcePort: sourcePortKey, targetPort: targetPortKey } = attributes;
103
- const { sourceNode, targetNode } = this;
85
+ protected getPoints(attributes: ParsedPolylineStyleProps): Point[] {
86
+ const controlPoints = this.getControlPoints(attributes);
104
87
 
105
- const [sourcePort, targetPort] = findPorts(sourceNode, targetNode, sourcePortKey, targetPortKey);
106
-
107
- return {
108
- sourcePoint: sourcePort ? getPortPosition(sourcePort) : sourceNode.getCenter(),
109
- targetPoint: targetPort ? getPortPosition(targetPort) : targetNode.getCenter(),
110
- sourcePort,
111
- targetPort,
112
- };
88
+ const [newSourcePoint, newTargetPoint] = this.getEndpoints(attributes, true, controlPoints);
89
+ return [newSourcePoint, ...controlPoints, newTargetPoint];
113
90
  }
114
91
 
115
- protected getControlPoints(attributes: ParsedPolylineStyleProps, sourcePoint: Point, targetPoint: Point): Point[] {
116
- const { controlPoints, router, routerPadding } = attributes;
117
- const { sourceNode, targetNode } = this;
118
-
119
- if (!router) return [...controlPoints];
120
-
121
- const sourceBBox = getNodeBBox(sourceNode, routerPadding);
122
- const targetBBox = getNodeBBox(targetNode, routerPadding);
123
-
124
- return orth(sourcePoint, targetPoint, sourceBBox, targetBBox, controlPoints, routerPadding);
92
+ protected getKeyPath(attributes: ParsedPolylineStyleProps): PathArray {
93
+ const points = this.getPoints(attributes);
94
+ return getPolylinePath(points, attributes.radius);
125
95
  }
126
96
 
127
97
  protected getLoopPath(attributes: ParsedPolylineStyleProps): PathArray {
@@ -129,7 +99,8 @@ export class Polyline extends BaseEdge {
129
99
  const node = this.sourceNode;
130
100
 
131
101
  const bbox = getNodeBBox(node);
132
- // 默认转折点距离为 bbox 的最大宽高的 1/4 | Default distance of the turning point is 1/4 of the maximum width and height of the bbox
102
+ // 默认转折点距离为 bbox 的最大宽高的 1/4
103
+ // Default distance of the turning point is 1/4 of the maximum width and height of the bbox
133
104
  const defaultDist = Math.max(getBBoxWidth(bbox), getBBoxHeight(bbox)) / 4;
134
105
 
135
106
  const {
@@ -1,13 +1,16 @@
1
1
  import type { BaseStyleProps, DisplayObject, DisplayObjectConfig, Group, IAnimation } from '@antv/g';
2
2
  import { CustomElement } from '@antv/g';
3
3
  import { isEmpty, isFunction, upperFirst } from '@antv/util';
4
+ import { ExtensionCategory } from '../../constants';
4
5
  import type { Keyframe } from '../../types';
5
6
  import { createAnimationsProxy, preprocessKeyframes } from '../../utils/animation';
6
7
  import { updateStyle } from '../../utils/element';
7
8
  import { subObject } from '../../utils/prefix';
9
+ import { format } from '../../utils/print';
8
10
  import { getSubShapeStyle } from '../../utils/style';
9
11
  import { replaceTranslateInTransform } from '../../utils/transform';
10
12
  import { setVisibility } from '../../utils/visibility';
13
+ import { getExtension } from './../../registry/get';
11
14
 
12
15
  export interface BaseShapeStyleProps extends BaseStyleProps {}
13
16
 
@@ -46,37 +49,55 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
46
49
  * @param Ctor - <zh> 图形类型 | <en> shape type
47
50
  * @param style - <zh> 图形样式 | <en> shape style
48
51
  * @param container - <zh> 容器 | <en> container
52
+ * @param hooks - <zh> 钩子函数 | <en> hooks
49
53
  * @returns <zh> 图形实例 | <en> shape instance
50
54
  */
51
55
  protected upsert<T extends DisplayObject>(
52
56
  className: string,
53
- Ctor: { new (...args: any[]): T },
57
+ Ctor: string | { new (...args: any[]): T },
54
58
  style: T['attributes'] | false,
55
59
  container: DisplayObject,
60
+ hooks?: UpsertHooks,
56
61
  ): T | undefined {
57
62
  const target = this.shapeMap[className] as T | undefined;
58
63
  // remove
59
64
  // 如果 style 为 false,则删除图形 / remove shape if style is false
60
65
  if (style === false) {
61
66
  if (target) {
67
+ hooks?.beforeDestroy?.(target);
62
68
  container.removeChild(target);
63
69
  delete this.shapeMap[className];
70
+ hooks?.afterDestroy?.(target);
64
71
  }
65
72
  return;
66
73
  }
67
74
 
75
+ const _Ctor = typeof Ctor === 'string' ? getExtension(ExtensionCategory.SHAPE, Ctor) : Ctor;
76
+
77
+ if (!_Ctor) {
78
+ throw new Error(format(`Shape ${Ctor} not found`));
79
+ }
80
+
68
81
  // create
69
- if (!target || target.destroyed || !(target instanceof Ctor)) {
70
- target?.destroy();
82
+ if (!target || target.destroyed || !(target instanceof _Ctor)) {
83
+ if (target) {
84
+ hooks?.beforeDestroy?.(target);
85
+ target?.destroy();
86
+ hooks?.afterDestroy?.(target);
87
+ }
71
88
 
72
- const instance = new Ctor({ className, style });
89
+ hooks?.beforeCreate?.();
90
+ const instance = new _Ctor({ className, style });
73
91
  container.appendChild(instance);
74
92
  this.shapeMap[className] = instance;
75
- return instance;
93
+ hooks?.afterCreate?.(instance);
94
+ return instance as T;
76
95
  }
77
96
 
78
97
  // update
98
+ hooks?.beforeUpdate?.(target);
79
99
  updateStyle(target, style);
100
+ hooks?.afterUpdate?.(target);
80
101
 
81
102
  return target;
82
103
  }
@@ -222,7 +243,7 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
222
243
 
223
244
  private setVisibility() {
224
245
  const { visibility } = this.attributes;
225
- setVisibility(this, visibility);
246
+ setVisibility(this, visibility, true);
226
247
  }
227
248
 
228
249
  public destroy(): void {
@@ -248,3 +269,52 @@ function releaseAnimation(target: DisplayObject, animation: IAnimation) {
248
269
  if (index > -1) target.activeAnimations.splice(index, 1);
249
270
  });
250
271
  }
272
+
273
+ /**
274
+ * <zh/> 图形 upsert 方法生命周期钩子
275
+ *
276
+ * <en/> Shape upsert method lifecycle hooks
277
+ */
278
+ export interface UpsertHooks {
279
+ /**
280
+ * <zh/> 图形创建前
281
+ *
282
+ * <en/> Before creating the shape
283
+ */
284
+ beforeCreate?: () => void;
285
+ /**
286
+ * <zh/> 图形创建后
287
+ *
288
+ * <en/> After creating the shape
289
+ * @param instance - <zh/> 图形实例 | <en/> shape instance
290
+ */
291
+ afterCreate?: (instance: DisplayObject) => void;
292
+ /**
293
+ * <zh/> 图形更新前
294
+ *
295
+ * <en/> Before updating the shape
296
+ * @param instance - <zh/> 图形实例 | <en/> shape instance
297
+ */
298
+ beforeUpdate?: (instance: DisplayObject) => void;
299
+ /**
300
+ * <zh/> 图形更新后
301
+ *
302
+ * <en/> After updating the shape
303
+ * @param instance - <zh/> 图形实例 | <en/> shape instance
304
+ */
305
+ afterUpdate?: (instance: DisplayObject) => void;
306
+ /**
307
+ * <zh/> 图形销毁前
308
+ *
309
+ * <en/> Before destroying the shape
310
+ * @param instance - <zh/> 图形实例 | <en/> shape instance
311
+ */
312
+ beforeDestroy?: (instance: DisplayObject) => void;
313
+ /**
314
+ * <zh/> 图形销毁后
315
+ *
316
+ * <en/> After destroying the shape
317
+ * @param instance - <zh/> 图形实例 | <en/> shape instance
318
+ */
319
+ afterDestroy?: (instance: DisplayObject) => void;
320
+ }
package/src/exports.ts CHANGED
@@ -67,12 +67,14 @@ export {
67
67
  BubbleSets,
68
68
  CameraSetting,
69
69
  Contextmenu,
70
+ EdgeFilterLens,
70
71
  Fullscreen,
71
72
  GridLine,
72
73
  History,
73
74
  Hull,
74
75
  Legend,
75
76
  Minimap,
77
+ Snapline,
76
78
  Timebar,
77
79
  Toolbar,
78
80
  Tooltip,
@@ -158,6 +160,7 @@ export type {
158
160
  LabelStyleProps,
159
161
  PolygonStyleProps,
160
162
  } from './elements/shapes';
163
+ export type { UpsertHooks } from './elements/shapes/base-shape';
161
164
  export type { ContourLabelStyleProps, ContourStyleProps } from './elements/shapes/contour';
162
165
  export type { BaseLayoutOptions, WebWorkerLayoutOptions } from './layouts/types';
163
166
  export type { CategoricalPalette } from './palettes/types';
@@ -166,12 +169,14 @@ export type {
166
169
  BubbleSetsOptions,
167
170
  CameraSettingOptions,
168
171
  ContextmenuOptions,
172
+ EdgeFilterLensOptions,
169
173
  FullscreenOptions,
170
174
  GridLineOptions,
171
175
  HistoryOptions,
172
176
  HullOptions,
173
177
  LegendOptions,
174
178
  MinimapOptions,
179
+ SnaplineOptions,
175
180
  TimebarOptions,
176
181
  ToolbarOptions,
177
182
  TooltipOptions,