@antv/g6 5.0.0-beta.29 → 5.0.0-beta.30

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 (172) hide show
  1. package/dist/g6.min.js +2 -3
  2. package/dist/lib/__tests__/demo/case/index.d.ts +7 -2
  3. package/dist/lib/__tests__/demo/case/layout-antv-dagre-flow-combo.d.ts +2 -0
  4. package/dist/lib/__tests__/demo/case/layout-antv-dagre-flow.d.ts +2 -0
  5. package/dist/lib/__tests__/demo/case/layout-dagre.d.ts +2 -0
  6. package/dist/lib/__tests__/demo/case/plugin-contextmenu.d.ts +2 -0
  7. package/dist/lib/__tests__/demo/case/plugin-tooltip.d.ts +2 -0
  8. package/dist/lib/__tests__/demo/case/plugin-watermark-image.d.ts +2 -0
  9. package/dist/lib/__tests__/demo/case/plugin-watermark.d.ts +2 -0
  10. package/dist/lib/__tests__/demo/static/common.d.ts +1 -1
  11. package/dist/lib/__tests__/demo/static/controller-layout-antv-dagre.d.ts +2 -0
  12. package/dist/lib/__tests__/setup.d.ts +1 -0
  13. package/dist/lib/__tests__/unit/layouts/dagre.spec.d.ts +1 -0
  14. package/dist/lib/__tests__/unit/plugins/plugin-contextmenu.spec.d.ts +1 -0
  15. package/dist/lib/__tests__/unit/plugins/plugin-watermark.spec.d.ts +1 -0
  16. package/dist/lib/__tests__/unit/plugins/tooltip.spec.d.ts +1 -0
  17. package/dist/lib/src/behaviors/drag-element.d.ts +4 -10
  18. package/dist/lib/src/behaviors/zoom-canvas.d.ts +1 -0
  19. package/dist/lib/src/elements/nodes/base-node.d.ts +1 -1
  20. package/dist/lib/src/exports.d.ts +2 -2
  21. package/dist/lib/src/layouts/index.d.ts +1 -1
  22. package/dist/lib/src/layouts/types.d.ts +8 -2
  23. package/dist/lib/src/plugins/contextmenu.d.ts +88 -0
  24. package/dist/lib/src/plugins/grid-line.d.ts +0 -1
  25. package/dist/lib/src/plugins/index.d.ts +6 -0
  26. package/dist/lib/src/plugins/tooltip.d.ts +39 -0
  27. package/dist/lib/src/plugins/watermark.d.ts +51 -0
  28. package/dist/lib/src/registry/extension/types.d.ts +5 -3
  29. package/dist/lib/src/runtime/element.d.ts +2 -2
  30. package/dist/lib/src/runtime/graph.d.ts +40 -0
  31. package/dist/lib/src/spec/behavior.d.ts +10 -2
  32. package/dist/lib/src/spec/plugin.d.ts +10 -2
  33. package/dist/lib/src/types/event.d.ts +6 -2
  34. package/dist/lib/src/types/plugin.d.ts +5 -0
  35. package/dist/lib/src/utils/contextmenu.d.ts +26 -0
  36. package/dist/lib/src/utils/dom.d.ts +7 -0
  37. package/dist/lib/src/utils/watermark.d.ts +18 -0
  38. package/esm/behaviors/drag-element.d.ts +4 -10
  39. package/esm/behaviors/drag-element.js +17 -20
  40. package/esm/behaviors/drag-element.js.map +1 -1
  41. package/esm/behaviors/zoom-canvas.d.ts +1 -0
  42. package/esm/behaviors/zoom-canvas.js +8 -7
  43. package/esm/behaviors/zoom-canvas.js.map +1 -1
  44. package/esm/elements/nodes/base-node.d.ts +1 -1
  45. package/esm/exports.d.ts +2 -2
  46. package/esm/layouts/index.d.ts +1 -1
  47. package/esm/layouts/index.js +1 -1
  48. package/esm/layouts/index.js.map +1 -1
  49. package/esm/layouts/types.d.ts +8 -2
  50. package/esm/plugins/contextmenu.d.ts +88 -0
  51. package/esm/plugins/contextmenu.js +123 -0
  52. package/esm/plugins/contextmenu.js.map +1 -0
  53. package/esm/plugins/grid-line.d.ts +0 -1
  54. package/esm/plugins/grid-line.js +5 -13
  55. package/esm/plugins/grid-line.js.map +1 -1
  56. package/esm/plugins/index.d.ts +6 -0
  57. package/esm/plugins/index.js +3 -0
  58. package/esm/plugins/index.js.map +1 -1
  59. package/esm/plugins/tooltip.d.ts +39 -0
  60. package/esm/plugins/tooltip.js +232 -0
  61. package/esm/plugins/tooltip.js.map +1 -0
  62. package/esm/plugins/watermark.d.ts +51 -0
  63. package/esm/plugins/watermark.js +53 -0
  64. package/esm/plugins/watermark.js.map +1 -0
  65. package/esm/registry/build-in.js +6 -2
  66. package/esm/registry/build-in.js.map +1 -1
  67. package/esm/registry/extension/types.d.ts +5 -3
  68. package/esm/runtime/behavior.js +1 -1
  69. package/esm/runtime/element.d.ts +2 -2
  70. package/esm/runtime/graph.d.ts +40 -0
  71. package/esm/runtime/graph.js +52 -0
  72. package/esm/runtime/graph.js.map +1 -1
  73. package/esm/runtime/layout.js +7 -1
  74. package/esm/runtime/layout.js.map +1 -1
  75. package/esm/spec/behavior.d.ts +10 -2
  76. package/esm/spec/plugin.d.ts +10 -2
  77. package/esm/types/event.d.ts +6 -2
  78. package/esm/types/plugin.d.ts +5 -0
  79. package/esm/types/plugin.js +2 -0
  80. package/esm/types/plugin.js.map +1 -0
  81. package/esm/utils/contextmenu.d.ts +26 -0
  82. package/esm/utils/contextmenu.js +45 -0
  83. package/esm/utils/contextmenu.js.map +1 -0
  84. package/esm/utils/dom.d.ts +7 -0
  85. package/esm/utils/dom.js +21 -0
  86. package/esm/utils/dom.js.map +1 -1
  87. package/esm/utils/layout.js +1 -1
  88. package/esm/utils/layout.js.map +1 -1
  89. package/esm/utils/watermark.d.ts +18 -0
  90. package/esm/utils/watermark.js +73 -0
  91. package/esm/utils/watermark.js.map +1 -0
  92. package/lib/behaviors/drag-element.d.ts +4 -10
  93. package/lib/behaviors/drag-element.js +16 -19
  94. package/lib/behaviors/drag-element.js.map +1 -1
  95. package/lib/behaviors/zoom-canvas.d.ts +1 -0
  96. package/lib/behaviors/zoom-canvas.js +7 -6
  97. package/lib/behaviors/zoom-canvas.js.map +1 -1
  98. package/lib/elements/nodes/base-node.d.ts +1 -1
  99. package/lib/exports.d.ts +2 -2
  100. package/lib/layouts/index.d.ts +1 -1
  101. package/lib/layouts/index.js +2 -1
  102. package/lib/layouts/index.js.map +1 -1
  103. package/lib/layouts/types.d.ts +8 -2
  104. package/lib/plugins/contextmenu.d.ts +88 -0
  105. package/lib/plugins/contextmenu.js +127 -0
  106. package/lib/plugins/contextmenu.js.map +1 -0
  107. package/lib/plugins/grid-line.d.ts +0 -1
  108. package/lib/plugins/grid-line.js +5 -13
  109. package/lib/plugins/grid-line.js.map +1 -1
  110. package/lib/plugins/index.d.ts +6 -0
  111. package/lib/plugins/index.js +7 -1
  112. package/lib/plugins/index.js.map +1 -1
  113. package/lib/plugins/tooltip.d.ts +39 -0
  114. package/lib/plugins/tooltip.js +236 -0
  115. package/lib/plugins/tooltip.js.map +1 -0
  116. package/lib/plugins/watermark.d.ts +51 -0
  117. package/lib/plugins/watermark.js +57 -0
  118. package/lib/plugins/watermark.js.map +1 -0
  119. package/lib/registry/build-in.js +4 -0
  120. package/lib/registry/build-in.js.map +1 -1
  121. package/lib/registry/extension/types.d.ts +5 -3
  122. package/lib/runtime/behavior.js +1 -1
  123. package/lib/runtime/element.d.ts +2 -2
  124. package/lib/runtime/graph.d.ts +40 -0
  125. package/lib/runtime/graph.js +52 -0
  126. package/lib/runtime/graph.js.map +1 -1
  127. package/lib/runtime/layout.js +7 -1
  128. package/lib/runtime/layout.js.map +1 -1
  129. package/lib/spec/behavior.d.ts +10 -2
  130. package/lib/spec/plugin.d.ts +10 -2
  131. package/lib/types/event.d.ts +6 -2
  132. package/lib/types/plugin.d.ts +5 -0
  133. package/lib/types/plugin.js +3 -0
  134. package/lib/types/plugin.js.map +1 -0
  135. package/lib/utils/contextmenu.d.ts +26 -0
  136. package/lib/utils/contextmenu.js +49 -0
  137. package/lib/utils/contextmenu.js.map +1 -0
  138. package/lib/utils/dom.d.ts +7 -0
  139. package/lib/utils/dom.js +23 -1
  140. package/lib/utils/dom.js.map +1 -1
  141. package/lib/utils/layout.js +1 -1
  142. package/lib/utils/layout.js.map +1 -1
  143. package/lib/utils/watermark.d.ts +18 -0
  144. package/lib/utils/watermark.js +78 -0
  145. package/lib/utils/watermark.js.map +1 -0
  146. package/package.json +13 -5
  147. package/src/behaviors/drag-element.ts +20 -28
  148. package/src/behaviors/zoom-canvas.ts +12 -7
  149. package/src/layouts/index.ts +1 -0
  150. package/src/layouts/types.ts +17 -5
  151. package/src/plugins/contextmenu.ts +199 -0
  152. package/src/plugins/grid-line.ts +7 -14
  153. package/src/plugins/index.ts +6 -0
  154. package/src/plugins/tooltip.ts +268 -0
  155. package/src/plugins/watermark.ts +98 -0
  156. package/src/registry/build-in.ts +6 -1
  157. package/src/registry/extension/types.ts +5 -3
  158. package/src/runtime/behavior.ts +1 -1
  159. package/src/runtime/graph.ts +60 -0
  160. package/src/runtime/layout.ts +7 -1
  161. package/src/spec/behavior.ts +11 -2
  162. package/src/spec/plugin.ts +11 -2
  163. package/src/types/event.ts +7 -2
  164. package/src/types/plugin.ts +6 -0
  165. package/src/utils/contextmenu.ts +62 -0
  166. package/src/utils/dom.ts +26 -0
  167. package/src/utils/layout.ts +1 -1
  168. package/src/utils/watermark.ts +93 -0
  169. package/dist/lib/__tests__/demo/case/layout-dagre-flow-combo.d.ts +0 -2
  170. package/dist/lib/__tests__/demo/case/layout-dagre-flow.d.ts +0 -2
  171. package/dist/lib/__tests__/demo/static/controller-layout-dagre.d.ts +0 -2
  172. /package/dist/lib/__tests__/unit/{layouts/dagre-flow.spec.d.ts → behaviors/drag-element-bug.spec.d.ts} +0 -0
