@antv/g6 5.0.13 → 5.0.15

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 (102) hide show
  1. package/dist/g6.min.js +2 -2
  2. package/dist/lib/behaviors/drag-canvas.d.ts +0 -1
  3. package/dist/lib/elements/shapes/badge.d.ts +1 -1
  4. package/dist/lib/elements/shapes/base-shape.d.ts +1 -1
  5. package/dist/lib/exports.d.ts +1 -0
  6. package/dist/lib/plugins/contextmenu/index.d.ts +3 -1
  7. package/dist/lib/plugins/minimap/index.d.ts +2 -0
  8. package/dist/lib/runtime/canvas.d.ts +17 -1
  9. package/dist/lib/spec/canvas.d.ts +7 -1
  10. package/dist/lib/utils/style.d.ts +1 -1
  11. package/dist/lib/utils/visibility.d.ts +2 -2
  12. package/dist/lib/version.d.ts +1 -1
  13. package/esm/behaviors/click-select.js +2 -3
  14. package/esm/behaviors/click-select.js.map +1 -1
  15. package/esm/behaviors/drag-canvas.d.ts +0 -1
  16. package/esm/behaviors/drag-canvas.js +7 -10
  17. package/esm/behaviors/drag-canvas.js.map +1 -1
  18. package/esm/behaviors/optimize-viewport-transform.js +1 -1
  19. package/esm/behaviors/optimize-viewport-transform.js.map +1 -1
  20. package/esm/elements/shapes/badge.d.ts +1 -1
  21. package/esm/elements/shapes/base-shape.d.ts +1 -1
  22. package/esm/elements/shapes/base-shape.js.map +1 -1
  23. package/esm/exports.d.ts +1 -0
  24. package/esm/exports.js +1 -0
  25. package/esm/exports.js.map +1 -1
  26. package/esm/plugins/contextmenu/index.d.ts +3 -1
  27. package/esm/plugins/contextmenu/index.js +4 -1
  28. package/esm/plugins/contextmenu/index.js.map +1 -1
  29. package/esm/plugins/history/util.js +1 -1
  30. package/esm/plugins/history/util.js.map +1 -1
  31. package/esm/plugins/minimap/index.d.ts +2 -0
  32. package/esm/plugins/minimap/index.js +24 -12
  33. package/esm/plugins/minimap/index.js.map +1 -1
  34. package/esm/runtime/canvas.d.ts +17 -1
  35. package/esm/runtime/canvas.js +26 -3
  36. package/esm/runtime/canvas.js.map +1 -1
  37. package/esm/runtime/element.js +1 -1
  38. package/esm/runtime/element.js.map +1 -1
  39. package/esm/runtime/graph.js +7 -2
  40. package/esm/runtime/graph.js.map +1 -1
  41. package/esm/spec/canvas.d.ts +7 -1
  42. package/esm/utils/style.d.ts +1 -1
  43. package/esm/utils/style.js +1 -1
  44. package/esm/utils/style.js.map +1 -1
  45. package/esm/utils/visibility.d.ts +2 -2
  46. package/esm/utils/visibility.js +35 -18
  47. package/esm/utils/visibility.js.map +1 -1
  48. package/esm/version.d.ts +1 -1
  49. package/esm/version.js +1 -1
  50. package/lib/behaviors/click-select.js +2 -3
  51. package/lib/behaviors/click-select.js.map +1 -1
  52. package/lib/behaviors/drag-canvas.d.ts +0 -1
  53. package/lib/behaviors/drag-canvas.js +6 -9
  54. package/lib/behaviors/drag-canvas.js.map +1 -1
  55. package/lib/behaviors/optimize-viewport-transform.js +1 -1
  56. package/lib/behaviors/optimize-viewport-transform.js.map +1 -1
  57. package/lib/elements/shapes/badge.d.ts +1 -1
  58. package/lib/elements/shapes/base-shape.d.ts +1 -1
  59. package/lib/elements/shapes/base-shape.js.map +1 -1
  60. package/lib/exports.d.ts +1 -0
  61. package/lib/exports.js +3 -1
  62. package/lib/exports.js.map +1 -1
  63. package/lib/plugins/contextmenu/index.d.ts +3 -1
  64. package/lib/plugins/contextmenu/index.js +4 -1
  65. package/lib/plugins/contextmenu/index.js.map +1 -1
  66. package/lib/plugins/history/util.js +1 -1
  67. package/lib/plugins/history/util.js.map +1 -1
  68. package/lib/plugins/minimap/index.d.ts +2 -0
  69. package/lib/plugins/minimap/index.js +24 -12
  70. package/lib/plugins/minimap/index.js.map +1 -1
  71. package/lib/runtime/canvas.d.ts +17 -1
  72. package/lib/runtime/canvas.js +26 -3
  73. package/lib/runtime/canvas.js.map +1 -1
  74. package/lib/runtime/element.js +1 -1
  75. package/lib/runtime/element.js.map +1 -1
  76. package/lib/runtime/graph.js +7 -2
  77. package/lib/runtime/graph.js.map +1 -1
  78. package/lib/spec/canvas.d.ts +7 -1
  79. package/lib/utils/style.d.ts +1 -1
  80. package/lib/utils/style.js +1 -1
  81. package/lib/utils/style.js.map +1 -1
  82. package/lib/utils/visibility.d.ts +2 -2
  83. package/lib/utils/visibility.js +35 -18
  84. package/lib/utils/visibility.js.map +1 -1
  85. package/lib/version.d.ts +1 -1
  86. package/lib/version.js +1 -1
  87. package/package.json +1 -1
  88. package/src/behaviors/click-select.ts +2 -3
  89. package/src/behaviors/drag-canvas.ts +7 -11
  90. package/src/behaviors/optimize-viewport-transform.ts +1 -1
  91. package/src/elements/shapes/base-shape.ts +4 -1
  92. package/src/exports.ts +1 -0
  93. package/src/plugins/contextmenu/index.ts +8 -3
  94. package/src/plugins/history/util.ts +1 -1
  95. package/src/plugins/minimap/index.ts +28 -15
  96. package/src/runtime/canvas.ts +26 -3
  97. package/src/runtime/element.ts +1 -1
  98. package/src/runtime/graph.ts +6 -3
  99. package/src/spec/canvas.ts +7 -1
  100. package/src/utils/style.ts +5 -2
  101. package/src/utils/visibility.ts +33 -16
  102. package/src/version.ts +1 -1
