@antv/g6 5.0.16 → 5.0.18

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 (162) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/dist/lib/behaviors/click-select.d.ts +0 -1
  3. package/dist/lib/behaviors/optimize-viewport-transform.d.ts +0 -1
  4. package/dist/lib/constants/events/canvas.d.ts +1 -1
  5. package/dist/lib/constants/registry.d.ts +7 -1
  6. package/dist/lib/elements/edges/polyline.d.ts +1 -0
  7. package/dist/lib/elements/effect.d.ts +19 -2
  8. package/dist/lib/elements/nodes/html.d.ts +14 -0
  9. package/dist/lib/elements/shapes/base-shape.d.ts +51 -2
  10. package/dist/lib/exports.d.ts +4 -2
  11. package/dist/lib/plugins/edge-filter-lens/index.d.ts +141 -0
  12. package/dist/lib/plugins/fisheye/index.d.ts +167 -0
  13. package/dist/lib/plugins/index.d.ts +4 -0
  14. package/dist/lib/plugins/tooltip.d.ts +1 -0
  15. package/dist/lib/registry/types.d.ts +4 -0
  16. package/dist/lib/runtime/element.d.ts +3 -1
  17. package/dist/lib/runtime/plugin.d.ts +1 -1
  18. package/dist/lib/types/element.d.ts +7 -0
  19. package/dist/lib/utils/dom.d.ts +8 -5
  20. package/dist/lib/version.d.ts +1 -1
  21. package/esm/behaviors/click-select.d.ts +0 -1
  22. package/esm/behaviors/click-select.js.map +1 -1
  23. package/esm/behaviors/optimize-viewport-transform.d.ts +0 -1
  24. package/esm/behaviors/optimize-viewport-transform.js +5 -4
  25. package/esm/behaviors/optimize-viewport-transform.js.map +1 -1
  26. package/esm/constants/events/canvas.d.ts +1 -1
  27. package/esm/constants/events/canvas.js +1 -1
  28. package/esm/constants/events/canvas.js.map +1 -1
  29. package/esm/constants/registry.d.ts +7 -1
  30. package/esm/constants/registry.js +6 -0
  31. package/esm/constants/registry.js.map +1 -1
  32. package/esm/elements/edges/polyline.d.ts +1 -0
  33. package/esm/elements/edges/polyline.js +5 -1
  34. package/esm/elements/edges/polyline.js.map +1 -1
  35. package/esm/elements/effect.d.ts +19 -2
  36. package/esm/elements/effect.js +19 -2
  37. package/esm/elements/effect.js.map +1 -1
  38. package/esm/elements/nodes/html.d.ts +14 -0
  39. package/esm/elements/nodes/html.js +10 -4
  40. package/esm/elements/nodes/html.js.map +1 -1
  41. package/esm/elements/shapes/base-shape.d.ts +51 -2
  42. package/esm/elements/shapes/base-shape.js +22 -4
  43. package/esm/elements/shapes/base-shape.js.map +1 -1
  44. package/esm/exports.d.ts +4 -2
  45. package/esm/exports.js +2 -1
  46. package/esm/exports.js.map +1 -1
  47. package/esm/plugins/contextmenu/index.js +1 -1
  48. package/esm/plugins/contextmenu/index.js.map +1 -1
  49. package/esm/plugins/edge-filter-lens/index.d.ts +141 -0
  50. package/esm/plugins/edge-filter-lens/index.js +241 -0
  51. package/esm/plugins/edge-filter-lens/index.js.map +1 -0
  52. package/esm/plugins/fisheye/index.d.ts +167 -0
  53. package/esm/plugins/fisheye/index.js +288 -0
  54. package/esm/plugins/fisheye/index.js.map +1 -0
  55. package/esm/plugins/index.d.ts +4 -0
  56. package/esm/plugins/index.js +2 -0
  57. package/esm/plugins/index.js.map +1 -1
  58. package/esm/plugins/snapline/index.js +2 -2
  59. package/esm/plugins/snapline/index.js.map +1 -1
  60. package/esm/plugins/tooltip.d.ts +1 -0
  61. package/esm/plugins/tooltip.js +16 -17
  62. package/esm/plugins/tooltip.js.map +1 -1
  63. package/esm/registry/build-in.js +20 -1
  64. package/esm/registry/build-in.js.map +1 -1
  65. package/esm/registry/store.js +1 -0
  66. package/esm/registry/store.js.map +1 -1
  67. package/esm/registry/types.d.ts +4 -0
  68. package/esm/runtime/element.d.ts +3 -1
  69. package/esm/runtime/element.js +8 -2
  70. package/esm/runtime/element.js.map +1 -1
  71. package/esm/runtime/plugin.d.ts +1 -1
  72. package/esm/runtime/plugin.js +8 -1
  73. package/esm/runtime/plugin.js.map +1 -1
  74. package/esm/types/element.d.ts +7 -0
  75. package/esm/utils/dom.d.ts +8 -5
  76. package/esm/utils/dom.js +24 -15
  77. package/esm/utils/dom.js.map +1 -1
  78. package/esm/version.d.ts +1 -1
  79. package/esm/version.js +1 -1
  80. package/lib/behaviors/click-select.d.ts +0 -1
  81. package/lib/behaviors/click-select.js.map +1 -1
  82. package/lib/behaviors/optimize-viewport-transform.d.ts +0 -1
  83. package/lib/behaviors/optimize-viewport-transform.js +5 -4
  84. package/lib/behaviors/optimize-viewport-transform.js.map +1 -1
  85. package/lib/constants/events/canvas.d.ts +1 -1
  86. package/lib/constants/events/canvas.js +1 -1
  87. package/lib/constants/events/canvas.js.map +1 -1
  88. package/lib/constants/registry.d.ts +7 -1
  89. package/lib/constants/registry.js +6 -0
  90. package/lib/constants/registry.js.map +1 -1
  91. package/lib/elements/edges/polyline.d.ts +1 -0
  92. package/lib/elements/edges/polyline.js +5 -1
  93. package/lib/elements/edges/polyline.js.map +1 -1
  94. package/lib/elements/effect.d.ts +19 -2
  95. package/lib/elements/effect.js +19 -2
  96. package/lib/elements/effect.js.map +1 -1
  97. package/lib/elements/nodes/html.d.ts +14 -0
  98. package/lib/elements/nodes/html.js +10 -4
  99. package/lib/elements/nodes/html.js.map +1 -1
  100. package/lib/elements/shapes/base-shape.d.ts +51 -2
  101. package/lib/elements/shapes/base-shape.js +22 -4
  102. package/lib/elements/shapes/base-shape.js.map +1 -1
  103. package/lib/exports.d.ts +4 -2
  104. package/lib/exports.js +7 -2
  105. package/lib/exports.js.map +1 -1
  106. package/lib/plugins/contextmenu/index.js +1 -1
  107. package/lib/plugins/contextmenu/index.js.map +1 -1
  108. package/lib/plugins/edge-filter-lens/index.d.ts +141 -0
  109. package/lib/plugins/edge-filter-lens/index.js +245 -0
  110. package/lib/plugins/edge-filter-lens/index.js.map +1 -0
  111. package/lib/plugins/fisheye/index.d.ts +167 -0
  112. package/lib/plugins/fisheye/index.js +292 -0
  113. package/lib/plugins/fisheye/index.js.map +1 -0
  114. package/lib/plugins/index.d.ts +4 -0
  115. package/lib/plugins/index.js +5 -1
  116. package/lib/plugins/index.js.map +1 -1
  117. package/lib/plugins/snapline/index.js +2 -2
  118. package/lib/plugins/snapline/index.js.map +1 -1
  119. package/lib/plugins/tooltip.d.ts +1 -0
  120. package/lib/plugins/tooltip.js +16 -17
  121. package/lib/plugins/tooltip.js.map +1 -1
  122. package/lib/registry/build-in.js +19 -0
  123. package/lib/registry/build-in.js.map +1 -1
  124. package/lib/registry/store.js +1 -0
  125. package/lib/registry/store.js.map +1 -1
  126. package/lib/registry/types.d.ts +4 -0
  127. package/lib/runtime/element.d.ts +3 -1
  128. package/lib/runtime/element.js +8 -2
  129. package/lib/runtime/element.js.map +1 -1
  130. package/lib/runtime/plugin.d.ts +1 -1
  131. package/lib/runtime/plugin.js +8 -1
  132. package/lib/runtime/plugin.js.map +1 -1
  133. package/lib/types/element.d.ts +7 -0
  134. package/lib/utils/dom.d.ts +8 -5
  135. package/lib/utils/dom.js +24 -15
  136. package/lib/utils/dom.js.map +1 -1
  137. package/lib/version.d.ts +1 -1
  138. package/lib/version.js +1 -1
  139. package/package.json +1 -1
  140. package/src/behaviors/click-select.ts +0 -1
  141. package/src/behaviors/optimize-viewport-transform.ts +4 -10
  142. package/src/constants/events/canvas.ts +1 -1
  143. package/src/constants/registry.ts +6 -0
  144. package/src/elements/edges/polyline.ts +7 -1
  145. package/src/elements/effect.ts +19 -2
  146. package/src/elements/nodes/html.ts +28 -4
  147. package/src/elements/shapes/base-shape.ts +75 -5
  148. package/src/exports.ts +8 -0
  149. package/src/plugins/contextmenu/index.ts +1 -1
  150. package/src/plugins/edge-filter-lens/index.ts +404 -0
  151. package/src/plugins/fisheye/index.ts +481 -0
  152. package/src/plugins/index.ts +4 -0
  153. package/src/plugins/snapline/index.ts +2 -2
  154. package/src/plugins/tooltip.ts +18 -17
  155. package/src/registry/build-in.ts +32 -0
  156. package/src/registry/store.ts +1 -0
  157. package/src/registry/types.ts +2 -0
  158. package/src/runtime/element.ts +11 -3
  159. package/src/runtime/plugin.ts +8 -1
  160. package/src/types/element.ts +7 -0
  161. package/src/utils/dom.ts +24 -15
  162. package/src/version.ts +1 -1
