@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,533 @@
1
+ import { ID } from '@antv/graphlib';
2
+ import { throttle, uniq } from '@antv/util';
3
+ import { ComboModel, EdgeModel, NodeModel } from '../../types';
4
+ import { Behavior } from '../../types/behavior';
5
+ import { IG6GraphEvent } from '../../types/event';
6
+ import { Point } from '../../types/common';
7
+ import { graphComboTreeDfs } from '../../util/data';
8
+
9
+ const DELEGATE_SHAPE_ID = 'g6-drag-combo-delegate-shape';
10
+
11
+ export interface DragComboOptions {
12
+ /**
13
+ * Whether to draw dragging nodes in transient layer.
14
+ * Ignored when enableDelegate is true.
15
+ * Defaults to true.
16
+ */
17
+ enableTransient?: boolean;
18
+ /**
19
+ * Whether to use a virtual rect moved with the dragging mouse instead of the node.
20
+ * Defaults to false.
21
+ */
22
+ enableDelegate?: boolean;
23
+ /**
24
+ * The drawing properties when the nodes are dragged.
25
+ * Only used when enableDelegate is true.
26
+ */
27
+ delegateStyle?: {
28
+ fill?: string;
29
+ stroke?: string;
30
+ fillOpacity?: number;
31
+ strokeOpacity?: number;
32
+ lineWidth?: number;
33
+ lineDash?: [number, number];
34
+ [key: string]: unknown;
35
+ };
36
+ /**
37
+ * The time in milliseconds to throttle moving. Useful to avoid the frequent calculation.
38
+ * Defaults to 0.
39
+ */
40
+ throttle?: number;
41
+ /**
42
+ * Whether to hide the related edges to avoid calculation while dragging nodes.
43
+ * Ignored when enableTransient or enableDelegate is true.
44
+ * Defaults to false.
45
+ */
46
+ hideRelatedEdges?: boolean;
47
+ /**
48
+ * The state name to be considered as "selected".
49
+ * Defaults to "selected".
50
+ */
51
+ selectedState?: string;
52
+ /**
53
+ * The event name to trigger when drag end.
54
+ */
55
+ eventName?: string;
56
+ /**
57
+ * Whether change the combo hierarchy structure or only change size.
58
+ */
59
+ updateComboStructure?: boolean;
60
+ /**
61
+ * Whether allow the behavior happen on the current item.
62
+ */
63
+ shouldBegin?: (event: IG6GraphEvent) => boolean;
64
+ }
65
+
66
+ const DEFAULT_OPTIONS: Required<DragComboOptions> = {
67
+ enableTransient: true,
68
+ enableDelegate: false,
69
+ delegateStyle: {
70
+ fill: '#F3F9FF',
71
+ fillOpacity: 0.5,
72
+ stroke: '#1890FF',
73
+ strokeOpacity: 0.9,
74
+ lineDash: [5, 5],
75
+ },
76
+ throttle: 16,
77
+ hideRelatedEdges: false,
78
+ selectedState: 'selected',
79
+ eventName: '',
80
+ updateComboStructure: true,
81
+ shouldBegin: () => true,
82
+ };
83
+
84
+ export default class DragCombo extends Behavior {
85
+ options: DragComboOptions;
86
+
87
+ // Private states
88
+ private hiddenEdges: EdgeModel[] = [];
89
+ private hiddenComboTreeRoots: (ComboModel | NodeModel)[] = [];
90
+ private originX: number;
91
+ private originY: number;
92
+ private previousX: number;
93
+ private previousY: number;
94
+ private originPositions: Array<{
95
+ id: ID;
96
+ x: number;
97
+ y: number;
98
+ // The following fields only have values when delegate is enabled.
99
+ minX?: number;
100
+ maxX?: number;
101
+ minY?: number;
102
+ maxY?: number;
103
+ }> = [];
104
+ private selectedComboIds: ID[];
105
+ private pointerDown: Point | undefined = undefined;
106
+ private dragging = false;
107
+
108
+ constructor(options: Partial<DragComboOptions>) {
109
+ const finalOptions = Object.assign({}, DEFAULT_OPTIONS, options);
110
+ if (finalOptions.enableDelegate) {
111
+ finalOptions.enableTransient = false;
112
+ }
113
+ if (finalOptions.enableDelegate || finalOptions.enableTransient) {
114
+ finalOptions.hideRelatedEdges = false;
115
+ }
116
+ super(finalOptions);
117
+ }
118
+
119
+ getEvents = () => {
120
+ const events = {
121
+ 'combo:pointerdown': this.onPointerDown,
122
+ pointermove: this.onPointerMove,
123
+ click: this.onPointerUp,
124
+ // FIXME: IG6Event -> keyboard event
125
+ keydown: this.onKeydown as any,
126
+ };
127
+ if (this.options.updateComboStructure) {
128
+ return {
129
+ 'combo:drop': this.onDropCombo,
130
+ 'canvas:drop': this.onDropCanvas,
131
+ ...events,
132
+ };
133
+ } else {
134
+ return {
135
+ pointerup: this.onPointerUp,
136
+ ...events,
137
+ };
138
+ }
139
+ };
140
+
141
+ /** Given selected node ids, get their related visible edges. */
142
+ private getRelatedEdges(
143
+ selectedComboIds: ID[],
144
+ comboTreeRoots: (ComboModel | NodeModel)[],
145
+ ) {
146
+ let edges = selectedComboIds.flatMap((comboId) =>
147
+ this.graph.getRelatedEdgesData(comboId),
148
+ );
149
+ graphComboTreeDfs(this.graph, comboTreeRoots, (child) => {
150
+ edges = edges.concat(this.graph.getRelatedEdgesData(child.id));
151
+ });
152
+ return uniq(edges).filter((edgeData) => {
153
+ return this.graph.getItemVisible(edgeData.id);
154
+ });
155
+ }
156
+
157
+ private getComboTreeItems(selectedComboIds: ID[]) {
158
+ let begins = [];
159
+ if (!this.options.updateComboStructure) {
160
+ // do not change the combo structure, begins are the roots
161
+ selectedComboIds.forEach((id) => {
162
+ const comboData = this.graph.getComboData(id);
163
+ let rootAncestor = comboData;
164
+ let parentId = comboData.data.parentId;
165
+ while (parentId) {
166
+ const parentData = this.graph.getComboData(parentId);
167
+ if (!parentData) break;
168
+ parentId = parentData.data.parentId;
169
+ rootAncestor = parentData;
170
+ }
171
+ begins.push(rootAncestor);
172
+ });
173
+ } else {
174
+ begins = selectedComboIds.map((id) => this.graph.getComboData(id));
175
+ }
176
+
177
+ const items = begins;
178
+ graphComboTreeDfs(this.graph, begins, (child) => items.push(child), 'TB');
179
+ return uniq(items).filter(
180
+ (item) =>
181
+ !selectedComboIds.includes(item.id) &&
182
+ this.graph.getItemVisible(item.id),
183
+ );
184
+ }
185
+
186
+ public onPointerDown(event: IG6GraphEvent) {
187
+ if (!this.options.shouldBegin(event)) return;
188
+ this.pointerDown = { x: event.canvas.x, y: event.canvas.y };
189
+ this.dragging = false;
190
+ }
191
+
192
+ public onPointerMove(event: IG6GraphEvent) {
193
+ if (!this.pointerDown) return;
194
+
195
+ const beginDeltaX = Math.abs(this.pointerDown.x - event.canvas.x);
196
+ const beginDeltaY = Math.abs(this.pointerDown.y - event.canvas.y);
197
+ if (beginDeltaX < 1 && beginDeltaY < 1) return;
198
+
199
+ const enableTransient =
200
+ this.options.enableTransient && this.graph.rendererType !== 'webgl-3d';
201
+
202
+ if (this.dragging) {
203
+ this.originPositions = this.selectedComboIds
204
+ .map((id) => {
205
+ if (!this.graph.getComboData(id)) {
206
+ console.warn('combo does not exist', id);
207
+ return;
208
+ }
209
+ const bounds = this.graph.getRenderBBox(id, true, enableTransient);
210
+ if (!bounds) return;
211
+ const [x, y] = bounds.center;
212
+ // If delegate is enabled, record bbox together.
213
+ if (this.options.enableDelegate) {
214
+ const [minX, minY] = bounds.min;
215
+ const [maxX, maxY] = bounds.max;
216
+ return { id, x, y, minX, minY, maxX, maxY };
217
+ }
218
+ return { id, x, y };
219
+ })
220
+ .filter(Boolean);
221
+ }
222
+
223
+ // pointerDown + first move = dragstart
224
+ if (!this.dragging) {
225
+ this.dragging = true;
226
+
227
+ const currentComboId = event.itemId;
228
+ let selectedComboIds = this.graph.findIdByState(
229
+ 'combo',
230
+ this.options.selectedState,
231
+ true,
232
+ );
233
+
234
+ // If current node is selected, drag all the selected nodes together.
235
+ // Otherwise drag current node.
236
+ if (
237
+ currentComboId &&
238
+ event.itemType === 'combo' &&
239
+ !selectedComboIds.includes(currentComboId)
240
+ ) {
241
+ selectedComboIds = [currentComboId];
242
+ }
243
+ this.selectedComboIds = selectedComboIds;
244
+
245
+ // Hide related edge.
246
+ if (this.options.hideRelatedEdges && !enableTransient) {
247
+ this.hiddenComboTreeRoots = this.getComboTreeItems(selectedComboIds);
248
+ this.hiddenEdges = this.getRelatedEdges(
249
+ selectedComboIds,
250
+ this.hiddenComboTreeRoots,
251
+ );
252
+ this.graph.hideItem(
253
+ this.hiddenEdges.map((edge) => edge.id),
254
+ true,
255
+ );
256
+ this.graph.hideItem(
257
+ this.hiddenComboTreeRoots.map((child) => child.id),
258
+ true,
259
+ );
260
+ }
261
+
262
+ // Draw transient nodes and edges.
263
+ if (enableTransient) {
264
+ // Draw transient edges and nodes.
265
+ this.hiddenComboTreeRoots = this.getComboTreeItems(selectedComboIds);
266
+ this.hiddenEdges = this.getRelatedEdges(
267
+ selectedComboIds,
268
+ this.hiddenComboTreeRoots,
269
+ );
270
+ selectedComboIds.forEach((comboId) => {
271
+ this.graph.drawTransient('combo', comboId, {
272
+ upsertAncestors: !this.options.updateComboStructure,
273
+ });
274
+ });
275
+ this.hiddenEdges.forEach((edge) => {
276
+ this.graph.drawTransient('edge', edge.id, {});
277
+ });
278
+
279
+ // Hide original edges and nodes. They will be restored when pointerup.
280
+ this.graph.hideItem(selectedComboIds, true);
281
+ this.graph.hideItem(
282
+ this.hiddenEdges.map((edge) => edge.id),
283
+ true,
284
+ );
285
+ this.graph.hideItem(
286
+ this.hiddenComboTreeRoots.map((child) => child.id),
287
+ true,
288
+ );
289
+ } else {
290
+ this.graph.frontItem(selectedComboIds);
291
+ }
292
+
293
+ // Throttle moving.
294
+ if (this.options.throttle > 0) {
295
+ this.throttledMoveCombos = throttle(
296
+ this.moveCombos,
297
+ this.options.throttle,
298
+ { leading: true, trailing: true },
299
+ );
300
+ } else {
301
+ this.throttledMoveCombos = this.moveCombos;
302
+ }
303
+
304
+ this.originX = event.canvas.x;
305
+ this.originY = event.canvas.y;
306
+ this.previousX = event.canvas.x;
307
+ this.previousY = event.canvas.y;
308
+ }
309
+
310
+ if (!this.originPositions.length || !this.dragging) {
311
+ return;
312
+ }
313
+
314
+ // @ts-ignore FIXME: type
315
+ const pointerEvent = event as PointerEvent;
316
+ // @ts-ignore FIXME: Type
317
+ const deltaX = pointerEvent.canvas.x - this.previousX;
318
+ // @ts-ignore FIXME: Type
319
+ const deltaY = pointerEvent.canvas.y - this.previousY;
320
+
321
+ if (this.options.enableDelegate) {
322
+ this.moveDelegate(deltaX, deltaY);
323
+ } else {
324
+ const enableTransient =
325
+ this.options.enableTransient && this.graph.rendererType !== 'webgl-3d';
326
+ this.throttledMoveCombos(
327
+ deltaX,
328
+ deltaY,
329
+ enableTransient,
330
+ !this.options.updateComboStructure,
331
+ );
332
+ }
333
+ }
334
+
335
+ public moveCombos(
336
+ deltaX: number,
337
+ deltaY: number,
338
+ transient: boolean,
339
+ upsertAncestors = true,
340
+ ) {
341
+ if (transient) {
342
+ // Move transient nodes
343
+ this.originPositions.forEach(({ id, x, y }) => {
344
+ this.graph.drawTransient('combo', id, {
345
+ data: {
346
+ x: x + deltaX,
347
+ y: y + deltaY,
348
+ },
349
+ upsertAncestors,
350
+ });
351
+ });
352
+ // Update transient edges.
353
+ this.hiddenEdges.forEach((edge) => {
354
+ this.graph.drawTransient('edge', edge.id, {});
355
+ });
356
+ } else {
357
+ this.graph.moveCombo(
358
+ this.originPositions.map(({ id }) => id),
359
+ deltaX,
360
+ deltaY,
361
+ upsertAncestors,
362
+ );
363
+ }
364
+
365
+ this.previousX += deltaX;
366
+ this.previousY += deltaY;
367
+ }
368
+
369
+ public throttledMoveCombos: Function = (
370
+ deltaX: number,
371
+ deltaY: number,
372
+ transient: boolean,
373
+ upsertAncestors = true,
374
+ ) => {
375
+ // Should be overrided when drag start.
376
+ };
377
+
378
+ public moveDelegate(deltaX: number, deltaY: number) {
379
+ const x1 = Math.min(
380
+ ...this.originPositions.map((position) => position.minX),
381
+ );
382
+ const y1 = Math.min(
383
+ ...this.originPositions.map((position) => position.minY),
384
+ );
385
+ const x2 = Math.max(
386
+ ...this.originPositions.map((position) => position.maxX),
387
+ );
388
+ const y2 = Math.max(
389
+ ...this.originPositions.map((position) => position.maxY),
390
+ );
391
+ this.graph.drawTransient('rect', DELEGATE_SHAPE_ID, {
392
+ style: {
393
+ x: x1 + deltaX,
394
+ y: y1 + deltaY,
395
+ width: x2 - x1,
396
+ height: y2 - y1,
397
+ ...this.options.delegateStyle,
398
+ },
399
+ });
400
+ }
401
+
402
+ public clearDelegate() {
403
+ this.graph.drawTransient('rect', DELEGATE_SHAPE_ID, { action: 'remove' });
404
+ }
405
+
406
+ public clearTransientItems() {
407
+ this.hiddenEdges.forEach((edge) => {
408
+ this.graph.drawTransient('combo', edge.source, { action: 'remove' });
409
+ this.graph.drawTransient('combo', edge.target, { action: 'remove' });
410
+ this.graph.drawTransient('edge', edge.id, { action: 'remove' });
411
+ });
412
+ this.hiddenComboTreeRoots.forEach((item) => {
413
+ const isCombo = item.data._isCombo;
414
+ this.graph.drawTransient(isCombo ? 'combo' : 'node', item.id, {
415
+ action: 'remove',
416
+ });
417
+ });
418
+ this.originPositions.forEach(({ id }) => {
419
+ this.graph.drawTransient('combo', id, { action: 'remove' });
420
+ });
421
+ }
422
+
423
+ public restoreHiddenItems() {
424
+ if (this.hiddenEdges.length) {
425
+ this.graph.showItem(
426
+ this.hiddenEdges.map((edge) => edge.id),
427
+ true,
428
+ );
429
+ this.hiddenEdges = [];
430
+ }
431
+ if (this.hiddenComboTreeRoots.length) {
432
+ this.graph.showItem(
433
+ this.hiddenComboTreeRoots.map((model) => model.id),
434
+ true,
435
+ );
436
+ this.hiddenComboTreeRoots = [];
437
+ }
438
+ const enableTransient =
439
+ this.options.enableTransient && this.graph.rendererType !== 'webgl-3d';
440
+ if (enableTransient) {
441
+ this.graph.showItem(
442
+ this.originPositions.map((position) => position.id),
443
+ true,
444
+ );
445
+ }
446
+ }
447
+
448
+ public onPointerUp(event: IG6GraphEvent) {
449
+ this.pointerDown = undefined;
450
+ this.dragging = false;
451
+ const enableTransient =
452
+ this.options.enableTransient && this.graph.rendererType !== 'webgl-3d';
453
+ // If transient or delegate was enabled, move the real nodes.
454
+ // if (enableTransient || this.options.enableDelegate) {
455
+ // @ts-ignore FIXME: type
456
+ const pointerEvent = event as PointerEvent;
457
+ const baseX = enableTransient ? this.originX : this.previousX;
458
+ const baseY = enableTransient ? this.originY : this.previousY;
459
+ // @ts-ignore FIXME: Type
460
+ const deltaX = pointerEvent.canvas.x - baseX + 0.01;
461
+ // @ts-ignore FIXME: Type
462
+ const deltaY = pointerEvent.canvas.y - baseY + 0.01;
463
+ this.moveCombos(deltaX, deltaY, false, true);
464
+ // }
465
+
466
+ if (enableTransient) {
467
+ this.clearTransientItems();
468
+ }
469
+
470
+ if (this.options.enableDelegate) {
471
+ this.clearDelegate();
472
+ }
473
+
474
+ // Restore all hidden items.
475
+ // For all hideRelatedEdges, enableTransient and enableDelegate cases.
476
+ this.restoreHiddenItems();
477
+
478
+ // Emit event.
479
+ if (this.options.eventName) {
480
+ this.graph.emit(this.options.eventName, {
481
+ itemIds: this.originPositions.map((position) => position.id),
482
+ });
483
+ }
484
+
485
+ // Reset state.
486
+ this.originPositions = [];
487
+ }
488
+
489
+ public onKeydown(event: KeyboardEvent) {
490
+ if (event.key !== 'Escape' && event.key !== 'Esc') {
491
+ return;
492
+ }
493
+ this.clearDelegate();
494
+ this.clearTransientItems();
495
+ this.restoreHiddenItems();
496
+
497
+ const enableTransient =
498
+ this.options.enableTransient && this.graph.rendererType !== 'webgl-3d';
499
+ // Restore node positions.
500
+ if (!enableTransient && !this.options.enableDelegate) {
501
+ const positionChanges = this.originPositions.map(({ id, x, y }) => {
502
+ return { id, data: { x, y } };
503
+ });
504
+ this.graph.updateComboPosition(positionChanges);
505
+ }
506
+
507
+ this.originPositions = [];
508
+ }
509
+
510
+ public onDropCombo(event: IG6GraphEvent) {
511
+ const newParentId = event.itemId;
512
+ this.originPositions.forEach(({ id }) => {
513
+ const model = this.graph.getComboData(id);
514
+ if (!model || model.id === newParentId) return;
515
+ const { parentId } = model.data;
516
+ if (parentId === newParentId) return;
517
+ // event.stopPropagation();
518
+ this.graph.updateData('combo', { id, data: { parentId: newParentId } });
519
+ });
520
+ this.onPointerUp(event);
521
+ }
522
+
523
+ public onDropCanvas(event: IG6GraphEvent) {
524
+ this.originPositions.forEach(({ id }) => {
525
+ const model = this.graph.getComboData(id);
526
+ if (!model) return;
527
+ const { parentId } = model.data;
528
+ if (!parentId) return;
529
+ this.graph.updateData('combo', { id, data: { parentId: undefined } });
530
+ });
531
+ this.onPointerUp(event);
532
+ }
533
+ }