@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
@@ -0,0 +1,268 @@
1
+ import type { TooltipStyleProps } from '@antv/component';
2
+ import { Tooltip as TooltipComponent } from '@antv/component';
3
+ import { get } from '@antv/util';
4
+ import type { RuntimeContext } from '../runtime/types';
5
+ import type { ElementDatum, ElementEvent, ElementType } from '../types';
6
+ import type { BasePluginOptions } from './base-plugin';
7
+ import { BasePlugin } from './base-plugin';
8
+
9
+ export interface TooltipOptions
10
+ extends BasePluginOptions,
11
+ Pick<TooltipStyleProps, 'position' | 'offset' | 'enterable' | 'style' | 'container'> {
12
+ /** <zh/> 触发方式 | <en/> Event type that triggers display of tooltip */
13
+ trigger?: 'hover' | 'click';
14
+ /** <zh/> 自定义内容 | <en/> Function for getting tooltip content */
15
+ getContent?: (evt: ElementEvent, items: ElementDatum[]) => HTMLElement | string;
16
+ /** <zh/> 是否启用 | <en/> Is enable */
17
+ enable?: boolean | ((evt: ElementEvent) => boolean);
18
+ }
19
+
20
+ export class Tooltip extends BasePlugin<TooltipOptions> {
21
+ static defaultOptions: Partial<TooltipOptions> = {
22
+ trigger: 'hover',
23
+ position: 'top-right',
24
+ enterable: false,
25
+ enable: true,
26
+ style: {
27
+ '.tooltip': {
28
+ visibility: 'hidden',
29
+ },
30
+ },
31
+ };
32
+ private currentTarget: string | null = null;
33
+ private tooltipElement: TooltipComponent | null = null;
34
+ private container: HTMLElement | null = null;
35
+
36
+ constructor(context: RuntimeContext, options: TooltipOptions) {
37
+ super(context, Object.assign({}, Tooltip.defaultOptions, options));
38
+ this.render();
39
+ this.bindEvents();
40
+ }
41
+
42
+ public getEvents(): { [key: string]: Function } {
43
+ if (this.options.trigger === 'click') {
44
+ return {
45
+ 'node:click': this.onClick,
46
+ 'edge:click': this.onClick,
47
+ 'combo:click': this.onClick,
48
+ 'canvas:click': this.onPointerLeave,
49
+ afterremoveitem: this.onPointerLeave,
50
+ contextmenu: this.onPointerLeave,
51
+ drag: this.onPointerLeave,
52
+ };
53
+ }
54
+
55
+ return {
56
+ 'node:pointerenter': this.onPointerEnter,
57
+ 'node:pointermove': this.onPointerMove,
58
+ 'canvas:pointermove': this.onCanvasMove,
59
+ 'edge:pointerenter': this.onPointerEnter,
60
+ 'edge:pointermove': this.onPointerMove,
61
+ 'combo:pointerenter': this.onPointerEnter,
62
+ 'combo:pointermove': this.onPointerMove,
63
+ contextmenu: this.onPointerLeave,
64
+ 'node:drag': this.onPointerLeave,
65
+ };
66
+ }
67
+
68
+ public update(options: Partial<TooltipOptions>) {
69
+ this.unbundEvents();
70
+ super.update(options);
71
+ if (this.tooltipElement) {
72
+ this.container?.removeChild(this.tooltipElement.HTMLTooltipElement);
73
+ }
74
+ this.tooltipElement = this.initTooltip();
75
+ this.bindEvents();
76
+ }
77
+
78
+ private render() {
79
+ const { canvas } = this.context;
80
+ const $container = canvas.getContainer();
81
+ if (!$container) return;
82
+ this.container = $container;
83
+ this.tooltipElement = this.initTooltip();
84
+ }
85
+
86
+ private unbundEvents() {
87
+ const { graph } = this.context;
88
+ /** The previous event binding needs to be removed when updating the trigger. */
89
+ const events = this.getEvents();
90
+ Object.keys(events).forEach((eventName) => {
91
+ graph.off(eventName, events[eventName]);
92
+ });
93
+ }
94
+
95
+ private bindEvents() {
96
+ const { graph } = this.context;
97
+ const events = this.getEvents();
98
+ Object.keys(events).forEach((eventName) => {
99
+ graph.on(eventName, events[eventName]);
100
+ });
101
+ }
102
+
103
+ private isEnable = (e: ElementEvent) => {
104
+ const { enable } = this.options;
105
+ if (typeof enable === 'function') {
106
+ return enable(e);
107
+ }
108
+ return enable;
109
+ };
110
+
111
+ public onClick = (e: ElementEvent) => {
112
+ const {
113
+ target: { id },
114
+ } = e;
115
+ // click the same item twice, tooltip will be hidden
116
+ if (this.currentTarget === id) {
117
+ this.currentTarget = null;
118
+ this.hideTooltip(e);
119
+ } else {
120
+ this.currentTarget = id;
121
+ this.showTooltip(e);
122
+ }
123
+ };
124
+
125
+ public onPointerMove = (e: ElementEvent) => {
126
+ const { target } = e;
127
+ if (!this.currentTarget || target.id === this.currentTarget) {
128
+ return;
129
+ }
130
+ this.showTooltip(e);
131
+ };
132
+
133
+ public onPointerLeave = (e: ElementEvent) => {
134
+ this.hideTooltip(e);
135
+ this.currentTarget = null;
136
+ };
137
+
138
+ public onCanvasMove = (e: ElementEvent) => {
139
+ this.hideTooltip(e);
140
+ this.currentTarget = null;
141
+ };
142
+
143
+ private onPointerEnter = (e: ElementEvent) => {
144
+ this.showTooltip(e);
145
+ };
146
+
147
+ public showTooltipById = (id: string, elementType: ElementType = 'node') => {
148
+ const e = {
149
+ targetType: elementType,
150
+ target: { id },
151
+ } as ElementEvent;
152
+ this.showTooltip(e);
153
+ };
154
+
155
+ private getItems = (id: string, targetType: ElementType) => {
156
+ const { model } = this.context;
157
+ switch (targetType) {
158
+ case 'node':
159
+ return model.getNodeData([id]);
160
+ case 'edge':
161
+ return model.getEdgeData([id]);
162
+ case 'combo':
163
+ return model.getComboData([id]);
164
+ default:
165
+ return [];
166
+ }
167
+ };
168
+
169
+ public showTooltip = (e: ElementEvent) => {
170
+ const {
171
+ targetType,
172
+ client,
173
+ target: { id, attributes = { color: '#1883FF' } },
174
+ } = e;
175
+ if (!this.tooltipElement || !this.isEnable(e)) return;
176
+ const { getContent } = this.options;
177
+ const { color, stroke } = attributes;
178
+ this.currentTarget = id;
179
+ const items: ElementDatum[] = this.getItems(id, targetType);
180
+ let x;
181
+ let y;
182
+ if (client) {
183
+ x = client.x;
184
+ y = client.y;
185
+ } else {
186
+ const style = get(items, '0.style', { x: 0, y: 0 });
187
+ x = style.x;
188
+ y = style.y;
189
+ }
190
+ let tooltipContent: { [key: string]: unknown } = {};
191
+ if (getContent) {
192
+ tooltipContent.content = getContent(e, items);
193
+ } else {
194
+ tooltipContent = {
195
+ title: targetType,
196
+ data: items.map((item) => {
197
+ return {
198
+ name: 'ID',
199
+ value: item.id || `${item.source} -> ${item.target}`,
200
+ color: color || stroke,
201
+ };
202
+ }),
203
+ };
204
+ }
205
+ this.tooltipElement.update({
206
+ x,
207
+ y,
208
+ style: {
209
+ '.tooltip': {
210
+ visibility: 'visible',
211
+ },
212
+ },
213
+ ...tooltipContent,
214
+ });
215
+ };
216
+
217
+ public hideTooltip = (e?: ElementEvent) => {
218
+ // if e is undefined, hide the tooltip, external call
219
+ if (!e) {
220
+ this.tooltipElement?.hide();
221
+ return;
222
+ }
223
+ if (!this.tooltipElement || !this.isEnable(e)) return;
224
+ const {
225
+ client: { x, y },
226
+ } = e;
227
+ this.tooltipElement.hide(x, y);
228
+ };
229
+
230
+ private initTooltip = () => {
231
+ const { canvas } = this.context;
232
+ const { center } = canvas.getBounds();
233
+ const $container = canvas.getContainer() as HTMLElement;
234
+ const { top, left } = $container.getBoundingClientRect();
235
+ const { style, position, enterable, container = { x: -left, y: -top } } = this.options;
236
+ const [x, y] = center;
237
+ const [width, height] = canvas.getSize();
238
+ const tooltipElement = new TooltipComponent({
239
+ className: 'tooltip',
240
+ style: {
241
+ x,
242
+ y,
243
+ container,
244
+ bounding: {
245
+ x: 0,
246
+ y: 0,
247
+ width,
248
+ height,
249
+ },
250
+ position,
251
+ enterable,
252
+ title: '',
253
+ offset: [10, 10],
254
+ style,
255
+ },
256
+ });
257
+ this.container?.appendChild(tooltipElement.HTMLTooltipElement);
258
+ return tooltipElement;
259
+ };
260
+
261
+ public destroy(): void {
262
+ this.unbundEvents();
263
+ if (this.tooltipElement) {
264
+ this.container?.removeChild(this.tooltipElement.HTMLTooltipElement);
265
+ }
266
+ super.destroy();
267
+ }
268
+ }
@@ -0,0 +1,98 @@
1
+ import type { RuntimeContext } from '../runtime/types';
2
+ import { createPluginContainer } from '../utils/dom';
3
+ import { getImageWatermark, getTextWateramrk } from '../utils/watermark';
4
+ import type { BasePluginOptions } from './base-plugin';
5
+ import { BasePlugin } from './base-plugin';
6
+
7
+ export type WatermarkOptions = BasePluginOptions & {
8
+ /** 单独一个水印的大小,这个水印最终会用来填充整个大小,所以 repeat 后的间距大小,通过这个 width height 设置 */
9
+ width?: number;
10
+ height?: number;
11
+ /** 透明度 */
12
+ opacity?: number;
13
+ /** 旋转角度 */
14
+ rotate?: number;
15
+ /** 图片地址,如果有值,则使用,否则使用文本 */
16
+ imageURL?: string;
17
+ /** 水印文本 */
18
+ text?: string;
19
+ /** 文本水印的文本样式 */
20
+ textFill: string;
21
+ textFontSize: number;
22
+ textFontFamily: string;
23
+ textFontWeight: string;
24
+ textFontVariant: string;
25
+ textAlign: CanvasTextAlign;
26
+ textBaseline: CanvasTextBaseline;
27
+ /** 背景的 CSS 样式 */
28
+ backgroundAttachment: string;
29
+ backgroundBlendMode: string;
30
+ backgroundClip: string;
31
+ backgroundColor: string;
32
+ backgroundImage: string;
33
+ backgroundOrigin: string;
34
+ backgroundPosition: string;
35
+ backgroundPositionX: string;
36
+ backgroundPositionY: string;
37
+ backgroundRepeat: string;
38
+ backgroundSize: string;
39
+ };
40
+
41
+ /**
42
+ * <zh/> 支持使用文本和图片作为水印,实现原理是在 Graph 容器的 div 上加上 background-image 属性,然后就可以通过 css 来控制水印的位置和样式。
43
+ * 对于文本,会使用隐藏 canvas 转成图片的方式来实现。
44
+ * <en/> Support using text and images as watermarks.
45
+ * The principle is to add the background-image property to the div of the Graph container,
46
+ * and then you can control the position and style of the watermark through css. For text,
47
+ * it will be converted to an image using a hidden canvas.
48
+ */
49
+ export class Watermark extends BasePlugin<WatermarkOptions> {
50
+ static defaultOptions: Partial<WatermarkOptions> = {
51
+ width: 200,
52
+ height: 100,
53
+ opacity: 0.2,
54
+ rotate: Math.PI / 12,
55
+ textFill: '#000',
56
+ textFontSize: 16,
57
+ textAlign: 'center',
58
+ textBaseline: 'middle',
59
+ backgroundRepeat: 'repeat',
60
+ };
61
+
62
+ private $element: HTMLElement = createPluginContainer('watermark');
63
+
64
+ constructor(context: RuntimeContext, options: WatermarkOptions) {
65
+ super(context, Object.assign({}, Watermark.defaultOptions, options));
66
+
67
+ const $container = this.context.canvas.getContainer();
68
+ $container!.appendChild(this.$element);
69
+
70
+ this.update(options);
71
+ }
72
+
73
+ public async update(options: Partial<WatermarkOptions>) {
74
+ super.update(options);
75
+
76
+ const { width, height, text, imageURL, ...rest } = this.options;
77
+
78
+ // Set the background style.
79
+ Object.keys(rest).forEach((key) => {
80
+ if (key.startsWith('background')) {
81
+ // @ts-expect-error ignore
82
+ this.$element.style[key] = options[key];
83
+ }
84
+ });
85
+
86
+ // Set the background image.
87
+ const base64 = imageURL
88
+ ? await getImageWatermark(width, height, imageURL, rest)
89
+ : await getTextWateramrk(width, height, text, rest);
90
+ this.$element.style.backgroundImage = `url(${base64})`;
91
+ }
92
+
93
+ public destroy(): void {
94
+ super.destroy();
95
+ // Remove the background dom.
96
+ this.$element.remove();
97
+ }
98
+ }
@@ -18,6 +18,7 @@ import {
18
18
  Triangle,
19
19
  } from '../elements';