@@ -0,0 +1,481 @@
1
+ import { pick } from '@antv/util';
2
+ import { CommonEvent } from '../../constants';
3
+ import type { CircleStyleProps } from '../../elements';
4
+ import { Circle } from '../../elements';
5
+ import type { RuntimeContext } from '../../runtime/types';
6
+ import type { NodeData } from '../../spec';
7
+ import type { NodeStyle } from '../../spec/element/node';
8
+ import type { ID, IDragEvent, IPointerEvent, Node, Point, PointObject } from '../../types';
9
+ import { arrayDiff } from '../../utils/diff';
10
+ import { idOf } from '../../utils/id';
11
+ import { parsePoint, toPointObject } from '../../utils/point';
12
+ import { positionOf } from '../../utils/position';
13
+ import { distance } from '../../utils/vector';
14
+ import type { BasePluginOptions } from '../base-plugin';
15
+ import { BasePlugin } from '../base-plugin';
16
+
17
+ /**
18
+ * <zh/> 鱼眼放大镜插件配置项
19
+ *
20
+ * <en/> Fisheye Plugin Options
21
+ */
22
+ export interface FisheyeOptions extends BasePluginOptions {
23
+ /**
24
+ * <zh/> 移动鱼眼放大镜的方式
25
+ * - `'pointermove'`:始终跟随鼠标移动
26
+ * - `'click'`:鼠标点击时移动
27
+ * - `'drag'`:拖拽移动
28
+ *
29
+ * <en/> The way to move the fisheye lens
30
+ * - `'pointermove'`: always follow the mouse movement
31
+ * - `'click'`: move when the mouse is clicked
32
+ * - `'drag'`: move by dragging
33
+ * @defaultValue `'pointermove'`
34
+ */
35
+ trigger?: 'pointermove' | 'drag' | 'click';
36
+ /**
37
+ * <zh/> 鱼眼放大镜半径
38
+ *
39
+ * <en/> The radius of the fisheye lens
40
+ * @remarks
41
+ * <img src="https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*unAvQqAb_NMAAAAAAAAAAAAADmJ7AQ/original" width="200" />
42
+ * @defaultValue 120
43
+ */
44
+ r?: number;
45
+ /**
46
+ * <zh/> 鱼眼放大镜可调整的最大半径,配合 `scaleRBy` 使用
47
+ *
48
+ * <en/> The maximum radius that the fisheye lens can be adjusted, used with `scaleRBy`
49
+ * @defaultValue 画布宽高的最小值的一半
50
+ */
51
+ maxR?: number;
52
+ /**
53
+ * <zh/> 鱼眼放大镜可调整的最小半径,配合 `scaleRBy` 使用
54
+ *
55
+ * <en/> The minimum radius that the fisheye lens can be adjusted, used with `scaleRBy`
56
+ * @defaultValue 0
57
+ */
58
+ minR?: number;
59
+ /**
60
+ * <zh/> 调整鱼眼放大镜范围半径的方式
61
+ * - `'wheel'`:滚轮调整
62
+ * - `'drag'`:拖拽调整
63
+ *
64
+ * <en/> The way to adjust the range radius of the fisheye lens
65
+ * - `'wheel'`: adjust by wheel
66
+ * - `'drag'`: adjust by drag
67
+ * @remarks
68
+ * <zh/> 如果 `trigger`、`scaleRBy` 和 `scaleDBy` 同时设置为 `'drag'`,优先级顺序为 `trigger` > `scaleRBy` > `scaleDBy`,只会为优先级最高的配置项绑定拖拽事件。同理,如果 `scaleRBy` 和 `scaleDBy` 同时设置为 `'wheel'`,只会为 `scaleRBy` 绑定滚轮事件
69
+ *
70
+ * <en/> If `trigger`, `scaleRBy`, and `scaleDBy` are set to `'drag'` at the same time, the priority order is `trigger` > `scaleRBy` > `scaleDBy`, and only the configuration item with the highest priority will be bound to the drag event. Similarly, if `scaleRBy` and `scaleDBy` are set to `'wheel'` at the same time, only `scaleRBy` will be bound to the wheel event
71
+ */
72
+ scaleRBy?: 'wheel' | 'drag';
73
+ /**
74
+ * <zh/> 畸变因子
75
+ *
76
+ * <en/> Distortion factor
77
+ * @remarks
78
+ * <img src="https://mdn.alipayobjects.com/huamei_qa8qxu/afts/img/A*4ITFR7GOl8UAAAAAAAAAAAAADmJ7AQ/original" width="200" />
79
+ * @defaultValue 1.5
80
+ */
81
+ d?: number;
82
+ /**
83
+ * <zh/> 鱼眼放大镜可调整的最大畸变因子,配合 `scaleDBy` 使用
84
+ *
85
+ * <en/> The maximum distortion factor that the fisheye lens can be adjusted, used with `scaleDBy`
86
+ * @defaultValue 5
87
+ */
88
+ maxD?: number;
89
+ /**
90
+ * <zh/> 鱼眼放大镜可调整的最小畸变因子,配合 `scaleDBy` 使用
91
+ *
92
+ * <en/> The minimum distortion factor that the fisheye lens can be adjusted, used with `scaleDBy`
93
+ * @defaultValue 0
94
+ */
95
+ minD?: number;
96
+ /**
97
+ * <zh/> 调整鱼眼放大镜畸变因子的方式
98
+ * - `'wheel'`:滚轮调整
99
+ * - `'drag'`:拖拽调整
100
+ *
101
+ * <en/> The way to adjust the distortion factor of the fisheye lens
102
+ * - `'wheel'`: adjust by wheel
103
+ * - `'drag'`: adjust by drag
104
+ */
105
+ scaleDBy?: 'wheel' | 'drag';
106
+ /**
107
+ * <zh/> 是否在鱼眼放大镜中显示畸变因子数值
108
+ *
109
+ * <en/> Whether to display the value of the distortion factor in the fisheye lens
110
+ * @defaultValue true
111
+ */
112
+ showDPercent?: boolean;
113
+ /**
114
+ * <zh/> 鱼眼放大镜样式
115
+ *
116
+ * <en/> Fisheye Lens Style
117
+ */
118
+ style?: Partial<CircleStyleProps>;
119
+ /**
120
+ * <zh/> 在鱼眼放大镜中的节点样式
121
+ *
122
+ * <en/> Node style in the fisheye lens
123
+ */
124
+ nodeStyle?: NodeStyle | ((datum: NodeData) => NodeStyle);
125
+ /**
126
+ * <zh/> 是否阻止默认事件
127
+ *
128
+ * <en/> Whether to prevent the default event
129
+ * @defaultValue true
130
+ */
131
+ preventDefault?: boolean;
132
+ }
133
+
134
+ const defaultLensStyle: Exclude<CircleStyleProps, 'r'> = {
135
+ fill: '#ccc',
136
+ fillOpacity: 0.1,
137
+ lineWidth: 2,
138
+ stroke: '#000',
139
+ strokeOpacity: 0.8,
140
+ labelFontSize: 12,
141
+ };
142
+
143
+ const R_DELTA = 0.05;
144
+ const D_DELTA = 0.1;
145
+
146
+ /**
147
+ * <zh/> 鱼眼放大镜
148
+ *
149
+ * <en/> Fisheye Distortion
150
+ * @remarks
151
+ * <zh/> Fisheye 鱼眼放大镜是为 focus+context 的探索场景设计的,它能够保证在放大关注区域的同时,保证上下文以及上下文与关注中心的关系不丢失。
152
+ *
153
+ * <en/> Fisheye is designed for focus+context exploration, it keeps the context and the relationships between context and the focus while magnifying the focus area.
154
+ */
155
+ export class Fisheye extends BasePlugin<FisheyeOptions> {
156
+ static defaultOptions: Partial<FisheyeOptions> = {
157
+ trigger: 'pointermove',
158
+ r: 120,
159
+ d: 1.5,
160
+ maxD: 5,
161
+ minD: 0,
162
+ showDPercent: true,
163
+ style: {},
164
+ nodeStyle: { label: true },
165
+ preventDefault: true,
166
+ };
167
+
168
+ constructor(context: RuntimeContext, options: FisheyeOptions) {
169
+ super(context, Object.assign({}, Fisheye.defaultOptions, options));
170
+ this.bindEvents();
171
+ }
172
+
173
+ private lens!: Circle;
174
+
175
+ private r = this.options.r;
176
+ private d = this.options.d;
177
+
178
+ private get canvas() {
179
+ return this.context.canvas.getLayer('transient');
180
+ }
181
+
182
+ private get isLensOn() {
183
+ return this.lens && !this.lens.destroyed;
184
+ }
185
+
186
+ protected onCreateFisheye = (event: IPointerEvent) => {
187
+ if (this.options.trigger === 'drag' && this.isLensOn) return;
188
+
189
+ const origin = parsePoint(event.canvas as PointObject);
190
+ this.onMagnify(origin);
191
+ };
192
+
193
+ protected onMagnify = (origin: Point) => {
194
+ if (origin.some(isNaN)) return;
195
+
196
+ this.renderLens(origin);
197
+ this.renderFocusElements();
198
+ };
199
+
200
+ private renderLens = (origin: Point) => {
201
+ const style = Object.assign({}, defaultLensStyle, this.options.style);
202
+
203
+ if (!this.isLensOn) {
204
+ this.lens = new Circle({ style });
205
+ this.canvas.appendChild(this.lens);
206
+ }
207
+
208
+ Object.assign(style, toPointObject(origin), {
209
+ size: this.r * 2,
210
+ label: this.options.showDPercent,
211
+ labelText: this.getDPercent(),
212
+ });
213
+
214
+ this.lens.update(style);
215
+ };
216
+
217
+ private getDPercent = () => {
218
+ const { minD, maxD } = this.options as Required<FisheyeOptions>;
219
+ const percent = Math.round(((this.d - minD) / (maxD - minD)) * 100);
220
+ return `${percent}%`;
221
+ };
222
+
223
+ private prevMagnifiedStyleMap = new Map<ID, NodeStyle>();
224
+ private prevOriginStyleMap = new Map<ID, NodeStyle>();
225
+
226
+ private renderFocusElements = () => {
227
+ if (!this.isLensOn) return;
228
+
229
+ const { graph } = this.context;
230
+ const origin = this.lens.getCenter();
231
+
232
+ const molecularParam = (this.d + 1) * this.r;
233
+
234
+ const magnifiedStyleMap = new Map<ID, NodeStyle>();
235
+ const originStyleMap = new Map<ID, NodeStyle>();
236
+
237
+ const nodeData = graph.getNodeData();
238
+ nodeData.forEach((datum) => {
239
+ const position = positionOf(datum);
240
+ const distanceToOrigin = distance(position, origin);
241
+ if (distanceToOrigin > this.r) return;
242
+
243
+ const magnifiedDistance = (molecularParam * distanceToOrigin) / (this.d * distanceToOrigin + this.r);
244
+ const [nodeX, nodeY] = position;
245
+ const [originX, originY] = origin;
246
+ const cos = (nodeX - originX) / distanceToOrigin;
247
+ const sin = (nodeY - originY) / distanceToOrigin;
248
+
249
+ const newPoint: Point = [originX + magnifiedDistance * cos, originY + magnifiedDistance * sin];
250
+ const nodeId = idOf(datum);
251
+
252
+ const style = this.getNodeStyle(datum);
253
+ const originStyle = pick(graph.getElementRenderStyle(nodeId), Object.keys(style));
254
+
255
+ magnifiedStyleMap.set(nodeId, { ...toPointObject(newPoint), ...style });
256
+ originStyleMap.set(nodeId, { ...toPointObject(position), ...originStyle });
257
+ });
258
+
259
+ this.updateStyle(magnifiedStyleMap, originStyleMap);
260
+ };
261
+
262
+ private getNodeStyle = (datum: NodeData) => {
263
+ const { nodeStyle } = this.options;
264
+ return typeof nodeStyle === 'function' ? nodeStyle(datum) : nodeStyle;
265
+ };
266
+
267
+ private updateStyle = (magnifiedStyleMap: Map<ID, NodeStyle>, originStyleMap: Map<ID, NodeStyle>) => {
268
+ const { graph, element } = this.context;
269
+
270
+ const { enter, exit, keep } = arrayDiff<ID>(
271
+ Array.from(this.prevMagnifiedStyleMap.keys()),
272
+ Array.from(magnifiedStyleMap.keys()),
273
+ (d) => d,
274
+ );
275
+
276
+ const relatedEdges = new Set<ID>();
277
+
278
+ const update = (nodeId: ID, style: NodeStyle) => {
279
+ const node = element!.getElement(nodeId) as Node;
280
+ node.update(style);
281
+
282
+ graph.getRelatedEdgesData(nodeId).forEach((datum) => {
283
+ relatedEdges.add(idOf(datum));
284
+ });
285
+ };
286
+
287
+ [...enter, ...keep].forEach((nodeId) => {
288
+ update(nodeId, magnifiedStyleMap.get(nodeId)!);
289
+ });
290
+
291
+ exit.forEach((nodeId) => {
292
+ update(nodeId, this.prevOriginStyleMap.get(nodeId)!);
293
+ this.prevOriginStyleMap.delete(nodeId);
294
+ });
295
+
296
+ relatedEdges.forEach((edgeId) => {
297
+ const edge = element!.getElement(edgeId);
298
+ edge?.update({});
299
+ });
300
+
301
+ this.prevMagnifiedStyleMap = magnifiedStyleMap;
302
+ originStyleMap.forEach((style, nodeId) => {
303
+ if (!this.prevOriginStyleMap.has(nodeId)) {
304
+ this.prevOriginStyleMap.set(nodeId, style);
305
+ }
306
+ });
307
+ };
308
+
309
+ private isWheelValid = (event: WheelEvent) => {
310
+ if (this.options.preventDefault) event.preventDefault();
311
+
312
+ if (!this.isLensOn) return false;
313
+
314
+ const { clientX, clientY } = event;
315
+ const scaleOrigin = this.context.graph.getCanvasByClient([clientX, clientY]);
316
+ const origin = this.lens.getCenter();
317
+ if (distance(scaleOrigin, origin) > this.r) return false;
318
+
319
+ return true;
320
+ };
321
+
322
+ private scaleR = (positive: boolean) => {
323
+ const { maxR, minR } = this.options;
324
+ const ratio = positive ? 1 / (1 - R_DELTA) : 1 - R_DELTA;
325
+ const canvasR = Math.min(...this.context.canvas.getSize()) / 2;
326
+ this.r = Math.max(minR || 0, Math.min(maxR || canvasR, this.r * ratio));
327
+ };
328
+
329
+ private scaleD = (positive: boolean) => {
330
+ const { maxD, minD } = this.options as Required<FisheyeOptions>;
331
+ const newD = positive ? this.d + D_DELTA : this.d - D_DELTA;
332
+ this.d = Math.max(minD, Math.min(maxD, newD));
333
+ };
334
+
335
+ private scaleRByWheel = (event: WheelEvent) => {
336
+ if (!this.isWheelValid(event)) return;
337
+
338
+ const { deltaX, deltaY } = event;
339
+ this.scaleR(deltaX + deltaY > 0);
340
+
341
+ const origin = this.lens.getCenter();
342
+ this.onMagnify(origin);
343
+ };
344
+
345
+ private scaleDByWheel = (event: WheelEvent) => {
346
+ if (!this.isWheelValid(event)) return;
347
+
348
+ const { deltaX, deltaY } = event;
349
+ this.scaleD(deltaX + deltaY > 0);
350
+
351
+ const origin = this.lens.getCenter();
352
+ this.onMagnify(origin);
353
+ };
354
+
355
+ private isDragValid = (event: IDragEvent) => {
356
+ if (this.options.preventDefault) event.preventDefault();
357
+
358
+ if (!this.isLensOn) return false;
359
+
360
+ const dragOrigin = parsePoint(event.canvas as PointObject);
361
+ const origin = this.lens.getCenter();
362
+ if (distance(dragOrigin, origin) > this.r) return false;
363
+
364
+ return true;
365
+ };
366
+
367
+ private isLensDragging = false;
368
+
369
+ private onDragStart = (event: IDragEvent) => {
370
+ if (!this.isDragValid(event)) return;
371
+
372
+ this.isLensDragging = true;
373
+ };
374
+
375
+ private onDrag = (event: IDragEvent) => {
376
+ if (!this.isLensDragging) return;
377
+
378
+ const dragOrigin = parsePoint(event.canvas as PointObject);
379
+ this.onMagnify(dragOrigin);
380
+ };
381
+
382
+ private onDragEnd = () => {
383
+ this.isLensDragging = false;
384
+ };
385
+
386
+ private scaleRByDrag = (event: IDragEvent) => {
387
+ if (!this.isLensDragging) return;
388
+
389
+ const { dx, dy } = event;
390
+ this.scaleR(dx - dy > 0);
391
+
392
+ const origin = this.lens.getCenter();
393
+ this.onMagnify(origin);
394
+ };
395
+
396
+ private scaleDByDrag = (event: IDragEvent) => {
397
+ if (!this.isLensDragging) return;
398
+
399
+ const { dx, dy } = event;
400
+ this.scaleD(dx - dy > 0);
401
+
402
+ const origin = this.lens.getCenter();
403
+ this.onMagnify(origin);
404
+ };
405
+
406
+ get graphDom() {
407
+ return this.context.graph.getCanvas().getContextService().getDomElement();
408
+ }
409
+
410
+ private bindEvents() {
411
+ const { graph } = this.context;
412
+ const { trigger, scaleRBy, scaleDBy } = this.options;
413
+
414
+ const canvas = graph.getCanvas().getLayer();
415
+
416
+ if (['click', 'drag'].includes(trigger)) {
417
+ canvas.addEventListener(CommonEvent.CLICK, this.onCreateFisheye);
418
+ }
419
+
420
+ if (trigger === 'pointermove') {
421
+ canvas.addEventListener(CommonEvent.POINTER_MOVE, this.onCreateFisheye);
422
+ }
423
+
424
+ if (trigger === 'drag' || scaleRBy === 'drag' || scaleDBy === 'drag') {
425
+ canvas.addEventListener(CommonEvent.DRAG_START, this.onDragStart);
426
+ canvas.addEventListener(CommonEvent.DRAG_END, this.onDragEnd);
427
+
428
+ const dragFunc = trigger === 'drag' ? this.onDrag : scaleRBy === 'drag' ? this.scaleRByDrag : this.scaleDByDrag;
429
+ canvas.addEventListener(CommonEvent.DRAG, dragFunc);
430
+ }
431
+
432
+ if (scaleRBy === 'wheel' || scaleDBy === 'wheel') {
433
+ const wheelFunc = scaleRBy === 'wheel' ? this.scaleRByWheel : this.scaleDByWheel;
434
+ this.graphDom?.addEventListener(CommonEvent.WHEEL, wheelFunc, { passive: false });
435
+ }
436
+ }
437
+
438
+ private unbindEvents() {
439
+ const { graph } = this.context;
440
+ const { trigger, scaleRBy, scaleDBy } = this.options;
441
+
442
+ const canvas = graph.getCanvas().getLayer();
443
+
444
+ if (['click', 'drag'].includes(trigger)) {
445
+ canvas.removeEventListener(CommonEvent.CLICK, this.onCreateFisheye);
446
+ }
447
+
448
+ if (trigger === 'pointermove') {
449
+ canvas.removeEventListener(CommonEvent.POINTER_MOVE, this.onCreateFisheye);
450
+ }
451
+
452
+ if (trigger === 'drag' || scaleRBy === 'drag' || scaleDBy === 'drag') {
453
+ canvas.removeEventListener(CommonEvent.DRAG_START, this.onDragStart);
454
+ canvas.removeEventListener(CommonEvent.DRAG_END, this.onDragEnd);
455
+
456
+ const dragFunc = trigger === 'drag' ? this.onDrag : scaleRBy === 'drag' ? this.scaleRByDrag : this.scaleDByDrag;
457
+ canvas.removeEventListener(CommonEvent.DRAG, dragFunc);
458
+ }
459
+
460
+ if (scaleRBy === 'wheel' || scaleDBy === 'wheel') {
461
+ const wheelFunc = scaleRBy === 'wheel' ? this.scaleRByWheel : this.scaleDByWheel;
462
+ this.graphDom?.removeEventListener(CommonEvent.WHEEL, wheelFunc);
463
+ }
464
+ }
465
+
466
+ public update(options: Partial<FisheyeOptions>) {
467
+ this.unbindEvents();
468
+ super.update(options);
469
+ this.bindEvents();
470
+ }
471
+
472
+ public destroy() {
473
+ this.unbindEvents();
474
+ if (this.isLensOn) {
475
+ this.lens?.destroy();
476
+ }
477
+ this.prevMagnifiedStyleMap.clear();
478
+ this.prevOriginStyleMap.clear();
479
+ super.destroy();
480
+ }
481
+ }
@@ -3,6 +3,8 @@ export { BasePlugin } from './base-plugin';
3
3
  export { BubbleSets } from './bubble-sets';
