@antv/g6 5.0.14 → 5.0.16

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 (173) 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/scroll-canvas.d.ts +7 -0
  4. package/dist/lib/behaviors/zoom-canvas.d.ts +7 -0
  5. package/dist/lib/elements/edges/base-edge.d.ts +2 -2
  6. package/dist/lib/elements/edges/polyline.d.ts +3 -20
  7. package/dist/lib/plugins/contextmenu/index.d.ts +3 -1
  8. package/dist/lib/plugins/index.d.ts +2 -0
  9. package/dist/lib/plugins/minimap/index.d.ts +2 -0
  10. package/dist/lib/plugins/snapline/index.d.ts +95 -0
  11. package/dist/lib/runtime/canvas.d.ts +17 -1
  12. package/dist/lib/runtime/element.d.ts +1 -0
  13. package/dist/lib/runtime/viewport.d.ts +8 -0
  14. package/dist/lib/types/index.d.ts +1 -0
  15. package/dist/lib/types/router.d.ts +103 -0
  16. package/dist/lib/utils/bbox.d.ts +8 -0
  17. package/dist/lib/utils/router/orth.d.ts +8 -8
  18. package/dist/lib/utils/router/shortest-path.d.ts +57 -0
  19. package/dist/lib/utils/text.d.ts +1 -1
  20. package/dist/lib/utils/visibility.d.ts +2 -1
  21. package/dist/lib/version.d.ts +1 -1
  22. package/esm/behaviors/brush-select.js +11 -6
  23. package/esm/behaviors/brush-select.js.map +1 -1
  24. package/esm/behaviors/drag-canvas.d.ts +13 -0
  25. package/esm/behaviors/drag-canvas.js +10 -1
  26. package/esm/behaviors/drag-canvas.js.map +1 -1
  27. package/esm/behaviors/optimize-viewport-transform.js +1 -1
  28. package/esm/behaviors/optimize-viewport-transform.js.map +1 -1
  29. package/esm/behaviors/scroll-canvas.d.ts +7 -0
  30. package/esm/behaviors/scroll-canvas.js +3 -1
  31. package/esm/behaviors/scroll-canvas.js.map +1 -1
  32. package/esm/behaviors/zoom-canvas.d.ts +7 -0
  33. package/esm/behaviors/zoom-canvas.js +3 -1
  34. package/esm/behaviors/zoom-canvas.js.map +1 -1
  35. package/esm/elements/edges/base-edge.d.ts +2 -2
  36. package/esm/elements/edges/base-edge.js +10 -4
  37. package/esm/elements/edges/base-edge.js.map +1 -1
  38. package/esm/elements/edges/polyline.d.ts +3 -20
  39. package/esm/elements/edges/polyline.js +29 -34
  40. package/esm/elements/edges/polyline.js.map +1 -1
  41. package/esm/elements/shapes/base-shape.js +1 -1
  42. package/esm/elements/shapes/base-shape.js.map +1 -1
  43. package/esm/plugins/contextmenu/index.d.ts +3 -1
  44. package/esm/plugins/contextmenu/index.js +4 -1
  45. package/esm/plugins/contextmenu/index.js.map +1 -1
  46. package/esm/plugins/index.d.ts +2 -0
  47. package/esm/plugins/index.js +1 -0
  48. package/esm/plugins/index.js.map +1 -1
  49. package/esm/plugins/minimap/index.d.ts +2 -0
  50. package/esm/plugins/minimap/index.js +24 -12
  51. package/esm/plugins/minimap/index.js.map +1 -1
  52. package/esm/plugins/snapline/index.d.ts +95 -0
  53. package/esm/plugins/snapline/index.js +268 -0
  54. package/esm/plugins/snapline/index.js.map +1 -0
  55. package/esm/registry/build-in.js +3 -2
  56. package/esm/registry/build-in.js.map +1 -1
  57. package/esm/runtime/canvas.d.ts +17 -1
  58. package/esm/runtime/canvas.js +26 -3
  59. package/esm/runtime/canvas.js.map +1 -1
  60. package/esm/runtime/element.d.ts +1 -0
  61. package/esm/runtime/element.js +11 -5
  62. package/esm/runtime/element.js.map +1 -1
  63. package/esm/runtime/viewport.d.ts +8 -0
  64. package/esm/runtime/viewport.js +35 -2
  65. package/esm/runtime/viewport.js.map +1 -1
  66. package/esm/types/index.d.ts +1 -0
  67. package/esm/types/router.d.ts +103 -0
  68. package/esm/types/router.js +2 -0
  69. package/esm/types/router.js.map +1 -0
  70. package/esm/utils/bbox.d.ts +8 -0
  71. package/esm/utils/bbox.js +19 -0
  72. package/esm/utils/bbox.js.map +1 -1
  73. package/esm/utils/router/orth.d.ts +8 -8
  74. package/esm/utils/router/orth.js +13 -7
  75. package/esm/utils/router/orth.js.map +1 -1
  76. package/esm/utils/router/shortest-path.d.ts +57 -0
  77. package/esm/utils/router/shortest-path.js +387 -0
  78. package/esm/utils/router/shortest-path.js.map +1 -0
  79. package/esm/utils/text.d.ts +1 -1
  80. package/esm/utils/visibility.d.ts +2 -1
  81. package/esm/utils/visibility.js +3 -2
  82. package/esm/utils/visibility.js.map +1 -1
  83. package/esm/version.d.ts +1 -1
  84. package/esm/version.js +1 -1
  85. package/lib/behaviors/brush-select.js +11 -6
  86. package/lib/behaviors/brush-select.js.map +1 -1
  87. package/lib/behaviors/drag-canvas.d.ts +13 -0
  88. package/lib/behaviors/drag-canvas.js +10 -1
  89. package/lib/behaviors/drag-canvas.js.map +1 -1
  90. package/lib/behaviors/optimize-viewport-transform.js +1 -1
  91. package/lib/behaviors/optimize-viewport-transform.js.map +1 -1
  92. package/lib/behaviors/scroll-canvas.d.ts +7 -0
  93. package/lib/behaviors/scroll-canvas.js +3 -1
  94. package/lib/behaviors/scroll-canvas.js.map +1 -1
  95. package/lib/behaviors/zoom-canvas.d.ts +7 -0
  96. package/lib/behaviors/zoom-canvas.js +3 -1
  97. package/lib/behaviors/zoom-canvas.js.map +1 -1
  98. package/lib/elements/edges/base-edge.d.ts +2 -2
  99. package/lib/elements/edges/base-edge.js +9 -3
  100. package/lib/elements/edges/base-edge.js.map +1 -1
  101. package/lib/elements/edges/polyline.d.ts +3 -20
  102. package/lib/elements/edges/polyline.js +29 -34
  103. package/lib/elements/edges/polyline.js.map +1 -1
  104. package/lib/elements/shapes/base-shape.js +1 -1
  105. package/lib/elements/shapes/base-shape.js.map +1 -1
  106. package/lib/plugins/contextmenu/index.d.ts +3 -1
  107. package/lib/plugins/contextmenu/index.js +4 -1
  108. package/lib/plugins/contextmenu/index.js.map +1 -1
  109. package/lib/plugins/index.d.ts +2 -0
  110. package/lib/plugins/index.js +3 -1
  111. package/lib/plugins/index.js.map +1 -1
  112. package/lib/plugins/minimap/index.d.ts +2 -0
  113. package/lib/plugins/minimap/index.js +24 -12
  114. package/lib/plugins/minimap/index.js.map +1 -1
  115. package/lib/plugins/snapline/index.d.ts +95 -0
  116. package/lib/plugins/snapline/index.js +272 -0
  117. package/lib/plugins/snapline/index.js.map +1 -0
  118. package/lib/registry/build-in.js +2 -1
  119. package/lib/registry/build-in.js.map +1 -1
  120. package/lib/runtime/canvas.d.ts +17 -1
  121. package/lib/runtime/canvas.js +26 -3
  122. package/lib/runtime/canvas.js.map +1 -1
  123. package/lib/runtime/element.d.ts +1 -0
  124. package/lib/runtime/element.js +10 -4
  125. package/lib/runtime/element.js.map +1 -1
  126. package/lib/runtime/viewport.d.ts +8 -0
  127. package/lib/runtime/viewport.js +34 -1
  128. package/lib/runtime/viewport.js.map +1 -1
  129. package/lib/types/index.d.ts +1 -0
  130. package/lib/types/router.d.ts +103 -0
  131. package/lib/types/router.js +3 -0
  132. package/lib/types/router.js.map +1 -0
  133. package/lib/utils/bbox.d.ts +8 -0
  134. package/lib/utils/bbox.js +20 -0
  135. package/lib/utils/bbox.js.map +1 -1
  136. package/lib/utils/router/orth.d.ts +8 -8
  137. package/lib/utils/router/orth.js +13 -7
  138. package/lib/utils/router/orth.js.map +1 -1
  139. package/lib/utils/router/shortest-path.d.ts +57 -0
  140. package/lib/utils/router/shortest-path.js +394 -0
  141. package/lib/utils/router/shortest-path.js.map +1 -0
  142. package/lib/utils/text.d.ts +1 -1
  143. package/lib/utils/visibility.d.ts +2 -1
  144. package/lib/utils/visibility.js +3 -2
  145. package/lib/utils/visibility.js.map +1 -1
  146. package/lib/version.d.ts +1 -1
  147. package/lib/version.js +1 -1
  148. package/package.json +1 -1
  149. package/src/behaviors/brush-select.ts +12 -5
  150. package/src/behaviors/drag-canvas.ts +24 -1
  151. package/src/behaviors/optimize-viewport-transform.ts +1 -0
  152. package/src/behaviors/scroll-canvas.ts +9 -1
  153. package/src/behaviors/zoom-canvas.ts +9 -1
  154. package/src/elements/edges/base-edge.ts +19 -5
  155. package/src/elements/edges/polyline.ts +33 -68
  156. package/src/elements/shapes/base-shape.ts +1 -1
  157. package/src/plugins/contextmenu/index.ts +8 -3
  158. package/src/plugins/grid-line.ts +1 -1
  159. package/src/plugins/index.ts +2 -0
  160. package/src/plugins/minimap/index.ts +28 -15
  161. package/src/plugins/snapline/index.ts +383 -0
  162. package/src/registry/build-in.ts +3 -1
  163. package/src/runtime/canvas.ts +26 -3
  164. package/src/runtime/element.ts +12 -5
  165. package/src/runtime/viewport.ts +42 -3
  166. package/src/types/index.ts +1 -0
  167. package/src/types/router.ts +107 -0
  168. package/src/utils/bbox.ts +24 -0
  169. package/src/utils/router/orth.ts +19 -10
  170. package/src/utils/router/shortest-path.ts +470 -0
  171. package/src/utils/text.ts +1 -1
  172. package/src/utils/visibility.ts +3 -1
  173. package/src/version.ts +1 -1