20
20
  import {
21
+ AntVDagreLayout,
21
22
  CircularLayout,
22
23
  ComboCombinedLayout,
23
24
  ConcentricLayout,
@@ -36,7 +37,7 @@ import {
36
37
  mindmap,
37
38
  } from '../layouts';
38
39
  import { blues, greens, oranges, spectral } from '../palettes';
39
- import { GridLine } from '../plugins';
40
+ import { Contextmenu, GridLine, Tooltip, Watermark } from '../plugins';
40
41
  import { dark, light } from '../themes';
41
42
  import type { ExtensionRegistry } from './types';
42
43
 
@@ -69,6 +70,7 @@ export const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
69
70
  'cubic-vertical': CubicVertical,
70
71
  },
71
72
  layout: {
73
+ 'antv-dagre': AntVDagreLayout,
72
74
  'combo-combined': ComboCombinedLayout,
73
75
  'compact-box': compactBox,
74
76
  'force-atlas2': ForceAtlas2Layout,
@@ -107,5 +109,8 @@ export const BUILT_IN_EXTENSIONS: ExtensionRegistry = {
107
109
  },
108
110
  plugin: {
109
111
  'grid-line': GridLine,
112
+ watermark: Watermark,
113
+ tooltip: Tooltip,
114
+ contextmenu: Contextmenu,
110
115
  },
111
116
  };
