@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.
- package/dist/g6.min.js +2 -2
- package/dist/lib/behaviors/drag-canvas.d.ts +13 -0
- package/dist/lib/behaviors/scroll-canvas.d.ts +7 -0
- package/dist/lib/behaviors/zoom-canvas.d.ts +7 -0
- package/dist/lib/elements/edges/base-edge.d.ts +2 -2
- package/dist/lib/elements/edges/polyline.d.ts +3 -20
- package/dist/lib/plugins/contextmenu/index.d.ts +3 -1
- package/dist/lib/plugins/index.d.ts +2 -0
- package/dist/lib/plugins/minimap/index.d.ts +2 -0
- package/dist/lib/plugins/snapline/index.d.ts +95 -0
- package/dist/lib/runtime/canvas.d.ts +17 -1
- package/dist/lib/runtime/element.d.ts +1 -0
- package/dist/lib/runtime/viewport.d.ts +8 -0
- package/dist/lib/types/index.d.ts +1 -0
- package/dist/lib/types/router.d.ts +103 -0
- package/dist/lib/utils/bbox.d.ts +8 -0
- package/dist/lib/utils/router/orth.d.ts +8 -8
- package/dist/lib/utils/router/shortest-path.d.ts +57 -0
- package/dist/lib/utils/text.d.ts +1 -1
- package/dist/lib/utils/visibility.d.ts +2 -1
- package/dist/lib/version.d.ts +1 -1
- package/esm/behaviors/brush-select.js +11 -6
- package/esm/behaviors/brush-select.js.map +1 -1
- package/esm/behaviors/drag-canvas.d.ts +13 -0
- package/esm/behaviors/drag-canvas.js +10 -1
- package/esm/behaviors/drag-canvas.js.map +1 -1
- package/esm/behaviors/optimize-viewport-transform.js +1 -1
- package/esm/behaviors/optimize-viewport-transform.js.map +1 -1
- package/esm/behaviors/scroll-canvas.d.ts +7 -0
- package/esm/behaviors/scroll-canvas.js +3 -1
- package/esm/behaviors/scroll-canvas.js.map +1 -1
- package/esm/behaviors/zoom-canvas.d.ts +7 -0
- package/esm/behaviors/zoom-canvas.js +3 -1
- package/esm/behaviors/zoom-canvas.js.map +1 -1
- package/esm/elements/edges/base-edge.d.ts +2 -2
- package/esm/elements/edges/base-edge.js +10 -4
- package/esm/elements/edges/base-edge.js.map +1 -1
- package/esm/elements/edges/polyline.d.ts +3 -20
- package/esm/elements/edges/polyline.js +29 -34
- package/esm/elements/edges/polyline.js.map +1 -1
- package/esm/elements/shapes/base-shape.js +1 -1
- package/esm/elements/shapes/base-shape.js.map +1 -1
- package/esm/plugins/contextmenu/index.d.ts +3 -1
- package/esm/plugins/contextmenu/index.js +4 -1
- package/esm/plugins/contextmenu/index.js.map +1 -1
- package/esm/plugins/index.d.ts +2 -0
- package/esm/plugins/index.js +1 -0
- package/esm/plugins/index.js.map +1 -1
- package/esm/plugins/minimap/index.d.ts +2 -0
- package/esm/plugins/minimap/index.js +24 -12
- package/esm/plugins/minimap/index.js.map +1 -1
- package/esm/plugins/snapline/index.d.ts +95 -0
- package/esm/plugins/snapline/index.js +268 -0
- package/esm/plugins/snapline/index.js.map +1 -0
- package/esm/registry/build-in.js +3 -2
- package/esm/registry/build-in.js.map +1 -1
- package/esm/runtime/canvas.d.ts +17 -1
- package/esm/runtime/canvas.js +26 -3
- package/esm/runtime/canvas.js.map +1 -1
- package/esm/runtime/element.d.ts +1 -0
- package/esm/runtime/element.js +11 -5
- package/esm/runtime/element.js.map +1 -1
- package/esm/runtime/viewport.d.ts +8 -0
- package/esm/runtime/viewport.js +35 -2
- package/esm/runtime/viewport.js.map +1 -1
- package/esm/types/index.d.ts +1 -0
- package/esm/types/router.d.ts +103 -0
- package/esm/types/router.js +2 -0
- package/esm/types/router.js.map +1 -0
- package/esm/utils/bbox.d.ts +8 -0
- package/esm/utils/bbox.js +19 -0
- package/esm/utils/bbox.js.map +1 -1
- package/esm/utils/router/orth.d.ts +8 -8
- package/esm/utils/router/orth.js +13 -7
- package/esm/utils/router/orth.js.map +1 -1
- package/esm/utils/router/shortest-path.d.ts +57 -0
- package/esm/utils/router/shortest-path.js +387 -0
- package/esm/utils/router/shortest-path.js.map +1 -0
- package/esm/utils/text.d.ts +1 -1
- package/esm/utils/visibility.d.ts +2 -1
- package/esm/utils/visibility.js +3 -2
- package/esm/utils/visibility.js.map +1 -1
- package/esm/version.d.ts +1 -1
- package/esm/version.js +1 -1
- package/lib/behaviors/brush-select.js +11 -6
- package/lib/behaviors/brush-select.js.map +1 -1
- package/lib/behaviors/drag-canvas.d.ts +13 -0
- package/lib/behaviors/drag-canvas.js +10 -1
- package/lib/behaviors/drag-canvas.js.map +1 -1
- package/lib/behaviors/optimize-viewport-transform.js +1 -1
- package/lib/behaviors/optimize-viewport-transform.js.map +1 -1
- package/lib/behaviors/scroll-canvas.d.ts +7 -0
- package/lib/behaviors/scroll-canvas.js +3 -1
- package/lib/behaviors/scroll-canvas.js.map +1 -1
- package/lib/behaviors/zoom-canvas.d.ts +7 -0
- package/lib/behaviors/zoom-canvas.js +3 -1
- package/lib/behaviors/zoom-canvas.js.map +1 -1
- package/lib/elements/edges/base-edge.d.ts +2 -2
- package/lib/elements/edges/base-edge.js +9 -3
- package/lib/elements/edges/base-edge.js.map +1 -1
- package/lib/elements/edges/polyline.d.ts +3 -20
- package/lib/elements/edges/polyline.js +29 -34
- package/lib/elements/edges/polyline.js.map +1 -1
- package/lib/elements/shapes/base-shape.js +1 -1
- package/lib/elements/shapes/base-shape.js.map +1 -1
- package/lib/plugins/contextmenu/index.d.ts +3 -1
- package/lib/plugins/contextmenu/index.js +4 -1
- package/lib/plugins/contextmenu/index.js.map +1 -1
- package/lib/plugins/index.d.ts +2 -0
- package/lib/plugins/index.js +3 -1
- package/lib/plugins/index.js.map +1 -1
- package/lib/plugins/minimap/index.d.ts +2 -0
- package/lib/plugins/minimap/index.js +24 -12
- package/lib/plugins/minimap/index.js.map +1 -1
- package/lib/plugins/snapline/index.d.ts +95 -0
- package/lib/plugins/snapline/index.js +272 -0
- package/lib/plugins/snapline/index.js.map +1 -0
- package/lib/registry/build-in.js +2 -1
- package/lib/registry/build-in.js.map +1 -1
- package/lib/runtime/canvas.d.ts +17 -1
- package/lib/runtime/canvas.js +26 -3
- package/lib/runtime/canvas.js.map +1 -1
- package/lib/runtime/element.d.ts +1 -0
- package/lib/runtime/element.js +10 -4
- package/lib/runtime/element.js.map +1 -1
- package/lib/runtime/viewport.d.ts +8 -0
- package/lib/runtime/viewport.js +34 -1
- package/lib/runtime/viewport.js.map +1 -1
- package/lib/types/index.d.ts +1 -0
- package/lib/types/router.d.ts +103 -0
- package/lib/types/router.js +3 -0
- package/lib/types/router.js.map +1 -0
- package/lib/utils/bbox.d.ts +8 -0
- package/lib/utils/bbox.js +20 -0
- package/lib/utils/bbox.js.map +1 -1
- package/lib/utils/router/orth.d.ts +8 -8
- package/lib/utils/router/orth.js +13 -7
- package/lib/utils/router/orth.js.map +1 -1
- package/lib/utils/router/shortest-path.d.ts +57 -0
- package/lib/utils/router/shortest-path.js +394 -0
- package/lib/utils/router/shortest-path.js.map +1 -0
- package/lib/utils/text.d.ts +1 -1
- package/lib/utils/visibility.d.ts +2 -1
- package/lib/utils/visibility.js +3 -2
- package/lib/utils/visibility.js.map +1 -1
- package/lib/version.d.ts +1 -1
- package/lib/version.js +1 -1
- package/package.json +1 -1
- package/src/behaviors/brush-select.ts +12 -5
- package/src/behaviors/drag-canvas.ts +24 -1
- package/src/behaviors/optimize-viewport-transform.ts +1 -0
- package/src/behaviors/scroll-canvas.ts +9 -1
- package/src/behaviors/zoom-canvas.ts +9 -1
- package/src/elements/edges/base-edge.ts +19 -5
- package/src/elements/edges/polyline.ts +33 -68
- package/src/elements/shapes/base-shape.ts +1 -1
- package/src/plugins/contextmenu/index.ts +8 -3
- package/src/plugins/grid-line.ts +1 -1
- package/src/plugins/index.ts +2 -0
- package/src/plugins/minimap/index.ts +28 -15
- package/src/plugins/snapline/index.ts +383 -0
- package/src/registry/build-in.ts +3 -1
- package/src/runtime/canvas.ts +26 -3
- package/src/runtime/element.ts +12 -5
- package/src/runtime/viewport.ts +42 -3
- package/src/types/index.ts +1 -0
- package/src/types/router.ts +107 -0
- package/src/utils/bbox.ts +24 -0
- package/src/utils/router/orth.ts +19 -10
- package/src/utils/router/shortest-path.ts +470 -0
- package/src/utils/text.ts +1 -1
- package/src/utils/visibility.ts +3 -1
- 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
|
+
};
|
package/src/registry/build-in.ts
CHANGED
|
@@ -65,6 +65,7 @@ import {
|
|
|
65
65
|
Hull,
|
|
66
66
|
Legend,
|
|
67
67
|
Minimap,
|
|
68
|
+
Snapline,
|
|
68
69
|
Timebar,
|
|
69
70
|
Toolbar,
|
|
70
71
|
Tooltip,
|
|
@@ -175,11 +176,12 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
|
|
|
175
176
|
history: History,
|
|
176
177
|
hull: Hull,
|
|
177
178
|
legend: Legend,
|
|
179
|
+
minimap: Minimap,
|
|
180
|
+
snapline: Snapline,
|
|
178
181
|
timebar: Timebar,
|
|
179
182
|
toolbar: Toolbar,
|
|
180
183
|
tooltip: Tooltip,
|
|
181
184
|
watermark: Watermark,
|
|
182
|
-
minimap: Minimap,
|
|
183
185
|
},
|
|
184
186
|
transform: {
|
|
185
187
|
'update-related-edges': UpdateRelatedEdge,
|
package/src/runtime/canvas.ts
CHANGED
|
@@ -159,11 +159,34 @@ export class Canvas {
|
|
|
159
159
|
});
|
|
160
160
|
}
|
|
161
161
|
|
|
162
|
-
|
|
162
|
+
/**
|
|
163
|
+
* <zh/> 获取画布边界
|
|
164
|
+
*
|
|
165
|
+
* <en/> Get canvas boundary
|
|
166
|
+
* @param group
|
|
167
|
+
* <zh/> 元素分组
|
|
168
|
+
* - undefined: 获取整个画布边界
|
|
169
|
+
* - 'elements': 仅获取元素边界
|
|
170
|
+
* - 'plugins': 仅获取插件边界
|
|
171
|
+
*
|
|
172
|
+
* <en/> Element group
|
|
173
|
+
* - undefined: Get the entire canvas boundary
|
|
174
|
+
* - 'elements': Get only the element boundary
|
|
175
|
+
* - 'plugins': Get only the plugin boundary
|
|
176
|
+
* @returns <zh/> 边界 <en/> Boundary
|
|
177
|
+
*/
|
|
178
|
+
public getBounds(group?: 'elements' | 'plugins') {
|
|
163
179
|
return getCombinedBBox(
|
|
164
180
|
Object.values(this.getLayers())
|
|
165
|
-
.map((canvas) =>
|
|
166
|
-
|
|
181
|
+
.map((canvas) => {
|
|
182
|
+
const g = group
|
|
183
|
+
? (canvas
|
|
184
|
+
.getRoot()
|
|
185
|
+
.childNodes.find((node) => (node as DisplayObject).classList.includes(group)) as DisplayObject)
|
|
186
|
+
: canvas.getRoot();
|
|
187
|
+
return g;
|
|
188
|
+
})
|
|
189
|
+
.filter((el) => el?.childNodes.length > 0)
|
|
167
190
|
.map((el) => el.getBounds()),
|
|
168
191
|
);
|
|
169
192
|
}
|
package/src/runtime/element.ts
CHANGED
|
@@ -22,7 +22,7 @@ import type {
|
|
|
22
22
|
State,
|
|
23
23
|
StyleIterationContext,
|
|
24
24
|
} from '../types';
|
|
25
|
-
import { cacheStyle,
|
|
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 {
|
|
@@ -52,7 +53,7 @@ export class ElementController {
|
|
|
52
53
|
public init() {
|
|
53
54
|
if (!this.container) {
|
|
54
55
|
const { canvas } = this.context;
|
|
55
|
-
this.container = canvas.appendChild(new Group());
|
|
56
|
+
this.container = canvas.appendChild(new Group({ className: 'elements' }));
|
|
56
57
|
}
|
|
57
58
|
}
|
|
58
59
|
|
|
@@ -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
|
-
|
|
474
|
-
if (exactStage === 'show')
|
|
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')
|
|
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
|
*
|
package/src/runtime/viewport.ts
CHANGED
|
@@ -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
|
-
|
|
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) {
|
package/src/types/index.ts
CHANGED
|
@@ -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';
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import type { Padding } from './padding';
|
|
2
|
+
import type { CardinalPlacement } from './placement';
|
|
3
|
+
import { Point } from './point';
|
|
4
|
+
|
|
5
|
+
export type Direction = CardinalPlacement;
|
|
6
|
+
|
|
7
|
+
export type PolylineRouter = false | OrthRouter | ShortestPathRouter;
|
|
8
|
+
|
|
9
|
+
export interface OrthRouter extends OrthRouterOptions {
|
|
10
|
+
/**
|
|
11
|
+
* <zh/> 正交路由,通过在路径上添加额外的控制点,使得边的每一段都保持水平或垂直
|
|
12
|
+
*
|
|
13
|
+
* <en/> Orthogonal routing that adds additional control points on the path to ensure each segment of the edge horizontal or vertical
|
|
14
|
+
* @remarks
|
|
15
|
+
* <zh/> 采用基于节点的相对位置和专家经验得出的寻径算法来模糊计算控制点,非最优解但计算速度快。该路由支持 `controlPoints` 来作为额外的控制点,但不支持自动避障。
|
|
16
|
+
*
|
|
17
|
+
* <en/> It uses a pathfinding algorithm based on the relative position of the nodes and expert experience to calculate the control points, which is not the optimal solution but is fast to calculate. This routing supports `controlPoints` as additional control points, but does not support automatic obstacle avoidance.
|
|
18
|
+
*/
|
|
19
|
+
type: 'orth';
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface ShortestPathRouter extends ShortestPathRouterOptions {
|
|
23
|
+
/**
|
|
24
|
+
* <zh/> 最短路径路由,是正交路由 `'orth'` 的智能版本。该路由由水平或垂直的正交线段组成。采用 A* 算法计算最短路径,并支持自动避开路径上的其他节点(障碍)
|
|
25
|
+
*
|
|
26
|
+
* <en/> The shortest path routing is an intelligent version of the orthogonal routing `'orth'`. The routing consists of horizontal or vertical orthogonal line segments. It uses the A* algorithm to calculate the shortest path and supports automatic avoidance of other nodes (obstacles) on the path.
|
|
27
|
+
*/
|
|
28
|
+
type: 'shortest-path';
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type RouterOptions = OrthRouterOptions | ShortestPathRouterOptions;
|
|
32
|
+
|
|
33
|
+
export interface OrthRouterOptions {
|
|
34
|
+
/**
|
|
35
|
+
* <zh/> 指定节点连接点与转角的最小距离
|
|
36
|
+
*
|
|
37
|
+
* <en/> The minimum distance between the node connection point and the corner
|
|
38
|
+
*/
|
|
39
|
+
padding?: Padding;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface ShortestPathRouterOptions {
|
|
43
|
+
/**
|
|
44
|
+
* <zh/> 节点锚点与转角的最小距离
|
|
45
|
+
*
|
|
46
|
+
* <en/> The minimum distance between the node anchor point and the corner
|
|
47
|
+
*/
|
|
48
|
+
offset?: Padding;
|
|
49
|
+
/**
|
|
50
|
+
* <zh/> grid 格子大小
|
|
51
|
+
*
|
|
52
|
+
* <en/> grid size
|
|
53
|
+
*/
|
|
54
|
+
gridSize?: number;
|
|
55
|
+
/**
|
|
56
|
+
* <zh/> 支持的最大旋转角度(弧度)
|
|
57
|
+
*
|
|
58
|
+
* <en/> Maximum allowable rotation angle (radian)
|
|
59
|
+
*/
|
|
60
|
+
maxAllowedDirectionChange?: number;
|
|
61
|
+
/**
|
|
62
|
+
* <zh/> 节点的可能起始方向
|
|
63
|
+
*
|
|
64
|
+
* <en/> Possible starting directions from a node
|
|
65
|
+
*/
|
|
66
|
+
startDirections?: Direction[];
|
|
67
|
+
/**
|
|
68
|
+
* <zh/> 节点的可能结束方向
|
|
69
|
+
*
|
|
70
|
+
* <en/> Possible ending directions from a node
|
|
71
|
+
*/
|
|
72
|
+
endDirections?: Direction[];
|
|
73
|
+
/**
|
|
74
|
+
* <zh/> 指定可移动的方向
|
|
75
|
+
*
|
|
76
|
+
* <en/> Allowed edge directions
|
|
77
|
+
*/
|
|
78
|
+
directionMap?: {
|
|
79
|
+
[key in Direction]: { stepX: number; stepY: number };
|
|
80
|
+
};
|
|
81
|
+
/**
|
|
82
|
+
* <zh/> 表示在路径搜索过程中某些路径的额外代价。key 为弧度值,value 为代价
|
|
83
|
+
*
|
|
84
|
+
* <en/> Penalties for direction changes. Key is the radian value, value is the penalty
|
|
85
|
+
*/
|
|
86
|
+
penalties?: {
|
|
87
|
+
[key: string]: number;
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* <zh/> 指定计算两点之间距离的函数
|
|
91
|
+
*
|
|
92
|
+
* <en/> Function to calculate the distance between two points
|
|
93
|
+
*/
|
|
94
|
+
distFunc?: (p1: Point, p2: Point) => number;
|
|
95
|
+
/**
|
|
96
|
+
* <zh/> 最大迭代次数
|
|
97
|
+
*
|
|
98
|
+
* <en/> Maximum loops
|
|
99
|
+
*/
|
|
100
|
+
maximumLoops?: number;
|
|
101
|
+
/**
|
|
102
|
+
* <zh/> 是否开启避障
|
|
103
|
+
*
|
|
104
|
+
* <en/> Whether to enable obstacle avoidance while computing the path
|
|
105
|
+
*/
|
|
106
|
+
enableObstacleAvoidance?: boolean;
|
|
107
|
+
}
|