@antv/g6 5.0.17 → 5.0.19

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 (142) 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/drag-element.d.ts +0 -1
  4. package/dist/lib/constants/events/canvas.d.ts +1 -1
  5. package/dist/lib/elements/effect.d.ts +7 -13
  6. package/dist/lib/elements/nodes/html.d.ts +14 -0
  7. package/dist/lib/elements/shapes/base-shape.d.ts +1 -1
  8. package/dist/lib/exports.d.ts +2 -2
  9. package/dist/lib/plugins/edge-filter-lens/index.d.ts +25 -9
  10. package/dist/lib/plugins/fisheye/index.d.ts +167 -0
  11. package/dist/lib/plugins/index.d.ts +2 -0
  12. package/dist/lib/plugins/tooltip.d.ts +1 -0
  13. package/dist/lib/runtime/element.d.ts +3 -1
  14. package/dist/lib/types/element.d.ts +7 -0
  15. package/dist/lib/utils/dom.d.ts +8 -5
  16. package/dist/lib/version.d.ts +1 -1
  17. package/esm/behaviors/click-select.d.ts +0 -1
  18. package/esm/behaviors/click-select.js.map +1 -1
  19. package/esm/behaviors/drag-element.d.ts +0 -1
  20. package/esm/behaviors/drag-element.js +14 -7
  21. package/esm/behaviors/drag-element.js.map +1 -1
  22. package/esm/constants/events/canvas.d.ts +1 -1
  23. package/esm/constants/events/canvas.js +1 -1
  24. package/esm/constants/events/canvas.js.map +1 -1
  25. package/esm/elements/combos/base-combo.js +4 -10
  26. package/esm/elements/combos/base-combo.js.map +1 -1
  27. package/esm/elements/edges/base-edge.js +18 -29
  28. package/esm/elements/edges/base-edge.js.map +1 -1
  29. package/esm/elements/effect.d.ts +7 -13
  30. package/esm/elements/effect.js +20 -33
  31. package/esm/elements/effect.js.map +1 -1
  32. package/esm/elements/nodes/base-node.js +21 -29
  33. package/esm/elements/nodes/base-node.js.map +1 -1
  34. package/esm/elements/nodes/html.d.ts +14 -0
  35. package/esm/elements/nodes/html.js +10 -4
  36. package/esm/elements/nodes/html.js.map +1 -1
  37. package/esm/elements/shapes/base-shape.d.ts +1 -1
  38. package/esm/elements/shapes/base-shape.js +1 -1
  39. package/esm/exports.d.ts +2 -2
  40. package/esm/exports.js +1 -1
  41. package/esm/exports.js.map +1 -1
  42. package/esm/plugins/contextmenu/index.js +1 -1
  43. package/esm/plugins/contextmenu/index.js.map +1 -1
  44. package/esm/plugins/edge-filter-lens/index.d.ts +25 -9
  45. package/esm/plugins/edge-filter-lens/index.js +19 -14
  46. package/esm/plugins/edge-filter-lens/index.js.map +1 -1
  47. package/esm/plugins/fisheye/index.d.ts +167 -0
  48. package/esm/plugins/fisheye/index.js +288 -0
  49. package/esm/plugins/fisheye/index.js.map +1 -0
  50. package/esm/plugins/index.d.ts +2 -0
  51. package/esm/plugins/index.js +1 -0
  52. package/esm/plugins/index.js.map +1 -1
  53. package/esm/plugins/tooltip.d.ts +1 -0
  54. package/esm/plugins/tooltip.js +16 -17
  55. package/esm/plugins/tooltip.js.map +1 -1
  56. package/esm/registry/build-in.js +2 -1
  57. package/esm/registry/build-in.js.map +1 -1
  58. package/esm/runtime/element.d.ts +3 -1
  59. package/esm/runtime/element.js +32 -9
  60. package/esm/runtime/element.js.map +1 -1
  61. package/esm/runtime/graph.js +2 -0
  62. package/esm/runtime/graph.js.map +1 -1
  63. package/esm/types/element.d.ts +7 -0
  64. package/esm/utils/dom.d.ts +8 -5
  65. package/esm/utils/dom.js +24 -15
  66. package/esm/utils/dom.js.map +1 -1
  67. package/esm/version.d.ts +1 -1
  68. package/esm/version.js +1 -1
  69. package/lib/behaviors/click-select.d.ts +0 -1
  70. package/lib/behaviors/click-select.js.map +1 -1
  71. package/lib/behaviors/drag-element.d.ts +0 -1
  72. package/lib/behaviors/drag-element.js +14 -7
  73. package/lib/behaviors/drag-element.js.map +1 -1
  74. package/lib/constants/events/canvas.d.ts +1 -1
  75. package/lib/constants/events/canvas.js +1 -1
  76. package/lib/constants/events/canvas.js.map +1 -1
  77. package/lib/elements/combos/base-combo.js +4 -10
  78. package/lib/elements/combos/base-combo.js.map +1 -1
  79. package/lib/elements/edges/base-edge.js +18 -29
  80. package/lib/elements/edges/base-edge.js.map +1 -1
  81. package/lib/elements/effect.d.ts +7 -13
  82. package/lib/elements/effect.js +20 -33
  83. package/lib/elements/effect.js.map +1 -1
  84. package/lib/elements/nodes/base-node.js +21 -29
  85. package/lib/elements/nodes/base-node.js.map +1 -1
  86. package/lib/elements/nodes/html.d.ts +14 -0
  87. package/lib/elements/nodes/html.js +10 -4
  88. package/lib/elements/nodes/html.js.map +1 -1
  89. package/lib/elements/shapes/base-shape.d.ts +1 -1
  90. package/lib/elements/shapes/base-shape.js +1 -1
  91. package/lib/exports.d.ts +2 -2
  92. package/lib/exports.js +2 -1
  93. package/lib/exports.js.map +1 -1
  94. package/lib/plugins/contextmenu/index.js +1 -1
  95. package/lib/plugins/contextmenu/index.js.map +1 -1
  96. package/lib/plugins/edge-filter-lens/index.d.ts +25 -9
  97. package/lib/plugins/edge-filter-lens/index.js +18 -13
  98. package/lib/plugins/edge-filter-lens/index.js.map +1 -1
  99. package/lib/plugins/fisheye/index.d.ts +167 -0
  100. package/lib/plugins/fisheye/index.js +292 -0
  101. package/lib/plugins/fisheye/index.js.map +1 -0
  102. package/lib/plugins/index.d.ts +2 -0
  103. package/lib/plugins/index.js +3 -1
  104. package/lib/plugins/index.js.map +1 -1
  105. package/lib/plugins/tooltip.d.ts +1 -0
  106. package/lib/plugins/tooltip.js +16 -17
  107. package/lib/plugins/tooltip.js.map +1 -1
  108. package/lib/registry/build-in.js +1 -0
  109. package/lib/registry/build-in.js.map +1 -1
  110. package/lib/runtime/element.d.ts +3 -1
  111. package/lib/runtime/element.js +32 -9
  112. package/lib/runtime/element.js.map +1 -1
  113. package/lib/runtime/graph.js +2 -0
  114. package/lib/runtime/graph.js.map +1 -1
  115. package/lib/types/element.d.ts +7 -0
  116. package/lib/utils/dom.d.ts +8 -5
  117. package/lib/utils/dom.js +24 -15
  118. package/lib/utils/dom.js.map +1 -1
  119. package/lib/version.d.ts +1 -1
  120. package/lib/version.js +1 -1
  121. package/package.json +2 -2
  122. package/src/behaviors/click-select.ts +0 -1
  123. package/src/behaviors/drag-element.ts +17 -7
  124. package/src/constants/events/canvas.ts +1 -1
  125. package/src/elements/combos/base-combo.ts +3 -2
  126. package/src/elements/edges/base-edge.ts +14 -11
  127. package/src/elements/effect.ts +20 -37
  128. package/src/elements/nodes/base-node.ts +21 -16
  129. package/src/elements/nodes/html.ts +28 -4
  130. package/src/elements/shapes/base-shape.ts +1 -1
  131. package/src/exports.ts +2 -0
  132. package/src/plugins/contextmenu/index.ts +1 -1
  133. package/src/plugins/edge-filter-lens/index.ts +36 -23
  134. package/src/plugins/fisheye/index.ts +481 -0
  135. package/src/plugins/index.ts +2 -0
  136. package/src/plugins/tooltip.ts +18 -17
  137. package/src/registry/build-in.ts +2 -0
  138. package/src/runtime/element.ts +32 -10
  139. package/src/runtime/graph.ts +1 -0
  140. package/src/types/element.ts +7 -0
  141. package/src/utils/dom.ts +24 -15
  142. 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
