@antv/g6 5.0.16 → 5.0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (91) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/dist/lib/behaviors/optimize-viewport-transform.d.ts +0 -1
  3. package/dist/lib/constants/registry.d.ts +7 -1
  4. package/dist/lib/elements/edges/polyline.d.ts +1 -0
  5. package/dist/lib/elements/shapes/base-shape.d.ts +51 -2
  6. package/dist/lib/exports.d.ts +3 -2
  7. package/dist/lib/plugins/edge-filter-lens/index.d.ts +125 -0
  8. package/dist/lib/plugins/index.d.ts +2 -0
  9. package/dist/lib/registry/types.d.ts +4 -0
  10. package/dist/lib/runtime/plugin.d.ts +1 -1
  11. package/dist/lib/version.d.ts +1 -1
  12. package/esm/behaviors/optimize-viewport-transform.d.ts +0 -1
  13. package/esm/behaviors/optimize-viewport-transform.js +5 -4
  14. package/esm/behaviors/optimize-viewport-transform.js.map +1 -1
  15. package/esm/constants/registry.d.ts +7 -1
  16. package/esm/constants/registry.js +6 -0
  17. package/esm/constants/registry.js.map +1 -1
  18. package/esm/elements/edges/polyline.d.ts +1 -0
  19. package/esm/elements/edges/polyline.js +5 -1
  20. package/esm/elements/edges/polyline.js.map +1 -1
  21. package/esm/elements/shapes/base-shape.d.ts +51 -2
  22. package/esm/elements/shapes/base-shape.js +22 -4
  23. package/esm/elements/shapes/base-shape.js.map +1 -1
  24. package/esm/exports.d.ts +3 -2
  25. package/esm/exports.js +1 -1
  26. package/esm/exports.js.map +1 -1
  27. package/esm/plugins/edge-filter-lens/index.d.ts +125 -0
  28. package/esm/plugins/edge-filter-lens/index.js +236 -0
  29. package/esm/plugins/edge-filter-lens/index.js.map +1 -0
  30. package/esm/plugins/index.d.ts +2 -0
  31. package/esm/plugins/index.js +1 -0
  32. package/esm/plugins/index.js.map +1 -1
  33. package/esm/plugins/snapline/index.js +2 -2
  34. package/esm/plugins/snapline/index.js.map +1 -1
  35. package/esm/registry/build-in.js +19 -1
  36. package/esm/registry/build-in.js.map +1 -1
  37. package/esm/registry/store.js +1 -0
  38. package/esm/registry/store.js.map +1 -1
  39. package/esm/registry/types.d.ts +4 -0
  40. package/esm/runtime/plugin.d.ts +1 -1
  41. package/esm/runtime/plugin.js +8 -1
  42. package/esm/runtime/plugin.js.map +1 -1
  43. package/esm/version.d.ts +1 -1
  44. package/esm/version.js +1 -1
  45. package/lib/behaviors/optimize-viewport-transform.d.ts +0 -1
  46. package/lib/behaviors/optimize-viewport-transform.js +5 -4
  47. package/lib/behaviors/optimize-viewport-transform.js.map +1 -1
  48. package/lib/constants/registry.d.ts +7 -1
  49. package/lib/constants/registry.js +6 -0
  50. package/lib/constants/registry.js.map +1 -1
  51. package/lib/elements/edges/polyline.d.ts +1 -0
  52. package/lib/elements/edges/polyline.js +5 -1
  53. package/lib/elements/edges/polyline.js.map +1 -1
  54. package/lib/elements/shapes/base-shape.d.ts +51 -2
  55. package/lib/elements/shapes/base-shape.js +22 -4
  56. package/lib/elements/shapes/base-shape.js.map +1 -1
  57. package/lib/exports.d.ts +3 -2
  58. package/lib/exports.js +3 -1
  59. package/lib/exports.js.map +1 -1
  60. package/lib/plugins/edge-filter-lens/index.d.ts +125 -0
  61. package/lib/plugins/edge-filter-lens/index.js +240 -0
  62. package/lib/plugins/edge-filter-lens/index.js.map +1 -0
  63. package/lib/plugins/index.d.ts +2 -0
  64. package/lib/plugins/index.js +3 -1
  65. package/lib/plugins/index.js.map +1 -1
  66. package/lib/plugins/snapline/index.js +2 -2
  67. package/lib/plugins/snapline/index.js.map +1 -1
  68. package/lib/registry/build-in.js +18 -0
  69. package/lib/registry/build-in.js.map +1 -1
  70. package/lib/registry/store.js +1 -0
  71. package/lib/registry/store.js.map +1 -1
  72. package/lib/registry/types.d.ts +4 -0
  73. package/lib/runtime/plugin.d.ts +1 -1
  74. package/lib/runtime/plugin.js +8 -1
  75. package/lib/runtime/plugin.js.map +1 -1
  76. package/lib/version.d.ts +1 -1
  77. package/lib/version.js +1 -1
  78. package/package.json +1 -1
  79. package/src/behaviors/optimize-viewport-transform.ts +4 -10
  80. package/src/constants/registry.ts +6 -0
  81. package/src/elements/edges/polyline.ts +7 -1
  82. package/src/elements/shapes/base-shape.ts +75 -5
  83. package/src/exports.ts +5 -0
  84. package/src/plugins/edge-filter-lens/index.ts +391 -0
  85. package/src/plugins/index.ts +2 -0
  86. package/src/plugins/snapline/index.ts +2 -2
  87. package/src/registry/build-in.ts +30 -0
  88. package/src/registry/store.ts +1 -0
  89. package/src/registry/types.ts +2 -0
  90. package/src/runtime/plugin.ts +8 -1
  91. package/src/version.ts +1 -1