@@ -1,11 +1,11 @@
1
1
  import type { RuntimeContext } from '../../runtime/types';
2
+ import type { Element } from '../../types';
2
3
  import type { IElementEvent } from '../../types/event';
3
4
  import { createPluginContainer, insertDOM } from '../../utils/dom';
4
5
  import type { BasePluginOptions } from '../base-plugin';
5
6
  import { BasePlugin } from '../base-plugin';
6
7
  import type { Item } from './util';
7
8
  import { CONTEXTMENU_CSS, getContentFromItems } from './util';
8
-
9
9
  /**
10
10
  * <zh/> 上下文菜单配置项
11
11
  *
@@ -42,7 +42,7 @@ export interface ContextmenuOptions extends BasePluginOptions {
42
42
  *
43
43
  * <en/> The callback method triggered when the menu is clicked
44
44
  */
45
- onClick?: (value: string, target: HTMLElement) => void;
45
+ onClick?: (value: string, target: HTMLElement, current: Element) => void;
46
46
  /**
47
47
  * <zh/> 返回菜单的项目列表,支持 `Promise` 类型的返回值。是 `getContent` 的快捷配置
48
48
  *
@@ -90,6 +90,8 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
90
90
 
91
91
  private $element!: HTMLElement;
92
92
 
93
+ private targetElement: Element | null = null;
94
+
93
95
  constructor(context: RuntimeContext, options: ContextmenuOptions) {
94
96
  super(context, Object.assign({}, Contextmenu.defaultOptions, options));
95
97
 
@@ -138,6 +140,8 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
138
140
  this.$element.style.left = `${event.client.x - clientRect.left + offset[0]}px`;
139
141
  this.$element.style.top = `${event.client.y - clientRect.top + offset[1]}px`;
140
142
  this.$element.style.display = 'block';
143
+
144
+ this.targetElement = event.target;
141
145
  }
142
146
 
143
147
  /**
@@ -147,6 +151,7 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
147
151
  */
148
152
  public hide() {
149
153
  this.$element.style.display = 'none';
154
+ this.targetElement = null;
150
155
  }
151
156
 