+ }
@@ -4,6 +4,7 @@ export { BubbleSets } from './bubble-sets';
4
4
  export { CameraSetting } from './camera-setting';
5
5
  export { Contextmenu } from './contextmenu';
6
6
  export { EdgeFilterLens } from './edge-filter-lens';
7
+ export { Fisheye } from './fisheye';
7
8
  export { Fullscreen } from './fullscreen';
8
9
  export { GridLine } from './grid-line';
9
10
  export { History } from './history';
@@ -22,6 +23,7 @@ export type { BubbleSetsOptions } from './bubble-sets';
22
23
  export type { CameraSettingOptions } from './camera-setting';
23
24
  export type { ContextmenuOptions } from './contextmenu';
24
25
  export type { EdgeFilterLensOptions } from './edge-filter-lens';
26
+ export type { FisheyeOptions } from './fisheye';
25
27
  export type { FullscreenOptions } from './fullscreen';
26
28
  export type { GridLineOptions } from './grid-line';
27
29
  export type { HistoryOptions } from './history';
@@ -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;
@@ -73,6 +73,7 @@ import {
73
73
  BubbleSets,
74
74
  Contextmenu,
75
75
  EdgeFilterLens,
76
+ Fisheye,
76
77
  Fullscreen,
77
78
  GridLine,
78
79
  History,
@@ -187,6 +188,7 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
187
188
  'grid-line': GridLine,
188
189
  background: Background,
189
190
  contextmenu: Contextmenu,
191
+ fisheye: Fisheye,
190
192
  fullscreen: Fullscreen,
191
193
  history: History,
192
194
  hull: Hull,
@@ -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,
@@ -581,9 +587,10 @@ export class ElementController {
581
587
  public async collapseNode(id: ID, animation: boolean): Promise<void> {
582
588
  const { model, layout } = this.context;
583
589
 
584
- const preprocess = this.computeChangesAndDrawData({ stage: 'collapse', animation })!;
590
+ const preLayoutData = this.computeChangesAndDrawData({ stage: 'collapse', animation });
591
+ if (!preLayoutData) return;
585
592
 
586
- this.markDestroyElement(preprocess.drawData);
593
+ this.markDestroyElement(preLayoutData.drawData);
587
594
 
588
595
  // 进行预布局,计算出所有元素的位置
589
596
  // Perform pre-layout to calculate the position of all elements
@@ -591,7 +598,9 @@ export class ElementController {
591
598
  model.updateData(result);
592
599
 
593
600
  // 重新计算数据 / Recalculate data
594
- const { drawData } = this.computeChangesAndDrawData({ stage: 'collapse', animation })!;
601
+ const data = this.computeChangesAndDrawData({ stage: 'collapse', animation });
602
+ if (!data) return;
603
+ const { drawData } = data;
595
604
  const { add, remove, update } = drawData;
596
605
  this.markDestroyElement(drawData);
597
606
  const context = { animation, stage: 'collapse', data: drawData } as const;
@@ -631,12 +640,15 @@ export class ElementController {
631
640
 
632
641
  const position = positionOf(model.getNodeData([id])[0]);
633
642
 
643
+ const preLayoutData = this.computeChangesAndDrawData({ stage: 'expand', animation });
644
+ if (!preLayoutData) return;
645
+
634
646
  // 首先创建展开的元素,然后进行预布局
635
647
  // First create the expanded element, then perform pre-layout
636
648
  const {
637
649
  drawData: { add },
638
- } = this.computeChangesAndDrawData({ stage: 'collapse', animation })!;
639
- this.createElements(add, { animation: false, stage: 'expand' });
650
+ } = preLayoutData;
651
+ this.createElements(add, { animation: false, stage: 'expand', target: id });
640
652
  // 重置动画 / Reset animation
641
653
  this.context.animation!.clear();
642
654
 
@@ -645,7 +657,9 @@ export class ElementController {
645
657
 
646
658
  // 重新计算数据 / Recalculate data
647
659
  this.computeStyle('expand');
648
- const { drawData } = this.computeChangesAndDrawData({ stage: 'collapse', animation })!;
660
+ const data = this.computeChangesAndDrawData({ stage: 'collapse', animation });
661
+ if (!data) return;
662
+ const { drawData } = data;
649
663
  const { update } = drawData;
650
664
 
651
665
  const context = { animation, stage: 'expand', data: drawData } as const;
@@ -687,7 +701,10 @@ export class ElementController {
687
701
  collapsed: true,
688
702
  });
689
703
 
690
- const { dataChanges, drawData } = this.computeChangesAndDrawData({ stage: 'collapse', animation })!;
704
+ const data = this.computeChangesAndDrawData({ stage: 'collapse', animation });
705
+ if (!data) return;
706
+
707
+ const { dataChanges, drawData } = data;
691
708
  this.markDestroyElement(drawData);
692
709
  const { update, remove } = drawData;
693
710
  const context = { animation, stage: 'collapse', data: drawData } as const;
@@ -723,9 +740,12 @@ export class ElementController {
723
740
 
724
741
  // 重新计算数据 / Recalculate data
725
742
  this.computeStyle('expand');
726
- const { dataChanges, drawData } = this.computeChangesAndDrawData({ stage: 'expand', animation })!;
743
+ const data = this.computeChangesAndDrawData({ stage: 'expand', animation });
744
+ if (!data) return;
745
+
746
+ const { dataChanges, drawData } = data;
727
747
  const { add, update } = drawData;
728
- const context = { animation, stage: 'expand', data: drawData } as const;
748
+ const context = { animation, stage: 'expand', data: drawData, target: id } as const;
729
749
 
730
750
  this.createElements(add, context);
731
751
  this.updateElements(update, context);
@@ -819,4 +839,6 @@ export interface DrawContext {
819
839
  collapseExpandTarget?: ID;
820
840
  /** <zh/> 绘制类型 | <en/> Draw type */
821
841
  type?: 'render' | 'draw';
842
+ /** <zh/> 展开阶段的目标元素 id | <en/> ID of the target element in the expand stage */
843
+ target?: ID;
822
844
  }
@@ -1019,6 +1019,7 @@ export class Graph extends EventEmitter {
1019
1019
  if (container instanceof Canvas) {
1020
1020
  this.context.canvas = container;
1021
1021
  if (cursor) container.setCursor(cursor);
1022
+ if (renderer) container.setRenderer(renderer);
1022
1023
  await container.ready;
1023
1024
  } else {
1024
1025
  const $container = isString(container) ? document.getElementById(container!) : container;
@@ -79,6 +79,13 @@ export interface BaseElementStyleProps extends BaseShapeStyleProps {
79
79
  * <en/> Element methods
80
80
  */
81
81
  export interface ElementMethods {
82
+ /**
83
+ * <zh/> 更新元素属性
84
+ *
85
+ * <en/> Update element attributes
86
+ * @param attr - <zh/> 属性 | <en/> Attributes
87
+ */
88
+ update(attr: any): void;
82
89
  /**
83
90
  * <zh/> 获取当前元素内的子图形
84
91
  *