@antv/g6 5.0.6 → 5.0.8

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 (203) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/dist/lib/behaviors/click-select.d.ts +8 -16
  3. package/dist/lib/behaviors/drag-canvas.d.ts +4 -0
  4. package/dist/lib/elements/base-element.d.ts +5 -4
  5. package/dist/lib/plugins/fullscreen/index.d.ts +2 -0
  6. package/dist/lib/registry/index.d.ts +5 -1
  7. package/dist/lib/runtime/animation.d.ts +7 -0
  8. package/dist/lib/runtime/canvas.d.ts +27 -38
  9. package/dist/lib/runtime/element.d.ts +1 -1
  10. package/dist/lib/runtime/graph.d.ts +1 -17
  11. package/dist/lib/runtime/viewport.d.ts +1 -0
  12. package/dist/lib/types/point.d.ts +5 -0
  13. package/dist/lib/utils/point.d.ts +4 -4
  14. package/dist/lib/utils/print.d.ts +15 -0
  15. package/dist/lib/utils/shortcut.d.ts +1 -0
  16. package/dist/lib/utils/state.d.ts +9 -0
  17. package/dist/lib/version.d.ts +1 -1
  18. package/esm/behaviors/brush-select.js +1 -2
  19. package/esm/behaviors/brush-select.js.map +1 -1
  20. package/esm/behaviors/click-select.d.ts +8 -16
  21. package/esm/behaviors/click-select.js +108 -90
  22. package/esm/behaviors/click-select.js.map +1 -1
  23. package/esm/behaviors/drag-canvas.d.ts +4 -0
  24. package/esm/behaviors/drag-canvas.js +29 -9
  25. package/esm/behaviors/drag-canvas.js.map +1 -1
  26. package/esm/behaviors/drag-element-force.js +2 -1
  27. package/esm/behaviors/drag-element-force.js.map +1 -1
  28. package/esm/behaviors/drag-element.js +1 -2
  29. package/esm/behaviors/drag-element.js.map +1 -1
  30. package/esm/behaviors/zoom-canvas.js.map +1 -1
  31. package/esm/elements/base-element.d.ts +5 -4
  32. package/esm/elements/base-element.js +3 -0
  33. package/esm/elements/base-element.js.map +1 -1
  34. package/esm/elements/combos/base-combo.js +2 -2
  35. package/esm/elements/combos/base-combo.js.map +1 -1
  36. package/esm/elements/nodes/html.js +3 -5
  37. package/esm/elements/nodes/html.js.map +1 -1
  38. package/esm/elements/shapes/base-shape.js +27 -3
  39. package/esm/elements/shapes/base-shape.js.map +1 -1
  40. package/esm/plugins/contextmenu/index.js +5 -2
  41. package/esm/plugins/contextmenu/index.js.map +1 -1
  42. package/esm/plugins/fullscreen/index.d.ts +2 -0
  43. package/esm/plugins/fullscreen/index.js +15 -2
  44. package/esm/plugins/fullscreen/index.js.map +1 -1
  45. package/esm/registry/extension/index.js +2 -1
  46. package/esm/registry/extension/index.js.map +1 -1
  47. package/esm/registry/index.d.ts +5 -1
  48. package/esm/registry/index.js +8 -3
  49. package/esm/registry/index.js.map +1 -1
  50. package/esm/runtime/animation.d.ts +7 -0
  51. package/esm/runtime/animation.js +17 -0
  52. package/esm/runtime/animation.js.map +1 -1
  53. package/esm/runtime/canvas.d.ts +27 -38
  54. package/esm/runtime/canvas.js +128 -175
  55. package/esm/runtime/canvas.js.map +1 -1
  56. package/esm/runtime/data.js +4 -3
  57. package/esm/runtime/data.js.map +1 -1
  58. package/esm/runtime/element.d.ts +1 -1
  59. package/esm/runtime/element.js +2 -1
  60. package/esm/runtime/element.js.map +1 -1
  61. package/esm/runtime/graph.d.ts +1 -17
  62. package/esm/runtime/graph.js +8 -34
  63. package/esm/runtime/graph.js.map +1 -1
  64. package/esm/runtime/layout.js +5 -2
  65. package/esm/runtime/layout.js.map +1 -1
  66. package/esm/runtime/viewport.d.ts +1 -0
  67. package/esm/runtime/viewport.js +13 -3
  68. package/esm/runtime/viewport.js.map +1 -1
  69. package/esm/types/point.d.ts +5 -0
  70. package/esm/utils/animation.js +9 -3
  71. package/esm/utils/animation.js.map +1 -1
  72. package/esm/utils/id.js +2 -1
  73. package/esm/utils/id.js.map +1 -1
  74. package/esm/utils/layout.js +9 -1
  75. package/esm/utils/layout.js.map +1 -1
  76. package/esm/utils/palette.js +2 -1
  77. package/esm/utils/palette.js.map +1 -1
  78. package/esm/utils/point.d.ts +4 -4
  79. package/esm/utils/point.js +2 -2
  80. package/esm/utils/point.js.map +1 -1
  81. package/esm/utils/print.d.ts +15 -0
  82. package/esm/utils/print.js +28 -0
  83. package/esm/utils/print.js.map +1 -0
  84. package/esm/utils/shortcut.d.ts +1 -0
  85. package/esm/utils/shortcut.js +7 -0
  86. package/esm/utils/shortcut.js.map +1 -1
  87. package/esm/utils/state.d.ts +9 -0
  88. package/esm/utils/state.js +11 -0
  89. package/esm/utils/state.js.map +1 -0
  90. package/esm/utils/theme.js +5 -1
  91. package/esm/utils/theme.js.map +1 -1
  92. package/esm/version.d.ts +1 -1
  93. package/esm/version.js +1 -1
  94. package/lib/behaviors/brush-select.js +1 -2
  95. package/lib/behaviors/brush-select.js.map +1 -1
  96. package/lib/behaviors/click-select.d.ts +8 -16
  97. package/lib/behaviors/click-select.js +106 -88
  98. package/lib/behaviors/click-select.js.map +1 -1
  99. package/lib/behaviors/drag-canvas.d.ts +4 -0
  100. package/lib/behaviors/drag-canvas.js +28 -8
  101. package/lib/behaviors/drag-canvas.js.map +1 -1
  102. package/lib/behaviors/drag-element-force.js +2 -1
  103. package/lib/behaviors/drag-element-force.js.map +1 -1
  104. package/lib/behaviors/drag-element.js +1 -2
  105. package/lib/behaviors/drag-element.js.map +1 -1
  106. package/lib/behaviors/zoom-canvas.js.map +1 -1
  107. package/lib/elements/base-element.d.ts +5 -4
  108. package/lib/elements/base-element.js +3 -0
  109. package/lib/elements/base-element.js.map +1 -1
  110. package/lib/elements/combos/base-combo.js +2 -2
  111. package/lib/elements/combos/base-combo.js.map +1 -1
  112. package/lib/elements/nodes/html.js +3 -5
  113. package/lib/elements/nodes/html.js.map +1 -1
  114. package/lib/elements/shapes/base-shape.js +27 -3
  115. package/lib/elements/shapes/base-shape.js.map +1 -1
  116. package/lib/plugins/contextmenu/index.js +5 -2
  117. package/lib/plugins/contextmenu/index.js.map +1 -1
  118. package/lib/plugins/fullscreen/index.d.ts +2 -0
  119. package/lib/plugins/fullscreen/index.js +15 -2
  120. package/lib/plugins/fullscreen/index.js.map +1 -1
  121. package/lib/registry/extension/index.js +2 -1
  122. package/lib/registry/extension/index.js.map +1 -1
  123. package/lib/registry/index.d.ts +5 -1
  124. package/lib/registry/index.js +8 -3
  125. package/lib/registry/index.js.map +1 -1
  126. package/lib/runtime/animation.d.ts +7 -0
  127. package/lib/runtime/animation.js +17 -0
  128. package/lib/runtime/animation.js.map +1 -1
  129. package/lib/runtime/canvas.d.ts +27 -38
  130. package/lib/runtime/canvas.js +127 -174
  131. package/lib/runtime/canvas.js.map +1 -1
  132. package/lib/runtime/data.js +4 -3
  133. package/lib/runtime/data.js.map +1 -1
  134. package/lib/runtime/element.d.ts +1 -1
  135. package/lib/runtime/element.js +2 -1
  136. package/lib/runtime/element.js.map +1 -1
  137. package/lib/runtime/graph.d.ts +1 -17
  138. package/lib/runtime/graph.js +8 -34
  139. package/lib/runtime/graph.js.map +1 -1
  140. package/lib/runtime/layout.js +5 -2
  141. package/lib/runtime/layout.js.map +1 -1
  142. package/lib/runtime/viewport.d.ts +1 -0
  143. package/lib/runtime/viewport.js +13 -3
  144. package/lib/runtime/viewport.js.map +1 -1
  145. package/lib/types/point.d.ts +5 -0
  146. package/lib/utils/animation.js +8 -2
  147. package/lib/utils/animation.js.map +1 -1
  148. package/lib/utils/id.js +2 -1
  149. package/lib/utils/id.js.map +1 -1
  150. package/lib/utils/layout.js +9 -1
  151. package/lib/utils/layout.js.map +1 -1
  152. package/lib/utils/palette.js +2 -1
  153. package/lib/utils/palette.js.map +1 -1
  154. package/lib/utils/point.d.ts +4 -4
  155. package/lib/utils/point.js +2 -2
  156. package/lib/utils/point.js.map +1 -1
  157. package/lib/utils/print.d.ts +15 -0
  158. package/lib/utils/print.js +32 -0
  159. package/lib/utils/print.js.map +1 -0
  160. package/lib/utils/shortcut.d.ts +1 -0
  161. package/lib/utils/shortcut.js +7 -0
  162. package/lib/utils/shortcut.js.map +1 -1
  163. package/lib/utils/state.d.ts +9 -0
  164. package/lib/utils/state.js +14 -0
  165. package/lib/utils/state.js.map +1 -0
  166. package/lib/utils/theme.js +5 -1
  167. package/lib/utils/theme.js.map +1 -1
  168. package/lib/version.d.ts +1 -1
  169. package/lib/version.js +1 -1
  170. package/package.json +8 -8
  171. package/src/behaviors/brush-select.ts +1 -2
  172. package/src/behaviors/click-select.ts +117 -88
  173. package/src/behaviors/drag-canvas.ts +32 -9
  174. package/src/behaviors/drag-element-force.ts +2 -1
  175. package/src/behaviors/drag-element.ts +1 -2
  176. package/src/behaviors/zoom-canvas.ts +2 -3
  177. package/src/elements/base-element.ts +8 -4
  178. package/src/elements/combos/base-combo.ts +2 -2
  179. package/src/elements/nodes/html.ts +3 -7
  180. package/src/elements/shapes/base-shape.ts +30 -3
  181. package/src/global.d.ts +12 -0
  182. package/src/plugins/contextmenu/index.ts +5 -2
  183. package/src/plugins/fullscreen/index.ts +18 -2
  184. package/src/registry/extension/index.ts +2 -1
  185. package/src/registry/index.ts +8 -2
  186. package/src/runtime/animation.ts +21 -0
  187. package/src/runtime/canvas.ts +140 -208
  188. package/src/runtime/data.ts +4 -3
  189. package/src/runtime/element.ts +3 -2
  190. package/src/runtime/graph.ts +8 -38
  191. package/src/runtime/layout.ts +5 -2
  192. package/src/runtime/viewport.ts +18 -5
  193. package/src/types/point.ts +6 -0
  194. package/src/utils/animation.ts +9 -2
  195. package/src/utils/id.ts +2 -1
  196. package/src/utils/layout.ts +7 -1
  197. package/src/utils/palette.ts +2 -1
  198. package/src/utils/point.ts +6 -6
  199. package/src/utils/print.ts +30 -0
  200. package/src/utils/shortcut.ts +9 -0
  201. package/src/utils/state.ts +12 -0
  202. package/src/utils/theme.ts +6 -1
  203. package/src/version.ts +1 -1
