@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
@@ -0,0 +1,383 @@
1
+ import { AABB, BaseStyleProps, DisplayObject, Line, LineStyleProps } from '@antv/g';
2
+ import { isEqual } from '@antv/util';
3
+ import { NodeEvent } from '../../constants';
4
+ import type { RuntimeContext } from '../../runtime/types';
5
+ import type { ID, IDragEvent, Node } from '../../types';
6
+ import { isVisible } from '../../utils/element';
7
+ import { divide } from '../../utils/vector';
8
+ import type { BasePluginOptions } from '../base-plugin';
9
+ import { BasePlugin } from '../base-plugin';
10
+
11
+ export interface SnaplineOptions extends BasePluginOptions {
12
+ /**
13
+ * <zh/> 对齐精度,即移动节点时与目标位置的距离小于 tolerance 时触发显示对齐线
14
+ *
15
+ * <en/> The alignment accuracy, that is, when the distance between the moved node and the target position is less than tolerance, the alignment line is displayed
16
+ * @defaultValue 5
17
+ */
18
+ tolerance?: number;
19
+ /**
20
+ * <zh/> 对齐线头尾的延伸距离。取值范围:[0, Infinity]
21
+ *
22
+ * <en/> The extension distance of the snapline. The value range is [0, Infinity]
23
+ * @defaultValue 20
24
+ */
25
+ offset?: number;
26
+ /**
27
+ * <zh/> 是否启用自动吸附
28
+ *
29
+ * <en/> Whether to enable automatic adsorption
30
+ * @defaultValue true
31
+ */
32
+ autoSnap?: boolean;
33
+ /**
34
+ * <zh/> 指定元素上的哪个图形作为参照图形
35
+ *
36
+ * <en/> Specifies which shape on the element to use as the reference shape
37
+ * @defaultValue `'key'`
38
+ * @remarks
39
+ * <zh/>
40
+ * - 'key' 使用元素的主图形作为参照图形
41
+ * - 也可以传入一个函数,接收元素对象,返回一个图形
42
+ *
43
+ * <en/>
44
+ * - `'key'` uses the key shape of the element as the reference shape
45
+ * - You can also pass in a function that receives the element and returns a shape
46
+ */
47
+ shape?: string | ((node: Node) => DisplayObject);
48
+ /**
49
+ * <zh/> 垂直对齐线样式
50
+ *
51
+ * <en/> Vertical snapline style
52
+ * @defaultValue `{ stroke: '#1783FF' }`
53
+ */
54
+ verticalLineStyle?: BaseStyleProps;
55
+ /**
56
+ * <zh/> 水平对齐线样式
57
+ *
58
+ * <en/> Horizontal snapline style
59
+ * @defaultValue `{ stroke: '#1783FF' }`
60
+ */
61
+ horizontalLineStyle?: BaseStyleProps;
62
+ /**
63
+ * <zh/> 过滤器,用于过滤不需要作为参考的节点
64
+ *
65
+ * <en/> Filter, used to filter nodes that do not need to be used as references
66
+ * @defaultValue `() => true`
67
+ */
68
+ filter?: (node: Node) => boolean;
69
+ }
70
+
71
+ const defaultLineStyle: LineStyleProps = { x1: 0, y1: 0, x2: 0, y2: 0, visibility: 'hidden' };
72
+
73
+ type Metadata = {
74
+ verticalX: number | null;
75
+ verticalMinY: number | null;
76
+ verticalMaxY: number | null;
77
+ horizontalY: number | null;
78
+ horizontalMinX: number | null;
79
+ horizontalMaxX: number | null;
80
+ };
81
+
82
+ export class Snapline extends BasePlugin<SnaplineOptions> {
83
+ static defaultOptions: Partial<SnaplineOptions> = {
84
+ tolerance: 5,
85
+ offset: 20,
86
+ autoSnap: true,
87
+ shape: 'key',
88
+ verticalLineStyle: { stroke: '#1783FF' },
89
+ horizontalLineStyle: { stroke: '#1783FF' },
90
+ filter: () => true,
91
+ };
92
+
93
+ private horizontalLine!: Line;
94
+ private verticalLine!: Line;
95
+
96
+ constructor(context: RuntimeContext, options: SnaplineOptions) {
97
+ super(context, Object.assign({}, Snapline.defaultOptions, options));
98
+ this.bindEvents();
99
+ }
100
+
101
+ private initSnapline = () => {
102
+ const canvas = this.context.canvas.getLayer('transient');
103
+
104
+ if (!this.horizontalLine) {
105
+ this.horizontalLine = canvas.appendChild(
106
+ new Line({ style: { ...defaultLineStyle, ...this.options.horizontalLineStyle } }),
107
+ );
108
+ }
109
+
110
+ if (!this.verticalLine) {
111
+ this.verticalLine = canvas.appendChild(
112
+ new Line({ style: { ...defaultLineStyle, ...this.options.verticalLineStyle } }),
113
+ );
114
+ }
115
+ };
116
+
117
+ private getNodes(): Node[] {
118
+ const { filter } = this.options;
119
+ const allNodes = this.context.element?.getNodes() || [];
120
+
121
+ // 不考虑超出画布视口范围、不可见的节点
122
+ // Nodes that are out of the canvas viewport range, invisible are not considered
123
+ const nodes = allNodes.filter((node) => {
124
+ return isVisible(node) && this.context.viewport?.isInViewport(node.getRenderBounds());
125
+ });
126
+
127
+ if (!filter) return nodes;
128
+
129
+ return nodes.filter((node) => filter(node));
130
+ }
131
+
132
+ private hideSnapline() {
133
+ this.horizontalLine.style.visibility = 'hidden';
134
+ this.verticalLine.style.visibility = 'hidden';
135
+ }
136
+
137
+ private getLineWidth(direction: 'horizontal' | 'vertical') {
138
+ const { lineWidth } = this.options[`${direction}LineStyle`] as LineStyleProps;
139
+ return +(lineWidth || defaultLineStyle.lineWidth || 1) / this.context.graph.getZoom();
140
+ }
141
+
142
+ private updateSnapline(metadata: Metadata) {
143
+ const { verticalX, verticalMinY, verticalMaxY, horizontalY, horizontalMinX, horizontalMaxX } = metadata;
144
+ const [canvasWidth, canvasHeight] = this.context.canvas.getSize();
145
+ const { offset } = this.options;
146
+
147
+ if (horizontalY !== null) {
148
+ Object.assign(this.horizontalLine.style, {
149
+ x1: offset === Infinity ? 0 : horizontalMinX! - offset,
150
+ y1: horizontalY,
151
+ x2: offset === Infinity ? canvasWidth : horizontalMaxX! + offset,
152
+ y2: horizontalY,
153
+ visibility: 'visible',
154
+ lineWidth: this.getLineWidth('horizontal'),
155
+ });
156
+ } else {
157
+ this.horizontalLine.style.visibility = 'hidden';
158
+ }
159
+
160
+ if (verticalX !== null) {
161
+ Object.assign(this.verticalLine.style, {
162
+ x1: verticalX,
163
+ y1: offset === Infinity ? 0 : verticalMinY! - offset,
164
+ x2: verticalX,
165
+ y2: offset === Infinity ? canvasHeight : verticalMaxY! + offset,
166
+ visibility: 'visible',
167
+ lineWidth: this.getLineWidth('vertical'),
168
+ });
169
+ } else {
170
+ this.verticalLine.style.visibility = 'hidden';
171
+ }
172
+ }
173
+
174
+ private isHorizontalSticking = false;
175
+ private isVerticalSticking = false;
176
+ private enableStick = true;
177
+
178
+ private autoSnapToLine = async (nodeId: ID, bbox: AABB, metadata: Metadata) => {
179
+ const { verticalX, horizontalY } = metadata;
180
+ const { tolerance } = this.options;
181
+ const {
182
+ min: [nodeMinX, nodeMinY],
183
+ max: [nodeMaxX, nodeMaxY],
184
+ center: [nodeCenterX, nodeCenterY],
185
+ } = bbox;
186
+
187
+ let dx = 0;
188
+ let dy = 0;
189
+ if (verticalX !== null) {
190
+ if (distance(nodeMaxX, verticalX) < tolerance) dx = verticalX - nodeMaxX;
191
+ if (distance(nodeMinX, verticalX) < tolerance) dx = verticalX - nodeMinX;
192
+ if (distance(nodeCenterX, verticalX) < tolerance) dx = verticalX - nodeCenterX;
193
+
194
+ if (dx !== 0) this.isVerticalSticking = true;
195
+ }
196
+ if (horizontalY !== null) {
197
+ if (distance(nodeMaxY, horizontalY) < tolerance) dy = horizontalY - nodeMaxY;
198
+ if (distance(nodeMinY, horizontalY) < tolerance) dy = horizontalY - nodeMinY;
199
+ if (distance(nodeCenterY, horizontalY) < tolerance) dy = horizontalY - nodeCenterY;
200
+
201
+ if (dy !== 0) this.isHorizontalSticking = true;
202
+ }
203
+ if (dx !== 0 || dy !== 0) {
204
+ // Stick to the line
205
+ await this.context.graph.translateElementBy({ [nodeId]: [dx, dy] }, false);
206
+ }
207
+ };
208
+
209
+ /**
210
+ * Get the delta of the drag
211
+ * @param event - drag event object
212
+ * @returns delta
213
+ * @internal
214
+ */
215
+ protected getDelta(event: IDragEvent<Node>) {
216
+ const zoom = this.context.graph.getZoom();
217
+ return divide([event.dx, event.dy], zoom);
218
+ }
219
+
220
+ private enableSnap = (event: IDragEvent<Node>) => {
221
+ const { target } = event;
222
+
223
+ const threshold = 0.5;
224
+
225
+ if (this.isHorizontalSticking || this.isVerticalSticking) {
226
+ const [dx, dy] = this.getDelta(event);
227
+ if (
228
+ this.isHorizontalSticking &&
229
+ this.isVerticalSticking &&
230
+ Math.abs(dx) <= threshold &&
231
+ Math.abs(dy) <= threshold
232
+ ) {
233
+ this.context.graph.translateElementBy({ [target.id]: [-dx, -dy] }, false);
234
+ return false;
235
+ } else if (this.isHorizontalSticking && Math.abs(dy) <= threshold) {
236
+ this.context.graph.translateElementBy({ [target.id]: [0, -dy] }, false);
237
+ return false;
238
+ } else if (this.isVerticalSticking && Math.abs(dx) <= threshold) {
239
+ this.context.graph.translateElementBy({ [target.id]: [-dx, 0] }, false);
240
+ return false;
241
+ } else {
242
+ this.isHorizontalSticking = false;
243
+ this.isVerticalSticking = false;
244
+ this.enableStick = false;
245
+ setTimeout(() => {
246
+ this.enableStick = true;
247
+ }, 200);
248
+ }
249
+ }
250
+
251
+ return this.enableStick;
252
+ };
253
+
254
+ private calcSnaplineMetadata = (target: Node, nodeBBox: AABB): Metadata => {
255
+ const { tolerance, shape } = this.options;
256
+
257
+ const {
258
+ min: [nodeMinX, nodeMinY],
259
+ max: [nodeMaxX, nodeMaxY],
260
+ center: [nodeCenterX, nodeCenterY],
261
+ } = nodeBBox;
262
+
263
+ let verticalX: number | null = null;
264
+ let verticalMinY: number | null = null;
265
+ let verticalMaxY: number | null = null;
266
+ let horizontalY: number | null = null;
267
+ let horizontalMinX: number | null = null;
268
+ let horizontalMaxX: number | null = null;
269
+
270
+ this.getNodes().some((snapNode: Node) => {
271
+ if (isEqual(target.id, snapNode.id)) return false;
272
+
273
+ const snapBBox = getShape(snapNode, shape).getRenderBounds();
274
+ const {
275
+ min: [snapMinX, snapMinY],
276
+ max: [snapMaxX, snapMaxY],
277
+ center: [snapCenterX, snapCenterY],
278
+ } = snapBBox;
279
+
280
+ if (verticalX === null) {
281
+ if (distance(snapCenterX, nodeCenterX) < tolerance) {
282
+ verticalX = snapCenterX;
283
+ } else if (distance(snapMinX, nodeMinX) < tolerance) {
284
+ verticalX = snapMinX;
285
+ } else if (distance(snapMinX, nodeMaxX) < tolerance) {
286
+ verticalX = snapMinX;
287
+ } else if (distance(snapMaxX, nodeMaxX) < tolerance) {
288
+ verticalX = snapMaxX;
289
+ } else if (distance(snapMaxX, nodeMinX) < tolerance) {
290
+ verticalX = snapMaxX;
291
+ }
292
+
293
+ if (verticalX !== null) {
294
+ verticalMinY = Math.min(snapMinY, nodeMinY);
295
+ verticalMaxY = Math.max(snapMaxY, nodeMaxY);
296
+ }
297
+ }
298
+
299
+ if (horizontalY === null) {
300
+ if (distance(snapCenterY, nodeCenterY) < tolerance) {
301
+ horizontalY = snapCenterY;
302
+ } else if (distance(snapMinY, nodeMinY) < tolerance) {
303
+ horizontalY = snapMinY;
304
+ } else if (distance(snapMinY, nodeMaxY) < tolerance) {
305
+ horizontalY = snapMinY;
306
+ } else if (distance(snapMaxY, nodeMaxY) < tolerance) {
307
+ horizontalY = snapMaxY;
308
+ } else if (distance(snapMaxY, nodeMinY) < tolerance) {
309
+ horizontalY = snapMaxY;
310
+ }
311
+
312
+ if (horizontalY !== null) {
313
+ horizontalMinX = Math.min(snapMinX, nodeMinX);
314
+ horizontalMaxX = Math.max(snapMaxX, nodeMaxX);
315
+ }
316
+ }
317
+
318
+ return verticalX !== null && horizontalY !== null;
319
+ });
320
+ return { verticalX, verticalMinY, verticalMaxY, horizontalY, horizontalMinX, horizontalMaxX };
321
+ };
322
+
323
+ protected onDragStart = () => {
324
+ this.initSnapline();
325
+ };
326
+
327
+ protected onDrag = async (event: IDragEvent<Node>) => {
328
+ const { target } = event;
329
+
330
+ if (this.options.autoSnap) {
331
+ const enable = this.enableSnap(event);
332
+ if (!enable) return;
333
+ }
334
+
335
+ const nodeBBox = getShape(target, this.options.shape).getRenderBounds();
336
+ const metadata = this.calcSnaplineMetadata(target, nodeBBox);
337
+
338
+ this.hideSnapline();
339
+
340
+ if (metadata.verticalX !== null || metadata.horizontalY !== null) {
341
+ this.updateSnapline(metadata);
342
+ }
343
+
344
+ if (this.options.autoSnap) {
345
+ await this.autoSnapToLine(target.id, nodeBBox, metadata);
346
+ }
347
+ };
348
+
349
+ protected onDragEnd = () => {
350
+ this.hideSnapline();
351
+ };
352
+
353
+ private async bindEvents() {
354
+ const { graph } = this.context;
355
+ graph.on(NodeEvent.DRAG_START, this.onDragStart);
356
+ graph.on(NodeEvent.DRAG, this.onDrag);
357
+ graph.on(NodeEvent.DRAG_END, this.onDragEnd);
358
+ }
359
+
360
+ private unbindEvents() {
361
+ const { graph } = this.context;
362
+ graph.off(NodeEvent.DRAG_START, this.onDragStart);
363
+ graph.off(NodeEvent.DRAG, this.onDrag);
364
+ graph.off(NodeEvent.DRAG_END, this.onDragEnd);
365
+ }
366
+
367
+ private destroyElements() {
368
+ this.horizontalLine?.destroy();
369
+ this.verticalLine?.destroy();
370
+ }
371
+
372
+ public destroy() {
373
+ this.destroyElements();
374
+ this.unbindEvents();
375
+ super.destroy();
376
+ }
377
+ }
378
+
379
+ const distance = (a: number, b: number) => Math.abs(a - b);
380
+
381
+ const getShape = (node: Node, shapeFilter: string | ((node: Node) => DisplayObject)) => {
382
+ return typeof shapeFilter === 'function' ? shapeFilter(node) : node.getShape(shapeFilter);
383
+ };
@@ -1,3 +1,15 @@
1
+ import {
2
+ Circle as GCircle,
3
+ Ellipse as GEllipse,
4
+ Group as GGroup,
5
+ HTML as GHTML,
6
+ Line as GLine,
7
+ Path as GPath,
8
+ Polygon as GPolygon,
9
+ Polyline as GPolyline,
10
+ Rect as GRect,
11
+ Text as GText,
12
+ } from '@antv/g';
1
13
  import { ComboCollapse, ComboExpand, Fade, NodeCollapse, NodeExpand, PathIn, PathOut, Translate } from '../animations';