@@ -1,13 +1,16 @@
1
1
  import type { BaseStyleProps, DisplayObject, DisplayObjectConfig, Group, IAnimation } from '@antv/g';
2
2
  import { CustomElement } from '@antv/g';
3
3
  import { isEmpty, isFunction, upperFirst } from '@antv/util';
4
+ import { ExtensionCategory } from '../../constants';
4
5
  import type { Keyframe } from '../../types';
5
6
  import { createAnimationsProxy, preprocessKeyframes } from '../../utils/animation';
6
7
  import { updateStyle } from '../../utils/element';
7
8
  import { subObject } from '../../utils/prefix';
9
+ import { format } from '../../utils/print';
8
10
  import { getSubShapeStyle } from '../../utils/style';
9
11
  import { replaceTranslateInTransform } from '../../utils/transform';
10
12
  import { setVisibility } from '../../utils/visibility';
13
+ import { getExtension } from './../../registry/get';
11
14
 
12
15
  export interface BaseShapeStyleProps extends BaseStyleProps {}
13
16
 
@@ -46,37 +49,55 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
46
49
  * @param Ctor - <zh> 图形类型 | <en> shape type
47
50
  * @param style - <zh> 图形样式 | <en> shape style
48
51
  * @param container - <zh> 容器 | <en> container
52
+ * @param hooks - <zh> 钩子函数 | <en> hooks
49
53
  * @returns <zh> 图形实例 | <en> shape instance
50
54
  */