152
157
  /**
@@ -218,7 +223,7 @@ export class Contextmenu extends BasePlugin<ContextmenuOptions> {
218
223
  if (event.target instanceof HTMLElement) {
219
224
  if (event.target.className.includes('g6-contextmenu-li')) {
220
225
  const value = event.target.getAttribute('value') as string;
221
- onClick?.(value, event.target);
226
+ onClick?.(value, event.target, this.targetElement!);
222
227
  this.hide();
223
228
  }
224
229
  }
@@ -18,7 +18,7 @@ export function alignFields(refObject: Record<string, any>, targetObject: Record
18
18
  if (isObject(refObject[key]) && !Array.isArray(refObject[key]) && refObject[key] !== null) {
19
19
  if (!targetObject[key]) targetObject[key] = {};
20
20
  alignFields(refObject[key], targetObject[key]);
21
- } else if (!targetObject[key]) {
21
+ } else if (targetObject[key] === undefined) {
22
22
  targetObject[key] = inferDefaultValue(key);
23
23
  }
24
24
  }
@@ -1,10 +1,10 @@
1
- import { Canvas, DisplayObject, IRenderer } from '@antv/g';
1
+ import { Canvas, DisplayObject, IRenderer, Landmark } from '@antv/g';
2
2
  import { Renderer } from '@antv/g-canvas';
3
3
  import { throttle } from '@antv/util';
4
4
  import { GraphEvent } from '../../constants';
5
5
  import type { RuntimeContext } from '../../runtime/types';
6
6
  import { GraphData } from '../../spec';
7
- import type { ElementDatum, ElementType, ID, IGraphLifeCycleEvent, Padding, Placement } from '../../types';
7
+ import type { ElementDatum, ElementType, ID, IGraphLifeCycleEvent, Padding, Placement, Vector3 } from '../../types';
8
8
  import { idOf } from '../../utils/id';
9
9
  import { parsePadding } from '../../utils/padding';
10
10
  import { parsePlacement } from '../../utils/placement';
@@ -201,7 +201,11 @@ export class Minimap extends BasePlugin<MinimapOptions> {
201
201
  if (!this.shapes.has(id)) {
202
202
  canvas.appendChild(cloneShape);
203
203
  this.shapes.set(id, cloneShape);
204
- } else this.shapes.get(id)!.attr(shape.attributes);
204
+ } else {
205
+ Object.entries(shape.attributes).forEach(([key, value]) => {
206
+ if (cloneShape.style[key] !== value) cloneShape.style[key] = value;
207
+ });
208
+ }
205
209
  };
206
210
 
207
211
  // 注意执行顺序 / Note the execution order
@@ -308,6 +312,23 @@ export class Minimap extends BasePlugin<MinimapOptions> {
308
312
  return this.canvas;
309
313
  }
310
314
 
315
+ private landmarkMap = new Map<string, Landmark>();
316
+
317
+ private createLandmark(position: Vector3, focalPoint: Vector3, zoom: number) {
318
+ const key = `${position.join(',')}-${focalPoint.join(',')}-${zoom}`;
319
+
320
+ if (this.landmarkMap.has(key)) return this.landmarkMap.get(key)!;
321
+
322
+ const camera = this.canvas.getCamera();
323
+ const landmark = camera.createLandmark(key, {
324
+ position,
325
+ focalPoint,
326
+ zoom,
327
+ });
328
+ this.landmarkMap.set(key, landmark);
329
+ return landmark;
330
+ }
331
+
311
332
  private setCamera() {
312
333
  const { canvas } = this.context;
313
334
 
@@ -319,7 +340,7 @@ export class Minimap extends BasePlugin<MinimapOptions> {
319
340
  padding,
320
341
  } = this.options;
321
342
  const [top, right, bottom, left] = parsePadding(padding);
322
- const { min: boundsMin, max: boundsMax, center } = canvas.getBounds();
343
+ const { min: boundsMin, max: boundsMax, center } = canvas.getBounds('elements');
323
344
  const boundsWidth = boundsMax[0] - boundsMin[0];
324
345
  const boundsHeight = boundsMax[1] - boundsMin[1];
325
346
 
@@ -330,9 +351,8 @@ export class Minimap extends BasePlugin<MinimapOptions> {
330
351
  const scaleY = availableHeight / boundsHeight;
331
352
  const scale = Math.min(scaleX, scaleY);
332
353
 
333
- camera.setPosition(center);
334
- camera.setFocalPoint(center);
335
- camera.setZoom(scale);
354
+ const landmark = this.createLandmark(center, center, scale);
355
+ camera.gotoLandmark(landmark, 0);
336
356
  }
337
357
 
338
358
  private mask: HTMLElement | null = null;
@@ -349,14 +369,7 @@ export class Minimap extends BasePlugin<MinimapOptions> {
349
369
  const width = maskMax.x - maskMin.x;
350
370
  const height = maskMax.y - maskMin.y;
351
371
 
352
- const zoom = this.context.canvas.getCamera().getZoom();
353
-
354
- // magic number
355
- const ratio = zoom * 0.5;
356
- const x = maskMin.x - width * ratio;
357
- const y = maskMin.y - height * ratio;
358
-
359
- return [x, y, width, height];
372
+ return [maskMin.x, maskMin.y, width, height];
360
373
  }
361
374
 
362
375
  /**
@@ -159,11 +159,34 @@ export class Canvas {
159
159
  });
160
160
  }
161
161
 
162
- public getBounds() {
162
+ /**
163
+ * <zh/> 获取画布边界
164
+ *
165
+ * <en/> Get canvas boundary
166
+ * @param group
167
+ * <zh/> 元素分组
168
+ * - undefined: 获取整个画布边界
169
+ * - 'elements': 仅获取元素边界
170
+ * - 'plugins': 仅获取插件边界
171
+ *
172
+ * <en/> Element group
173
+ * - undefined: Get the entire canvas boundary
174
+ * - 'elements': Get only the element boundary
175
+ * - 'plugins': Get only the plugin boundary
176
+ * @returns <zh/> 边界 <en/> Boundary
177
+ */
178
+ public getBounds(group?: 'elements' | 'plugins') {
163
179
  return getCombinedBBox(
164
180
  Object.values(this.getLayers())
165
- .map((canvas) => canvas.document.documentElement)
166
- .filter((el) => el.childNodes.length > 0)
181
+ .map((canvas) => {
182
+ const g = group
183
+ ? (canvas
184
+ .getRoot()
185
+ .childNodes.find((node) => (node as DisplayObject).classList.includes(group)) as DisplayObject)
186
+ : canvas.getRoot();
187
+ return g;
188
+ })
189
+ .filter((el) => el?.childNodes.length > 0)
167
190
  .map((el) => el.getBounds()),
168
191
  );
169
192
  }
