@antv/g6 5.0.0-beta.4 → 5.0.0-beta.6

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 (246) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/esm/index.d.ts +15 -1
  3. package/esm/index.js +1 -1
  4. package/esm/item/combo.d.ts +1 -1
  5. package/esm/item/combo.js +16 -9
  6. package/esm/item/combo.js.map +1 -1
  7. package/esm/item/edge.d.ts +2 -1
  8. package/esm/item/edge.js +18 -8
  9. package/esm/item/edge.js.map +1 -1
  10. package/esm/item/item.d.ts +3 -2
  11. package/esm/item/item.js +12 -6
  12. package/esm/item/item.js.map +1 -1
  13. package/esm/item/node.d.ts +2 -1
  14. package/esm/item/node.js +19 -10
  15. package/esm/item/node.js.map +1 -1
  16. package/esm/runtime/controller/item.d.ts +1 -0
  17. package/esm/runtime/controller/item.js +271 -151
  18. package/esm/runtime/controller/item.js.map +1 -1
  19. package/esm/runtime/graph.d.ts +98 -18
  20. package/esm/runtime/graph.js +427 -75
  21. package/esm/runtime/graph.js.map +1 -1
  22. package/esm/stdlib/behavior/activate-relations.js +2 -2
  23. package/esm/stdlib/behavior/activate-relations.js.map +1 -1
  24. package/esm/stdlib/behavior/brush-select.js +1 -2
  25. package/esm/stdlib/behavior/brush-select.js.map +1 -1
  26. package/esm/stdlib/behavior/click-select.js +1 -1
  27. package/esm/stdlib/behavior/click-select.js.map +1 -1
  28. package/esm/stdlib/behavior/create-edge.d.ts +57 -0
  29. package/esm/stdlib/behavior/create-edge.js +206 -0
  30. package/esm/stdlib/behavior/create-edge.js.map +1 -0
  31. package/esm/stdlib/behavior/drag-canvas.d.ts +1 -1
  32. package/esm/stdlib/behavior/drag-canvas.js +51 -28
  33. package/esm/stdlib/behavior/drag-canvas.js.map +1 -1
  34. package/esm/stdlib/behavior/drag-combo.d.ts +2 -2
  35. package/esm/stdlib/behavior/drag-combo.js +43 -32
  36. package/esm/stdlib/behavior/drag-combo.js.map +1 -1
  37. package/esm/stdlib/behavior/drag-node.d.ts +3 -3
  38. package/esm/stdlib/behavior/drag-node.js +76 -63
  39. package/esm/stdlib/behavior/drag-node.js.map +1 -1
  40. package/esm/stdlib/behavior/index.d.ts +2 -0
  41. package/esm/stdlib/behavior/index.js +2 -0
  42. package/esm/stdlib/behavior/index.js.map +1 -1
  43. package/esm/stdlib/behavior/lasso-select.d.ts +1 -1
  44. package/esm/stdlib/behavior/lasso-select.js +1 -2
  45. package/esm/stdlib/behavior/lasso-select.js.map +1 -1
  46. package/esm/stdlib/behavior/scroll-canvas.d.ts +67 -0
  47. package/esm/stdlib/behavior/scroll-canvas.js +266 -0
  48. package/esm/stdlib/behavior/scroll-canvas.js.map +1 -0
  49. package/esm/stdlib/behavior/zoom-canvas.d.ts +1 -1
  50. package/esm/stdlib/behavior/zoom-canvas.js +47 -24
  51. package/esm/stdlib/behavior/zoom-canvas.js.map +1 -1
  52. package/esm/stdlib/index.d.ts +6 -0
  53. package/esm/stdlib/index.js +15 -9
  54. package/esm/stdlib/index.js.map +1 -1
  55. package/esm/stdlib/item/edge/base.d.ts +1 -1
  56. package/esm/stdlib/item/edge/base.js +13 -4
  57. package/esm/stdlib/item/edge/base.js.map +1 -1
  58. package/esm/stdlib/item/node/base.d.ts +1 -1
  59. package/esm/stdlib/item/node/base.js +11 -12
  60. package/esm/stdlib/item/node/base.js.map +1 -1
  61. package/esm/stdlib/item/node/image.d.ts +1 -1
  62. package/esm/stdlib/item/node/image.js +1 -1
  63. package/esm/stdlib/item/node/image.js.map +1 -1
  64. package/esm/stdlib/plugin/history/combo-command.js +2 -2
  65. package/esm/stdlib/plugin/history/combo-command.js.map +1 -1
  66. package/esm/stdlib/plugin/history/index.d.ts +10 -10
  67. package/esm/stdlib/plugin/history/index.js +14 -14
  68. package/esm/stdlib/plugin/history/index.js.map +1 -1
  69. package/esm/stdlib/plugin/history/item-data-command.js +4 -4
  70. package/esm/stdlib/plugin/history/item-data-command.js.map +1 -1
  71. package/esm/stdlib/plugin/history/layer-updated-command.js +2 -2
  72. package/esm/stdlib/plugin/history/layer-updated-command.js.map +1 -1
  73. package/esm/stdlib/plugin/history/state-updated-command.js +1 -1
  74. package/esm/stdlib/plugin/history/state-updated-command.js.map +1 -1
  75. package/esm/stdlib/plugin/history/visibility-updated-command.js +2 -2
  76. package/esm/stdlib/plugin/history/visibility-updated-command.js.map +1 -1
  77. package/esm/stdlib/plugin/hull/hullComponent.d.ts +1 -1
  78. package/esm/stdlib/plugin/hull/hullComponent.js +3 -6
  79. package/esm/stdlib/plugin/hull/hullComponent.js.map +1 -1
  80. package/esm/stdlib/plugin/index.d.ts +1 -0
  81. package/esm/stdlib/plugin/index.js +1 -0
  82. package/esm/stdlib/plugin/index.js.map +1 -1
  83. package/esm/stdlib/plugin/minimap/index.d.ts +5 -1
  84. package/esm/stdlib/plugin/minimap/index.js +109 -8
  85. package/esm/stdlib/plugin/minimap/index.js.map +1 -1
  86. package/esm/stdlib/plugin/snapline/index.d.ts +163 -0
  87. package/esm/stdlib/plugin/snapline/index.js +1052 -0
  88. package/esm/stdlib/plugin/snapline/index.js.map +1 -0
  89. package/esm/stdlib/plugin/types.d.ts +1 -0
  90. package/esm/types/graph.d.ts +45 -19
  91. package/esm/types/hook.d.ts +22 -2
  92. package/esm/types/spec.d.ts +19 -3
  93. package/esm/util/bbox.d.ts +16 -1
  94. package/esm/util/bbox.js +29 -0
  95. package/esm/util/bbox.js.map +1 -1
  96. package/esm/util/item.d.ts +16 -1
  97. package/esm/util/item.js +31 -8
  98. package/esm/util/item.js.map +1 -1
  99. package/esm/util/shape.js +2 -2
  100. package/esm/util/shape.js.map +1 -1
  101. package/esm/util/warn.d.ts +14 -0
  102. package/esm/util/warn.js +4 -0
  103. package/esm/util/warn.js.map +1 -0
  104. package/lib/index.d.ts +15 -1
  105. package/lib/index.js +1 -1
  106. package/lib/item/combo.d.ts +1 -1
  107. package/lib/item/combo.js +16 -9
  108. package/lib/item/combo.js.map +1 -1
  109. package/lib/item/edge.d.ts +2 -1
  110. package/lib/item/edge.js +18 -8
  111. package/lib/item/edge.js.map +1 -1
  112. package/lib/item/item.d.ts +3 -2
  113. package/lib/item/item.js +12 -6
  114. package/lib/item/item.js.map +1 -1
  115. package/lib/item/node.d.ts +2 -1
  116. package/lib/item/node.js +19 -10
  117. package/lib/item/node.js.map +1 -1
  118. package/lib/runtime/controller/item.d.ts +1 -0
  119. package/lib/runtime/controller/item.js +270 -150
  120. package/lib/runtime/controller/item.js.map +1 -1
  121. package/lib/runtime/graph.d.ts +98 -18
  122. package/lib/runtime/graph.js +427 -75
  123. package/lib/runtime/graph.js.map +1 -1
  124. package/lib/stdlib/behavior/activate-relations.js +2 -2
  125. package/lib/stdlib/behavior/activate-relations.js.map +1 -1
  126. package/lib/stdlib/behavior/brush-select.js +1 -2
  127. package/lib/stdlib/behavior/brush-select.js.map +1 -1
  128. package/lib/stdlib/behavior/click-select.js +1 -1
  129. package/lib/stdlib/behavior/click-select.js.map +1 -1
  130. package/lib/stdlib/behavior/create-edge.d.ts +57 -0
  131. package/lib/stdlib/behavior/create-edge.js +210 -0
  132. package/lib/stdlib/behavior/create-edge.js.map +1 -0
  133. package/lib/stdlib/behavior/drag-canvas.d.ts +1 -1
  134. package/lib/stdlib/behavior/drag-canvas.js +51 -28
  135. package/lib/stdlib/behavior/drag-canvas.js.map +1 -1
  136. package/lib/stdlib/behavior/drag-combo.d.ts +2 -2
  137. package/lib/stdlib/behavior/drag-combo.js +43 -32
  138. package/lib/stdlib/behavior/drag-combo.js.map +1 -1
  139. package/lib/stdlib/behavior/drag-node.d.ts +3 -3
  140. package/lib/stdlib/behavior/drag-node.js +76 -63
  141. package/lib/stdlib/behavior/drag-node.js.map +1 -1
  142. package/lib/stdlib/behavior/index.d.ts +2 -0
  143. package/lib/stdlib/behavior/index.js +2 -0
  144. package/lib/stdlib/behavior/index.js.map +1 -1
  145. package/lib/stdlib/behavior/lasso-select.d.ts +1 -1
  146. package/lib/stdlib/behavior/lasso-select.js +1 -2
  147. package/lib/stdlib/behavior/lasso-select.js.map +1 -1
  148. package/lib/stdlib/behavior/scroll-canvas.d.ts +67 -0
  149. package/lib/stdlib/behavior/scroll-canvas.js +270 -0
  150. package/lib/stdlib/behavior/scroll-canvas.js.map +1 -0
  151. package/lib/stdlib/behavior/zoom-canvas.d.ts +1 -1
  152. package/lib/stdlib/behavior/zoom-canvas.js +47 -24
  153. package/lib/stdlib/behavior/zoom-canvas.js.map +1 -1
  154. package/lib/stdlib/index.d.ts +6 -0
  155. package/lib/stdlib/index.js +15 -9
  156. package/lib/stdlib/index.js.map +1 -1
  157. package/lib/stdlib/item/edge/base.d.ts +1 -1
  158. package/lib/stdlib/item/edge/base.js +13 -4
  159. package/lib/stdlib/item/edge/base.js.map +1 -1
  160. package/lib/stdlib/item/node/base.d.ts +1 -1
  161. package/lib/stdlib/item/node/base.js +11 -12
  162. package/lib/stdlib/item/node/base.js.map +1 -1
  163. package/lib/stdlib/item/node/image.d.ts +1 -1
  164. package/lib/stdlib/item/node/image.js +1 -1
  165. package/lib/stdlib/item/node/image.js.map +1 -1
  166. package/lib/stdlib/plugin/history/combo-command.js +2 -2
  167. package/lib/stdlib/plugin/history/combo-command.js.map +1 -1
  168. package/lib/stdlib/plugin/history/index.d.ts +10 -10
  169. package/lib/stdlib/plugin/history/index.js +14 -14
  170. package/lib/stdlib/plugin/history/index.js.map +1 -1
  171. package/lib/stdlib/plugin/history/item-data-command.js +4 -4
  172. package/lib/stdlib/plugin/history/item-data-command.js.map +1 -1
  173. package/lib/stdlib/plugin/history/layer-updated-command.js +2 -2
  174. package/lib/stdlib/plugin/history/layer-updated-command.js.map +1 -1
  175. package/lib/stdlib/plugin/history/state-updated-command.js +1 -1
  176. package/lib/stdlib/plugin/history/state-updated-command.js.map +1 -1
  177. package/lib/stdlib/plugin/history/visibility-updated-command.js +2 -2
  178. package/lib/stdlib/plugin/history/visibility-updated-command.js.map +1 -1
  179. package/lib/stdlib/plugin/hull/hullComponent.d.ts +1 -1
  180. package/lib/stdlib/plugin/hull/hullComponent.js +3 -6
  181. package/lib/stdlib/plugin/hull/hullComponent.js.map +1 -1
  182. package/lib/stdlib/plugin/index.d.ts +1 -0
  183. package/lib/stdlib/plugin/index.js +1 -0
  184. package/lib/stdlib/plugin/index.js.map +1 -1
  185. package/lib/stdlib/plugin/minimap/index.d.ts +5 -1
  186. package/lib/stdlib/plugin/minimap/index.js +109 -8
  187. package/lib/stdlib/plugin/minimap/index.js.map +1 -1
  188. package/lib/stdlib/plugin/snapline/index.d.ts +163 -0
  189. package/lib/stdlib/plugin/snapline/index.js +1056 -0
  190. package/lib/stdlib/plugin/snapline/index.js.map +1 -0
  191. package/lib/stdlib/plugin/types.d.ts +1 -0
  192. package/lib/types/graph.d.ts +45 -19
  193. package/lib/types/hook.d.ts +22 -2
  194. package/lib/types/spec.d.ts +19 -3
  195. package/lib/util/bbox.d.ts +16 -1
  196. package/lib/util/bbox.js +32 -1
  197. package/lib/util/bbox.js.map +1 -1
  198. package/lib/util/item.d.ts +16 -1
  199. package/lib/util/item.js +33 -9
  200. package/lib/util/item.js.map +1 -1
  201. package/lib/util/shape.js +2 -2
  202. package/lib/util/shape.js.map +1 -1
  203. package/lib/util/warn.d.ts +14 -0
  204. package/lib/util/warn.js +8 -0
  205. package/lib/util/warn.js.map +1 -0
  206. package/package.json +2 -2
  207. package/src/index.ts +1 -1
  208. package/src/item/combo.ts +14 -9
  209. package/src/item/edge.ts +17 -8
  210. package/src/item/item.ts +11 -6
  211. package/src/item/node.ts +24 -16
  212. package/src/runtime/controller/item.ts +240 -72
  213. package/src/runtime/graph.ts +536 -80
  214. package/src/stdlib/behavior/activate-relations.ts +2 -2
  215. package/src/stdlib/behavior/brush-select.ts +4 -2
  216. package/src/stdlib/behavior/click-select.ts +1 -1
  217. package/src/stdlib/behavior/create-edge.ts +295 -0
  218. package/src/stdlib/behavior/drag-canvas.ts +67 -29
  219. package/src/stdlib/behavior/drag-combo.ts +10 -10
  220. package/src/stdlib/behavior/drag-node.ts +18 -18
  221. package/src/stdlib/behavior/index.ts +2 -0
  222. package/src/stdlib/behavior/lasso-select.ts +5 -3
  223. package/src/stdlib/behavior/scroll-canvas.ts +348 -0
  224. package/src/stdlib/behavior/zoom-canvas.ts +61 -25
  225. package/src/stdlib/index.ts +16 -7
  226. package/src/stdlib/item/edge/base.ts +13 -5
  227. package/src/stdlib/item/node/base.ts +15 -14
  228. package/src/stdlib/item/node/image.ts +1 -1
  229. package/src/stdlib/plugin/history/combo-command.ts +2 -2
  230. package/src/stdlib/plugin/history/index.ts +14 -14
  231. package/src/stdlib/plugin/history/item-data-command.ts +4 -4
  232. package/src/stdlib/plugin/history/layer-updated-command.ts +2 -2
  233. package/src/stdlib/plugin/history/state-updated-command.ts +1 -1
  234. package/src/stdlib/plugin/history/visibility-updated-command.ts +2 -2
  235. package/src/stdlib/plugin/hull/hullComponent.ts +8 -5
  236. package/src/stdlib/plugin/index.ts +1 -0
  237. package/src/stdlib/plugin/minimap/index.ts +113 -9
  238. package/src/stdlib/plugin/snapline/index.ts +1277 -0
  239. package/src/stdlib/plugin/types.ts +1 -0
  240. package/src/types/graph.ts +53 -22
  241. package/src/types/hook.ts +22 -2
  242. package/src/types/spec.ts +22 -3
  243. package/src/util/bbox.ts +38 -1
  244. package/src/util/item.ts +65 -28
  245. package/src/util/shape.ts +2 -2
  246. package/src/util/warn.ts +20 -0