@@ -1,9 +1,11 @@
1
- type ExtensionMetaOptions = {
1
+ interface ExtensionMetaOptions {
2
2
  type: string;
3
3
  key: string;
4
- };
4
+ }
5
5
 
6
- type CustomExtensionOptions = Record<string, any>;
6
+ interface CustomExtensionOptions {
7
+ [key: string]: any;
8
+ }
7
9
 
8
10
  /**
9
11
  * <zh/> 扩展配置项
@@ -90,7 +90,7 @@ export class BehaviorController extends ExtensionController<BaseBehavior<CustomB
90
90
  graph.emit(CanvasEvent.DBLCLICK, stdEvent);
91
91
  }
92
92
 
93
- // 右键菜单 / ContextMenu
93
+ // 右键菜单 / Contextmenu
94
94
  if (type === CanvasEvent.POINTER_DOWN && button === 2) {
95
95
  const contextMenuEvent = {
96
96
  ...stdEvent,
@@ -18,6 +18,8 @@ import type {
18
18
  PluginOptions,
19
19
  ThemeOptions,
20
20
  } from '../spec';
21
+ import { UpdateBehaviorOption } from '../spec/behavior';
22
+ import { UpdatePluginOption } from '../spec/plugin';
21
23
  import type {
22
24
  CallableValue,
23
25
  DataID,
@@ -198,20 +200,78 @@ export class Graph extends EventEmitter {
198
200
  return this.options.layout!;
199
201
  }
200
202
 
203
+ /**
204
+ * <zh/> 设置交互
205
+ *
206
+ * <en/> Set behaviors
207
+ * @param behaviors - <zh/> 交互配置 | <en/> behavior configuration
208
+ */
201
209
  public setBehaviors(behaviors: CallableValue<BehaviorOptions>): void {
202
210
  this.options.behaviors = isFunction(behaviors) ? behaviors(this.getBehaviors()) : behaviors;
203
211
  this.context.behavior?.setBehaviors(this.options.behaviors);
204
212
  }