4
4
  export { CameraSetting } from './camera-setting';
5
5
  export { Contextmenu } from './contextmenu';
6
+ export { EdgeFilterLens } from './edge-filter-lens';
7
+ export { Fisheye } from './fisheye';
6
8
  export { Fullscreen } from './fullscreen';
7
9
  export { GridLine } from './grid-line';
8
10
  export { History } from './history';
@@ -20,6 +22,8 @@ export type { BasePluginOptions } from './base-plugin';
20
22
  export type { BubbleSetsOptions } from './bubble-sets';
21
23
  export type { CameraSettingOptions } from './camera-setting';
22
24
  export type { ContextmenuOptions } from './contextmenu';
25
+ export type { EdgeFilterLensOptions } from './edge-filter-lens';
26
+ export type { FisheyeOptions } from './fisheye';
23
27
  export type { FullscreenOptions } from './fullscreen';
24
28
  export type { GridLineOptions } from './grid-line';
25
29
  export type { HistoryOptions } from './history';
@@ -365,8 +365,8 @@ export class Snapline extends BasePlugin<SnaplineOptions> {
365
365
  }
366
366
 
367
367
  private destroyElements() {
368
- this.horizontalLine.destroy();
369
- this.verticalLine.destroy();
368
+ this.horizontalLine?.destroy();
369
+ this.verticalLine?.destroy();
370
370
  }