@@ -118,7 +118,7 @@ export class ActivateRelations extends Behavior {
118
118
  edgeIds,
119
119
  );
120
120
 
121
- graph.batch(() => {
121
+ graph.historyBatch(() => {
122
122
  /** 节点 */
123
123
  graph.setItemState(activeNodeIds, ACTIVE_STATE, true);
124
124
  graph.setItemState(inactiveNodeIds, ACTIVE_STATE, false);
@@ -132,7 +132,7 @@ export class ActivateRelations extends Behavior {
132
132
  }
133
133
  public clearActiveState(e: any) {
134
134
  const { activeState: ACTIVE_STATE } = this.options;
135
- this.graph.batch(() => {
135
+ this.graph.historyBatch(() => {
136
136
  this.graph.setItemState(this.prevNodeIds, ACTIVE_STATE, false);
137
137
  this.graph.setItemState(this.prevEdgeIds, ACTIVE_STATE, false);
138
138
  });
@@ -345,8 +345,10 @@ export class BrushSelect extends Behavior {
345
345
  const { graph, options } = this;
346
346
  const { brushStyle } = options;
347
347
  const brush = graph.drawTransient('rect', BRUSH_SHAPE_ID, {
348
- style: brushStyle,
349
- capture: false,
348
+ style: {
349
+ ...brushStyle,
350
+ pointerEvents: 'none',
351
+ },
350
352
  });
351
353
  return brush;
352
354
  }
@@ -136,7 +136,7 @@ export class ClickSelect extends Behavior {
136
136
 
137
137
  // Select/Unselect item.
138
138
  if (this.options.shouldUpdate(event)) {
139
- this.graph.batch(() => {
139
+ this.graph.historyBatch(() => {
140
140
  if (!multiple) {
141
141
  // Not multiple, clear all currently selected items
142
142
  this.graph.setItemState(this.selectedIds, state, false);
@@ -0,0 +1,295 @@
1
+ import type { IG6GraphEvent } from '../../types';
2
+ import { warn } from '../../util/warn';
3
+ import { generateEdgeID } from '../../util/item';
4
+ import { Behavior } from '../../types/behavior';
5
+ import { EdgeDisplayModelData } from '../../types/edge';
6
+
7
+ const KEYBOARD_TRIGGERS = ['shift', 'ctrl', 'control', 'alt', 'meta'] as const;
8
+ const EVENT_TRIGGERS = ['click', 'drag'] as const;
9
+
10
+ const VIRTUAL_EDGE_ID = 'g6-create-edge-virtual-edge';
11
+ const DUMMY_NODE_ID = 'g6-create-edge-dummy-node';
12
+
13
+ // type Trigger = (typeof EVENT_TRIGGERS)[number];
14
+
15
+ interface CreateEdgeOptions {
16
+ /**
17
+ * The triggering conditions for this interaction can be either 'click' or 'drag'.
18
+ * Default to `click`.
19
+ */
20
+ trigger: (typeof EVENT_TRIGGERS)[number];
21
+
22
+ /**
23
+ * Keyboard keys serve as auxiliary triggers for this interaction.
24
+ * cound be 'shift', 'ctrl', 'control', 'alt', 'meta', undefined.
25
+ */
26
+ key?: (typeof KEYBOARD_TRIGGERS)[number];
27
+
28
+ /**
29
+ * Config of the created edge.
30
+ */
31
+ edgeConfig: EdgeDisplayModelData;
32
+ /**
33
+ * The event name to trigger after creating the virtual edge.
34
+ */
35
+ createVirtualEventName?: string;
36
+ /**
37
+ * The event name to trigger after creating the actual edge.
38
+ */
39
+ createActualEventName?: string;
40
+ /**
41
+ * The event name to trigger after canceling the behavior.
42
+ */
43
+ cancelCreateEventName?: string;
44
+ /**
45
+ * Whether allow the behavior happen on the current item.
46
+ */
47
+ shouldBegin: (event: IG6GraphEvent) => boolean;
48
+ /**
49
+ *
50
+ * Whether it is allowed to end the creation of edges under the current conditions being operated.
51
+ */
52
+ shouldEnd: (event: IG6GraphEvent) => boolean;
53
+ }
54
+
55
+ const DEFAULT_OPTIONS: CreateEdgeOptions = {
56
+ trigger: 'click',
57
+ key: undefined,
58
+ shouldBegin: () => true,
59
+ shouldEnd: () => false,
60
+ edgeConfig: {},
61
+ };
62
+
63
+ export class CreateEdge extends Behavior {
64
+ isKeyDown = false;
65
+ addingEdge = null;
66
+ dummyNode = null;
67
+
68
+ constructor(options: Partial<CreateEdgeOptions>) {
69
+ super(Object.assign({}, DEFAULT_OPTIONS, options));
70
+ this.validateOptions(options);
71
+ }
72
+
73
+ validateOptions(options: Partial<CreateEdgeOptions>) {
74
+ if (options.trigger && !EVENT_TRIGGERS.includes(options.trigger)) {
75
+ warn({
76
+ optionName: `create-edge.trigger`,
77
+ shouldBe: EVENT_TRIGGERS,
78
+ now: options.trigger,
79
+ scope: 'behavior',
80
+ });
81
+
82
+ this.options.trigger = DEFAULT_OPTIONS.trigger;
83
+ }
84
+
85
+ if (options.key && !KEYBOARD_TRIGGERS.includes(options.key)) {
86
+ warn({
87
+ optionName: `create-edge.key`,
88
+ shouldBe: KEYBOARD_TRIGGERS,
89
+ now: options.key,
90
+ scope: 'behavior',
91
+ });
92
+ this.options.key = DEFAULT_OPTIONS.key;
93
+ }
94
+ }
95
+
96
+ getEvents = () => {
97
+ const { trigger, key } = this.options;
98
+ const [CLICK_NAME] = EVENT_TRIGGERS;
99
+
100
+ const triggerEvents =
101
+ trigger === CLICK_NAME
102
+ ? {
103
+ 'node:click': this.handleCreateEdge,
104
+ pointermove: this.updateEndPoint,
105
+ 'edge:click': this.cancelCreating,
106
+ 'canvas:click': this.cancelCreating,
107
+ 'combo:click': this.handleCreateEdge,
108
+ }
109
+ : {
110
+ 'node:dragstart': this.handleCreateEdge,
111
+ 'combo:dragstart': this.handleCreateEdge,
112
+ drag: this.updateEndPoint,
113
+ drop: this.onDrop,
114
+ };
115
+
116
+ const keyboardEvents = key
117
+ ? {
118
+ keydown: this.onKeyDown,
119
+ keyup: this.onKeyUp,
120
+ }
121
+ : {};
122
+
123
+ return { ...triggerEvents, ...keyboardEvents } as Record<
124
+ string,
125
+ (e: IG6GraphEvent) => void
126
+ >;
127
+ };
128
+
129
+ handleCreateEdge = (e: IG6GraphEvent) => {
130
+ if (this.options.key && !this.isKeyDown) {
131
+ return;
132
+ }
133
+
134
+ if (this.options.shouldEnd(e)) {
135
+ return;
136
+ }
137
+
138
+ const { graph, options, addingEdge } = this;
139
+ const currentNodeId = e.itemId;
140
+
141
+ const { edgeConfig, createVirtualEventName, createActualEventName } =
142
+ options;
143
+
144
+ if (addingEdge) {
145
+ // create edge end, add the actual edge to graph and remove the virtual edge and node
146
+ graph.addData('edge', {
147
+ id: generateEdgeID(addingEdge.source, currentNodeId),
148
+ source: addingEdge.source,
149
+ target: currentNodeId,
150
+ data: {
151
+ ...edgeConfig,
152
+ type:
153
+ currentNodeId === addingEdge.source ? 'loop-edge' : edgeConfig.type,
154
+ },
155
+ });
156
+ if (createActualEventName) {
157
+ graph.emit(createActualEventName, { edge: addingEdge });
158
+ }
159
+ this.cancelCreating();
160
+
161
+ return;
162
+ }
163
+
164
+ this.dummyNode = graph.addData('node', {
165
+ id: DUMMY_NODE_ID,
166
+ data: {
167
+ x: e.canvas.x,
168
+ y: e.canvas.y,
169
+ keyShape: {
170
+ opacity: 0,
171
+ interactive: false,
172
+ },
173
+ labelShape: {
174
+ opacity: 0,
175
+ },
176
+ anchorPoints: [[0.5, 0.5]],
177
+ },
178
+ });
179
+ this.addingEdge = graph.addData('edge', {
180
+ id: VIRTUAL_EDGE_ID,
181
+ source: currentNodeId,
182
+ target: DUMMY_NODE_ID,
183
+ data: {
184
+ ...edgeConfig,
185
+ },
186
+ });
187
+ if (createVirtualEventName) {
188
+ graph.emit(createVirtualEventName, { edge: this.addingEdge });
189
+ }
190
+ };
191
+
192
+ onDrop = async (e: IG6GraphEvent) => {
193
+ const { addingEdge, options, graph } = this;
194
+
195
+ const { edgeConfig, key, createActualEventName } = options;
196
+ if (key && !this.isKeyDown) {
197
+ return;
198
+ }
199
+
200
+ if (!addingEdge) {
201
+ return;
202
+ }
203
+
204
+ const elements = await this.graph.canvas.document.elementsFromPoint(
205
+ e.canvas.x,
206
+ e.canvas.y,
207
+ );
208
+ const currentIds = elements
209
+ // @ts-ignore TODO: G type
210
+ .map((ele) => ele.parentNode.getAttribute?.('data-item-id'))
211
+ .filter((id) => id !== undefined && !DUMMY_NODE_ID !== id);
212
+ const dropId = currentIds.find(
213
+ (id) => this.graph.getComboData(id) || this.graph.getNodeData(id),
214
+ );
215
+
216
+ if (!dropId) {
217
+ this.cancelCreating();
218
+ return;
219
+ }
220
+
221
+ graph.addData('edge', {
222
+ id: generateEdgeID(addingEdge.source, dropId),
223
+ source: addingEdge.source,
224
+ target: dropId,
225
+ data: {
226
+ ...edgeConfig,
227
+ type: dropId === addingEdge.source ? 'loop-edge' : edgeConfig.type,
228
+ },
229
+ });
230
+ if (createActualEventName) {
231
+ graph.emit(createActualEventName, { edge: addingEdge });
232
+ }
233
+ this.cancelCreating();
234
+ };
235
+
236
+ updateEndPoint = (e: IG6GraphEvent) => {
237
+ const { options, graph, addingEdge, isKeyDown } = this;
238
+ if (options.key && !isKeyDown) {
239
+ return;
240
+ }
241
+
242
+ if (!addingEdge) {
243
+ return;
244
+ }
245
+
246
+ const sourceId = addingEdge.source,
247
+ targetId = addingEdge.target;
248
+
249
+ if (!graph.getItemById(sourceId)) {
250
+ this.addingEdge = null;
251
+ return;
252
+ }
253
+
254
+ graph.updatePosition('node', {
255
+ id: targetId,
256
+ data: {
257
+ x: e.canvas.x,
258
+ y: e.canvas.y,
259
+ },
260
+ });
261
+ };
262
+
263
+ cancelCreating = () => {
264
+ if (this.addingEdge) {
265
+ this.graph.removeData('edge', VIRTUAL_EDGE_ID);
266
+ this.addingEdge = null;
267
+ }
268
+ if (this.dummyNode) {
269
+ this.graph.removeData('node', DUMMY_NODE_ID);
270
+ this.dummyNode = null;
271
+ }
272
+ if (this.options.cancelCreateEventName) {
273
+ this.graph.emit(this.options.cancelCreateEventName, {});
274
+ }
275
+ };
276
+
277
+ onKeyDown = (e: KeyboardEvent) => {
278
+ const code = e.key;
279
+
280
+ if (!code) {
281
+ return;
282
+ }
283
+
284
+ if (code.toLocaleLowerCase() === this.options.key) {
285
+ this.isKeyDown = true;
286
+ }
287
+ };
288
+
289
+ onKeyUp = (e: IG6GraphEvent) => {
290
+ if (this.addingEdge) {
291
+ this.cancelCreating();
292
+ }
293
+ this.isKeyDown = false;
294
+ };
295
+ }
@@ -1,4 +1,5 @@
1
- import { ID, IG6GraphEvent } from 'types';
1
+ import { isNumber } from '@antv/util';
2
+ import { ID, IG6GraphEvent } from '../../types';
2
3
  import { Behavior } from '../../types/behavior';
3
4
  import { Point } from '../../types/common';
4
5
 
@@ -53,7 +54,7 @@ export interface DragCanvasOptions {
53
54
  }
54
55
 
55
56
  const DEFAULT_OPTIONS: Required<DragCanvasOptions> = {
56
- enableOptimize: false,
57
+ enableOptimize: undefined,
57
58
  dragOnItems: false,
58
59
  trigger: 'drag',
59
60
  direction: 'both',
@@ -132,25 +133,44 @@ export class DragCanvas extends Behavior {
132
133
 
133
134
  private hideShapes() {
134
135
  const { graph } = this;
135
- if (this.options.enableOptimize) {
136
+
137
+ const { tileBehavior: graphBehaviorOptimize, tileBehaviorSize = 1000 } =
138
+ graph.getSpecification().optimize || {};
139
+ const optimize = this.options.enableOptimize || graphBehaviorOptimize;
140
+ const shouldOptimize = isNumber(optimize)
141
+ ? graph.getAllNodesData().length > optimize
142
+ : optimize;
143
+
144
+ if (shouldOptimize) {
136
145
  this.hiddenEdgeIds = graph
137
146
  .getAllEdgesData()
138
147
  .map((edge) => edge.id)
139
148
  .filter((id) => graph.getItemVisible(id) === true);
140
- graph.executeWithoutStacking(() => {
141
- graph.hideItem(this.hiddenEdgeIds, true);
142
- this.hiddenNodeIds = graph
143
- .getAllNodesData()
144
- .map((node) => node.id)
145
- .filter((id) => graph.getItemVisible(id) === true);
146
- // draw node's keyShapes on transient, and then hidden the real nodes;
147
- // this.hiddenNodeIds.forEach((id) => {
148
- // graph.drawTransient('node', id, {
149
- // onlyDrawKeyShape: true,
150
- // });
151
- // });
152
- // graph.hideItem(this.hiddenNodeIds, true);
153
- });
149
+ this.hiddenNodeIds = graph
150
+ .getAllNodesData()
151
+ .map((node) => node.id)
152
+ .filter((id) => graph.getItemVisible(id) === true);
153
+ let requestId;
154
+ const hiddenIds = [...this.hiddenNodeIds];
155
+ const sectionNum = Math.ceil(hiddenIds.length / tileBehaviorSize);
156
+ const sections = Array.from({ length: sectionNum }, (v, i) =>
157
+ hiddenIds.slice(
158
+ i * tileBehaviorSize,
159
+ i * tileBehaviorSize + tileBehaviorSize,
160
+ ),
161
+ );
162
+ const update = () => {
163
+ if (!sections.length) {
164
+ cancelAnimationFrame(requestId);
165
+ return;
166
+ }
167
+ const section = sections.shift();
168
+ graph.executeWithNoStack(() => {
169
+ graph.hideItem(section, false, true);
170
+ });
171
+ requestId = requestAnimationFrame(update);
172
+ };
173
+ requestId = requestAnimationFrame(update);
154
174
  }
155
175
  }
156
176
 
@@ -241,19 +261,37 @@ export class DragCanvas extends Behavior {
241
261
  this.pointerDownAt = undefined;
242
262
  this.dragging = false;
243
263
 
244
- const { graph } = this;
245
- if (this.options.enableOptimize) {
246
- graph.startBatch();
247
- if (this.hiddenEdgeIds) {
248
- graph.showItem(this.hiddenEdgeIds, true);
264
+ const { graph, hiddenNodeIds, hiddenEdgeIds = [] } = this;
265
+ const { tileBehavior: graphBehaviorOptimize, tileBehaviorSize = 1000 } =
266
+ graph.getSpecification().optimize || {};
267
+ const optimize = this.options.enableOptimize || graphBehaviorOptimize;
268
+ const shouldOptimize = isNumber(optimize)
269
+ ? graph.getAllNodesData().length > optimize
270
+ : optimize;
271
+
272
+ if (shouldOptimize) {
273
+ if (hiddenNodeIds) {
274
+ let requestId;
275
+ const hiddenIds = [...hiddenNodeIds, ...hiddenEdgeIds];
276
+ const sectionNum = Math.ceil(hiddenIds.length / tileBehaviorSize);
277
+ const sections = Array.from({ length: sectionNum }, (v, i) =>
278
+ hiddenIds.slice(
279
+ i * tileBehaviorSize,
280
+ i * tileBehaviorSize + tileBehaviorSize,
281
+ ),
282
+ );
283
+ const update = () => {
284
+ if (!sections.length) {
285
+ cancelAnimationFrame(requestId);
286
+ return;
287
+ }
288
+ graph.startHistoryBatch();
289
+ graph.showItem(sections.shift(), false);
290
+ graph.stopHistoryBatch();
291
+ requestId = requestAnimationFrame(update);
292
+ };
293
+ requestId = requestAnimationFrame(update);
249
294
  }
250
- // if (this.hiddenNodeIds) {
251
- // this.hiddenNodeIds.forEach((id) => {
252
- // this.graph.drawTransient('node', id, { action: 'remove' });
253
- // });
254
- // graph.showItem(this.hiddenNodeIds, true);
255
- // }
256
- graph.stopBatch();
257
295
  }
258
296
  }
259
297
 
@@ -250,7 +250,7 @@ export class DragCombo extends Behavior {
250
250
  selectedComboIds,
251
251
  this.hiddenComboTreeRoots,
252
252
  );
253
- this.graph.executeWithoutStacking(() => {
253
+ this.graph.executeWithNoStack(() => {
254
254
  this.graph.hideItem(
255
255
  this.hiddenEdges.map((edge) => edge.id),
256
256
  true,
@@ -280,7 +280,7 @@ export class DragCombo extends Behavior {
280
280
  });
281
281
 
282
282
  // Hide original edges and nodes. They will be restored when pointerup.
283
- this.graph.executeWithoutStacking(() => {
283
+ this.graph.executeWithNoStack(() => {
284
284
  this.graph.hideItem(selectedComboIds, true);
285
285
  this.graph.hideItem(
286
286
  this.hiddenEdges.map((edge) => edge.id),
@@ -429,7 +429,7 @@ export class DragCombo extends Behavior {
429
429
  }
430
430
 
431
431
  public restoreHiddenItems(positions?: Position[]) {
432
- this.graph.pauseStacking();
432
+ this.graph.pauseStack();
433
433
  if (this.hiddenEdges.length) {
434
434
  this.graph.showItem(
435
435
  this.hiddenEdges.map((edge) => edge.id),
@@ -452,7 +452,7 @@ export class DragCombo extends Behavior {
452
452
  true,
453
453
  );
454
454
  }
455
- this.graph.resumeStacking();
455
+ this.graph.resumeStack();
456
456
  }
457
457
 
458
458
  public onPointerUp(event: IG6GraphEvent) {
@@ -526,8 +526,8 @@ export class DragCombo extends Behavior {
526
526
  this.originPositions = [];
527
527
  }
528
528
 
529
- public onDropNode(event: IG6GraphEvent) {
530
- const elements = this.graph.canvas.document.elementsFromPointSync(
529
+ public async onDropNode(event: IG6GraphEvent) {
530
+ const elements = await this.graph.canvas.document.elementsFromPoint(
531
531
  event.canvas.x,
532
532
  event.canvas.y,
533
533
  );
@@ -561,7 +561,7 @@ export class DragCombo extends Behavior {
561
561
 
562
562
  public onDropCombo(event: IG6GraphEvent) {
563
563
  const newParentId = event.itemId;
564
- this.graph.startBatch();
564
+ this.graph.startHistoryBatch();
565
565
  this.originPositions.forEach(({ id }) => {
566
566
  const model = this.graph.getComboData(id);
567
567
  if (!model || model.id === newParentId) return;
@@ -570,12 +570,12 @@ export class DragCombo extends Behavior {
570
570
  // event.stopPropagation();
571
571
  this.graph.updateData('combo', { id, data: { parentId: newParentId } });
572
572
  });
573
- this.graph.stopBatch();
573
+ this.graph.stopHistoryBatch();
574
574
  this.onPointerUp(event);
575
575
  }
576
576
 
577
577
  public onDropCanvas(event: IG6GraphEvent) {
578
- this.graph.startBatch();
578
+ this.graph.startHistoryBatch();
579
579
  this.originPositions.forEach(({ id }) => {
580
580
  const model = this.graph.getComboData(id);
581
581
  if (!model) return;
@@ -583,7 +583,7 @@ export class DragCombo extends Behavior {
583
583
  if (!parentId) return;
584
584
  this.graph.updateData('combo', { id, data: { parentId: undefined } });
585
585
  });
586
- this.graph.stopBatch();
586
+ this.graph.stopHistoryBatch();
587
587
  this.onPointerUp(event);
588
588
  }
589
589
  }
@@ -258,7 +258,7 @@ export class DragNode extends Behavior {
258
258
  this.selectedNodeIds,
259
259
  this.hiddenComboTreeItems,
260
260
  );
261
- this.graph.executeWithoutStacking(() => {
261
+ this.graph.executeWithNoStack(() => {
262
262
  this.graph.hideItem(
263
263
  this.hiddenEdges.map((edge) => edge.id),
264
264
  true,
@@ -292,7 +292,7 @@ export class DragNode extends Behavior {
292
292
  });
293
293
 
294
294
  // Hide original edges and nodes. They will be restored when pointerup.
295
- this.graph.executeWithoutStacking(() => {
295
+ this.graph.executeWithNoStack(() => {
296
296
  this.graph.hideItem(this.selectedNodeIds, true);
297
297
  this.graph.hideItem(
298
298
  this.hiddenEdges.map((edge) => edge.id),
@@ -457,7 +457,7 @@ export class DragNode extends Behavior {
457
457
  this.graph.drawTransient('rect', DELEGATE_SHAPE_ID, { action: 'remove' });
458
458
  }
459
459
 
460
- public clearTransientItems() {
460
+ public clearTransientItems(positions: Array<Position>) {
461
461
  this.hiddenEdges.forEach((edge) => {
462
462
  this.graph.drawTransient('node', edge.source, { action: 'remove' });
463
463
  this.graph.drawTransient('node', edge.target, { action: 'remove' });
@@ -472,13 +472,13 @@ export class DragNode extends Behavior {
472
472
  action: 'remove',
473
473
  });
474
474
  });
475
- this.originPositions.forEach(({ id }) => {
475
+ positions.forEach(({ id }) => {
476
476
  this.graph.drawTransient('node', id, { action: 'remove' });
477
477
  });
478
478
  }
479
479
 
480
480
  public restoreHiddenItems(positions?: Position[]) {
481
- this.graph.pauseStacking();
481
+ this.graph.pauseStack();
482
482
  if (this.hiddenEdges.length) {
483
483
  this.graph.showItem(
484
484
  this.hiddenEdges.map((edge) => edge.id),
@@ -508,7 +508,7 @@ export class DragNode extends Behavior {
508
508
  true,
509
509
  );
510
510
  }
511
- this.graph.resumeStacking();
511
+ this.graph.resumeStack();
512
512
  }
513
513
 
514
514
  public clearState() {
@@ -543,7 +543,7 @@ export class DragNode extends Behavior {
543
543
  debounce((positions) => {
544
544
  // restore the hidden items after move real nodes done
545
545
  if (enableTransient) {
546
- this.clearTransientItems();
546
+ this.clearTransientItems(positions);
547
547
  }
548
548
 
549
549
  if (this.options.enableDelegate) {
@@ -573,7 +573,7 @@ export class DragNode extends Behavior {
573
573
  return;
574
574
  }
575
575
  this.clearDelegate();
576
- this.clearTransientItems();
576
+ this.clearTransientItems(this.originPositions);
577
577
  this.restoreHiddenItems();
578
578
 
579
579
  const enableTransient =
@@ -589,8 +589,8 @@ export class DragNode extends Behavior {
589
589
  this.clearState();
590
590
  }
591
591
 
592
- public onDropNode(event: IG6GraphEvent) {
593
- const elements = this.graph.canvas.document.elementsFromPointSync(
592
+ public async onDropNode(event: IG6GraphEvent) {
593
+ const elements = await this.graph.canvas.document.elementsFromPoint(
594
594
  event.canvas.x,
595
595
  event.canvas.y,
596
596
  );
@@ -608,7 +608,7 @@ export class DragNode extends Behavior {
608
608
  ? this.graph.getNodeData(dropId).data.parentId
609
609
  : dropId;
610
610
 
611
- this.graph.startBatch();
611
+ this.graph.startHistoryBatch();
612
612
  this.originPositions.forEach(({ id }) => {
613
613
  const model = this.graph.getNodeData(id);
614
614
  if (!model) return;
@@ -621,13 +621,13 @@ export class DragNode extends Behavior {
621
621
  this.graph.updateData('node', { id, data: { parentId: newParentId } });
622
622
  });
623
623
  this.onPointerUp(event);
624
- this.graph.stopBatch();
624
+ this.graph.stopHistoryBatch();
625
625
  }
626
626
 
627
627
  public onDropCombo(event: IG6GraphEvent) {
628
628
  event.stopPropagation();
629
629
  const newParentId = event.itemId;
630
- this.graph.startBatch();
630
+ this.graph.startHistoryBatch();
631
631
  this.onPointerUp(event);
632
632
  this.originPositions.forEach(({ id }) => {
633
633
  const model = this.graph.getNodeData(id);
@@ -637,11 +637,11 @@ export class DragNode extends Behavior {
637
637
  this.graph.updateData('node', { id, data: { parentId: newParentId } });
638
638
  });
639
639
  this.clearState();
640
- this.graph.stopBatch();
640
+ this.graph.stopHistoryBatch();
641
641
  }
642
642
 
643
- public onDropCanvas(event: IG6GraphEvent) {
644
- const elements = this.graph.canvas.document.elementsFromPointSync(
643
+ public async onDropCanvas(event: IG6GraphEvent) {
644
+ const elements = await this.graph.canvas.document.elementsFromPoint(
645
645
  event.canvas.x,
646
646
  event.canvas.y,
647
647
  );
@@ -657,7 +657,7 @@ export class DragNode extends Behavior {
657
657
  const parentId = this.graph.getNodeData(dropId)
658
658
  ? this.graph.getNodeData(dropId).data.parentId
659
659
  : dropId;
660
- this.graph.startBatch();
660
+ this.graph.startHistoryBatch();
661
661
  this.onPointerUp(event);
662
662
  const nodeToUpdate = [];
663
663
  this.originPositions.forEach(({ id }) => {
@@ -671,6 +671,6 @@ export class DragNode extends Behavior {
671
671
  });
672
672
  if (nodeToUpdate.length) this.graph.updateData('node', nodeToUpdate);
673
673
  this.clearState();
674
- this.graph.stopBatch();
674
+ this.graph.stopHistoryBatch();
675
675
  }
676
676
  }
@@ -13,4 +13,6 @@ export * from './orbit-canvas-3d';
13
13
  export * from './rotate-canvas-3d';
14
14
  export * from './track-canvas-3d';
15
15
  export * from './zoom-canvas-3d';
16
+ export * from './create-edge';
16
17
  export * from './shortcuts-call';
18
+ export * from './scroll-canvas';