205
213
 
214
+ /**
215
+ * <zh/> 更新交互
216
+ *
217
+ * <en/> Update behavior
218
+ * @param behavior - <zh/> 交互配置 | <en/> behavior configuration
219
+ */
220
+ public updateBehavior(behavior: UpdateBehaviorOption): void {
221
+ this.setBehaviors((behaviors) =>
222
+ behaviors.map((_behavior) => {
223
+ if (isObject(_behavior) && _behavior.key === behavior.key) {
224
+ return { ..._behavior, ...behavior };
225
+ }
226
+ return _behavior;
227
+ }),
228
+ );
229
+ }
230
+
231
+ /**
232
+ * <zh/> 获取交互
233
+ *
234
+ * <en/> Get behaviors
235
+ * @returns <zh/> 交互配置 | <en/> behavior configuration
236
+ */
206
237
  public getBehaviors(): BehaviorOptions {
207
238
  return this.options.behaviors || [];
208
239
  }
209
240
 
241
+ /**
242
+ * <zh/> 设置插件
243
+ *
244
+ * <en/> Set plugins
245
+ * @param plugins - <zh/> 插件配置 | <en/> plugin configuration
246
+ */
210
247
  public setPlugins(plugins: CallableValue<PluginOptions>): void {
211
248
  this.options.plugins = isFunction(plugins) ? plugins(this.getPlugins()) : plugins;
212
249
  this.context.plugin?.setPlugins(this.options.plugins);
213
250
  }
