@antv/g6 5.0.0-alpha.7 → 5.0.0-alpha.9

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 (297) hide show
  1. package/dist/g6.min.js +6 -19
  2. package/esm/index.d.ts +26 -6
  3. package/esm/item/combo.d.ts +72 -2
  4. package/esm/item/combo.js +122 -2
  5. package/esm/item/combo.js.map +1 -1
  6. package/esm/item/edge.d.ts +3 -3
  7. package/esm/item/edge.js +4 -5
  8. package/esm/item/edge.js.map +1 -1
  9. package/esm/item/item.d.ts +4 -4
  10. package/esm/item/item.js +38 -9
  11. package/esm/item/item.js.map +1 -1
  12. package/esm/item/node.d.ts +20 -16
  13. package/esm/item/node.js +14 -8
  14. package/esm/item/node.js.map +1 -1
  15. package/esm/runtime/controller/data.d.ts +12 -4
  16. package/esm/runtime/controller/data.js +354 -62
  17. package/esm/runtime/controller/data.js.map +1 -1
  18. package/esm/runtime/controller/interaction.js +16 -4
  19. package/esm/runtime/controller/interaction.js.map +1 -1
  20. package/esm/runtime/controller/item.d.ts +11 -2
  21. package/esm/runtime/controller/item.js +344 -52
  22. package/esm/runtime/controller/item.js.map +1 -1
  23. package/esm/runtime/controller/layout.js +19 -5
  24. package/esm/runtime/controller/layout.js.map +1 -1
  25. package/esm/runtime/graph.d.ts +54 -17
  26. package/esm/runtime/graph.js +165 -34
  27. package/esm/runtime/graph.js.map +1 -1
  28. package/esm/stdlib/behavior/click-select.d.ts +3 -0
  29. package/esm/stdlib/behavior/click-select.js +13 -1
  30. package/esm/stdlib/behavior/click-select.js.map +1 -1
  31. package/esm/stdlib/behavior/collapse-expand-combo.d.ts +29 -0
  32. package/esm/stdlib/behavior/collapse-expand-combo.js +35 -0
  33. package/esm/stdlib/behavior/collapse-expand-combo.js.map +1 -0
  34. package/esm/stdlib/behavior/drag-combo.d.ts +99 -0
  35. package/esm/stdlib/behavior/drag-combo.js +341 -0
  36. package/esm/stdlib/behavior/drag-combo.js.map +1 -0
  37. package/esm/stdlib/behavior/drag-node.d.ts +14 -10
  38. package/esm/stdlib/behavior/drag-node.js +134 -37
  39. package/esm/stdlib/behavior/drag-node.js.map +1 -1
  40. package/esm/stdlib/behavior/hover-activate.d.ts +3 -1
  41. package/esm/stdlib/behavior/hover-activate.js +3 -1
  42. package/esm/stdlib/behavior/hover-activate.js.map +1 -1
  43. package/esm/stdlib/behavior/orbit-canvas-3d.js +1 -1
  44. package/esm/stdlib/behavior/orbit-canvas-3d.js.map +1 -1
  45. package/esm/stdlib/behavior/track-canvas-3d.js +1 -1
  46. package/esm/stdlib/behavior/track-canvas-3d.js.map +1 -1
  47. package/esm/stdlib/index.d.ts +25 -3
  48. package/esm/stdlib/index.js +25 -3
  49. package/esm/stdlib/index.js.map +1 -1
  50. package/esm/stdlib/item/combo/circle.d.ts +29 -0
  51. package/esm/stdlib/item/combo/circle.js +56 -0
  52. package/esm/stdlib/item/combo/circle.js.map +1 -0
  53. package/esm/stdlib/item/edge/base.d.ts +2 -2
  54. package/esm/stdlib/item/edge/base.js +3 -0
  55. package/esm/stdlib/item/edge/base.js.map +1 -1
  56. package/esm/stdlib/item/edge/cubic-horizon.d.ts +37 -0
  57. package/esm/stdlib/item/edge/cubic-horizon.js +71 -0
  58. package/esm/stdlib/item/edge/cubic-horizon.js.map +1 -0
  59. package/esm/stdlib/item/edge/cubic-vertical.d.ts +37 -0
  60. package/esm/stdlib/item/edge/cubic-vertical.js +71 -0
  61. package/esm/stdlib/item/edge/cubic-vertical.js.map +1 -0
  62. package/esm/stdlib/item/edge/cubic.d.ts +60 -0
  63. package/esm/stdlib/item/edge/cubic.js +99 -0
  64. package/esm/stdlib/item/edge/cubic.js.map +1 -0
  65. package/esm/stdlib/item/edge/index.d.ts +1 -0
  66. package/esm/stdlib/item/edge/index.js +1 -0
  67. package/esm/stdlib/item/edge/index.js.map +1 -1
  68. package/esm/stdlib/item/edge/line.d.ts +1 -1
  69. package/esm/stdlib/item/edge/quadratic.d.ts +41 -0
  70. package/esm/stdlib/item/edge/quadratic.js +66 -0
  71. package/esm/stdlib/item/edge/quadratic.js.map +1 -0
  72. package/esm/stdlib/item/node/base.d.ts +38 -37
  73. package/esm/stdlib/item/node/base.js +4 -4
  74. package/esm/stdlib/item/node/base.js.map +1 -1
  75. package/esm/stdlib/item/node/base3d.d.ts +1 -0
  76. package/esm/stdlib/item/node/base3d.js +1 -0
  77. package/esm/stdlib/item/node/base3d.js.map +1 -1
  78. package/esm/stdlib/item/node/index.d.ts +1 -0
  79. package/esm/stdlib/item/node/index.js +1 -0
  80. package/esm/stdlib/item/node/index.js.map +1 -1
  81. package/esm/stdlib/item/node/rect.d.ts +31 -0
  82. package/esm/stdlib/item/node/rect.js +59 -0
  83. package/esm/stdlib/item/node/rect.js.map +1 -0
  84. package/esm/stdlib/plugin/fisheye/index.d.ts +102 -0
  85. package/esm/stdlib/plugin/fisheye/index.js +487 -0
  86. package/esm/stdlib/plugin/fisheye/index.js.map +1 -0
  87. package/esm/stdlib/plugin/menu/index.d.ts +42 -0
  88. package/esm/stdlib/plugin/menu/index.js +236 -0
  89. package/esm/stdlib/plugin/menu/index.js.map +1 -0
  90. package/esm/stdlib/plugin/tooltip/index.d.ts +75 -0
  91. package/esm/stdlib/plugin/tooltip/index.js +377 -0
  92. package/esm/stdlib/plugin/tooltip/index.js.map +1 -0
  93. package/esm/stdlib/theme/dark.js +4 -0
  94. package/esm/stdlib/theme/dark.js.map +1 -1
  95. package/esm/stdlib/theme/light.js +54 -9
  96. package/esm/stdlib/theme/light.js.map +1 -1
  97. package/esm/types/combo.d.ts +52 -3
  98. package/esm/types/data.d.ts +4 -4
  99. package/esm/types/edge.d.ts +2 -3
  100. package/esm/types/event.d.ts +9 -1
  101. package/esm/types/event.js +8 -8
  102. package/esm/types/event.js.map +1 -1
  103. package/esm/types/graph.d.ts +49 -13
  104. package/esm/types/hook.d.ts +8 -1
  105. package/esm/types/item.d.ts +4 -4
  106. package/esm/types/node.d.ts +4 -5
  107. package/esm/types/theme.d.ts +1 -1
  108. package/esm/util/animate.js.map +1 -1
  109. package/esm/util/canvas.js +8 -0
  110. package/esm/util/canvas.js.map +1 -1
  111. package/esm/util/data.d.ts +65 -0
  112. package/esm/util/data.js +150 -0
  113. package/esm/util/data.js.map +1 -0
  114. package/esm/util/event.d.ts +8 -0
  115. package/esm/util/event.js +16 -0
  116. package/esm/util/event.js.map +1 -1
  117. package/esm/util/item.d.ts +2 -1
  118. package/esm/util/item.js +36 -13
  119. package/esm/util/item.js.map +1 -1
  120. package/esm/util/shape.d.ts +39 -4
  121. package/esm/util/shape.js +101 -1
  122. package/esm/util/shape.js.map +1 -1
  123. package/esm/util/zoom.d.ts +2 -2
  124. package/lib/index.d.ts +26 -6
  125. package/lib/item/combo.d.ts +72 -2
  126. package/lib/item/combo.js +122 -2
  127. package/lib/item/combo.js.map +1 -1
  128. package/lib/item/edge.d.ts +3 -3
  129. package/lib/item/edge.js +4 -5
  130. package/lib/item/edge.js.map +1 -1
  131. package/lib/item/item.d.ts +4 -4
  132. package/lib/item/item.js +37 -8
  133. package/lib/item/item.js.map +1 -1
  134. package/lib/item/node.d.ts +20 -16
  135. package/lib/item/node.js +14 -8
  136. package/lib/item/node.js.map +1 -1
  137. package/lib/runtime/controller/data.d.ts +12 -4
  138. package/lib/runtime/controller/data.js +353 -61
  139. package/lib/runtime/controller/data.js.map +1 -1
  140. package/lib/runtime/controller/interaction.js +15 -3
  141. package/lib/runtime/controller/interaction.js.map +1 -1
  142. package/lib/runtime/controller/item.d.ts +11 -2
  143. package/lib/runtime/controller/item.js +342 -50
  144. package/lib/runtime/controller/item.js.map +1 -1
  145. package/lib/runtime/controller/layout.js +19 -5
  146. package/lib/runtime/controller/layout.js.map +1 -1
  147. package/lib/runtime/graph.d.ts +54 -17
  148. package/lib/runtime/graph.js +165 -34
  149. package/lib/runtime/graph.js.map +1 -1
  150. package/lib/stdlib/behavior/click-select.d.ts +3 -0
  151. package/lib/stdlib/behavior/click-select.js +13 -1
  152. package/lib/stdlib/behavior/click-select.js.map +1 -1
  153. package/lib/stdlib/behavior/collapse-expand-combo.d.ts +29 -0
  154. package/lib/stdlib/behavior/collapse-expand-combo.js +38 -0
  155. package/lib/stdlib/behavior/collapse-expand-combo.js.map +1 -0
  156. package/lib/stdlib/behavior/drag-combo.d.ts +99 -0
  157. package/lib/stdlib/behavior/drag-combo.js +344 -0
  158. package/lib/stdlib/behavior/drag-combo.js.map +1 -0
  159. package/lib/stdlib/behavior/drag-node.d.ts +14 -10
  160. package/lib/stdlib/behavior/drag-node.js +134 -37
  161. package/lib/stdlib/behavior/drag-node.js.map +1 -1
  162. package/lib/stdlib/behavior/hover-activate.d.ts +3 -1
  163. package/lib/stdlib/behavior/hover-activate.js +3 -2
  164. package/lib/stdlib/behavior/hover-activate.js.map +1 -1
  165. package/lib/stdlib/behavior/orbit-canvas-3d.js +1 -1
  166. package/lib/stdlib/behavior/orbit-canvas-3d.js.map +1 -1
  167. package/lib/stdlib/behavior/track-canvas-3d.js +1 -1
  168. package/lib/stdlib/behavior/track-canvas-3d.js.map +1 -1
  169. package/lib/stdlib/index.d.ts +25 -3
  170. package/lib/stdlib/index.js +25 -3
  171. package/lib/stdlib/index.js.map +1 -1
  172. package/lib/stdlib/item/combo/circle.d.ts +29 -0
  173. package/lib/stdlib/item/combo/circle.js +60 -0
  174. package/lib/stdlib/item/combo/circle.js.map +1 -0
  175. package/lib/stdlib/item/edge/base.d.ts +2 -2
  176. package/lib/stdlib/item/edge/base.js +3 -0
  177. package/lib/stdlib/item/edge/base.js.map +1 -1
  178. package/lib/stdlib/item/edge/cubic-horizon.d.ts +37 -0
  179. package/lib/stdlib/item/edge/cubic-horizon.js +75 -0
  180. package/lib/stdlib/item/edge/cubic-horizon.js.map +1 -0
  181. package/lib/stdlib/item/edge/cubic-vertical.d.ts +37 -0
  182. package/lib/stdlib/item/edge/cubic-vertical.js +75 -0
  183. package/lib/stdlib/item/edge/cubic-vertical.js.map +1 -0
  184. package/lib/stdlib/item/edge/cubic.d.ts +60 -0
  185. package/lib/stdlib/item/edge/cubic.js +103 -0
  186. package/lib/stdlib/item/edge/cubic.js.map +1 -0
  187. package/lib/stdlib/item/edge/index.d.ts +1 -0
  188. package/lib/stdlib/item/edge/index.js +1 -0
  189. package/lib/stdlib/item/edge/index.js.map +1 -1
  190. package/lib/stdlib/item/edge/line.d.ts +1 -1
  191. package/lib/stdlib/item/edge/quadratic.d.ts +41 -0
  192. package/lib/stdlib/item/edge/quadratic.js +70 -0
  193. package/lib/stdlib/item/edge/quadratic.js.map +1 -0
  194. package/lib/stdlib/item/node/base.d.ts +38 -37
  195. package/lib/stdlib/item/node/base.js +4 -4
  196. package/lib/stdlib/item/node/base.js.map +1 -1
  197. package/lib/stdlib/item/node/base3d.d.ts +1 -0
  198. package/lib/stdlib/item/node/base3d.js +1 -0
  199. package/lib/stdlib/item/node/base3d.js.map +1 -1
  200. package/lib/stdlib/item/node/index.d.ts +1 -0
  201. package/lib/stdlib/item/node/index.js +1 -0
  202. package/lib/stdlib/item/node/index.js.map +1 -1
  203. package/lib/stdlib/item/node/rect.d.ts +31 -0
  204. package/lib/stdlib/item/node/rect.js +63 -0
  205. package/lib/stdlib/item/node/rect.js.map +1 -0
  206. package/lib/stdlib/plugin/fisheye/index.d.ts +102 -0
  207. package/lib/stdlib/plugin/fisheye/index.js +490 -0
  208. package/lib/stdlib/plugin/fisheye/index.js.map +1 -0
  209. package/lib/stdlib/plugin/menu/index.d.ts +42 -0
  210. package/lib/stdlib/plugin/menu/index.js +242 -0
  211. package/lib/stdlib/plugin/menu/index.js.map +1 -0
  212. package/lib/stdlib/plugin/tooltip/index.d.ts +75 -0
  213. package/lib/stdlib/plugin/tooltip/index.js +383 -0
  214. package/lib/stdlib/plugin/tooltip/index.js.map +1 -0
  215. package/lib/stdlib/theme/dark.js +4 -0
  216. package/lib/stdlib/theme/dark.js.map +1 -1
  217. package/lib/stdlib/theme/light.js +54 -9
  218. package/lib/stdlib/theme/light.js.map +1 -1
  219. package/lib/types/combo.d.ts +52 -3
  220. package/lib/types/data.d.ts +4 -4
  221. package/lib/types/edge.d.ts +2 -3
  222. package/lib/types/event.d.ts +9 -1
  223. package/lib/types/event.js +10 -10
  224. package/lib/types/event.js.map +1 -1
  225. package/lib/types/graph.d.ts +49 -13
  226. package/lib/types/hook.d.ts +8 -1
  227. package/lib/types/item.d.ts +4 -4
  228. package/lib/types/item.js +1 -1
  229. package/lib/types/item.js.map +1 -1
  230. package/lib/types/node.d.ts +4 -5
  231. package/lib/types/theme.d.ts +1 -1
  232. package/lib/util/animate.js.map +1 -1
  233. package/lib/util/canvas.js +8 -0
  234. package/lib/util/canvas.js.map +1 -1
  235. package/lib/util/data.d.ts +65 -0
  236. package/lib/util/data.js +161 -0
  237. package/lib/util/data.js.map +1 -0
  238. package/lib/util/event.d.ts +8 -0
  239. package/lib/util/event.js +18 -1
  240. package/lib/util/event.js.map +1 -1
  241. package/lib/util/item.d.ts +2 -1
  242. package/lib/util/item.js +36 -13
  243. package/lib/util/item.js.map +1 -1
  244. package/lib/util/shape.d.ts +39 -4
  245. package/lib/util/shape.js +105 -2
  246. package/lib/util/shape.js.map +1 -1
  247. package/lib/util/zoom.d.ts +2 -2
  248. package/package.json +15 -18
  249. package/src/item/combo.ts +220 -2
  250. package/src/item/edge.ts +7 -8
  251. package/src/item/item.ts +58 -15
  252. package/src/item/node.ts +47 -24
  253. package/src/runtime/controller/data.ts +390 -68
  254. package/src/runtime/controller/interaction.ts +23 -3
  255. package/src/runtime/controller/item.ts +444 -64
  256. package/src/runtime/controller/layout.ts +24 -5
  257. package/src/runtime/graph.ts +225 -38
  258. package/src/stdlib/behavior/click-select.ts +15 -2
  259. package/src/stdlib/behavior/collapse-expand-combo.ts +59 -0
  260. package/src/stdlib/behavior/drag-combo.ts +533 -0
  261. package/src/stdlib/behavior/drag-node.ts +195 -62
  262. package/src/stdlib/behavior/hover-activate.ts +4 -5
  263. package/src/stdlib/behavior/orbit-canvas-3d.ts +1 -1
  264. package/src/stdlib/behavior/track-canvas-3d.ts +1 -1
  265. package/src/stdlib/index.ts +25 -3
  266. package/src/stdlib/item/combo/circle.ts +113 -0
  267. package/src/stdlib/item/edge/base.ts +5 -2
  268. package/src/stdlib/item/edge/cubic-horizon.ts +114 -0
  269. package/src/stdlib/item/edge/cubic-vertical.ts +116 -0
  270. package/src/stdlib/item/edge/cubic.ts +198 -0
  271. package/src/stdlib/item/edge/index.ts +1 -0
  272. package/src/stdlib/item/edge/quadratic.ts +136 -0
  273. package/src/stdlib/item/node/base.ts +79 -41
  274. package/src/stdlib/item/node/base3d.ts +2 -0
  275. package/src/stdlib/item/node/index.ts +1 -0
  276. package/src/stdlib/item/node/rect.ts +124 -0
  277. package/src/stdlib/plugin/fisheye/index.ts +578 -0
  278. package/src/stdlib/plugin/menu/index.ts +260 -0
  279. package/src/stdlib/plugin/tooltip/index.ts +424 -0
  280. package/src/stdlib/theme/dark.ts +4 -0
  281. package/src/stdlib/theme/light.ts +77 -9
  282. package/src/types/combo.ts +61 -3
  283. package/src/types/data.ts +6 -4
  284. package/src/types/edge.ts +2 -2
  285. package/src/types/event.ts +8 -8
  286. package/src/types/graph.ts +73 -16
  287. package/src/types/hook.ts +8 -1
  288. package/src/types/item.ts +10 -9
  289. package/src/types/node.ts +4 -4
  290. package/src/types/theme.ts +1 -1
  291. package/src/util/animate.ts +2 -3
  292. package/src/util/canvas.ts +10 -0
  293. package/src/util/data.ts +178 -0
  294. package/src/util/event.ts +29 -0
  295. package/src/util/item.ts +75 -12
  296. package/src/util/shape.ts +142 -5
  297. package/src/util/zoom.ts +2 -2