51
55
  protected upsert<T extends DisplayObject>(
52
56
  className: string,
53
- Ctor: { new (...args: any[]): T },
57
+ Ctor: string | { new (...args: any[]): T },
54
58
  style: T['attributes'] | false,
55
59
  container: DisplayObject,
60
+ hooks?: UpsertHooks,
56
61
  ): T | undefined {
57
62
  const target = this.shapeMap[className] as T | undefined;
58
63
  // remove
59
64
  // 如果 style 为 false,则删除图形 / remove shape if style is false
60
65
  if (style === false) {
61
66
  if (target) {
67
+ hooks?.beforeDestroy?.(target);
62
68
  container.removeChild(target);
63
69
  delete this.shapeMap[className];
70
+ hooks?.afterDestroy?.(target);
64
71
  }
65
72
  return;
66
73
  }
67
74
 
75
+ const _Ctor = typeof Ctor === 'string' ? getExtension(ExtensionCategory.SHAPE, Ctor) : Ctor;
76
+
77
+ if (!_Ctor) {
78
+ throw new Error(format(`Shape ${Ctor} not found`));
79
+ }
80
+
68
81
  // create
69
- if (!target || target.destroyed || !(target instanceof Ctor)) {
70
- target?.destroy();
82
+ if (!target || target.destroyed || !(target instanceof _Ctor)) {
83
+ if (target) {
84
+ hooks?.beforeDestroy?.(target);
85
+ target?.destroy();
86
+ hooks?.afterDestroy?.(target);
87
+ }
71
88
 
72
- const instance = new Ctor({ className, style });
89
+ hooks?.beforeCreate?.();
90
+ const instance = new _Ctor({ className, style });
73
91
  container.appendChild(instance);
74
92
  this.shapeMap[className] = instance;
75
- return instance;
93
+ hooks?.afterCreate?.(instance);
94
+ return instance as T;
76
95
  }
77
96
 
78
97
  // update
98
+ hooks?.beforeUpdate?.(target);
79
99
  updateStyle(target, style);
100
+ hooks?.afterUpdate?.(target);
80
101
 
81
102
  return target;
82
103
  }
@@ -248,3 +269,52 @@ function releaseAnimation(target: DisplayObject, animation: IAnimation) {
248
269
  if (index > -1) target.activeAnimations.splice(index, 1);
249
270
  });
250
271
  }