@@ -1,13 +1,13 @@
1
1
  import { isFunction } from '@antv/util';
2
- import { CanvasEvent, CommonEvent, GraphEvent } from '../constants';
2
+ import { CanvasEvent, CommonEvent } from '../constants';
3
3
  import { ELEMENT_TYPES } from '../constants/element';
4
4
  import type { RuntimeContext } from '../runtime/types';
5
- import type { ElementType, ID, IPointerEvent, State } from '../types';
6
- import type { ElementLifeCycleEvent } from '../utils/event';
7
- import { idOf, idsOf } from '../utils/id';
5
+ import type { Element, ElementType, ID, IPointerEvent, State } from '../types';
6
+ import { idOf } from '../utils/id';
8
7
  import { getElementNthDegreeIds } from '../utils/relation';
9
8
  import type { ShortcutKey } from '../utils/shortcut';
10
9
  import { Shortcut } from '../utils/shortcut';
10
+ import { statesOf } from '../utils/state';
11
11
  import type { BaseBehaviorOptions } from './base-behavior';
12
12
  import { BaseBehavior } from './base-behavior';
13
13
 
@@ -108,10 +108,6 @@ export interface ClickSelectOptions extends BaseBehaviorOptions {
108
108
  * <en/> When the mouse clicks on an element, you can activate the state of the element, such as selecting nodes or edges. When the degree is 1, clicking on a node will highlight the current node and its directly adjacent nodes and edges.
109
109
  */
110
110
  export class ClickSelect extends BaseBehavior<ClickSelectOptions> {
111
- private select: Set<ID> = new Set<ID>();
112
-
113
- private neighbor: Set<ID> = new Set<ID>();
114
-
115
111
  private shortcut: Shortcut;
116
112
 
117
113
  static defaultOptions: Partial<ClickSelectOptions> = {
@@ -138,20 +134,17 @@ export class ClickSelect extends BaseBehavior<ClickSelectOptions> {
138
134
  graph.on(`${type}:${CommonEvent.CLICK}`, this.onClickSelect);
139
135
  });
140
136
  graph.on(CanvasEvent.CLICK, this.onClickCanvas);
141
- graph.on(GraphEvent.AFTER_ELEMENT_UPDATE, this.syncState);
142
137
  }
143
138
 
144
- private onClickSelect = (event: IPointerEvent) => {
139
+ private onClickSelect = async (event: IPointerEvent<Element>) => {
145
140
  if (!this.validate(event)) return;
146
- this.updateState(event);
141
+ await this.updateState(event);
147
142
  this.options.onClick?.(event);
148
143
  };
149
144
 
150
- private onClickCanvas = (event: IPointerEvent) => {
145
+ private onClickCanvas = async (event: IPointerEvent) => {
151
146
  if (!this.validate(event)) return;
152
- this.updateState(event);
153
- this.select.clear();
154
- this.neighbor.clear();
147
+ await this.clearState();
155
148
  this.options.onClick?.(event);
156
149
  };
157
150
 
@@ -160,96 +153,135 @@ export class ClickSelect extends BaseBehavior<ClickSelectOptions> {
160
153
  return multiple && this.shortcut.match(trigger);
161
154
  }
162
155
 
163
- /**
164
- * <zh/> syncState 会忽略因交互操作导致的状态更新
165
- *
166
- * <en/> syncState will ignore state updates caused by interactive operations
167
- */
168
- private updating = false;
156
+ private getNeighborIds(event: IPointerEvent<Element>) {
157
+ const { target, targetType } = event;
158
+ const { graph } = this.context;
159
+ const { degree } = this.options;
160
+ return getElementNthDegreeIds(
161
+ graph,
162
+ targetType as ElementType,
163
+ target.id,
164
+ typeof degree === 'function' ? degree(event) : degree,
165
+ ).filter((id) => id !== target.id);
166
+ }
169
167
 
170
- private async updateState(event: IPointerEvent) {
171
- const { state: select, unselectedState: unselect, neighborState: neighbor, animation, degree } = this.options;
172
- if (!select && !unselect) return;
168
+ private async updateState(event: IPointerEvent<Element>) {
169
+ const { state: selectState, unselectedState, neighborState, animation } = this.options;
170
+ if (!selectState && !neighborState && !unselectedState) return;
173
171
 
174
- const target = event.target;
172
+ const { target } = event;
175
173
  const { graph } = this.context;
176
174
 
177
- if ('id' in target) {
178
- const id = target.id;
179
- const datum = graph.getElementData(id);
180
- if (datum?.states?.includes(select)) {
181
- this.select.delete(id);
182
- } else {
183
- if (!this.isMultipleSelect) this.select.clear();
184
- this.select.add(id);
185
- }
186
- }
187
- // 点击了空白处 / click canvas
188
- else this.select.clear();
175
+ const datum = graph.getElementData(target.id);
176
+
177
+ const type = statesOf(datum).includes(selectState) ? 'unselect' : 'select';
189
178
 
190
179
  const states: Record<ID, State[]> = {};
191
180
 
192
- if (select) {
193
- const exclude = [unselect, neighbor];
194
- this.select.forEach((id) => {
195
- const state = graph.getElementState(id);
196
- states[id] = uniq([...state.filter((s) => !exclude.includes(s)), select]);
197
- });
198
- }
181
+ const isMultipleSelect = this.isMultipleSelect;
182
+
183
+ const click = [target.id];
184
+ const neighbor = this.getNeighborIds(event);
185
+
186
+ if (!isMultipleSelect) {
187
+ if (type === 'select') {
188
+ Object.assign(states, this.getClearStates(!!unselectedState));
189
+ const addState = (list: ID[], state: State) => {
190
+ list.forEach((id) => {
191
+ if (!states[id]) states[id] = [];
192
+ states[id].push(state);
193
+ });
194
+ };
195
+ addState(click, selectState);
196
+ addState(neighbor, neighborState);
197
+ if (unselectedState) {
198
+ Object.keys(states).forEach((id) => {
199
+ if (!click.includes(id) && !neighbor.includes(id)) states[id].push(unselectedState);
200
+ });
201
+ }
202
+ } else Object.assign(states, this.getClearStates());
203
+ } else {
204
+ Object.assign(states, this.getDataStates());
205
+
206
+ if (type === 'select') {
207
+ const addState = (list: ID[], state: State) => {
208
+ list.forEach((id) => {
209
+ const datum = graph.getElementData(id);
210
+ const dataStatesSet = new Set(statesOf(datum));
211
+ dataStatesSet.add(state);
212
+ dataStatesSet.delete(unselectedState);
213
+ states[id] = Array.from(dataStatesSet);
214
+ });
215
+ };
199
216
 
200
- const neighborIds = new Set<ID>();
201
- if (neighbor) {
202
- const d = typeof degree === 'function' ? degree(event) : degree;
203
- if (d) {
204
- const targetType = event.targetType as ElementType;
205
- this.select.forEach((id) => {
206
- getElementNthDegreeIds(graph, targetType, id, d).forEach((id) => {
207
- if (!this.select.has(id)) neighborIds.add(id);
217
+ addState(click, selectState);
218
+ addState(neighbor, neighborState);
219
+ if (unselectedState) {
220
+ Object.keys(states).forEach((id) => {
221
+ const _states = states[id];
222
+ if (
223
+ !_states.includes(selectState) &&
224
+ !_states.includes(neighborState) &&
225
+ !_states.includes(unselectedState)
226
+ ) {
227
+ states[id].push(unselectedState);
228
+ }
208
229
  });
230
+ }
231
+ } else {
232
+ const targetState = states[target.id];
233
+ states[target.id] = targetState.filter((s) => s !== selectState && s !== neighborState);
234
+ if (!targetState.includes(unselectedState)) states[target.id].push(unselectedState);
235
+ neighbor.forEach((id) => {
236
+ states[id] = states[id].filter((s) => s !== neighborState);
237
+ if (!states[id].includes(selectState)) states[id].push(unselectedState);
209
238
  });
210
239
  }
211
- const exclude = [select, unselect];
212
- neighborIds.forEach((id) => {
213
- const state = graph.getElementState(id);
214
- states[id] = uniq([...state.filter((s) => !exclude.includes(s)), neighbor]);
215
- });
216
240
  }
217
241
 
218
- const exclude = [select, neighbor, unselect];
219
- idsOf(graph.getData(), true).forEach((id) => {
220
- if (!this.select.has(id) && !neighborIds.has(id)) {
221
- const state = graph.getElementState(id);
222
- const filtered = state.filter((s) => !exclude.includes(s));
223
- // 仅在有选中元素时应用 unselect 状态
224
- // Apply unselect state only when there are selected elements
225
- if (unselect && this.select.size) states[id] = uniq([...filtered, unselect]);
226
- else states[id] = filtered;
227
- }
242
+ await graph.setElementState(states, animation);
243
+ }
244
+
245
+ private getDataStates() {
246
+ const { graph } = this.context;
247
+ const { nodes, edges, combos } = graph.getData();
248
+
249
+ const states: Record<ID, State[]> = {};
250
+ [...nodes, ...edges, ...combos].forEach((data) => {
251
+ states[idOf(data)] = statesOf(data);
228
252
  });
229
253
 
230
- this.updating = true;
231
- await graph.setElementState(states, animation);
232
- this.updating = false;
254
+ return states;
233
255
  }
234
256
 
235
257
  /**
236
- * <zh/> 同步状态
258
+ * <zh/> 获取需要清除的状态
237
259
  *
238
- * <en/> Sync state
239
- * @remarks
240
- * <zh/> 避免其他操作更新状态后,this.select 与实际状态不一致
241
- *
242
- * <en/> Avoid inconsistency between this.select and the actual state after other operations update the state
243
- * @param event - <zh/> 元素生命周期事件 | <en/> Element life cycle event
260
+ * <en/> Get the states that need to be cleared
261
+ * @param complete - <zh/> 是否返回所有状态 | <en/> Whether to return all states
262
+ * @returns - <zh/> 需要清除的状态 | <en/> States that need to be cleared
244
263
  */
245
- private syncState = (event: ElementLifeCycleEvent) => {
246
- if (this.updating) return;
247
- const { data } = event;
248
- const id = idOf(data);
249
- const states = data.states || [];
250
- if (states.includes(this.options.state)) this.select.add(id);
251
- else this.select.delete(id);
252
- };
264
+ private getClearStates(complete = false) {
265
+ const { graph } = this.context;
266
+ const { state, unselectedState, neighborState } = this.options;
267
+ const statesToClear = new Set([state, unselectedState, neighborState]);
268
+ const { nodes, edges, combos } = graph.getData();
269
+
270
+ const states: Record<ID, State[]> = {};
271
+ [...nodes, ...edges, ...combos].forEach((data) => {
272
+ const datumStates = statesOf(data);
273
+ const newStates = datumStates.filter((s) => !statesToClear.has(s));
274
+ if (complete) states[idOf(data)] = newStates;
275
+ else if (newStates.length !== datumStates.length) states[idOf(data)] = newStates;
276
+ });
277
+
278
+ return states;
279
+ }
280
+
281
+ private async clearState() {
282
+ const { graph } = this.context;
283
+ await graph.setElementState(this.getClearStates(), this.options.animation);
284
+ }
253
285
 
254
286
  private validate(event: IPointerEvent) {
255
287
  if (this.destroyed) return false;
@@ -265,7 +297,6 @@ export class ClickSelect extends BaseBehavior<ClickSelectOptions> {
265
297
  graph.off(`${type}:${CommonEvent.CLICK}`, this.onClickSelect);
266
298
  });
267
299
  graph.off(CanvasEvent.CLICK, this.onClickCanvas);
268
- graph.off(GraphEvent.AFTER_ELEMENT_UPDATE, this.syncState);
269
300
  }
270
301
 
271
302
  public destroy() {
@@ -273,5 +304,3 @@ export class ClickSelect extends BaseBehavior<ClickSelectOptions> {
273
304
  super.destroy();
274
305
  }
275
306
  }
276
-
277
- const uniq = <T>(array: T[]): T[] => Array.from(new Set(array));
@@ -1,5 +1,5 @@
1
1
  import type { Cursor } from '@antv/g';
2
- import { isFunction, isObject } from '@antv/util';
2
+ import { debounce, isFunction, isObject } from '@antv/util';
3
3
  import { CanvasEvent } from '../constants';
4
4
  import type { RuntimeContext } from '../runtime/types';
5
5
  import type { IKeyboardEvent, IPointerEvent, Vector2, ViewportAnimationEffectTiming } from '../types';
@@ -76,7 +76,6 @@ export class DragCanvas extends BaseBehavior<DragCanvasOptions> {
76
76
 
77
77
  this.bindEvents();
78
78
  this.defaultCursor = this.context.canvas.getConfig().cursor || 'default';
79
- context.canvas.setCursor('grab');
80
79
  }
81
80
 
82
81
  /**
@@ -97,7 +96,6 @@ export class DragCanvas extends BaseBehavior<DragCanvasOptions> {
97
96
  const { graph } = this.context;
98
97
 
99
98
  if (isObject(trigger)) {
100
- graph.off(CanvasEvent.DRAG, this.onDrag);
101
99
  const { up = [], down = [], left = [], right = [] } = trigger;
102
100
 
103
101
  this.shortcut.bind(up, (event) => this.onTranslate([0, 1], event));
@@ -105,24 +103,46 @@ export class DragCanvas extends BaseBehavior<DragCanvasOptions> {
105
103
  this.shortcut.bind(left, (event) => this.onTranslate([1, 0], event));
106
104
  this.shortcut.bind(right, (event) => this.onTranslate([-1, 0], event));
107
105
  } else {
106
+ graph.on(CanvasEvent.DRAG_START, this.onDragStart);
108
107
  graph.on(CanvasEvent.DRAG, this.onDrag);
108
+ graph.on(CanvasEvent.DRAG_END, this.onDragEnd);
109
109
  }
110
110
  }
111
111
 
112
- private onDrag = (event: IPointerEvent) => {
112
+ private isDragging = false;
113
+
114
+ private onDragStart = (event: IPointerEvent) => {
113
115
  if (!this.validate(event)) return;
114
- if (event.targetType === 'canvas') {
115
- this.translate([event.movement.x, event.movement.y], false);
116
- this.options.onFinish?.();
116
+ if (event.targetType !== 'canvas') return;
117
+ this.isDragging = true;
118
+ this.context.canvas.setCursor('grabbing');
119
+ };
120
+
121
+ private onDrag = (event: IPointerEvent) => {
122
+ if (!this.isDragging) return;
123
+ const { x, y } = event.movement;
124
+ if ((x | y) !== 0) {
125
+ this.translate([x, y], false);
117
126
  }
118
127
  };
119
128
 
129
+ private onDragEnd = () => {
130
+ this.isDragging = false;
131
+ this.context.canvas.setCursor(this.defaultCursor);
132
+ this.options.onFinish?.();
133
+ };
134
+
135
+ private invokeOnFinish = debounce(() => {
136
+ this.options.onFinish?.();
137
+ }, 300);
138
+
120
139
  private async onTranslate(value: Vector2, event: IPointerEvent | IKeyboardEvent) {
121
140
  if (!this.validate(event)) return;
122
141
  const { sensitivity } = this.options;
123
142
  const delta = sensitivity * -1;
124
143
  await this.translate(multiply(value, delta) as Vector2, this.options.animation);
125
- this.options.onFinish?.();
144
+
145
+ this.invokeOnFinish();
126
146
  }
127
147
 
128
148
  /**
@@ -146,7 +166,10 @@ export class DragCanvas extends BaseBehavior<DragCanvasOptions> {
146
166
 
147
167
  private unbindEvents() {
148
168
  this.shortcut.unbindAll();
149
- this.context.graph.off(CanvasEvent.DRAG, this.onDrag);
169
+ const { graph } = this.context;
170
+ graph.off(CanvasEvent.DRAG_START, this.onDragStart);
171
+ graph.off(CanvasEvent.DRAG, this.onDrag);
172
+ graph.off(CanvasEvent.DRAG_END, this.onDragEnd);
150
173
  }
151
174
 
152
175
  public destroy(): void {
@@ -1,6 +1,7 @@
1
1
  import type { ID, IElementDragEvent, Point } from '../types';
2
2
  import { idOf } from '../utils/id';
3
3
  import { getLayoutProperty, invokeLayoutMethod } from '../utils/layout';
4
+ import { print } from '../utils/print';
4
5
  import { add } from '../utils/vector';
5
6
  import type { DragElementOptions } from './drag-element';
6
7
  import { DragElement } from './drag-element';
@@ -37,7 +38,7 @@ export class DragElementForce extends DragElement {
37
38
 
38
39
  // 未使用力导布局 / The force layout is not used
39
40
  if (!this.forceLayoutInstance) {
40
- console.warn('DragElementForce only works with d3-force or d3-force-3d layout');
41
+ print.warn('DragElementForce only works with d3-force or d3-force-3d layout');
41
42
  return false;
42
43
  }
43
44
 
@@ -376,11 +376,10 @@ export class DragElement extends BaseBehavior<DragElementOptions> {
376
376
  } else {
377
377
  this.shadow = new Rect({
378
378
  style: {
379
+ $layer: 'transient',
379
380
  ...shadowStyle,
380
381
  ...positionStyle,
381
382
  pointerEvents: 'none',
382
- // @ts-expect-error $layer is not in the type definition
383
- $layer: 'transient',
384
383
  },
385
384
  });
386
385
  this.context.canvas.appendChild(this.shadow);
@@ -1,8 +1,7 @@
1
- import type { PointLike } from '@antv/g';
2
1
  import { clamp, isArray, isFunction, isObject } from '@antv/util';
3
2
  import { CommonEvent } from '../constants';
4
3
  import type { RuntimeContext } from '../runtime/types';
5
- import type { IKeyboardEvent, IWheelEvent, Point, ViewportAnimationEffectTiming } from '../types';
4
+ import type { IKeyboardEvent, IWheelEvent, Point, PointObject, ViewportAnimationEffectTiming } from '../types';
6
5
  import { parsePoint } from '../utils/point';
7
6
  import type { ShortcutKey } from '../utils/shortcut';
8
7
  import { Shortcut } from '../utils/shortcut';
@@ -141,7 +140,7 @@ export class ZoomCanvas extends BaseBehavior<ZoomCanvasOptions> {
141
140
 
142
141
  let origin: Point | undefined;
143
142
  if ('viewport' in event) {
144
- origin = parsePoint(event.viewport as PointLike);
143
+ origin = parsePoint(event.viewport as PointObject);
145
144
  }
146
145
 
147
146
  const { sensitivity, onFinish } = this.options;
@@ -1,9 +1,13 @@
1
- import { IAnimation } from '@antv/g';
2
- import { Keyframe } from '../types';
3
- import type { BaseShapeStyleProps } from './shapes';
1
+ import type { IAnimation } from '@antv/g';
2
+ import type { RuntimeContext } from '../runtime/types';
3
+ import type { BaseElementStyleProps, Keyframe } from '../types';
4
4
  import { BaseShape } from './shapes';
5
5
 
6
- export abstract class BaseElement<T extends BaseShapeStyleProps> extends BaseShape<T> {
6
+ export abstract class BaseElement<T extends BaseElementStyleProps> extends BaseShape<T> {
7
+ protected get context(): RuntimeContext {
8
+ return this.attributes.context!;
9
+ }
10
+
7
11
  protected get parsedAttributes() {
8
12
  return this.attributes as Required<T>;
9
13
  }
@@ -194,7 +194,7 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
194
194
  }
195
195
 
196
196
  protected getComboZIndex(attributes: Required<S>): number {
197
- const ancestors = attributes.context!.model.getAncestorsData(this.id, COMBO_KEY) || [];
197
+ const ancestors = this.context.model.getAncestorsData(this.id, COMBO_KEY) || [];
198
198
  return ancestors.length;
199
199
  }
200
200
 
@@ -231,7 +231,7 @@ export abstract class BaseCombo<S extends BaseComboStyleProps = BaseComboStylePr
231
231
  Object.assign(this.style, comboStyle);
232
232
  // Sync combo position to model
233
233
  const { x, y } = comboStyle;
234
- attributes.context!.model.syncComboDatum({ id: this.id, style: { x, y } });
234
+ this.context.model.syncComboDatum({ id: this.id, style: { x, y } });
235
235
  }
236
236
 
237
237
  public render(attributes: Required<S>, container: Group = this) {
@@ -50,7 +50,7 @@ export class HTML extends BaseNode<HTMLStyleProps> {
50
50
  private rootPointerEvent = new FederatedPointerEvent(null);
51
51
 
52
52
  private get eventService() {
53
- return this.attributes.context!.canvas.context.eventService;
53
+ return this.context.canvas.context.eventService;
54
54
  }
55
55
 
56
56
  private get events() {
@@ -105,7 +105,7 @@ export class HTML extends BaseNode<HTMLStyleProps> {
105
105
  }
106
106
 
107
107
  private forwardEvents = (nativeEvent: PointerEvent) => {
108
- const canvas = this.attributes.context!.canvas.main;
108
+ const canvas = this.context.canvas;
109
109
  const iCanvas = canvas.context.renderingContext.root!.ownerDocument!.defaultView!;
110
110
 
111
111
  const normalizedEvents = this.normalizeToPointerEvent(nativeEvent, iCanvas);
@@ -232,11 +232,7 @@ export class HTML extends BaseNode<HTMLStyleProps> {
232
232
  let x: number;
233
233
  let y: number;
234
234
  const { offsetX, offsetY, clientX, clientY } = nativeEvent;
235
- if (
236
- this.attributes.context?.canvas.main.context.config.supportsCSSTransform &&
237
- !isNil(offsetX) &&
238
- !isNil(offsetY)
239
- ) {
235
+ if (this.context.canvas.context.config.supportsCSSTransform && !isNil(offsetX) && !isNil(offsetY)) {
240
236
  x = offsetX;
241
237
  y = offsetY;
242
238
  } else {
@@ -152,7 +152,10 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
152
152
  }
153
153
 
154
154
  const result = super.animate(keyframes, options);
155
- if (result) animationMap.push(result);
155
+ if (result) {
156
+ releaseAnimation(this, result);
157
+ animationMap.push(result);
158
+ }
156
159
 
157
160
  if (Array.isArray(keyframes) && keyframes.length > 0) {
158
161
  // 如果 keyframes 中仅存在 skippedAttrs 中的属性,则仅更新父元素属性(跳过子图形)
@@ -170,7 +173,10 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
170
173
  method.call(this, { ...this.attributes, ...style }),
171
174
  );
172
175
  const result = shape.animate(preprocessKeyframes(subKeyframes), options);
173
- if (result) animationMap.push(result);
176
+ if (result) {
177
+ releaseAnimation(shape, result);
178
+ animationMap.push(result);
179
+ }
174
180
  }
175
181
  });
176
182
 
@@ -185,7 +191,10 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
185
191
  const shape = shapeSet[key];
186
192
  if (shape) {
187
193
  const result = shape.animate(preprocessKeyframes(subKeyframes), options);
188
- if (result) animationMap.push(result);
194
+ if (result) {
195
+ releaseAnimation(shape, result);
196
+ animationMap.push(result);
197
+ }
189
198
  }
190
199
  });
191
200
  }
@@ -217,3 +226,21 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
217
226
  super.destroy();
218
227
  }
219
228
  }
229
+
230
+ /**
231
+ * <zh/> 释放动画
232
+ *
233
+ * <en/> Release animation
234
+ * @param target - <zh/> 目标对象 | <en/> target object
235
+ * @param animation - <zh/> 动画实例 | <en/> animation instance
236
+ * @description see: https://github.com/antvis/G/issues/1731
237
+ */
238
+ function releaseAnimation(target: DisplayObject, animation: IAnimation) {
239
+ animation.oncancel = () => console.log('cancel');
240
+ animation?.finished.then(() => {
241
+ // @ts-expect-error private property
242
+ const index = target.activeAnimations.findIndex((_) => _ === animation);
243
+ // @ts-expect-error private property
244
+ if (index > -1) target.activeAnimations.splice(index, 1);
245
+ });
246
+ }
@@ -0,0 +1,12 @@
1
+ import '@antv/g';
2
+
3
+ declare module '@antv/g' {
4
+ interface BaseStyleProps {
5
+ /**
6
+ * <zh/> 图形所在的图层,默认为 'main'。
7
+ *
8
+ * <en/> The layer where the shape is located, default is 'main'.
9
+ */
10
+ $layer?: string;
11
+ }
12
+ }
@@ -126,6 +126,7 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
126
126
  const content = await this.getDOMContent(event);
127
127
 
128
128
  if (content instanceof HTMLElement) {
129
+ this.$element.innerHTML = '';
129
130
  this.$element.appendChild(content);
130
131
  } else {
131
132
  this.$element.innerHTML = content;
@@ -213,13 +214,15 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
213
214
  };
214
215
 
215
216
  private onMenuItemClick = (event: MouseEvent) => {
216
- const { onClick } = this.options;
217
+ const { onClick, trigger } = this.options;
217
218
  if (event.target instanceof HTMLElement) {
218
219
  if (event.target.className.includes('g6-contextmenu-li')) {
219
220
  const value = event.target.getAttribute('value') as string;
220
221
  onClick?.(value, event.target);
222
+ this.hide();
221
223
  }
222
224
  }
223
- this.hide();
225
+
226
+ if (trigger !== 'click') this.hide();
224
227
  };
225
228
  }
@@ -1,4 +1,5 @@
1
1
  import type { RuntimeContext } from '../../runtime/types';
2
+ import { print } from '../../utils/print';
2
3
  import type { ShortcutKey } from '../../utils/shortcut';
3
4
  import { Shortcut } from '../../utils/shortcut';
4
5
  import type { BasePluginOptions } from '../base-plugin';
@@ -57,7 +58,10 @@ export class Fullscreen extends BasePlugin<FullscreenOptions> {
57
58
 
58
59
  private shortcut: Shortcut;
59
60
 
61
+ private style: HTMLStyleElement;
62
+
60
63
  private $el = this.context.canvas.getContainer()!;
64
+
61
65
  private graphSize: [number, number] = [0, 0];
62
66
 
63
67
  constructor(context: RuntimeContext, options: FullscreenOptions) {
@@ -67,7 +71,13 @@ export class Fullscreen extends BasePlugin<FullscreenOptions> {
67
71
 
68
72
  this.bindEvents();
69
73
 
70
- this.$el.style.backgroundColor = this.context.graph.getBackground()!;
74
+ this.style = document.createElement('style');
75
+ document.head.appendChild(this.style);
76
+ this.style.innerHTML = `
77
+ :not(:root):fullscreen::backdrop {
78
+ background: transparent;
79
+ }
80
+ `;
71
81
  }
72
82
 
73
83
  private bindEvents() {
@@ -123,7 +133,7 @@ export class Fullscreen extends BasePlugin<FullscreenOptions> {
123
133
  public request() {
124
134
  if (document.fullscreenElement || !isFullscreenEnabled()) return;
125
135
  this.$el.requestFullscreen().catch((err: Error) => {
126
- console.debug(`Error attempting to enable full-screen: ${err.message} (${err.name})`);
136
+ print.warn(`Error attempting to enable full-screen: ${err.message} (${err.name})`);
127
137
  });
128
138
  }
129
139
 
@@ -149,6 +159,12 @@ export class Fullscreen extends BasePlugin<FullscreenOptions> {
149
159
  super.update(options);
150
160
  this.bindEvents();
151
161
  }
162
+
163
+ public destroy(): void {
164
+ this.exit();
165
+ this.style.remove();
166
+ super.destroy();
167
+ }
152
168
  }
153
169
 
154
170
  /**
@@ -5,6 +5,7 @@ import type { RuntimeContext } from '../../runtime/types';
5
5
  import type { IEvent } from '../../types';
6
6
  import { arrayDiff } from '../../utils/diff';
7
7
  import { parseExtensions } from '../../utils/extension';
8
+ import { print } from '../../utils/print';
8
9
  import type { STDExtensionOption } from './types';
9
10
 
10
11
  export abstract class ExtensionController<E extends BaseExtension<any>> {
@@ -42,7 +43,7 @@ export abstract class ExtensionController<E extends BaseExtension<any>> {
42
43
 
43
44
  const { key, type } = extension;
44
45
  const Ctor = getExtension(category, type);
45
- if (!Ctor) return;
46
+ if (!Ctor) return print.warn(`The extension ${type} of ${category} is not registered.`);
46
47
 
47
48
  const instance = new Ctor(this.context, extension);
48
49
  this.extensionMap[key] = instance as E;