371
371
 
372
372
  public destroy() {
@@ -271,6 +271,7 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
271
271
  };
272
272
  }
273
273
  this.tooltipElement.update({
274
+ ...this.tooltipStyleProps,
274
275
  x,
275
276
  y,
276
277
  style: {
@@ -300,7 +301,7 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
300
301
  this.tooltipElement.hide(x, y);
301
302
  };
302
303
 
303
- private initTooltip = () => {
304
+ private get tooltipStyleProps() {
304
305
  const { canvas } = this.context;
305
306
  const { center } = canvas.getBounds();
306
307
  const $container = canvas.getContainer() as HTMLElement;
@@ -308,24 +309,24 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
308
309
  const { style, position, enterable, container = { x: -left, y: -top }, title, offset } = this.options;
309
310
  const [x, y] = center;
310
311
  const [width, height] = canvas.getSize();
312
+
313
+ return {
314
+ x,
315
+ y,
316
+ container,
317
+ title,
318
+ bounding: { x: 0, y: 0, width, height },
319
+ position,
320
+ enterable,
321
+ offset,
322
+ style,
323
+ };
324
+ }
325
+
326
+ private initTooltip = () => {
311
327
  const tooltipElement = new TooltipComponent({
312
328
  className: 'tooltip',
313
- style: {
314
- x,
315
- y,
316
- container,
317
- title,
318
- bounding: {
319
- x: 0,
320
- y: 0,
321
- width,
322
- height,
323
- },
324
- position,
325
- enterable,
326
- offset,
327
- style,
328
- },
329
+ style: this.tooltipStyleProps,
329
330
  });
330
331
  this.container?.appendChild(tooltipElement.HTMLTooltipElement);
331
332
  return tooltipElement;
@@ -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,6 +72,8 @@ import {
59
72
  Background,
60
73
  BubbleSets,
61
74
  Contextmenu,
75
+ EdgeFilterLens,
76
+ Fisheye,
62
77
  Fullscreen,
63
78
  GridLine,
64
79
  History,
@@ -169,9 +184,11 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
169
184
  },
170
185
  plugin: {
171
186
  'bubble-sets': BubbleSets,
187
+ 'edge-filter-lens': EdgeFilterLens,
172
188
  'grid-line': GridLine,
173
189
  background: Background,
174
190
  contextmenu: Contextmenu,
191
+ fisheye: Fisheye,
175
192
  fullscreen: Fullscreen,
176
193
  history: History,
177
194
  hull: Hull,
@@ -191,6 +208,21 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
191
208
  'process-parallel-edges': ProcessParallelEdges,
192
209
  'get-edge-actual-ends': GetEdgeActualEnds,
193
210
  },
211
+ shape: {
212
+ circle: GCircle,
213
+ ellipse: GEllipse,
214
+ group: GGroup,
215
+ html: GHTML,
216
+ image: ImageShape,
217
+ line: GLine,
218
+ path: GPath,
219
+ polygon: GPolygon,
220
+ polyline: GPolyline,
221
+ rect: GRect,
222
+ text: GText,
223
+ label: LabelShape,
224
+ badge: BadgeShape,
225
+ },
194
226
  };
195
227
 
196
228
  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
  }
@@ -224,7 +224,7 @@ export class ElementController {
224
224
  return this.elementMap[id] as T;
225
225
  }
226
226
 
227
- public getElementZIndex(id: ID) {
227
+ public getElementZIndex(id: ID): number {
228
228
  const element = this.getElement(id);
229
229
  if (!element) return 0;
230
230
  return element.style.zIndex ?? 0;
@@ -382,6 +382,12 @@ export class ElementController {
382
382
 
383
383
  this.emit(new ElementLifeCycleEvent(GraphEvent.BEFORE_ELEMENT_CREATE, elementType, datum), context);
384
384
 
385
+ if (context.stage === 'expand') {
386
+ // 如果是展开的元素,需要将其 zIndex 提升至目标元素的上层
387
+ const targetZIndex = this.getElementZIndex(context.target!);
388
+ if (!style.zIndex || style.zIndex < targetZIndex) style.zIndex = targetZIndex + (style.zIndex ?? 0);
389
+ }
390
+
385
391
  const element = this.container.appendChild(
386
392
  new Ctor({
387
393
  id,
@@ -636,7 +642,7 @@ export class ElementController {
636
642
  const {
637
643
  drawData: { add },
638
644
  } = this.computeChangesAndDrawData({ stage: 'collapse', animation })!;
639
- this.createElements(add, { animation: false, stage: 'expand' });
645
+ this.createElements(add, { animation: false, stage: 'expand', target: id });
640
646
  // 重置动画 / Reset animation
641
647
  this.context.animation!.clear();
642
648
 
@@ -725,7 +731,7 @@ export class ElementController {
725
731
  this.computeStyle('expand');
726
732
  const { dataChanges, drawData } = this.computeChangesAndDrawData({ stage: 'expand', animation })!;
727
733
  const { add, update } = drawData;
728
- const context = { animation, stage: 'expand', data: drawData } as const;
734
+ const context = { animation, stage: 'expand', data: drawData, target: id } as const;
729
735
 
730
736
  this.createElements(add, context);
731
737
  this.updateElements(update, context);
@@ -819,4 +825,6 @@ export interface DrawContext {
819
825
  collapseExpandTarget?: ID;
820
826
  /** <zh/> 绘制类型 | <en/> Draw type */
821
827
  type?: 'render' | 'draw';
828
+ /** <zh/> 展开阶段的目标元素 id | <en/> ID of the target element in the expand stage */
829
+ target?: ID;
822
830
  }