@@ -8,7 +8,7 @@ import type { BehaviorEvent, EdgeDirection, Point, PrefixObject } from '../types
8
8
  import { getBBoxSize, getCombinedBBox } from '../utils/bbox';
9
9
  import { idOf } from '../utils/id';
10
10
  import { subStyleProps } from '../utils/prefix';
11
- import { subtract } from '../utils/vector';
11
+ import { divide, subtract } from '../utils/vector';
12
12
  import type { BaseBehaviorOptions } from './base-behavior';
13
13
  import { BaseBehavior } from './base-behavior';
14
14
 
@@ -25,12 +25,6 @@ export interface DragElementOptions extends BaseBehaviorOptions, PrefixObject<Ba
25
25
  * <en/> Whether to enable the function of dragging the node
26
26
  */
27
27
  enable?: boolean | ((event: BehaviorEvent<FederatedMouseEvent> | BehaviorEvent<KeyboardEvent>) => boolean);
28
- /**
29
- * <zh/> 支持拖拽的元素类型
30
- *
31
- * <en/> Supported element types for dragging
32
- */
33
- draggableElement: ('node' | 'combo')[];
34
28
  /**
35
29
  * <zh/> 拖拽操作效果
36
30
  * - link: 将拖拽元素置入为目标元素的子元素
@@ -72,7 +66,7 @@ export interface DragElementOptions extends BaseBehaviorOptions, PrefixObject<Ba
72
66
  *
73
67
  * <en/> Edges will not be hidden when using the drag shadow
74
68
  */