214
251
 
252
+ /**
253
+ * <zh/> 更新插件
254
+ *
255
+ * <en/> Update plugin
256
+ * @param plugin - <zh/> 插件配置 | <en/> plugin configuration
257
+ */
258
+ public updatePlugin(plugin: UpdatePluginOption): void {
259
+ this.setPlugins((plugins) =>
260
+ plugins.map((_plugin) => {
261
+ if (isObject(_plugin) && _plugin.key === plugin.key) {
262
+ return { ..._plugin, ...plugin };
263
+ }
264
+ return _plugin;
265
+ }),
266
+ );
267
+ }
268
+
269
+ /**
270
+ * <zh/> 获取插件
271
+ *
272
+ * <en/> Get plugins
273
+ * @returns <zh/> 插件配置 | <en/> plugin configuration
274
+ */
215
275
  public getPlugins(): PluginOptions {
216
276
  return this.options.plugins || [];
217
277
  }
@@ -362,8 +362,14 @@ export class LayoutController {
362
362
  dataToUpdate.nodes.push({ id, style: { x, y, z } });
363
363
  });
364
364
 
365
- edges.forEach(({ id, data: { controlPoints } }) => {
365
+ edges.forEach(({ id, data: { points = [], controlPoints = points.slice(1, points.length - 1) } }) => {
366
+ /**
367
+ * antv-dagre 返回 controlPoints,dagre 返回 points
368
+ * antv-dagre returns controlPoints, dagre returns points
369
+ */
366
370
  if (controlPoints?.length) {
371
+ // points 的第一个点是起点,最后一个点是终点,中间的点是控制点
372
+ // The first point of points is the starting point, and the last point is the end point, and the middle point is the control point
367
373
  dataToUpdate.edges.push({ id, style: { controlPoints: controlPoints.map(parsePoint) } });
368
374
  }
369
375
  });
@@ -1,6 +1,15 @@
1
1
  import type { BuiltInBehaviorOptions } from '../behaviors/types';
2
- import type { ExtensionOptions, LooselyExtensionOption } from '../registry/extension/types';
2
+ import type { ExtensionOptions } from '../registry/extension/types';
3
3
 
4
4
  export type BehaviorOptions = ExtensionOptions<BuiltInBehaviorOptions>;
5
5
 
6
- export type CustomBehaviorOption = LooselyExtensionOption;
6
+ export interface UpdateBehaviorOption {
7
+ key: string;
8
+ [key: string]: any;
9
+ }
10
+
11
+ export interface CustomBehaviorOption {
12
+ type?: string;
13
+ key?: string;
14
+ [key: string]: any;
15
+ }
@@ -1,6 +1,15 @@
1
1
  import type { BuiltInPluginOptions } from '../plugins/types';
2
- import type { ExtensionOptions, LooselyExtensionOption } from '../registry/extension/types';
2
+ import type { ExtensionOptions } from '../registry/extension/types';
3
3
 
4
4
  export type PluginOptions = ExtensionOptions<BuiltInPluginOptions>;
5
5
 
6
- export type CustomPluginOption = LooselyExtensionOption;
6
+ export interface UpdatePluginOption {
7
+ key: string;
8
+ [key: string]: any;
9
+ }
10
+
11
+ export interface CustomPluginOption {
12
+ type?: string;
13
+ key?: string;
14
+ [key: string]: any;
15
+ }
@@ -1,6 +1,11 @@
1
- import type { Document } from '@antv/g';
2
- import { Edge, Node } from './element';
1
+ import type { Document, FederatedMouseEvent } from '@antv/g';
2
+ import type { BehaviorEvent } from './behavior';
3
+ import { Edge, ElementType, Node } from './element';
3
4
 
4
5
  export type Listener = (event: any) => void;
5
6
 
6
7
  export type Target = Document | Node | Edge | null;
8
+
9
+ export interface ElementEvent extends BehaviorEvent<FederatedMouseEvent> {
10
+ targetType: ElementType;
11
+ }
@@ -0,0 +1,6 @@
1
+ import type { DisplayObject, FederatedEvent } from '@antv/g';
2
+
3
+ export type PluginEvent<T extends Event | FederatedEvent = Event> = Omit<T, 'target'> & {
4
+ targetType: 'canvas' | 'node' | 'edge' | 'combo';
5
+ target: DisplayObject;
6
+ };
@@ -0,0 +1,62 @@
1
+ /**
2
+ * <zh/> 右键菜单显示项目。
3
+ * <en/> The item of the right-click menu.
4
+ */
5
+ export type Item = {
6
+ /**
7
+ * <zh/> 菜单项显示的名字。
8
+ * <en/> The name of the menu item.
9
+ */
10
+ name: string;
11
+ /**
12
+ * <zh/> 菜单项对应的值。
13
+ * <en/> The value corresponding to the menu item.
14
+ */
15
+ value: string;
16
+ };
17
+
18
+ /**
19
+ * Get the content of the right-click menu.
20
+ * @param items - context menu items
21
+ * @returns HTML string
22
+ */
23
+ export function getContentFromItems(items: Item[]) {
24
+ return `
25
+ <ul class="g6-contextmenu-ul">
26
+ ${items.map((item) => `<li class="g6-contextmenu-li" value="${item.value}">${item.name}</li>`).join('')}
27
+ </ul>
28
+ `;
29
+ }
30
+
31
+ /**
32
+ * Style of the right-click menu, same with `tooltip`.
33
+ */
34
+ export const CONTEXTMENU_CSS = `
35
+ .g6-contextmenu {
36
+ font-size: 12px;
37
+ background-color: rgba(255, 255, 255, 0.96);
38
+ border-radius: 4px;
39
+ overflow: hidden;
40
+ box-shadow: rgba(0, 0, 0, 0.12) 0px 6px 12px 0px;
41
+ transition: visibility 0.2s cubic-bezier(0.23, 1, 0.32, 1) 0s, left 0.4s cubic-bezier(0.23, 1, 0.32, 1) 0s, top 0.4s cubic-bezier(0.23, 1, 0.32, 1) 0s;
42
+ }
43
+
44
+ .g6-contextmenu-ul {
45
+ max-width: 256px;
46
+ min-width: 96px;
47
+ list-style: none;
48
+ padding: 0;
49
+ margin: 0;
50
+ }
51
+
52
+ .g6-contextmenu-li {
53
+ padding: 8px 12px;
54
+ cursor: pointer;
55
+ user-select: none;
56
+ }
57
+
58
+ .g6-contextmenu-li:hover {
59
+ background-color: #f5f5f5;
60
+ cursor: pointer;
61
+ }
62
+ `;
package/src/utils/dom.ts CHANGED
@@ -42,3 +42,29 @@ export function sizeOf(container: HTMLElement): [number, number] {
42
42
  Math.max(isNumber(effectiveHeight) ? effectiveHeight : MIN_CHART_HEIGHT, MIN_CHART_HEIGHT),
43
43
  ];
44
44
  }
45
+
46
+ /**
47
+ * Create a plugin DOM element.
48
+ * @param type - plugin type
49
+ * @param cover - cover the container
50
+ * @returns plugin DOM element
51
+ */
52
+ export function createPluginContainer(type: string, cover = true) {
53
+ const el = document.createElement('div');
54
+
55
+ el.setAttribute('class', `g6-${type}`);
56
+
57
+ el.style.position = 'absolute';
58
+ el.style.display = 'block';
59
+
60
+ if (cover) {
61
+ el.style.top = '0px';
62
+ el.style.left = '0px';
63
+ el.style.height = '100%';
64
+ el.style.width = '100%';
65
+ el.style.overflow = 'hidden';
66
+ el.style.pointerEvents = 'none';
67
+ }
68
+
69
+ return el;
70
+ }
@@ -11,7 +11,7 @@ import type { STDLayoutOptions } from '../spec/layout';
11
11
  export function isComboLayout(options: STDLayoutOptions) {
12
12
  const { type } = options;
13
13
  if (['comboCombined', 'comboForce'].includes(type)) return true;
14
- if (type === 'dagre' && options.sortByCombo) return true;
14
+ if (type === 'antv-dagre' && options.sortByCombo) return true;
15
15
  return false;
16
16
  }
17
17