@@ -0,0 +1,260 @@
1
+ import { isString } from '@antv/util';
2
+ import { createDom, modifyCSS } from '@antv/dom-util';
3
+ import Item from 'item/item';
4
+ import insertCss from 'insert-css';
5
+ import { IGraph } from '../../../types';
6
+ import { Plugin as Base, IPluginBaseConfig } from '../../../types/plugin';
7
+ import { IG6GraphEvent } from '../../../types/event';
8
+ typeof document !== 'undefined' &&
9
+ insertCss(`
10
+ .g6-component-contextmenu {
11
+ border: 1px solid #e2e2e2;
12
+ border-radius: 4px;
13
+ font-size: 12px;
14
+ color: #545454;
15
+ background-color: rgba(255, 255, 255, 0.9);
16
+ padding: 10px 8px;
17
+ box-shadow: rgb(174, 174, 174) 0px 0px 10px;
18
+ }
19
+ .g6-contextmenu-ul {
20
+ padding: 0;
21
+ margin: 0;
22
+ list-style: none;
23
+ }
24
+ .g6-loading-dom {
25
+ border: 5px solid #e5e5e5;
26
+ border-top: 5px solid #227EFF;
27
+ border-radius: 50%;
28
+ width: 25px;
29
+ height: 25px;
30
+ animation: turn-around 1.5s linear infinite;
31
+ }
32
+ @keyframes turn-around {
33
+ 0% { transform: rotate(0deg); }
34
+ 100% { transform: rotate(360deg); }
35
+ }
36
+ `);
37
+
38
+ interface MenuConfig extends IPluginBaseConfig {
39
+ handleMenuClick?: (target: HTMLElement, item: Item) => void;
40
+ // return the content of menu, support the `Promise` type return value.
41
+ getContent?: (
42
+ evt?: IG6GraphEvent,
43
+ ) => HTMLDivElement | string | Promise<HTMLDivElement | string>;
44
+ offsetX?: number;
45
+ offsetY?: number;
46
+ shouldBegin?: (evt?: IG6GraphEvent) => boolean;
47
+ itemTypes?: string[];
48
+ trigger?: 'click' | 'contextmenu';
49
+ onHide?: () => boolean;
50
+ //loading Dom
51
+ loadingContent?: HTMLDivElement | string;
52
+ liHoverStyle?: { [key: string]: string };
53
+ }
54
+
55
+ export default class Menu extends Base {
56
+ private menu;
57
+ private handleMenuClickWrapper;
58
+ private handler;
59
+ private currentTarget;
60
+ private asyncMenu;
61
+ constructor(options?: MenuConfig) {
62
+ super(options);
63
+ }
64
+
65
+ public getDefaultCfgs(): MenuConfig {
66
+ return {
67
+ offsetX: 6,
68
+ offsetY: 6,
69
+ handleMenuClick: undefined,
70
+ getContent: (e) => {
71
+ return `
72
+ <ul class='g6-contextmenu-ul'>
73
+ <li class='g6-contextmenu-li'>菜单项1</li>
74
+ <li class='g6-contextmenu-li'>菜单项2</li>
75
+ </ul>
76
+ `;
77
+ },
78
+ shouldBegin: (e) => {
79
+ return true;
80
+ },
81
+ // hide menu
82
+ onHide: () => {
83
+ return true;
84
+ },
85
+ itemTypes: ['node', 'edge', 'combo'],
86
+ trigger: 'click',
87
+ container: null,
88
+ loadingContent: `<div class='g6-loading-dom'></div>`,
89
+ liHoverStyle: {
90
+ color: 'white',
91
+ 'background-color': '#227EFF',
92
+ },
93
+ };
94
+ }
95
+
96
+ public getEvents() {
97
+ return this.options.trigger === 'click'
98
+ ? { click: this.onMenuShow, touchend: this.onMenuShow }
99
+ : { contextmenu: this.onMenuShow };
100
+ }
101
+
102
+ public init(graph: IGraph) {
103
+ super.init(graph);
104
+ const className = this.options.className;
105
+ insertCss(`
106
+ .g6-contextmenu-li:hover{
107
+ ${Object.keys(this.options.liHoverStyle)
108
+ .map((k) => {
109
+ return `${k}:${this.options.liHoverStyle[k]};`;
110
+ })
111
+ .join('')}
112
+ }
113
+ `);
114
+ const menu = createDom(
115
+ `<div class=${className || 'g6-component-contextmenu'}></div>`,
116
+ );
117
+ modifyCSS(menu, { top: '0px', position: 'absolute', visibility: 'hidden' });
118
+ let container: HTMLDivElement | null | string = this.options.container;
119
+ if (!container) {
120
+ container = this.graph.container as HTMLDivElement;
121
+ }
122
+ if (isString(container)) {
123
+ container = document.getElementById(container) as HTMLDivElement;
124
+ }
125
+ container.appendChild(menu);
126
+ this.menu = menu;
127
+ }
128
+
129
+ protected async onMenuShow(e: IG6GraphEvent) {
130
+ const self = this;
131
+ e.preventDefault();
132
+ self.onMenuHide();
133
+ const itemTypes = this.options.itemTypes;
134
+ if (!e.itemId) {
135
+ if (itemTypes.indexOf('canvas') === -1) {
136
+ self.onMenuHide();
137
+ return;
138
+ }
139
+ } else {
140
+ if (e.itemId && e.itemType && itemTypes.indexOf(e.itemType) === -1) {
141
+ self.onMenuHide();
142
+ return;
143
+ }
144
+ }
145
+
146
+ const shouldBegin = this.options.shouldBegin;
147
+ if (!shouldBegin(e)) return;
148
+ const menuDom = this.menu;
149
+ const graph = this.graph;
150
+ const menu = this.options.getContent(e);
151
+
152
+ const width: number = graph.getSize()[0];
153
+ const height: number = graph.getSize()[1];
154
+ const bbox = menuDom.getBoundingClientRect();
155
+ const offsetX = this.options.offsetX || 0;
156
+ const offsetY = this.options.offsetY || 0;
157
+ const graphTop = this.graph.container.offsetTop;
158
+ const graphLeft = this.graph.container.offsetLeft;
159
+ let x = e.viewport.x + graphLeft + offsetX;
160
+ let y = e.viewport.y + graphTop + offsetY;
161
+
162
+ // when the menu is (part of) out of the canvas
163
+ if (x + bbox.width > width) {
164
+ x -= bbox.width + graphLeft + offsetX;
165
+ }
166
+ if (y + bbox.height > height) {
167
+ y -= bbox.height + graphTop + offsetY;
168
+ }
169
+ modifyCSS(menuDom, {
170
+ top: `${y}px`,
171
+ left: `${x}px`,
172
+ visibility: 'visible',
173
+ });
174
+ if (isString(menu)) {
175
+ //the type is string
176
+ menuDom.innerHTML = menu;
177
+ } else if (menu instanceof HTMLDivElement) {
178
+ //the type is htmldom
179
+ menuDom.innerHTML = menu.outerHTML;
180
+ } else {
181
+ //the type is Promise
182
+ if (isString(this.options.loadingContent)) {
183
+ menuDom.innerHTML = this.options.loadingContent;
184
+ } else {
185
+ menuDom.innerHTML = this.options.loadingContent.outerHTML;
186
+ }
187
+ if (e.itemId != this.currentTarget || !this.asyncMenu) {
188
+ this.currentTarget = e.itemId;
189
+ this.asyncMenu = await this.options.getContent(e);
190
+ }
191
+ if (e.itemId != this.currentTarget) {
192
+ //The menu is not displayed, if `itemId` is unmatched.
193
+ return;
194
+ }
195
+ if (isString(this.asyncMenu)) {
196
+ menuDom.innerHTML = this.asyncMenu;
197
+ } else {
198
+ menuDom.innerHTML = this.asyncMenu.outerHTML;
199
+ }
200
+ }
201
+ this.removeMenuEventListener();
202
+
203
+ const handleMenuClick = this.options.handleMenuClick;
204
+ if (handleMenuClick) {
205
+ this.handleMenuClickWrapper = (event) => {
206
+ handleMenuClick(event.target, e.itemId, graph);
207
+ };
208
+ menuDom.addEventListener('click', this.handleMenuClickWrapper);
209
+ }
210
+
211
+ let triggeredByFirstClick = this.options.trigger === 'click';
212
+ const handler = (e) => {
213
+ if (triggeredByFirstClick) {
214
+ triggeredByFirstClick = false;
215
+ return;
216
+ }
217
+ self.onMenuHide();
218
+ };
219
+
220
+ document.body.addEventListener('click', handler);
221
+ this.handler = handler;
222
+ }
223
+
224
+ private removeMenuEventListener() {
225
+ const handleMenuClickWrapper = this.handleMenuClickWrapper;
226
+ const handler = this.handler;
227
+
228
+ if (handleMenuClickWrapper) {
229
+ const menuDom = this.menu;
230
+ menuDom.removeEventListener('click', handleMenuClickWrapper);
231
+ this.handleMenuClickWrapper = null;
232
+ }
233
+ if (handler) {
234
+ document.body.removeEventListener('click', handler);
235
+ }
236
+ }
237
+
238
+ private onMenuHide() {
239
+ const menuDom = this.menu;
240
+ if (menuDom) {
241
+ modifyCSS(menuDom, { visibility: 'hidden' });
242
+ this.removeMenuEventListener();
243
+ }
244
+ }
245
+
246
+ public destroy() {
247
+ const menu = this.menu;
248
+ this.removeMenuEventListener();
249
+ if (menu) {
250
+ let container = this.options.container;
251
+ if (!container) {
252
+ container = this.graph.container as HTMLDivElement;
253
+ }
254
+ if (isString(container)) {
255
+ container = document.getElementById(container) as HTMLDivElement;
256
+ }
257
+ container.removeChild(menu);
258
+ }
259
+ }
260
+ }
@@ -0,0 +1,424 @@
1
+ // TODO: update type define.
2
+ import { isString, isArray } from '@antv/util';
3
+ import { createDom, modifyCSS } from '@antv/dom-util';
4
+ import insertCss from 'insert-css';
5
+ import { AABB } from '@antv/g';
6
+ import { IGraph } from '../../../types';
7
+ import { Plugin as Base, IPluginBaseConfig } from '../../../types/plugin';
8
+ import { IG6GraphEvent } from '../../../types/event';
9
+
10
+ typeof document !== 'undefined' &&
11
+ insertCss(`
12
+ .g6-component-tooltip {
13
+ border: 1px solid #e2e2e2;
14
+ border-radius: 4px;
15
+ font-size: 12px;
16
+ color: #545454;
17
+ background-color: rgba(255, 255, 255, 0.9);
18
+ padding: 10px 8px;
19
+ box-shadow: rgb(174, 174, 174) 0px 0px 10px;
20
+ }
21
+ .tooltip-type {
22
+ padding: 0;
23
+ margin: 0;
24
+ }
25
+ .tooltip-id {
26
+ color: #531dab;
27
+ }
28
+ .g6-loading-dom {
29
+ border: 5px solid #e5e5e5;
30
+ border-top: 5px solid #227EFF;
31
+ border-radius: 50%;
32
+ width: 25px;
33
+ height: 25px;
34
+ animation: turn-around 1.5s linear infinite;
35
+ }
36
+ @keyframes turn-around {
37
+ 0% { transform: rotate(0deg); }
38
+ 100% { transform: rotate(360deg); }
39
+ }
40
+ `);
41
+
42
+ type Placement =
43
+ | 'top'
44
+ | 'left'
45
+ | 'right'
46
+ | 'bottom'
47
+ | 'topLeft'
48
+ | 'leftTop'
49
+ | 'topRight'
50
+ | 'rightTop'
51
+ | 'bottomLeft'
52
+ | 'leftBottom'
53
+ | 'bottomRight'
54
+ | 'rightBottom';
55
+
56
+ interface TooltipConfig extends IPluginBaseConfig {
57
+ getContent?: (
58
+ evt?: IG6GraphEvent,
59
+ ) => HTMLDivElement | string | Promise<HTMLDivElement | string>;
60
+ offsetX?: number;
61
+ offsetY?: number;
62
+ shouldBegin?: (evt?: IG6GraphEvent) => boolean;
63
+ // more detail type instead of "string[]"
64
+ itemTypes?: ('node' | 'edge' | 'combo' | 'canvas')[];
65
+ trigger?: 'pointerenter' | 'click';
66
+ fixToNode?: [number, number] | Placement | undefined;
67
+ loadingContent?: HTMLDivElement | string;
68
+ }
69
+
70
+ export default class Tooltip extends Base {
71
+ private tooltip;
72
+ private container;
73
+ private currentTarget;
74
+ private asyncTooltip;
75
+ private currentAsyncTarget;
76
+ private hidenTimer; //delay hiding tooltip
77
+
78
+ constructor(options?: TooltipConfig) {
79
+ super(options);
80
+ }
81
+
82
+ public getDefaultCfgs(): TooltipConfig {
83
+ return {
84
+ offsetX: 0,
85
+ offsetY: 0,
86
+ getContent: (e) => {
87
+ return `
88
+ <div>
89
+ <h4 class='tooltip-type'>类型: ${e.itemType}</h4>
90
+ <span class='tooltip-id'>ID: ${e.itemId}</span>
91
+ </div>
92
+ `;
93
+ },
94
+ shouldBegin: (e) => {
95
+ return true;
96
+ },
97
+ itemTypes: ['node', 'edge', 'combo'],
98
+ trigger: 'pointerenter',
99
+ fixToNode: undefined,
100
+ loadingContent: `<div class='g6-loading-dom'></div>`,
101
+ };
102
+ }
103
+
104
+ public getEvents() {
105
+ if (this.options.trigger === 'click') {
106
+ return {
107
+ 'node:click': this.onClick,
108
+ 'edge:click': this.onClick,
109
+ 'combo:click': this.onClick,
110
+ 'canvas:click': this.onPointerLeave,
111
+ afterremoveitem: this.onPointerLeave,
112
+ contextmenu: this.onPointerLeave,
113
+ drag: this.onPointerLeave,
114
+ };
115
+ }
116
+
117
+ return {
118
+ 'node:pointerenter': this.onPointerEnter,
119
+ 'node:pointerleave': this.onPointerLeave,
120
+ 'node:pointermove': this.onPointerMove,
121
+ 'edge:pointerenter': this.onPointerEnter,
122
+ 'edge:pointerleave': this.onPointerLeave,
123
+ 'edge:pointermove': this.onPointerMove,
124
+ 'combo:pointerenter': this.onPointerEnter,
125
+ 'combo:pointerleave': this.onPointerLeave,
126
+ 'combo:pointermove': this.onPointerMove,
127
+ afterremoveitem: this.onPointerLeave,
128
+ contextmenu: this.onPointerLeave,
129
+ 'node:drag': this.onPointerLeave,
130
+ };
131
+ }
132
+
133
+ public init(graph: IGraph) {
134
+ super.init(graph);
135
+ const className = this.options.className;
136
+ const tooltip = createDom(
137
+ `<div class='${className || 'g6-component-tooltip'}'></div>`,
138
+ );
139
+ modifyCSS(tooltip, {
140
+ position: 'absolute',
141
+ visibility: 'hidden',
142
+ display: 'none',
143
+ });
144
+ if (this.options.trigger !== 'click') {
145
+ tooltip.addEventListener('pointerenter', (e) => {
146
+ modifyCSS(tooltip, {
147
+ visibility: 'visible',
148
+ display: 'unset',
149
+ });
150
+ });
151
+ tooltip.addEventListener('pointerleave', (e) => {
152
+ this.hideTooltip();
153
+ });
154
+ }
155
+ //`container` string type in v5
156
+ let container: HTMLDivElement | null | string = this.options.container;
157
+ if (!container) {
158
+ container = this.graph.container as HTMLDivElement;
159
+ }
160
+ if (isString(container)) {
161
+ container = document.getElementById(container) as HTMLDivElement;
162
+ }
163
+ container.appendChild(tooltip);
164
+ this.tooltip = tooltip;
165
+ this.container = container;
166
+ }
167
+
168
+ public onClick(e: IG6GraphEvent) {
169
+ if (
170
+ e.itemId &&
171
+ e.itemType &&
172
+ this.options.itemTypes.indexOf(e.itemType) === -1
173
+ )
174
+ return;
175
+ const { itemId } = e;
176
+ // click the same item twice, tooltip will be hidden
177
+ if (this.currentTarget === itemId) {
178
+ this.currentTarget = null;
179
+ this.hideTooltip();
180
+ this.graph.emit('tooltipchange', { itemId: itemId, action: 'hide' });
181
+ } else {
182
+ this.currentTarget = itemId;
183
+ this.showTooltip(e);
184
+ // this.graph.emit('tooltipchange', { itemId: itemId, action: 'show' });
185
+ }
186
+ }
187
+
188
+ public onPointerEnter(e: IG6GraphEvent) {
189
+ if (
190
+ e.itemId &&
191
+ e.itemType &&
192
+ this.options.itemTypes.indexOf(e.itemType) === -1
193
+ )
194
+ return;
195
+ const { itemId } = e;
196
+ this.currentTarget = itemId;
197
+ this.showTooltip(e);
198
+ this.graph.emit('tooltipchange', { itemId: e.itemId, action: 'show' });
199
+ }
200
+
201
+ public onPointerMove(e: IG6GraphEvent) {
202
+ if (
203
+ e.itemId &&
204
+ e.itemType &&
205
+ this.options.itemTypes.indexOf(e.itemType) === -1
206
+ )
207
+ return;
208
+ if (!this.currentTarget || e.itemId === this.currentTarget) {
209
+ return;
210
+ }
211
+ this.showTooltip(e);
212
+ }
213
+
214
+ public onPointerLeave() {
215
+ this.hideTooltip();
216
+ this.graph.emit('tooltipchange', {
217
+ itemId: this.currentTarget,
218
+ action: 'hide',
219
+ });
220
+ this.currentTarget = null;
221
+ }
222
+
223
+ public clearContainer() {
224
+ this.container.innerHTML = '';
225
+ }
226
+
227
+ public async showTooltip(e: IG6GraphEvent) {
228
+ clearTimeout(this.hidenTimer);
229
+ if (!e.itemId) {
230
+ return;
231
+ }
232
+ if (e.itemType && this.options.itemTypes.indexOf(e.itemType) === -1) return;
233
+ const tooltip = this.options.getContent(e);
234
+ const tooltipDom = this.tooltip;
235
+ // modify the position first because of the async function
236
+ modifyCSS(tooltipDom, {
237
+ display: 'unset',
238
+ });
239
+ const res = this.updatePosition(e);
240
+ modifyCSS(tooltipDom, {
241
+ visibility: 'visible',
242
+ left: `${res.x}px`,
243
+ top: `${res.y}px`,
244
+ });
245
+ if (isString(tooltip)) {
246
+ tooltipDom.innerHTML = tooltip;
247
+ } else if (tooltip instanceof HTMLDivElement) {
248
+ this.clearContainer();
249
+ this.container.appendChild(tooltip);
250
+ } else {
251
+ //promise type
252
+ this.asyncTooltip = null; //avoid trigger many time
253
+ if (isString(this.options.loadingContent)) {
254
+ tooltipDom.innerHTML = this.options.loadingContent;
255
+ } else {
256
+ tooltipDom.innerHTML = this.options.loadingContent.outerHTML;
257
+ }
258
+ if (!this.asyncTooltip || e.itemId !== this.currentAsyncTarget) {
259
+ this.currentAsyncTarget = e.itemId;
260
+ this.asyncTooltip = await this.options.getContent(e);
261
+ }
262
+ if (e.itemId != this.currentAsyncTarget) {
263
+ return;
264
+ }
265
+ if (isString(this.asyncTooltip)) {
266
+ tooltipDom.innerHTML = this.asyncTooltip;
267
+ } else {
268
+ this.clearContainer();
269
+ this.container.appendChild(this.asyncTooltip);
270
+ }
271
+ }
272
+ }
273
+
274
+ public hideTooltip() {
275
+ const tooltip = this.tooltip;
276
+ if (tooltip) {
277
+ this.hidenTimer = setTimeout(() => {
278
+ modifyCSS(tooltip, { visibility: 'hidden', display: 'none' });
279
+ }, 100);
280
+ tooltip.addEventListener('pointerenter', (e) => {
281
+ clearTimeout(this.hidenTimer);
282
+ });
283
+ tooltip.addEventListener('pointerleave', (e) => {
284
+ modifyCSS(tooltip, { visibility: 'hidden', display: 'none' });
285
+ });
286
+ }
287
+ }
288
+
289
+ public updatePosition(e: IG6GraphEvent): { x: number; y: number } {
290
+ const shouldBegin = this.options.shouldBegin;
291
+ const tooltip = this.tooltip;
292
+ if (!shouldBegin(e)) {
293
+ modifyCSS(tooltip, {
294
+ visibility: 'hidden',
295
+ display: 'none',
296
+ });
297
+ return;
298
+ }
299
+ const graph: IGraph = this.graph;
300
+ const width: number = graph.getSize()[0];
301
+ const height: number = graph.getSize()[1];
302
+ const offsetX = this.options.offsetX || 0;
303
+ const offsetY = this.options.offsetY || 0;
304
+ let point = {
305
+ x: e.viewport.x,
306
+ y: e.viewport.y,
307
+ };
308
+
309
+ const fixToNode = this.options.fixToNode;
310
+ //handle `fixToNode` config
311
+ if (e.itemType && e.itemType === 'node' && fixToNode) {
312
+ const itemBBox = graph.getRenderBBox(e.itemId) as AABB;
313
+ const itemWidth = itemBBox.max[0] - itemBBox.min[0];
314
+ const itemHeight = itemBBox.max[1] - itemBBox.min[1];
315
+ if (isString(fixToNode)) {
316
+ switch (fixToNode) {
317
+ case 'right': {
318
+ point = {
319
+ x: itemBBox.min[0] + itemWidth * 1,
320
+ y: itemBBox.min[1] + itemHeight * 0.5,
321
+ };
322
+ break;
323
+ }
324
+ case 'rightTop':
325
+ case 'topRight': {
326
+ point = {
327
+ x: itemBBox.min[0] + itemWidth * 1,
328
+ y: itemBBox.min[1] + itemHeight * 0,
329
+ };
330
+ break;
331
+ }
332
+ case 'rightBottom':
333
+ case 'bottomRight': {
334
+ point = {
335
+ x: itemBBox.min[0] + itemWidth * 1,
336
+ y: itemBBox.min[1] + itemHeight * 1,
337
+ };
338
+ break;
339
+ }
340
+ case 'bottom': {
341
+ point = {
342
+ x: itemBBox.min[0] + itemWidth * 0,
343
+ y: itemBBox.min[1] + itemHeight * 1,
344
+ };
345
+ break;
346
+ }
347
+ case 'top': {
348
+ const tooltipBBox = tooltip.getBoundingClientRect();
349
+ point = {
350
+ x: itemBBox.min[0] + itemWidth * 0,
351
+ y: itemBBox.min[1] - tooltipBBox.height,
352
+ };
353
+ break;
354
+ }
355
+ case 'left': {
356
+ const tooltipBBox = tooltip.getBoundingClientRect();
357
+ point = {
358
+ x: itemBBox.min[0] - tooltipBBox.width,
359
+ y: itemBBox.min[1] + itemHeight * 0.5,
360
+ };
361
+ break;
362
+ }
363
+ case 'leftTop':
364
+ case 'topLeft': {
365
+ const tooltipBBox = tooltip.getBoundingClientRect();
366
+ point = {
367
+ x: itemBBox.min[0] - tooltipBBox.width,
368
+ y: itemBBox.min[1] + itemHeight * 0,
369
+ };
370
+ break;
371
+ }
372
+ case 'leftBottom':
373
+ case 'bottomLeft': {
374
+ const tooltipBBox = tooltip.getBoundingClientRect();
375
+ point = {
376
+ x: itemBBox.min[0] - tooltipBBox.width,
377
+ y: itemBBox.min[1] + itemHeight * 1,
378
+ };
379
+ break;
380
+ }
381
+ default:
382
+ //right
383
+ point = {
384
+ x: itemBBox.min[0] + itemWidth * 1,
385
+ y: itemBBox.min[1] + itemHeight * 0.5,
386
+ };
387
+ console.warn(
388
+ `The '${this.options.fixToNode}' fixToNode position configuration is not supported, please use 'top'|'left'| 'right'| 'bottom'| 'topLeft'| 'leftTop'| 'topRight'| 'rightTop'| 'bottomLeft'| 'leftBottom'| 'bottomRight'| 'rightBottom', or use array to config, like: [0,5,1]`,
389
+ );
390
+ break;
391
+ }
392
+ } else if (isArray(fixToNode) && fixToNode.length >= 2) {
393
+ point = {
394
+ x: itemBBox.min[0] + itemWidth * fixToNode[0],
395
+ y: itemBBox.min[1] + itemHeight * fixToNode[1],
396
+ };
397
+ }
398
+ }
399
+
400
+ const { x, y } = point;
401
+ const graphContainer = this.graph.container;
402
+ const res = {
403
+ x: x + graphContainer.offsetLeft + offsetX,
404
+ y: y + graphContainer.offsetTop + offsetY,
405
+ };
406
+
407
+ //tooltip dom bbox
408
+ const bbox = tooltip.getBoundingClientRect();
409
+ if (x + bbox.width + offsetX > width) {
410
+ res.x -= bbox.width + offsetX;
411
+ }
412
+ if (y + bbox.height + offsetY > height) {
413
+ res.y -= bbox.height + offsetY;
414
+ if (res.y < 0) {
415
+ res.y = 0;
416
+ }
417
+ }
418
+ return res;
419
+ }
420
+
421
+ public destroy() {
422
+ this.container.removeChild(this.tooltip);
423
+ }
424
+ }
@@ -106,6 +106,7 @@ export default {
106
106
  opacity: 0.45,
107
107
  lineWidth: 20,
108
108
  zIndex: -1,
109
+ pointerEvents: 'none',
109
110
  visible: true,
110
111
  },
111
112
  },
@@ -117,6 +118,7 @@ export default {
117
118
  opacity: 0.25,
118
119
  lineWidth: 20,
119
120
  zIndex: -1,
121
+ pointerEvents: 'none',
120
122
  visible: true,
121
123
  },
122
124
  },
@@ -235,6 +237,7 @@ export default {
235
237
  opacity: 0.25,
236
238
  lineWidth: 12,
237
239
  zIndex: -1,
240
+ pointerEvents: 'none',
238
241
  visible: true,
239
242
  },
240
243
  },
@@ -246,6 +249,7 @@ export default {
246
249
  opacity: 0.25,
247
250
  lineWidth: 12,
248
251
  zIndex: -1,
252
+ pointerEvents: 'none',
249
253
  visible: true,
250
254
  },
251
255
  },