75
- hideEdges?: 'none' | 'all' | EdgeDirection;
69
+ hideEdge?: 'none' | 'all' | EdgeDirection;
76
70
  /**
77
71
  * <zh/> 是否启用幽灵节点,即用一个图形代替节点跟随鼠标移动
78
72
  *
@@ -84,17 +78,16 @@ export interface DragElementOptions extends BaseBehaviorOptions, PrefixObject<Ba
84
78
  *
85
79
  * <en/> Callback when dragging is completed
86
80
  */
87
- onfinish?: (ids: ID[]) => void;
81
+ onFinish?: (ids: ID[]) => void;
88
82
  }
89
83
 
90
84
  export class DragElement extends BaseBehavior<DragElementOptions> {
91
85
  static defaultOptions: Partial<DragElementOptions> = {
92
86
  animation: true,
93
- enable: true,
94
- draggableElement: ['node', 'combo'],
87
+ enable: (event) => ['node', 'combo'].includes(event.targetType),
95
88
  dropEffect: 'move',
96
89
  state: 'selected',
97
- hideEdges: 'none',
90
+ hideEdge: 'none',
98
91
  shadowZIndex: 100,
99
92
  shadowFill: '#F3F9FF',
100
93
  shadowFillOpacity: 0.5,
@@ -105,6 +98,8 @@ export class DragElement extends BaseBehavior<DragElementOptions> {
105
98
 
106
99
  private enable: boolean = false;
107
100
 
101
+ private enableElements = ['node', 'combo'];
102
+
108
103
  private target: ID[] = [];
109
104
 
110
105
  private shadow?: Rect;
@@ -118,10 +113,6 @@ export class DragElement extends BaseBehavior<DragElementOptions> {
118
113
  return this.options.animation;
119
114
  }
120
115
 
121
- private get element() {
122
- return new Set<string>(this.options.draggableElement);
123
- }
124
-
125
116
  constructor(context: RuntimeContext, options: DragElementOptions) {
126
117
  super(context, Object.assign({}, DragElement.defaultOptions, options));
127
118
  this.bindEvents();
@@ -136,7 +127,7 @@ export class DragElement extends BaseBehavior<DragElementOptions> {
136
127
  const { graph } = this.context;
137
128
  this.unbindEvents();
138
129
 
139
- this.element.forEach((type) => {
130
+ this.enableElements.forEach((type) => {
140
131
  graph.on(`${type}:${CommonEvent.DRAG_START}`, this.onDragStart);
141
132
  graph.on(`${type}:${CommonEvent.DRAG}`, this.onDrag);
142
133
  graph.on(`${type}:${CommonEvent.DRAG_END}`, this.onDragEnd);
@@ -164,17 +155,19 @@ export class DragElement extends BaseBehavior<DragElementOptions> {
164
155
  if (!this.enable) return;
165
156
 
166
157
  this.target = this.getSelectedNodeIDs([event.target.id]);
167
- this.hideEdges();
158
+ this.hideEdge();
168
159
  this.context.graph.frontElement(this.target);
169
160
  if (this.options.shadow) this.createShadow(this.target);
170
161
  };
171
162
 
172
163
  private onDrag = (event: DragEvent) => {
173
164
  if (!this.enable) return;
165
+ const zoom = this.context.graph.getZoom();
174
166
  const { dx, dy } = event;
167
+ const delta = divide([dx, dy], zoom);
175
168
 
176
- if (this.options.shadow) this.moveShadow([dx, dy]);
177
- else this.moveElement(this.target, [dx, dy]);
169
+ if (this.options.shadow) this.moveShadow(delta);
170
+ else this.moveElement(this.target, delta);
178
171
  };
179
172
 
180
173
  private onDragEnd = () => {
@@ -187,7 +180,7 @@ export class DragElement extends BaseBehavior<DragElementOptions> {
187
180
  this.moveElement(this.target, [dx, dy]);
188
181
  }
189
182
  this.showEdges();
190
- this.options.onfinish?.(this.target);
183
+ this.options.onFinish?.(this.target);
191
184
  this.target = [];
192
185
  };
193
186
 
@@ -209,7 +202,6 @@ export class DragElement extends BaseBehavior<DragElementOptions> {
209
202
 
210
203
  private validate(event: DragEvent) {
211
204
  if (this.destroyed) return false;
212
- if (!this.element.has(event.targetType)) return false;
213
205
  const { enable } = this.options;
214
206
  if (isFunction(enable)) return enable(event);
215
207
  return !!enable;
@@ -270,14 +262,14 @@ export class DragElement extends BaseBehavior<DragElementOptions> {
270
262
  this.hiddenEdges = [];
271
263
  }
272
264
 
273
- private hideEdges() {
274
- const { hideEdges, shadow } = this.options;
275
- if (hideEdges === 'none' || shadow) return;
265
+ private hideEdge() {
266
+ const { hideEdge, shadow } = this.options;
267
+ if (hideEdge === 'none' || shadow) return;
276
268
  const { graph } = this.context;
277
- if (hideEdges === 'all') this.hiddenEdges = graph.getEdgeData().map(idOf);
269
+ if (hideEdge === 'all') this.hiddenEdges = graph.getEdgeData().map(idOf);
278
270
  else {
279
271
  this.hiddenEdges = Array.from(
280
- new Set(this.target.map((id) => graph.getRelatedEdgesData(id, hideEdges).map(idOf)).flat()),
272
+ new Set(this.target.map((id) => graph.getRelatedEdgesData(id, hideEdge).map(idOf)).flat()),
281
273
  );
282
274
  }
283
275
  graph.hideElement(this.hiddenEdges);
@@ -286,7 +278,7 @@ export class DragElement extends BaseBehavior<DragElementOptions> {
286
278
  private unbindEvents() {
287
279
  const { graph } = this.context;
288
280
 
289
- this.element.forEach((type) => {
281
+ this.enableElements.forEach((type) => {
290
282
  graph.off(`${type}:${CommonEvent.DRAG_START}`, this.onDragStart);
291
283
  graph.off(`${type}:${CommonEvent.DRAG}`, this.onDrag);
292
284
  graph.off(`${type}:${CommonEvent.DRAG_END}`, this.onDragEnd);
@@ -1,5 +1,5 @@
1
1
  import type { PointLike } from '@antv/g';
2
- import { isArray, isFunction, isObject } from '@antv/util';
2
+ import { clamp, isArray, isFunction, isObject } from '@antv/util';
3
3
  import { CanvasEvent } from '../constants';
4
4
  import type { RuntimeContext } from '../runtime/types';
5
5
  import type { BehaviorEvent, Point, ViewportAnimationEffectTiming } from '../types';
@@ -87,14 +87,14 @@ export class ZoomCanvas extends BaseBehavior<ZoomCanvasOptions> {
87
87
  this.preventDefault(CanvasEvent.WHEEL);
88
88
  this.shortcut.bind([...trigger, CanvasEvent.WHEEL], (event) => {
89
89
  const { deltaX, deltaY } = event;
90
- this.zoom(-(deltaY ?? deltaX), event);
90
+ this.zoom(-(deltaY ?? deltaX), event, false);
91
91
  });
92
92
  }
93
93
 
94
94
  if (isObject(trigger)) {
95
95
  const { zoomIn = [], zoomOut = [], reset = [] } = trigger as CombinationKey;
96
- this.shortcut.bind(zoomIn, (event) => this.zoom(1, event));
97
- this.shortcut.bind(zoomOut, (event) => this.zoom(-1, event));
96
+ this.shortcut.bind(zoomIn, (event) => this.zoom(10, event, this.options.animation));
97
+ this.shortcut.bind(zoomOut, (event) => this.zoom(-10, event, this.options.animation));
98
98
  this.shortcut.bind(reset, this.onReset);
99
99
  }
100
100
  }
@@ -105,8 +105,13 @@ export class ZoomCanvas extends BaseBehavior<ZoomCanvasOptions> {
105
105
  * <en/> Zoom canvas
106
106
  * @param value - <zh/> 缩放值, > 0 放大, < 0 缩小 | <en/> Zoom value, > 0 zoom in, < 0 zoom out
107
107
  * @param event - <zh/> 事件对象 | <en/> Event object
108
+ * @param animation - <zh/> 缩放动画配置 | <en/> Zoom animation configuration
108
109
  */
109
- private zoom = async (value: number, event: BehaviorEvent<WheelEvent> | BehaviorEvent<KeyboardEvent>) => {
110
+ private zoom = async (
111
+ value: number,
112
+ event: BehaviorEvent<WheelEvent> | BehaviorEvent<KeyboardEvent>,
113
+ animation: ZoomCanvasOptions['animation'],
114
+ ) => {
110
115
  if (!this.validate(event)) return;
111
116
  const { graph } = this.context;
112
117
 
@@ -116,9 +121,9 @@ export class ZoomCanvas extends BaseBehavior<ZoomCanvasOptions> {
116
121
  }
117
122
 
118
123
  const { sensitivity, onfinish } = this.options;
119
- const diff = (value * sensitivity) / 10;
124
+ const ratio = 1 + (clamp(value, -50, 50) * sensitivity) / 100;
120
125
  const zoom = graph.getZoom();
121
- await graph.zoomTo(zoom + diff, this.options.animation, origin);
126
+ await graph.zoomTo(zoom * ratio, animation, origin);
122
127
 
123
128
  onfinish?.();
124
129
  };
@@ -1,5 +1,6 @@
1
1
  export { compactBox, dendrogram, indented, mindmap } from '@antv/hierarchy';
2
2
  export {
3
+ AntVDagreLayout,
3
4
  CircularLayout,
4
5
  ComboCombinedLayout,
5
6
  ConcentricLayout,
@@ -1,7 +1,9 @@
1
1
  import type {
2
+ AntVDagreLayoutOptions,
2
3
  CircularLayoutOptions,
3
4
  ConcentricLayoutOptions,
4
5
  D3ForceLayoutOptions,
6
+ DagreLayoutOptions,
5
7
  ForceAtlas2LayoutOptions,
6
8
  ForceLayoutOptions,
7
9
  FruchtermanLayoutOptions,
@@ -13,16 +15,18 @@ import type {
13
15
  import type { NodeData } from '../spec/data';
14
16
 
15
17
  export type BuiltInLayoutOptions =
18
+ | AntVDagreLayout
16
19
  | CircularLayout
17
- | RandomLayout
18
20
  | ConcentricLayout
21
+ | D3ForceLayout
22
+ | DagreLayout
23
+ | ForceAtlas2
24
+ | ForceLayout
25
+ | FruchtermanLayout
19
26
  | GridLayout
20
27
  | MDSLayout
21
28
  | RadialLayout
22
- | FruchtermanLayout
23
- | D3ForceLayout
24
- | ForceLayout
25
- | ForceAtlas2;
29
+ | RandomLayout;
26
30
 
27
31
  export interface BaseLayoutOptions extends AnimationOptions, WebWorkerLayoutOptions, PresetLayoutOptions {
28
32
  type: string;
@@ -77,6 +81,14 @@ interface ForceAtlas2 extends BaseLayoutOptions, ForceAtlas2LayoutOptions {
77
81
  type: 'forceAtlas2';
78
82
  }
79
83
 
84
+ interface AntVDagreLayout extends BaseLayoutOptions, AntVDagreLayoutOptions {
85
+ type: 'antv-dagre';
86
+ }
87
+
88
+ interface DagreLayout extends BaseLayoutOptions, DagreLayoutOptions {
89
+ type: 'dagre';
90
+ }
91
+
80
92
  export interface PresetLayoutOptions {
81
93
  presetLayout?: BaseLayoutOptions;
82
94
  }
@@ -0,0 +1,199 @@
1
+ import { insertCss } from 'insert-css';
2
+ import type { RuntimeContext } from '../runtime/types';
3
+ import type { ElementEvent } from '../types/event';
4
+ import type { Item } from '../utils/contextmenu';
5
+ import { CONTEXTMENU_CSS, getContentFromItems } from '../utils/contextmenu';
6
+ import { createPluginContainer } from '../utils/dom';
7
+ import type { BasePluginOptions } from './base-plugin';
8
+ import { BasePlugin } from './base-plugin';
9
+
10
+ /**
11
+ * <zh/> 右键菜单插件的配置项。
12
+ * <en/> The configuration item of the right-click menu plugin.
13
+ */
14
+ export type ContextmenuOptions = BasePluginOptions & {
15
+ /**
16
+ * <zh/> 给菜单的 DOM 追加的 classname,便于自定义样式。默认是包含 `g6-contextmenu`。
17
+ * <en/> The classname appended to the menu DOM for custom styles. The default is `g6-contextmenu`.
18
+ */
19
+ className?: string;
20
+ /**
21
+ * <zh/> 如何触发右键菜单,可以是 'click' 或者 'contextmenu',默认是 'contextmenu'。
22
+ * <en/> How to trigger the context menu, default is 'contextmenu'.
23
+ */
24
+ trigger?: 'click' | 'contextmenu';
25
+ /**
26
+ * <zh/> 菜单显式 X、Y 方向的偏移量,默认是 [4, 4]。
27
+ * <en/> The offset X, y direction of the menu, default is [4, 4].
28
+ */
29
+ offset?: [number, number];
30
+ /**
31
+ * <zh/> 当菜单被点击后,出发的回调方法。
32
+ * <en/> The callback method triggered when the menu is clicked.
33
+ */
34
+ onClick?: (v: string, target: HTMLElement) => void;
35
+ /**
36
+ * <zh/> 返回菜单的项目列表,支持 `Promise` 类型的返回值。是 `getContent` 的快捷配置。
37
+ * <en/> Return the list of menu items, support the `Promise` type return value. It is a shortcut configuration of `getContent`.
38
+ */
39
+ getItems?: (event: ElementEvent) => Item[] | Promise<Item[]>;
40
+ /**
41
+ * <zh/> 返回菜单的内容,支持 `Promise` 类型的返回值,也可以使用 `getItems` 来快捷配置。
42
+ * <en/> Return the content of menu, support the `Promise` type return value, you can also use `getItems` for shortcut configuration.
43
+ */
44
+ getContent?: (event: ElementEvent) => HTMLElement | string | Promise<HTMLElement | string>;
45
+ /**
46
+ * <zh/> Loading 时候的菜单内容,用于 getContent 返回 Promise 的时候。
47
+ * <en/> The menu content when loading is used when getContent returns a Promise.
48
+ */
49
+ loadingContent: HTMLElement | string;
50
+ /**
51
+ * <zh/> 插件是否可用,通过参数来判断是否支持右键菜单,默认全部可用。
52
+ * <en/> Whether the plugin is available, determine whether the right-click menu is supported through parameters, The default is all available.
53
+ */
54
+ enable?: boolean | ((event: ElementEvent) => boolean);
55
+ };
56
+
57
+ /**
58
+ * <zh/> 支持处理事件,并显示右键菜单,在菜单点击之后,可以触发相应的事件。
59
+ * <en/> Support processing events and displaying right-click menus.
60
+ * After clicking the menu, you can trigger the corresponding event.
61
+ */
62
+ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
63
+ static defaultOptions: Partial<ContextmenuOptions> = {
64
+ trigger: 'contextmenu',
65
+ offset: [4, 4],
66
+ loadingContent: '<div class="g6-contextmenu-loading">Loading...</div>',
67
+ getContent: () => 'It is a empty context menu.',
68
+ enable: () => true,
69
+ };
70
+
71
+ private $element: HTMLElement = createPluginContainer('contextmenu', false);
72
+
73
+ constructor(context: RuntimeContext, options: ContextmenuOptions) {
74
+ super(context, Object.assign({}, Contextmenu.defaultOptions, options));
75
+
76
+ const $container = this.context.canvas.getContainer();
77
+ $container!.appendChild(this.$element);
78
+
79
+ // 设置样式
80
+ insertCss(CONTEXTMENU_CSS);
81
+
82
+ this.update(options);
83
+ }
84
+
85
+ /**
86
+ * <zh/> 根据传入的元素,显示右键菜单。
87
+ * <en/> Display the right-click menu based on the incoming element.
88
+ * @param e - 事件
89
+ */
90
+ public async showContextmenu(e: ElementEvent) {
91
+ const { enable, offset } = this.options;
92
+
93
+ if ((typeof enable === 'function' && !enable(e)) || !enable) {
94
+ this.hideContextmenu();
95
+ return;
96
+ }
97
+
98
+ const content = await this.getDOMContent(e);
99
+
100
+ if (content instanceof HTMLElement) {
101
+ this.$element.appendChild(content);
102
+ } else {
103
+ this.$element.innerHTML = content;
104
+ }
105
+
106
+ // NOTICE: 为什么事件中的 client 是相对浏览器,而不是画布容器?
107
+ const clientRect = this.context.graph.getCanvas().getContainer()!.getBoundingClientRect();
108
+
109
+ this.$element.style.left = `${e.client.x - clientRect.left + offset[0]}px`;
110
+ this.$element.style.top = `${e.client.y - clientRect.top + offset[1]}px`;
111
+ this.$element.style.display = 'block';
112
+ }
113
+
114
+ /**
115
+ * <zh/> 隐藏右键菜单。
116
+ * <en/> Hide the right-click menu.
117
+ */
118
+ public hideContextmenu() {
119
+ this.$element.style.display = 'none';
120
+ }
121
+
122
+ /**
123
+ * <zh/> 更新右键菜单的配置项。
124
+ * <en/> Update the configuration of the right-click menu.
125
+ * @param options - 配置项
126
+ */
127
+ public update(options: Partial<ContextmenuOptions>) {
128
+ this.unbindEvents();
129
+ super.update(options);
130
+ this.bindEvents();
131
+ }
132
+
133
+ /**
134
+ * <zh/> 销毁右键菜单。
135
+ * <en/> Destroy the right-click menu.
136
+ */
137
+ public destroy(): void {
138
+ this.unbindEvents();
139
+ super.destroy();
140
+ this.$element.remove();
141
+ }
142
+
143
+ private async getDOMContent(e: ElementEvent) {
144
+ const { getContent, getItems } = this.options;
145
+
146
+ if (getItems) {
147
+ return getContentFromItems(await getItems(e));
148
+ }
149
+ return await getContent(e);
150
+ }
151
+
152
+ private bindEvents() {
153
+ const { graph } = this.context;
154
+ const { trigger } = this.options;
155
+
156
+ graph.on(`canvas:${trigger}`, this.onTriggerEvent);
157
+ graph.on(`node:${trigger}`, this.onTriggerEvent);
158
+ graph.on(`edge:${trigger}`, this.onTriggerEvent);
159
+ graph.on(`combo:${trigger}`, this.onTriggerEvent);
160
+
161
+ document.addEventListener('click', this.onMenuItemClick);
162
+ }
163
+
164
+ private unbindEvents() {
165
+ const { graph } = this.context;
166
+ const { trigger } = this.options;
167
+
168
+ graph.off(`canvas:${trigger}`, this.onTriggerEvent);
169
+ graph.off(`node:${trigger}`, this.onTriggerEvent);
170
+ graph.off(`edge:${trigger}`, this.onTriggerEvent);
171
+ graph.off(`combo:${trigger}`, this.onTriggerEvent);
172
+
173
+ document.removeEventListener('click', this.onMenuItemClick);
174
+ }
175
+
176
+ private onTriggerEvent = (e: ElementEvent) => {
177
+ // `contextmenu` 事件默认会触发浏览器的右键菜单,需要阻止默认事件
178
+ // `click` 事件不需要阻止默认事件
179
+ e.preventDefault?.();
180
+ this.showContextmenu(e);
181
+ };
182
+
183
+ private onMenuItemClick = (e: MouseEvent) => {
184
+ const { onClick } = this.options;
185
+ if (e.target instanceof HTMLElement) {
186
+ if (e.target.className.includes('g6-contextmenu-li')) {
187
+ const v = e.target.getAttribute('value') as string;
188
+ onClick && onClick(v, e.target);
189
+
190
+ this.hideContextmenu();
191
+ }
192
+
193
+ // 点击其他地方,隐藏菜单
194
+ if (!this.context.graph.getCanvas().getContainer()!.contains(e.target)) {
195
+ this.hideContextmenu();
196
+ }
197
+ }
198
+ };
199
+ }
@@ -2,6 +2,7 @@ import { BaseStyleProps } from '@antv/g';
2
2
  import { GraphEvent } from '../constants';
3
3
  import type { RuntimeContext } from '../runtime/types';
4
4
  import { Point } from '../types';
5
+ import { createPluginContainer } from '../utils/dom';
5
6
  import { ViewportEvent } from '../utils/event';
6
7
  import { add, mod } from '../utils/vector';
7
8
  import type { BasePluginOptions } from './base-plugin';
@@ -35,13 +36,17 @@ export class GridLine extends BasePlugin<GridLineOptions> {
35
36
  tickStrokeOpacity: 0.5,
36
37
  };
37
38
 
38
- private $element: HTMLElement = document.createElement('div');
39
+ private $element: HTMLElement = createPluginContainer('grid-line');
39
40
 
40
41
  private offset: Point = [0, 0];
41
42
 
42
43
  constructor(context: RuntimeContext, options: GridLineOptions) {
43
44
  super(context, Object.assign({}, GridLine.defaultOptions, options));
44
- this.render();
45
+
46
+ const $container = this.context.canvas.getContainer()!;
47
+ $container.appendChild(this.$element);
48
+
49
+ this.updateStyle();
45
50
  this.bindEvents();
46
51
  }
47
52
 
@@ -55,22 +60,10 @@ export class GridLine extends BasePlugin<GridLineOptions> {
55
60
  graph.on(GraphEvent.AFTER_TRANSFORM, this.onTransform);
56
61
  }
57
62
 
58
- private render() {
59
- const { canvas } = this.context;
60
- const $container = canvas.getContainer();
61
- if (!$container) return;
62
-
63
- this.$element.className = 'g6-grid-line';
64
- this.updateStyle();
65
- $container.appendChild(this.$element);
66
- }
67
-
68
63
  private updateStyle() {
69
64
  const { size, stroke, lineWidth, border, borderLineWidth, borderStroke, borderStyle } = this.options;
70
65
 
71
66
  Object.assign(this.$element.style, {
72
- width: '100%',
73
- height: '100%',
74
67
  border: border ? `${borderLineWidth}px ${borderStyle} ${borderStroke}` : 'none',
75
68
  backgroundImage: `linear-gradient(${stroke} ${lineWidth}px, transparent ${lineWidth}px), linear-gradient(90deg, ${stroke} ${lineWidth}px, transparent ${lineWidth}px)`,
76
69
  backgroundSize: `${size}px ${size}px`,
@@ -1,5 +1,11 @@
1
1
  export { BasePlugin } from './base-plugin';
2
+ export { Contextmenu } from './contextmenu';
2
3
  export { GridLine } from './grid-line';
4
+ export { Tooltip } from './tooltip';
5
+ export { Watermark } from './watermark';
3
6
 
4
7
  export type { BasePluginOptions } from './base-plugin';
8
+ export type { ContextmenuOptions } from './contextmenu';
5
9
  export type { GridLineOptions } from './grid-line';
10
+ export type { TooltipOptions } from './tooltip';
11
+ export type { WatermarkOptions } from './watermark';