2
14
  import {
3
15
  BrushSelect,
@@ -35,6 +47,7 @@ import {
35
47
  Star,
36
48
  Triangle,
37
49
  } from '../elements';
50
+ import { Badge as BadgeShape, Image as ImageShape, Label as LabelShape } from '../elements/shapes';
38
51
  import {
39
52
  AntVDagreLayout,
40
53
  CircularLayout,
@@ -59,12 +72,14 @@ import {
59
72
  Background,
60
73
  BubbleSets,
61
74
  Contextmenu,
75
+ EdgeFilterLens,
62
76
  Fullscreen,
63
77
  GridLine,
64
78
  History,
65
79
  Hull,
66
80
  Legend,
67
81
  Minimap,
82
+ Snapline,
68
83
  Timebar,
69
84
  Toolbar,
70
85
  Tooltip,
@@ -168,6 +183,7 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
168
183
  },
169
184
  plugin: {
170
185
  'bubble-sets': BubbleSets,
186
+ 'edge-filter-lens': EdgeFilterLens,
171
187
  'grid-line': GridLine,
172
188
  background: Background,
173
189
  contextmenu: Contextmenu,
@@ -175,11 +191,12 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
175
191
  history: History,
176
192
  hull: Hull,
177
193
  legend: Legend,
194
+ minimap: Minimap,
195
+ snapline: Snapline,
178
196
  timebar: Timebar,
179
197
  toolbar: Toolbar,
180
198
  tooltip: Tooltip,
181
199
  watermark: Watermark,
182
- minimap: Minimap,
183
200
  },
184
201
  transform: {
185
202
  'update-related-edges': UpdateRelatedEdge,
@@ -189,6 +206,21 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
189
206
  'process-parallel-edges': ProcessParallelEdges,
190
207
  'get-edge-actual-ends': GetEdgeActualEnds,
191
208
  },
209
+ shape: {
210
+ circle: GCircle,
211
+ ellipse: GEllipse,
212
+ group: GGroup,
213
+ html: GHTML,
214
+ image: ImageShape,
215
+ line: GLine,
216
+ path: GPath,
217
+ polygon: GPolygon,
218
+ polyline: GPolyline,
219
+ rect: GRect,
220
+ text: GText,
221
+ label: LabelShape,
222
+ badge: BadgeShape,
223
+ },
192
224
  };
193
225
 
194
226
  import type { ExtensionCategory } from '../constants';
@@ -16,4 +16,5 @@ export const EXTENSION_REGISTRY: ExtensionRegistry = {
16
16
  theme: {},
17
17
  plugin: {},
18
18
  transform: {},
19
+ shape: {},
19
20
  };
@@ -1,3 +1,4 @@
1
+ import type { DisplayObject } from '@antv/g';
1
2
  import type { STDAnimation } from '../animations/types';
2
3
  import type { Behavior } from '../behaviors/types';
3
4
  import type { Layout } from '../layouts/types';
@@ -23,4 +24,5 @@ export interface ExtensionRegistry {
23
24
  plugin: Record<string, { new (...args: any[]): Plugin }>;
24
25
  animation: Record<string, STDAnimation>; // animation spec
25
26
  transform: Record<string, { new (...args: any[]): Transform }>;
27
+ shape: Record<string, { new (...args: any[]): DisplayObject }>;
26
28
  }
@@ -22,7 +22,7 @@ import type {
22
22
  State,
23
23
  StyleIterationContext,
24
24
  } from '../types';
25
- import { cacheStyle, getCachedStyle, hasCachedStyle, setCacheStyle } from '../utils/cache';
25
+ import { cacheStyle, hasCachedStyle } from '../utils/cache';
26
26
  import { reduceDataChanges } from '../utils/change';
27
27
  import { isCollapsed } from '../utils/collapsibility';
28
28
  import { markToBeDestroyed, updateStyle } from '../utils/element';
@@ -34,6 +34,7 @@ import { positionOf } from '../utils/position';
34
34
  import { print } from '../utils/print';
35
35
  import { computeElementCallbackStyle } from '../utils/style';
36
36
  import { themeOf } from '../utils/theme';
37
+ import { setVisibility } from '../utils/visibility';
37
38
  import type { RuntimeContext } from './types';
38
39
 
39
40
  export class ElementController {
@@ -451,6 +452,10 @@ export class ElementController {
451
452
 
452
453
  const exactStage = stage !== 'visibility' ? stage : style.visibility === 'hidden' ? 'hide' : 'show';
453
454
 
455
+ // 避免立即将 visibility 设置为 hidden,导致元素不可见,而是在 after 阶段再设置
456
+ // Avoid setting visibility to hidden immediately, causing the element to be invisible, but set it in the after phase
457
+ if (exactStage === 'hide') delete style['visibility'];
458
+
454
459
  this.context.animation?.add(
455
460
  {
456
461
  element,
@@ -470,14 +475,14 @@ export class ElementController {
470
475
  // 缓存原始透明度 / Cache original opacity
471
476
  // 会在 animation controller 中访问该缓存值 / The cached value will be accessed in the animation controller
472
477
  if (!hasCachedStyle(element, 'opacity')) cacheStyle(element, 'opacity');
473
- setCacheStyle(element, 'visibility', exactStage === 'show' ? 'visible' : 'hidden');
474
- if (exactStage === 'show') updateStyle(element, { visibility: 'visible' });
478
+ this.visibilityCache.set(element, exactStage === 'show' ? 'visible' : 'hidden');
479
+ if (exactStage === 'show') setVisibility(element, 'visible');
475
480
  }
476
481
  },
477
482
  after: () => {
478
483
  const element = this.elementMap[id];
479
484
  if (stage === 'collapse') updateStyle(element, style);
480
- if (exactStage === 'hide') updateStyle(element, { visibility: getCachedStyle(element, 'visibility') });
485
+ if (exactStage === 'hide') setVisibility(element, this.visibilityCache.get(element));
481
486
  this.emit(new ElementLifeCycleEvent(GraphEvent.AFTER_ELEMENT_UPDATE, elementType, datum), context);
482
487
  element.onUpdate?.();
483
488
  },
@@ -498,6 +503,8 @@ export class ElementController {
498
503
  });
499
504
  }
500
505
 
506
+ private visibilityCache = new WeakMap<Element, BaseStyleProps['visibility']>();
507
+
501
508
  /**
502
509
  * <zh/> 标记销毁元素
503
510
  *
@@ -1,6 +1,7 @@
1
1
  import type { BasePlugin } from '../plugins/base-plugin';
2
2
  import { ExtensionController } from '../registry/extension';
3
3
  import type { CustomPluginOption, PluginOptions } from '../spec/plugin';
4
+ import { print } from '../utils/print';
4
5
  import type { RuntimeContext } from './types';
5
6
 
6
7
  export class PluginController extends ExtensionController<BasePlugin<CustomPluginOption>> {
@@ -16,6 +17,12 @@ export class PluginController extends ExtensionController<BasePlugin<CustomPlugi
16
17
  }
17
18
 
18
19
  public getPluginInstance(key: string) {
19
- return this.extensionMap[key];
20
+ const exactly = this.extensionMap[key];
21
+ if (exactly) return exactly;
22
+
23
+ print.warn(`Cannot find the plugin ${key}, will try to find it by type.`);
24
+
25
+ const fussily = this.extensions.find((extension) => extension.type === key);
26
+ if (fussily) return this.extensionMap[fussily.key];
20
27
  }
21
28
  }
@@ -1,10 +1,11 @@
1
- import { AABB } from '@antv/g';
1
+ import { AABB, ICamera } from '@antv/g';
2
2
  import { clamp, isNumber, pick } from '@antv/util';
3
3
  import { AnimationType, GraphEvent } from '../constants';
4
4
  import type { FitViewOptions, ID, Point, TransformOptions, Vector2, ViewportAnimationEffectTiming } from '../types';
5
5
  import { getAnimationOptions } from '../utils/animation';
6
- import { getBBoxSize, getCombinedBBox } from '../utils/bbox';
6
+ import { getBBoxSize, getCombinedBBox, isPointInBBox } from '../utils/bbox';
7
7
  import { AnimateEvent, ViewportEvent, emit } from '../utils/event';
8
+ import { isPoint } from '../utils/is';
8
9
  import { parsePadding } from '../utils/padding';
9
10
  import { add, divide, subtract } from '../utils/vector';
10
11
  import type { RuntimeContext } from './types';
@@ -30,7 +31,25 @@ export class ViewportController {
30
31
  }
31
32
 
32
33
  private get camera() {
33
- return this.context.canvas.getCamera();
34
+ const { canvas } = this.context;
35
+ return new Proxy(canvas.getCamera(), {
36
+ get: (target, prop: keyof ICamera) => {
37
+ const layers = Object.entries(canvas.getLayers()).filter(([name]) => !['main'].includes(name));
38
+ const cameras = layers.map(([, layer]) => layer.getCamera());
39
+
40
+ const value = target[prop];
41
+ if (typeof value === 'function') {
42
+ return (...args: any[]) => {
43
+ const result = (value as (...args: any[]) => any).apply(target, args);
44
+ cameras.forEach((camera) => {
45
+ (camera[prop] as (...args: any[]) => any).apply(camera, args);
46
+ });
47
+
48
+ return result;
49
+ };
50
+ }
51
+ },
52
+ });
34
53
  }
35
54
 
36
55
  private landmarkCounter = 0;
@@ -255,6 +274,26 @@ export class ViewportController {
255
274
  return bboxInViewport;
256
275
  }
257
276
 
277
+ /**
278
+ * <zh/> 判断点或包围盒是否在视口中
279
+ *
280
+ * <en/> Determine whether the point or bounding box is in the viewport
281
+ * @param target - <zh/> 点或包围盒 | <en/> Point or bounding box
282
+ * @returns - <zh/> 是否在视口中 | <en/> Whether it is in the viewport
283
+ */
284
+ public isInViewport(target: Point | AABB) {
285
+ const { graph } = this.context;
286
+ const size = this.getCanvasSize();
287
+
288
+ const [x1, y1] = graph.getCanvasByViewport([0, 0]);
289
+ const [x2, y2] = graph.getCanvasByViewport(size);
290
+
291
+ const viewportBBox = new AABB();
292
+ viewportBBox.setMinMax([x1, y1, 0], [x2, y2, 0]);
293
+
294
+ return isPoint(target) ? isPointInBBox(target, viewportBBox) : viewportBBox.intersects(target);
295
+ }
296
+
258
297
  public cancelAnimation() {
259
298
  // @ts-expect-error landmarks is private
260
299
  if (this.camera.landmarks?.length) {
@@ -16,6 +16,7 @@ export type * from './padding';
16
16
  export type * from './placement';
17
17
  export type * from './point';
18
18
  export type * from './prefix';
19
+ export type * from './router';
19
20
  export type * from './size';
20
21
  export type * from './state';
21
22
  export type * from './style';