@@ -52,7 +52,7 @@ export class ElementController {
52
52
  public init() {
53
53
  if (!this.container) {
54
54
  const { canvas } = this.context;
55
- this.container = canvas.appendChild(new Group());
55
+ this.container = canvas.appendChild(new Group({ className: 'elements' }));
56
56
  }
57
57
  }
58
58
 
@@ -117,7 +117,8 @@ export class Graph extends EventEmitter {
117
117
  * @apiCategory option
118
118
  */
119
119
  public setOptions(options: GraphOptions): void {
120
- const { behaviors, combo, data, edge, height, layout, node, plugins, theme, transforms, width, renderer } = options;
120
+ const { behaviors, combo, data, edge, height, layout, node, plugins, theme, transforms, width, cursor, renderer } =
121
+ options;
121
122
 
122
123
  if (renderer) {
123
124
  const canvas = this.context.canvas;
@@ -130,6 +131,7 @@ export class Graph extends EventEmitter {
130
131
 
131
132
  Object.assign(this.options, options);
132
133
 
134
+ if (cursor) this.context.canvas?.setCursor(cursor);
133
135
  if (behaviors) this.setBehaviors(behaviors);
134
136
  if (combo) this.setCombo(combo);
135
137
  if (data) this.setData(data);
@@ -1013,10 +1015,10 @@ export class Graph extends EventEmitter {
1013
1015
  private async initCanvas() {
1014
1016
  if (this.context.canvas) return await this.context.canvas.ready;
1015
1017
 
1016
- const { container = 'container', width, height, renderer, background } = this.options;
1017
-
1018
+ const { container = 'container', width, height, renderer, cursor, background } = this.options;
1018
1019
  if (container instanceof Canvas) {
1019
1020
  this.context.canvas = container;
1021
+ if (cursor) container.setCursor(cursor);
1020
1022
  await container.ready;
1021
1023
  } else {
1022
1024
  const $container = isString(container) ? document.getElementById(container!) : container;
@@ -1030,6 +1032,7 @@ export class Graph extends EventEmitter {
1030
1032
  height: height || containerSize[1],
1031
1033
  background,
1032
1034
  renderer,
1035
+ cursor,
1033
1036
  });
1034
1037
 
1035
1038
  this.context.canvas = canvas;
@@ -1,4 +1,4 @@
1
- import type { IRenderer } from '@antv/g';
1
+ import type { Cursor, IRenderer } from '@antv/g';
2
2
  import type { Canvas } from '../runtime/canvas';
3
3
 
4
4
  /**
@@ -51,4 +51,10 @@ export interface CanvasOptions {
51
51
  * <en/> device pixel ratio
52
52
  */
53
53
  devicePixelRatio?: number;
54
+ /**
55
+ * <zh/> 指针样式
56
+ *
57
+ * <en/> cursor style
58
+ */
59
+ cursor?: Cursor;
54
60
  }
@@ -61,7 +61,10 @@ export function mergeOptions(opt1: DisplayObjectConfig<any>, opt2: DisplayObject
61
61
  */
62
62
  export function getSubShapeStyle<T extends Record<string, any>>(
63
63
  style: T,
64
- ): Omit<T, 'x' | 'y' | 'z' | 'transform' | 'transformOrigin' | 'className' | 'class' | 'context' | 'zIndex'> {
65
- const { x, y, z, class: cls, className, transform, transformOrigin, context, zIndex, ...rest } = style;
64
+ ): Omit<
65
+ T,
66
+ 'x' | 'y' | 'z' | 'transform' | 'transformOrigin' | 'className' | 'class' | 'context' | 'zIndex' | 'visibility'
67
+ > {
68
+ const { x, y, z, class: cls, className, transform, transformOrigin, context, zIndex, visibility, ...rest } = style;
66
69
  return rest;
67
70
  }
@@ -1,15 +1,13 @@
1
1
  import type { BaseStyleProps, DisplayObject } from '@antv/g';
2
- import { cacheStyle, getCachedStyle, hasCachedStyle } from './cache';
3
- import { getDescendantShapes } from './shape';
4
2
 
5
- const PropertyKey = 'visibility';
3
+ const ORIGINAL_MAP = new WeakMap<DisplayObject, BaseStyleProps['visibility']>();
6
4
 
7
5
  /**
8
6
  * <zh/> 设置图形实例的可见性
9
7
  *
10
8
  * <en/> Set the visibility of the shape instance
11
9
  * @param shape - <zh/> 图形实例 | <en/> shape instance
12
- * @param visibility - <zh/> 可见性 | <en/> visibility
10
+ * @param value - <zh/> 可见性 | <en/> visibility
13
11
  * @param filter - <zh/> 筛选出需要设置可见性的图形 | <en/> Filter out the shapes that need to set visibility
14
12
  * @remarks
15
13
  * <zh/> 在设置 enableCSSParsing 为 false 的情况下,复合图形无法继承父属性,因此需要对所有子图形应用相同的可见性
@@ -18,21 +16,40 @@ const PropertyKey = 'visibility';
18
16
  */
19
17
  export function setVisibility(
20
18
  shape: DisplayObject,
21
- visibility: BaseStyleProps['visibility'],
22
- filter?: (shapes: DisplayObject[]) => DisplayObject[],
19
+ value: BaseStyleProps['visibility'],
20
+ filter?: (shape: DisplayObject) => boolean,
23
21
  ) {
24
- let shapes = [shape, ...getDescendantShapes(shape)];
22
+ if (value === undefined) return;
25
23
 
26
- if (filter) shapes = filter?.(shapes);
24
+ const traverse = (current: DisplayObject, scope = value) => {
25
+ const walk = (val = scope) => (current.childNodes as DisplayObject[]).forEach((node) => traverse(node, val));
27
26
 
28
- shapes.forEach((sp) => {
29
- if (!hasCachedStyle(sp, PropertyKey)) cacheStyle(sp, PropertyKey);
30
- const cachedVisibility = getCachedStyle(sp, PropertyKey);
27
+ if (filter && !filter(current)) return walk();
31
28
 
32
- // 如果子图形为隐藏状态,始终保持隐藏状态
33
- // If the child shape is hidden, keep it hidden
34
- if (shape !== sp && cachedVisibility === 'hidden') return;
29
+ if (current === shape) {
30
+ shape.style.visibility = value;
31
+ ORIGINAL_MAP.delete(shape);
32
+ walk(value);
33
+ } else {
34
+ if (!ORIGINAL_MAP.has(current)) ORIGINAL_MAP.set(current, current.style.visibility);
35
35
 
36
- sp.style.visibility = visibility;
37
- });
36
+ const computedValue = scope === 'hidden' || getOriginalValue(current) === 'hidden' ? 'hidden' : 'visible';
37
+ current.style.visibility = computedValue;
38
+ walk(computedValue);
39
+ }
40
+ };
41
+
42
+ traverse(shape);
43
+ }
44
+
45
+ /**
46
+ * <zh/> 获取图形原本的可见性
47
+ *
48
+ * <en/> Get the original visibility of the shape
49
+ * @param shape - <zh/> 图形实例 | <en/> shape instance
50
+ * @returns <zh/> 可见性 | <en/> visibility
51
+ */
52
+ function getOriginalValue(shape: DisplayObject) {
53
+ if (ORIGINAL_MAP.has(shape)) return ORIGINAL_MAP.get(shape);
54
+ return shape.style.visibility;
38
55
  }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '5.0.13';
1
+ export const version = '5.0.15';