@@ -144,9 +144,16 @@ export class BrushSelect extends BaseBehavior<BrushSelectOptions> {
144
144
  */
145
145
  protected onPointerDown(event: IPointerEvent) {
146
146
  if (!this.validate(event) || !this.isKeydown() || this.startPoint) return;
147
- const { canvas } = this.context;
147
+ const { canvas, graph } = this.context;
148
+ const style = { ...this.options.style };
148
149
 
149
- this.rectShape = new Rect({ id: 'g6-brush-select', style: this.options.style });
150
+ // 根据缩放比例调整 lineWidth
151
+ // Adjust lineWidth according to the zoom ratio
152
+ if (this.options.style.lineWidth) {
153
+ style.lineWidth = +this.options.style.lineWidth / graph.getZoom();
154
+ }
155
+
156
+ this.rectShape = new Rect({ id: 'g6-brush-select', style });
150
157
  canvas.appendChild(this.rectShape);
151
158
 
152
159
  this.startPoint = [event.canvas.x, event.canvas.y];
@@ -327,8 +334,7 @@ export class BrushSelect extends BaseBehavior<BrushSelectOptions> {
327
334
  protected isKeydown(): boolean {
328
335
  const { trigger } = this.options;
329
336
  const keys = (Array.isArray(trigger) ? trigger : [trigger]) as string[];
330
- if (!trigger || keys.includes('drag')) return true;
331
- return this.shortcut!.match(keys);
337
+ return this.shortcut!.match(keys.filter((key) => key !== 'drag'));
332
338
  }
333
339
 
334
340
  /**
@@ -348,7 +354,6 @@ export class BrushSelect extends BaseBehavior<BrushSelectOptions> {
348
354
 
349
355
  private bindEvents() {
350
356
  const { graph } = this.context;
351
- this.unbindEvents();
352
357
 
353
358
  graph.on(CommonEvent.POINTER_DOWN, this.onPointerDown);
354
359
  graph.on(CommonEvent.POINTER_MOVE, this.onPointerMove);
@@ -373,7 +378,9 @@ export class BrushSelect extends BaseBehavior<BrushSelectOptions> {
373
378
  * @internal
374
379
  */
375
380
  public update(options: Partial<BrushSelectOptions>) {
381
+ this.unbindEvents();
376
382
  this.options = deepMix(this.options, options);
383
+ this.bindEvents();
377
384
  }
378
385
 
379
386
  /**
@@ -28,6 +28,19 @@ export interface DragCanvasOptions extends BaseBehaviorOptions {
28
28
  * @defaultValue true
29
29
  */
30
30
  enable?: boolean | ((event: IPointerEvent | IKeyboardEvent) => boolean);
31
+ /**
32
+ * <zh/> 允许拖拽方向
33
+ * - `'x'`: 只允许水平拖拽
34
+ * - `'y'`: 只允许垂直拖拽
35
+ * - `'both'`: 不受限制,允许水平和垂直拖拽
36
+ *
37
+ * <en/> Allowed drag direction
38
+ * - `'x'`: Only allow horizontal drag
39
+ * - `'y'`: Only allow vertical drag
40
+ * - `'both'`: Allow horizontal and vertical drag
41
+ * @defaultValue `'both'`
42
+ */
43
+ direction?: 'x' | 'y' | 'both';
31
44
  /**
32
45
  * <zh/> 触发拖拽的方式,默认使用指针按下拖拽
33
46
  *
@@ -66,6 +79,7 @@ export class DragCanvas extends BaseBehavior<DragCanvasOptions> {
66
79
  return true;
67
80
  },
68
81
  sensitivity: 10,
82
+ direction: 'both',
69
83
  };
70
84
 
71
85
  private shortcut: Shortcut;
@@ -155,7 +169,16 @@ export class DragCanvas extends BaseBehavior<DragCanvasOptions> {
155
169
  * @internal
156
170
  */
157
171
  protected async translate(offset: Vector2, animation?: ViewportAnimationEffectTiming) {
158
- await this.context.graph.translateBy(offset, animation);
172
+ let [dx, dy] = offset;
173
+
174
+ const { direction } = this.options;
175
+ if (direction === 'x') {
176
+ dy = 0;
177
+ } else if (direction === 'y') {
178
+ dx = 0;
179
+ }
180
+
181
+ await this.context.graph.translateBy([dx, dy], animation);
159
182
  }
160
183
 
161
184
  private validate(event: IPointerEvent | IKeyboardEvent) {
@@ -72,6 +72,7 @@ export class OptimizeViewportTransform extends BaseBehavior<OptimizeViewportTran
72
72
  setVisibility(
73
73
  element,
74
74
  visibility,
75
+ false,
75
76
  (shape) => !!shape.className && !excludedClassnames?.includes(shape.className),
76
77
  );
77
78
  });
@@ -55,6 +55,13 @@ export interface ScrollCanvasOptions extends BaseBehaviorOptions {
55
55
  * <en/> Callback when scrolling is completed
56
56
  */
57
57
  onFinish?: () => void;
58
+ /**
59
+ * <zh/> 是否阻止默认事件
60
+ *
61
+ * <en/> Whether to prevent the default event
62
+ * @defaultValue true
63
+ */
64
+ preventDefault?: boolean;
58
65
  }
59
66
 
60
67
  /**
@@ -66,6 +73,7 @@ export class ScrollCanvas extends BaseBehavior<ScrollCanvasOptions> {
66
73
  static defaultOptions: Partial<ScrollCanvasOptions> = {
67
74
  enable: true,
68
75
  sensitivity: 1,
76
+ preventDefault: true,
69
77
  };
70
78
 
71
79
  private shortcut: Shortcut;
@@ -115,7 +123,7 @@ export class ScrollCanvas extends BaseBehavior<ScrollCanvasOptions> {
115
123
  }
116
124
 
117
125
  private onWheel = async (event: WheelEvent) => {
118
- event.preventDefault();
126
+ if (this.options.preventDefault) event.preventDefault();
119
127
  const diffX = event.deltaX;
120
128
  const diffY = event.deltaY;
121
129
 
@@ -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
  /**
@@ -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,42 @@ 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 getPoints(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
+ const [newSourcePoint, newTargetPoint] = this.getEndpoints(attributes, true, controlPoints);
83
+ return [newSourcePoint, ...controlPoints, newTargetPoint];
94
84
  }
95
85
 
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;
104
-
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
- };
113
- }
114
-
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);
86
+ protected getKeyPath(attributes: ParsedPolylineStyleProps): PathArray {
87
+ const points = this.getPoints(attributes);
88
+ return getPolylinePath(points, attributes.radius);
125
89
  }
126
90
 
127
91
  protected getLoopPath(attributes: ParsedPolylineStyleProps): PathArray {
@@ -129,7 +93,8 @@ export class Polyline extends BaseEdge {
129
93
  const node = this.sourceNode;
130
94
 
131
95
  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
96
+ // 默认转折点距离为 bbox 的最大宽高的 1/4
97
+ // Default distance of the turning point is 1/4 of the maximum width and height of the bbox
133
98
  const defaultDist = Math.max(getBBoxWidth(bbox), getBBoxHeight(bbox)) / 4;
134
99
 
135
100
  const {
@@ -222,7 +222,7 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
222
222
 
223
223
  private setVisibility() {
224
224
  const { visibility } = this.attributes;
225
- setVisibility(this, visibility);
225
+ setVisibility(this, visibility, true);
226
226
  }
227
227
 
228
228
  public destroy(): void {
@@ -1,11 +1,11 @@
1
1
  import type { RuntimeContext } from '../../runtime/types';
2
+ import type { Element } from '../../types';
2
3
  import type { IElementEvent } from '../../types/event';
3
4
  import { createPluginContainer, insertDOM } from '../../utils/dom';
4
5
  import type { BasePluginOptions } from '../base-plugin';
5
6
  import { BasePlugin } from '../base-plugin';
6
7
  import type { Item } from './util';
7
8
  import { CONTEXTMENU_CSS, getContentFromItems } from './util';
8
-
9
9
  /**
10
10
  * <zh/> 上下文菜单配置项
11
11
  *
@@ -42,7 +42,7 @@ export interface ContextmenuOptions extends BasePluginOptions {
42
42
  *
43
43
  * <en/> The callback method triggered when the menu is clicked
44
44
  */
45
- onClick?: (value: string, target: HTMLElement) => void;
45
+ onClick?: (value: string, target: HTMLElement, current: Element) => void;
46
46
  /**
47
47
  * <zh/> 返回菜单的项目列表,支持 `Promise` 类型的返回值。是 `getContent` 的快捷配置
48
48
  *
@@ -90,6 +90,8 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
90
90
 
91
91
  private $element!: HTMLElement;
92
92
 
93
+ private targetElement: Element | null = null;
94
+
93
95
  constructor(context: RuntimeContext, options: ContextmenuOptions) {
94
96
  super(context, Object.assign({}, Contextmenu.defaultOptions, options));
95
97
 
@@ -138,6 +140,8 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
138
140
  this.$element.style.left = `${event.client.x - clientRect.left + offset[0]}px`;
139
141
  this.$element.style.top = `${event.client.y - clientRect.top + offset[1]}px`;
140
142
  this.$element.style.display = 'block';
143
+
144
+ this.targetElement = event.target;
141
145
  }
142
146
 
143
147
  /**
@@ -147,6 +151,7 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
147
151
  */
148
152
  public hide() {
149
153
  this.$element.style.display = 'none';
154
+ this.targetElement = null;
150
155
  }
151
156
 
152
157
  /**
@@ -218,7 +223,7 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
218
223
  if (event.target instanceof HTMLElement) {
219
224
  if (event.target.className.includes('g6-contextmenu-li')) {
220
225
  const value = event.target.getAttribute('value') as string;
221
- onClick?.(value, event.target);
226
+ onClick?.(value, event.target, this.targetElement!);
222
227
  this.hide();
223
228
  }
224
229
  }
@@ -1,6 +1,6 @@
1
1
  import { GraphEvent } from '../constants';
2
2
  import type { RuntimeContext } from '../runtime/types';
3
- import { Point } from '../types';
3
+ import type { Point } from '../types';
4
4
  import { createPluginContainer, insertBefore } from '../utils/dom';
5
5
  import { ViewportEvent } from '../utils/event';
6
6
  import { add, mod } from '../utils/vector';
@@ -9,6 +9,7 @@ export { History } from './history';
9
9
  export { Hull } from './hull';
10
10
  export { Legend } from './legend';
11
11
  export { Minimap } from './minimap';
12
+ export { Snapline } from './snapline';
12
13
  export { Timebar } from './timebar';
13
14
  export { Toolbar } from './toolbar';
14
15
  export { Tooltip } from './tooltip';
@@ -25,6 +26,7 @@ export type { HistoryOptions } from './history';
25
26
  export type { HullOptions } from './hull';
26
27
  export type { LegendOptions } from './legend';
27
28
  export type { MinimapOptions } from './minimap';
29
+ export type { SnaplineOptions } from './snapline';
28
30
  export type { TimebarOptions } from './timebar';
29
31
  export type { ToolbarOptions } from './toolbar';
30
32
  export type { TooltipOptions } from './tooltip';
@@ -1,10 +1,10 @@
1
- import { Canvas, DisplayObject, IRenderer } from '@antv/g';
1
+ import { Canvas, DisplayObject, IRenderer, Landmark } from '@antv/g';
2
2
  import { Renderer } from '@antv/g-canvas';
3
3
  import { throttle } from '@antv/util';
4
4
  import { GraphEvent } from '../../constants';
5
5
  import type { RuntimeContext } from '../../runtime/types';
6
6
  import { GraphData } from '../../spec';
7
- import type { ElementDatum, ElementType, ID, IGraphLifeCycleEvent, Padding, Placement } from '../../types';
7
+ import type { ElementDatum, ElementType, ID, IGraphLifeCycleEvent, Padding, Placement, Vector3 } from '../../types';
8
8
  import { idOf } from '../../utils/id';
9
9
  import { parsePadding } from '../../utils/padding';
10
10
  import { parsePlacement } from '../../utils/placement';
@@ -201,7 +201,11 @@ export class Minimap extends BasePlugin<MinimapOptions> {
201
201
  if (!this.shapes.has(id)) {
202
202
  canvas.appendChild(cloneShape);
203
203
  this.shapes.set(id, cloneShape);
204
- } else this.shapes.get(id)!.attr(shape.attributes);
204
+ } else {
205
+ Object.entries(shape.attributes).forEach(([key, value]) => {
206
+ if (cloneShape.style[key] !== value) cloneShape.style[key] = value;
207
+ });
208
+ }
205
209
  };
206
210
 
207
211
  // 注意执行顺序 / Note the execution order
@@ -308,6 +312,23 @@ export class Minimap extends BasePlugin<MinimapOptions> {
308
312
  return this.canvas;
309
313
  }
310
314
 
315
+ private landmarkMap = new Map<string, Landmark>();
316
+
317
+ private createLandmark(position: Vector3, focalPoint: Vector3, zoom: number) {
318
+ const key = `${position.join(',')}-${focalPoint.join(',')}-${zoom}`;
319
+
320
+ if (this.landmarkMap.has(key)) return this.landmarkMap.get(key)!;
321
+
322
+ const camera = this.canvas.getCamera();
323
+ const landmark = camera.createLandmark(key, {
324
+ position,
325
+ focalPoint,
326
+ zoom,
327
+ });
328
+ this.landmarkMap.set(key, landmark);
329
+ return landmark;
330
+ }
331
+
311
332
  private setCamera() {
312
333
  const { canvas } = this.context;
313
334
 
@@ -319,7 +340,7 @@ export class Minimap extends BasePlugin<MinimapOptions> {
319
340
  padding,
320
341
  } = this.options;
321
342
  const [top, right, bottom, left] = parsePadding(padding);
322
- const { min: boundsMin, max: boundsMax, center } = canvas.getBounds();
343
+ const { min: boundsMin, max: boundsMax, center } = canvas.getBounds('elements');
323
344
  const boundsWidth = boundsMax[0] - boundsMin[0];
324
345
  const boundsHeight = boundsMax[1] - boundsMin[1];
325
346
 
@@ -330,9 +351,8 @@ export class Minimap extends BasePlugin<MinimapOptions> {
330
351
  const scaleY = availableHeight / boundsHeight;
331
352
  const scale = Math.min(scaleX, scaleY);
332
353
 
333
- camera.setPosition(center);
334
- camera.setFocalPoint(center);
335
- camera.setZoom(scale);
354
+ const landmark = this.createLandmark(center, center, scale);
355
+ camera.gotoLandmark(landmark, 0);
336
356
  }
337
357
 
338
358
  private mask: HTMLElement | null = null;
@@ -349,14 +369,7 @@ export class Minimap extends BasePlugin<MinimapOptions> {
349
369
  const width = maskMax.x - maskMin.x;
350
370
  const height = maskMax.y - maskMin.y;
351
371
 
352
- const zoom = this.context.canvas.getCamera().getZoom();
353
-
354
- // magic number
355
- const ratio = zoom * 0.5;
356
- const x = maskMin.x - width * ratio;
357
- const y = maskMin.y - height * ratio;
358
-
359
- return [x, y, width, height];
372
+ return [maskMin.x, maskMin.y, width, height];
360
373
  }
361
374
 
362
375
  /**