272
+
273
+ /**
274
+ * <zh/> 图形 upsert 方法生命周期钩子
275
+ *
276
+ * <en/> Shape upsert method lifecycle hooks
277
+ */
278
+ export interface UpsertHooks {
279
+ /**
280
+ * <zh/> 图形创建前
281
+ *
282
+ * <en/> Before creating the shape
283
+ */
284
+ beforeCreate?: () => void;
285
+ /**
286
+ * <zh/> 图形创建后
287
+ *
288
+ * <en/> After creating the shape
289
+ * @param instance - <zh/> 图形实例 | <en/> shape instance
290
+ */
291
+ afterCreate?: (instance: DisplayObject) => void;
292
+ /**
293
+ * <zh/> 图形更新前
294
+ *
295
+ * <en/> Before updating the shape
296
+ * @param instance - <zh/> 图形实例 | <en/> shape instance
297
+ */
298
+ beforeUpdate?: (instance: DisplayObject) => void;
299
+ /**
300
+ * <zh/> 图形更新后
301
+ *
302
+ * <en/> After updating the shape
303
+ * @param instance - <zh/> 图形实例 | <en/> shape instance
304
+ */
305
+ afterUpdate?: (instance: DisplayObject) => void;
306
+ /**
307
+ * <zh/> 图形销毁前
308
+ *
309
+ * <en/> Before destroying the shape
310
+ * @param instance - <zh/> 图形实例 | <en/> shape instance
311
+ */
312
+ beforeDestroy?: (instance: DisplayObject) => void;
313
+ /**
314
+ * <zh/> 图形销毁后
315
+ *
316
+ * <en/> After destroying the shape
317
+ * @param instance - <zh/> 图形实例 | <en/> shape instance
318
+ */
319
+ afterDestroy?: (instance: DisplayObject) => void;
320
+ }
package/src/exports.ts CHANGED
@@ -67,12 +67,14 @@ export {
67
67
  BubbleSets,
68
68
  CameraSetting,
69
69
  Contextmenu,
70
+ EdgeFilterLens,
70
71
  Fullscreen,
71
72
  GridLine,
72
73
  History,
73
74
  Hull,
74
75
  Legend,
75
76
  Minimap,
77
+ Snapline,
76
78
  Timebar,
77
79
  Toolbar,
78
80
  Tooltip,
@@ -158,6 +160,7 @@ export type {
158
160
  LabelStyleProps,
159
161
  PolygonStyleProps,
160
162
  } from './elements/shapes';
163
+ export type { UpsertHooks } from './elements/shapes/base-shape';
161
164
  export type { ContourLabelStyleProps, ContourStyleProps } from './elements/shapes/contour';
162
165
  export type { BaseLayoutOptions, WebWorkerLayoutOptions } from './layouts/types';
163
166
  export type { CategoricalPalette } from './palettes/types';
@@ -166,12 +169,14 @@ export type {
166
169
  BubbleSetsOptions,
167
170
  CameraSettingOptions,
168
171
  ContextmenuOptions,
172
+ EdgeFilterLensOptions,
169
173
  FullscreenOptions,
170
174
  GridLineOptions,
171
175
  HistoryOptions,
172
176
  HullOptions,
173
177
  LegendOptions,
174
178
  MinimapOptions,
179
+ SnaplineOptions,
175
180
  TimebarOptions,
176
181
  ToolbarOptions,
177
182
  TooltipOptions,
@@ -0,0 +1,391 @@
1
+ import type { BaseStyleProps } from '@antv/g';
2
+ import { CommonEvent } from '../../constants';
3
+ import { Circle } from '../../elements';
4
+ import type { RuntimeContext } from '../../runtime/types';
5
+ import type { EdgeData, GraphData, NodeData } from '../../spec';
6
+ import type { EdgeStyle } from '../../spec/element/edge';
7
+ import type { NodeStyle } from '../../spec/element/node';
8
+ import type {
9
+ Element,
10
+ ElementDatum,
11
+ ElementType,
12
+ ID,
13
+ IDragEvent,
14
+ IPointerEvent,
15
+ Point,
16
+ PointObject,
17
+ } from '../../types';
18
+ import { idOf } from '../../utils/id';
19
+ import { parsePoint } from '../../utils/point';
20
+ import { positionOf } from '../../utils/position';
21
+ import { distance } from '../../utils/vector';
22
+ import type { BasePluginOptions } from '../base-plugin';
23
+ import { BasePlugin } from '../base-plugin';
24
+
25
+ export interface EdgeFilterLensOptions extends BasePluginOptions {
26
+ /**
27
+ * <zh/> 移动透镜的方式
28
+ * - `'pointermove'`:始终跟随鼠标移动
29
+ * - `'click'`:鼠标点击时透镜移动
30
+ * - `'drag'`:拖拽透镜
31
+ *
32
+ * <en/> The way to move the lens
33
+ * - `'pointermove'`: always follow the mouse movement
34
+ * - `'click'`: move the lens when the mouse clicks
35
+ * - `'drag'`: drag the lens
36
+ * @defaultValue 'pointermove'
37
+ */
38
+ trigger?: 'pointermove' | 'click' | 'drag';
39
+ /**
40
+ * <zh/> 透镜的半径
41
+ *
42
+ * <en/> The radius of the lens
43
+ * @defaultValue 60
44
+ */
45
+ r?: number;
46
+ /**
47
+ * <zh/> 透镜的最大半径。只有在开启 `scaleRByWheel` 时生效
48
+ *
49
+ * <en/> The maximum radius of the lens. Only valid when `scaleRByWheel` is enabled
50
+ * @defaultValue canvas 宽高最小值的一半
51
+ */
52
+ maxR?: number;
53
+ /**
54
+ * <zh/> 透镜的最小半径。只有在开启 `scaleRByWheel` 时生效
55
+ *
56
+ * <en/> The minimum radius of the lens. Only valid when `scaleRByWheel` is enabled
57
+ * @defaultValue 0
58
+ */
59
+ minR?: number;
60
+ /**
61
+ * <zh/> 是否通过滚轮缩放透镜的半径
62
+ *
63
+ * <en/> Whether to scale the radius of the lens by wheel
64
+ * @defaultValue true
65
+ */
66
+ scaleRByWheel?: boolean;
67
+ /**
68
+ * <zh/> 边显示的条件
69
+ * - `'both'`:只有起始节点和目标节点都在透镜中时,边才会显示
70
+ * - `'source'`:只有起始节点在透镜中时,边才会显示
71
+ * - `'target'`:只有目标节点在透镜中时,边才会显示
72
+ * - `'either'`:只要起始节点或目标节点有一个在透镜中时,边就会显示
73
+ *
74
+ * <zh/> The condition for displaying the edge
75
+ * - `'both'`: The edge is displayed only when both the source node and the target node are in the lens
76
+ * - `'source'`: The edge is displayed only when the source node is in the lens
77
+ * - `'target'`: The edge is displayed only when the target node is in the lens
78
+ * - `'either'`: The edge is displayed when either the source node or the target node is in the lens
79
+ * @defaultValue 'both'
80
+ */
81
+ nodeType?: 'both' | 'source' | 'target' | 'either';
82
+ /**
83
+ * <zh/> 过滤出始终不在透镜中显示的元素
84
+ *
85
+ * <en/> Filter elements that are never displayed in the lens
86
+ * @param id - <zh/> 元素的 id | <en/> The id of the element
87
+ * @param elementType - <zh/> 元素的类型 | <en/> The type of the element
88
+ * @returns <zh/> 是否显示 | <en/> Whether to display
89
+ */
90
+ filter?: (id: ID, elementType: ElementType) => boolean;
91
+ /**
92
+ * <zh/> 透镜的样式
93
+ *
94
+ * <en/> The style of the lens
95
+ */
96
+ style?: BaseStyleProps;
97
+ /**
98
+ * <zh/> 在透镜中节点的样式
99
+ *
100
+ * <en/> The style of the nodes displayed in the lens
101
+ */
102
+ nodeStyle?: NodeStyle | ((datum: NodeData) => NodeStyle);
103
+ /**
104
+ * <zh/> 在透镜中边的样式
105
+ *
106
+ * <en/> The style of the edges displayed in the lens
107
+ */
108
+ edgeStyle?: EdgeStyle | ((datum: EdgeData) => EdgeStyle);
109
+ /**
110
+ * <zh/> 是否阻止默认事件
111
+ *
112
+ * <en/> Whether to prevent the default event
113
+ * @defaultValue true
114
+ */
115
+ preventDefault?: boolean;
116
+ }
117
+
118
+ const defaultLensStyle: BaseStyleProps = {
119
+ fill: '#fff',
120
+ fillOpacity: 1,
121
+ lineWidth: 1,
122
+ stroke: '#000',
123
+ strokeOpacity: 0.8,
124
+ };
125
+
126
+ const DELTA = 0.05;
127
+
128
+ export class EdgeFilterLens extends BasePlugin<EdgeFilterLensOptions> {
129
+ static defaultOptions: Partial<EdgeFilterLensOptions> = {
130
+ trigger: 'pointermove',
131
+ r: 60,
132
+ nodeType: 'both',
133
+ filter: () => true,
134
+ style: { lineWidth: 2 },
135
+ nodeStyle: { label: false },
136
+ edgeStyle: { label: true },
137
+ scaleRByWheel: true,
138
+ preventDefault: true,
139
+ };
140
+
141
+ constructor(context: RuntimeContext, options: EdgeFilterLensOptions) {
142
+ super(context, Object.assign({}, EdgeFilterLens.defaultOptions, options));
143
+ this.bindEvents();
144
+ }
145
+
146
+ private lens!: Circle;
147
+
148
+ private shapes = new Map<ID, Element>();
149
+
150
+ private r = this.options.r;
151
+
152
+ private get canvas() {
153
+ return this.context.canvas.getLayer('transient');
154
+ }
155
+
156
+ private get isLensOn() {
157
+ return this.lens && !this.lens.destroyed;
158
+ }
159
+
160
+ protected onEdgeFilter = (event: IPointerEvent) => {
161
+ if (this.options.trigger === 'drag' && this.isLensOn) return;
162
+
163
+ const origin = parsePoint(event.canvas as PointObject);
164
+ this.renderLens(origin);
165
+ this.renderFocusElements();
166
+ };
167
+
168
+ private renderLens = (origin: Point) => {
169
+ const [x, y] = origin;
170
+ const positionStyle = { size: this.r * 2, x, y };
171
+
172
+ if (!this.isLensOn) {
173
+ const style = Object.assign({}, defaultLensStyle, this.options.style, positionStyle);
174
+ this.lens = new Circle({ style });
175
+ } else {
176
+ this.lens.update(positionStyle);
177
+ }
178
+
179
+ this.canvas.appendChild(this.lens);
180
+ };
181
+
182
+ private getFilterData = (): Required<GraphData> => {
183
+ const { filter } = this.options;
184
+ const { model } = this.context;
185
+ const data = model.getData();
186
+
187
+ if (!filter) return data;
188
+
189
+ const { nodes, edges, combos } = data;
190
+
191
+ return {
192
+ nodes: nodes.filter((node) => filter(idOf(node), 'node')),
193
+ edges: edges.filter((edge) => filter(idOf(edge), 'edge')),
194
+ combos: combos.filter((combo) => filter(idOf(combo), 'combo')),
195
+ };
196
+ };
197
+
198
+ private getFocusElements = (origin: Point) => {
199
+ const { nodes, edges } = this.getFilterData();
200
+
201
+ const focusNodes = nodes.filter((datum) => distance(positionOf(datum), origin) < this.r);
202
+ const focusNodeIds = focusNodes.map((node) => idOf(node));
203
+
204
+ const focusEdges = edges.filter((datum) => {
205
+ const { source, target } = datum;
206
+ const isSourceFocus = focusNodeIds.includes(source);
207
+ const isTargetFocus = focusNodeIds.includes(target);
208
+
209
+ switch (this.options.nodeType) {
210
+ case 'both':
211
+ return isSourceFocus && isTargetFocus;
212
+ case 'either':
213
+ return isSourceFocus !== isTargetFocus;
214
+ case 'source':
215
+ return isSourceFocus && !isTargetFocus;
216
+ case 'target':
217
+ return !isSourceFocus && isTargetFocus;
218
+ default:
219
+ return false;
220
+ }
221
+ });
222
+
223
+ return { nodes: focusNodes, edges: focusEdges };
224
+ };
225
+
226
+ private renderFocusElements = () => {
227
+ const { element, graph } = this.context;
228
+ if (!this.isLensOn) return;
229
+
230
+ const origin = this.lens.getCenter();
231
+ const { nodes, edges } = this.getFocusElements(origin);
232
+
233
+ const ids = new Set<ID>();
234
+
235
+ const { nodeStyle, edgeStyle } = this.options;
236
+
237
+ const iterate = (datum: ElementDatum) => {
238
+ const id = idOf(datum);
239
+ ids.add(id);
240
+
241
+ const shape = element!.getElement(id);
242
+ if (!shape) return;
243
+
244
+ const cloneShape = this.shapes.get(id) || shape.cloneNode();
245
+
246
+ cloneShape.setPosition(shape.getPosition());
247
+ cloneShape.id = shape.id;
248
+
249
+ if (!this.shapes.has(id)) {
250
+ this.canvas.appendChild(cloneShape);
251
+ this.shapes.set(id, cloneShape);
252
+ } else {
253
+ Object.entries(shape.attributes).forEach(([key, value]) => {
254
+ if (cloneShape.style[key] !== value) cloneShape.style[key] = value;
255
+ });
256
+ }
257
+
258
+ const elementType = graph.getElementType(id) as Exclude<ElementType, 'combo'>;
259
+ const style = this.getElementStyle(elementType, datum);
260
+
261
+ // @ts-ignore
262
+ cloneShape.update(style);
263
+ };
264
+
265
+ nodes.forEach(iterate);
266
+ edges.forEach(iterate);
267
+
268
+ this.shapes.forEach((shape, id) => {
269
+ if (!ids.has(id)) {
270
+ shape.destroy();
271
+ this.shapes.delete(id);
272
+ }
273
+ });
274
+ };
275
+
276
+ private getElementStyle(elementType: ElementType, datum: ElementDatum) {
277
+ const styler = elementType === 'node' ? this.options.nodeStyle : this.options.edgeStyle;
278
+ if (typeof styler === 'function') return styler(datum as any);
279
+ return styler;
280
+ }
281
+
282
+ private scaleRByWheel = (event: WheelEvent) => {
283
+ if (this.options.preventDefault) event.preventDefault();
284
+ const { clientX, clientY, deltaX, deltaY } = event;
285
+ const { graph, canvas } = this.context;
286
+ const scaleOrigin = graph.getCanvasByClient([clientX, clientY]);
287
+ const origin = this.lens?.getCenter();
288
+
289
+ if (!this.isLensOn || distance(scaleOrigin, origin) > this.r) {
290
+ return;
291
+ }
292
+
293
+ const { maxR, minR } = this.options;
294
+ const ratio = deltaX + deltaY > 0 ? 1 / (1 - DELTA) : 1 - DELTA;
295
+ const canvasR = Math.min(...canvas.getSize()) / 2;
296
+ this.r = Math.max(minR || 0, Math.min(maxR || canvasR, this.r * ratio));
297
+
298
+ this.renderLens(origin);
299
+ this.renderFocusElements();
300
+ };
301
+
302
+ get graphDom() {
303
+ return this.context.graph.getCanvas().getContextService().getDomElement();
304
+ }
305
+
306
+ private isLensDragging = false;
307
+
308
+ private onDragStart = (event: IDragEvent) => {
309
+ const dragOrigin = parsePoint(event.canvas as PointObject);
310
+ const origin = this.lens?.getCenter();
311
+
312
+ if (!this.isLensOn || distance(dragOrigin, origin) > this.r) return;
313
+
314
+ this.isLensDragging = true;
315
+ };
316
+
317
+ private onDrag = (event: IDragEvent) => {
318
+ if (!this.isLensDragging) return;
319
+
320
+ const dragOrigin = parsePoint(event.canvas as PointObject);
321
+ this.renderLens(dragOrigin);
322
+ this.renderFocusElements();
323
+ };
324
+
325
+ private onDragEnd = () => {
326
+ this.isLensDragging = false;
327
+ };
328
+
329
+ private bindEvents() {
330
+ const { graph } = this.context;
331
+ const { trigger, scaleRByWheel } = this.options;
332
+
333
+ const canvas = graph.getCanvas().getLayer();
334
+
335
+ if (['click', 'drag'].includes(trigger)) {
336
+ canvas.addEventListener(CommonEvent.CLICK, this.onEdgeFilter);
337
+ }
338
+
339
+ if (trigger === 'pointermove') {
340
+ canvas.addEventListener(CommonEvent.POINTER_MOVE, this.onEdgeFilter);
341
+ } else if (trigger === 'drag') {
342
+ canvas.addEventListener(CommonEvent.DRAG_START, this.onDragStart);
343
+ canvas.addEventListener(CommonEvent.DRAG, this.onDrag);
344
+ canvas.addEventListener(CommonEvent.DRAG_END, this.onDragEnd);
345
+ }
346
+
347
+ if (scaleRByWheel) {
348
+ this.graphDom?.addEventListener(CommonEvent.WHEEL, this.scaleRByWheel, { passive: false });
349
+ }
350
+ }
351
+
352
+ private unbindEvents() {
353
+ const { graph } = this.context;
354
+ const { trigger, scaleRByWheel } = this.options;
355
+ const canvas = graph.getCanvas().getLayer();
356
+
357
+ if (['click', 'drag'].includes(trigger)) {
358
+ canvas.removeEventListener(CommonEvent.CLICK, this.onEdgeFilter);
359
+ }
360
+
361
+ if (trigger === 'pointermove') {
362
+ canvas.removeEventListener(CommonEvent.POINTER_MOVE, this.onEdgeFilter);
363
+ } else if (trigger === 'drag') {
364
+ canvas.removeEventListener(CommonEvent.DRAG_START, this.onDragStart);
365
+ canvas.removeEventListener(CommonEvent.DRAG, this.onDrag);
366
+ canvas.removeEventListener(CommonEvent.DRAG_END, this.onDragEnd);
367
+ }
368
+
369
+ if (scaleRByWheel) {
370
+ this.graphDom?.removeEventListener(CommonEvent.WHEEL, this.scaleRByWheel);
371
+ }
372
+ }
373
+
374
+ public update(options: Partial<EdgeFilterLensOptions>) {
375
+ this.unbindEvents();
376
+ super.update(options);
377
+ this.bindEvents();
378
+ }
379
+
380
+ public destroy() {
381
+ this.unbindEvents();
382
+ if (this.isLensOn) {
383
+ this.lens.destroy();
384
+ }
385
+ this.shapes.forEach((shape, id) => {
386
+ shape.destroy();
387
+ this.shapes.delete(id);
388
+ });
389
+ super.destroy();
390
+ }
391
+ }
@@ -3,6 +3,7 @@ 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';
6
7
  export { Fullscreen } from './fullscreen';
7
8
  export { GridLine } from './grid-line';
8
9
  export { History } from './history';
@@ -20,6 +21,7 @@ export type { BasePluginOptions } from './base-plugin';
20
21
  export type { BubbleSetsOptions } from './bubble-sets';
21
22
  export type { CameraSettingOptions } from './camera-setting';
22
23
  export type { ContextmenuOptions } from './contextmenu';
24
+ export type { EdgeFilterLensOptions } from './edge-filter-lens';
23
25
  export type { FullscreenOptions } from './fullscreen';
24
26
  export type { GridLineOptions } from './grid-line';
25
27
  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() {
@@ -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,7 @@ import {
59
72
  Background,
60
73
  BubbleSets,
61
74
  Contextmenu,
75
+ EdgeFilterLens,
62
76
  Fullscreen,
63
77
  GridLine,
64
78
  History,
@@ -169,6 +183,7 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
169
183
  },
170
184
  plugin: {
171
185
  'bubble-sets': BubbleSets,
186
+ 'edge-filter-lens': EdgeFilterLens,
172
187
  'grid-line': GridLine,
173
188
  background: Background,
174
189
  contextmenu: Contextmenu,
@@ -191,6 +206,21 @@ const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
191
206
  'process-parallel-edges': ProcessParallelEdges,
192
207
  'get-edge-actual-ends': GetEdgeActualEnds,
193
208
  },
209
+ shape: {
210
+ circle: GCircle,
211
+ ellipse: GEllipse,
212
+ group: GGroup,
213
+ html: GHTML,
214
+ image: ImageShape,
215
+ line: GLine,
216
+ path: GPath,
217
+ polygon: GPolygon,
218
+ polyline: GPolyline,
219
+ rect: GRect,
220
+ text: GText,
221
+ label: LabelShape,
222
+ badge: BadgeShape,
223
+ },
194
224
  };
195
225
 
196
226
  import type { ExtensionCategory } from '../constants';
@@ -16,4 +16,5 @@ export const EXTENSION_REGISTRY: ExtensionRegistry = {
16
16
  theme: {},
17
17
  plugin: {},
18
18
  transform: {},
19
+ shape: {},
19
20
  };
@@ -1,3 +1,4 @@
1
+ import type { DisplayObject } from '@antv/g';
1
2
  import type { STDAnimation } from '../animations/types';
2
3
  import type { Behavior } from '../behaviors/types';
3
4
  import type { Layout } from '../layouts/types';
@@ -23,4 +24,5 @@ export interface ExtensionRegistry {
23
24
  plugin: Record<string, { new (...args: any[]): Plugin }>;
24
25
  animation: Record<string, STDAnimation>; // animation spec
25
26
  transform: Record<string, { new (...args: any[]): Transform }>;
27
+ shape: Record<string, { new (...args: any[]): DisplayObject }>;
26
28
  }
@@ -1,6 +1,7 @@
1
1
  import type { BasePlugin } from '../plugins/base-plugin';
2
2
  import { ExtensionController } from '../registry/extension';
3
3
  import type { CustomPluginOption, PluginOptions } from '../spec/plugin';
4
+ import { print } from '../utils/print';
4
5
  import type { RuntimeContext } from './types';
5
6
 
6
7
  export class PluginController extends ExtensionController<BasePlugin<CustomPluginOption>> {
@@ -16,6 +17,12 @@ export class PluginController extends ExtensionController<BasePlugin<CustomPlugi
16
17
  }
17
18
 
18
19
  public getPluginInstance(key: string) {
19
- return this.extensionMap[key];
20
+ const exactly = this.extensionMap[key];
21
+ if (exactly) return exactly;
22
+
23
+ print.warn(`Cannot find the plugin ${key}, will try to find it by type.`);
24
+
25
+ const fussily = this.extensions.find((extension) => extension.type === key);
26
+ if (fussily) return this.extensionMap[fussily.key];
20
27
  }
21
28
  }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '5.0.16';
1
+ export const version = '5.0.17';