@antv/g6 5.0.13 → 5.0.14

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 (73) 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/spec/canvas.d.ts +7 -1
  7. package/dist/lib/utils/style.d.ts +1 -1
  8. package/dist/lib/utils/visibility.d.ts +2 -2
  9. package/dist/lib/version.d.ts +1 -1
  10. package/esm/behaviors/click-select.js +2 -3
  11. package/esm/behaviors/click-select.js.map +1 -1
  12. package/esm/behaviors/drag-canvas.d.ts +0 -1
  13. package/esm/behaviors/drag-canvas.js +7 -10
  14. package/esm/behaviors/drag-canvas.js.map +1 -1
  15. package/esm/behaviors/optimize-viewport-transform.js +1 -1
  16. package/esm/behaviors/optimize-viewport-transform.js.map +1 -1
  17. package/esm/elements/shapes/badge.d.ts +1 -1
  18. package/esm/elements/shapes/base-shape.d.ts +1 -1
  19. package/esm/elements/shapes/base-shape.js.map +1 -1
  20. package/esm/exports.d.ts +1 -0
  21. package/esm/exports.js +1 -0
  22. package/esm/exports.js.map +1 -1
  23. package/esm/plugins/history/util.js +1 -1
  24. package/esm/plugins/history/util.js.map +1 -1
  25. package/esm/runtime/graph.js +7 -2
  26. package/esm/runtime/graph.js.map +1 -1
  27. package/esm/spec/canvas.d.ts +7 -1
  28. package/esm/utils/style.d.ts +1 -1
  29. package/esm/utils/style.js +1 -1
  30. package/esm/utils/style.js.map +1 -1
  31. package/esm/utils/visibility.d.ts +2 -2
  32. package/esm/utils/visibility.js +35 -18
  33. package/esm/utils/visibility.js.map +1 -1
  34. package/esm/version.d.ts +1 -1
  35. package/esm/version.js +1 -1
  36. package/lib/behaviors/click-select.js +2 -3
  37. package/lib/behaviors/click-select.js.map +1 -1
  38. package/lib/behaviors/drag-canvas.d.ts +0 -1
  39. package/lib/behaviors/drag-canvas.js +6 -9
  40. package/lib/behaviors/drag-canvas.js.map +1 -1
  41. package/lib/behaviors/optimize-viewport-transform.js +1 -1
  42. package/lib/behaviors/optimize-viewport-transform.js.map +1 -1
  43. package/lib/elements/shapes/badge.d.ts +1 -1
  44. package/lib/elements/shapes/base-shape.d.ts +1 -1
  45. package/lib/elements/shapes/base-shape.js.map +1 -1
  46. package/lib/exports.d.ts +1 -0
  47. package/lib/exports.js +3 -1
  48. package/lib/exports.js.map +1 -1
  49. package/lib/plugins/history/util.js +1 -1
  50. package/lib/plugins/history/util.js.map +1 -1
  51. package/lib/runtime/graph.js +7 -2
  52. package/lib/runtime/graph.js.map +1 -1
  53. package/lib/spec/canvas.d.ts +7 -1
  54. package/lib/utils/style.d.ts +1 -1
  55. package/lib/utils/style.js +1 -1
  56. package/lib/utils/style.js.map +1 -1
  57. package/lib/utils/visibility.d.ts +2 -2
  58. package/lib/utils/visibility.js +35 -18
  59. package/lib/utils/visibility.js.map +1 -1
  60. package/lib/version.d.ts +1 -1
  61. package/lib/version.js +1 -1
  62. package/package.json +1 -1
  63. package/src/behaviors/click-select.ts +2 -3
  64. package/src/behaviors/drag-canvas.ts +7 -11
  65. package/src/behaviors/optimize-viewport-transform.ts +1 -1
  66. package/src/elements/shapes/base-shape.ts +4 -1
  67. package/src/exports.ts +1 -0
  68. package/src/plugins/history/util.ts +1 -1
  69. package/src/runtime/graph.ts +6 -3
  70. package/src/spec/canvas.ts +7 -1
  71. package/src/utils/style.ts +5 -2
  72. package/src/utils/visibility.ts +33 -16
  73. package/src/version.ts +1 -1
@@ -4,11 +4,11 @@ import type { BaseStyleProps, DisplayObject } from '@antv/g';
4
4
  *
5
5
  * <en/> Set the visibility of the shape instance
6
6
  * @param shape - <zh/> 图形实例 | <en/> shape instance
7
- * @param visibility - <zh/> 可见性 | <en/> visibility
7
+ * @param value - <zh/> 可见性 | <en/> visibility
8
8
  * @param filter - <zh/> 筛选出需要设置可见性的图形 | <en/> Filter out the shapes that need to set visibility
9
9
  * @remarks
10
10
  * <zh/> 在设置 enableCSSParsing 为 false 的情况下,复合图形无法继承父属性,因此需要对所有子图形应用相同的可见性
11
11
  *
12
12
  * <en/> After setting enableCSSParsing to false, the compound shape cannot inherit the parent attribute, so the same visibility needs to be applied to all child shapes
13
13
  */
14
- export declare function setVisibility(shape: DisplayObject, visibility: BaseStyleProps['visibility'], filter?: (shapes: DisplayObject[]) => DisplayObject[]): void;
14
+ export declare function setVisibility(shape: DisplayObject, value: BaseStyleProps['visibility'], filter?: (shape: DisplayObject) => boolean): void;
@@ -1,34 +1,51 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.setVisibility = setVisibility;
4
- const cache_1 = require("./cache");
5
- const shape_1 = require("./shape");
6
- const PropertyKey = 'visibility';
4
+ const ORIGINAL_MAP = new WeakMap();
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 的情况下,复合图形无法继承父属性,因此需要对所有子图形应用相同的可见性
16
14
  *
17
15
  * <en/> After setting enableCSSParsing to false, the compound shape cannot inherit the parent attribute, so the same visibility needs to be applied to all child shapes
18
16
  */
19
- function setVisibility(shape, visibility, filter) {
20
- let shapes = [shape, ...(0, shape_1.getDescendantShapes)(shape)];
21
- if (filter)
22
- shapes = filter?.(shapes);
23
- shapes.forEach((sp) => {
24
- if (!(0, cache_1.hasCachedStyle)(sp, PropertyKey))
25
- (0, cache_1.cacheStyle)(sp, PropertyKey);
26
- const cachedVisibility = (0, cache_1.getCachedStyle)(sp, PropertyKey);
27
- // 如果子图形为隐藏状态,始终保持隐藏状态
28
- // If the child shape is hidden, keep it hidden
29
- if (shape !== sp && cachedVisibility === 'hidden')
30
- return;
31
- sp.style.visibility = visibility;
32
- });
17
+ function setVisibility(shape, value, filter) {
18
+ if (value === undefined)
19
+ return;
20
+ const traverse = (current, scope = value) => {
21
+ const walk = (val = scope) => current.childNodes.forEach((node) => traverse(node, val));
22
+ if (filter && !filter(current))
23
+ return walk();
24
+ if (current === shape) {
25
+ shape.style.visibility = value;
26
+ ORIGINAL_MAP.delete(shape);
27
+ walk(value);
28
+ }
29
+ else {
30
+ if (!ORIGINAL_MAP.has(current))
31
+ ORIGINAL_MAP.set(current, current.style.visibility);
32
+ const computedValue = scope === 'hidden' || getOriginalValue(current) === 'hidden' ? 'hidden' : 'visible';
33
+ current.style.visibility = computedValue;
34
+ walk(computedValue);
35
+ }
36
+ };
37
+ traverse(shape);
38
+ }
39
+ /**
40
+ * <zh/> 获取图形原本的可见性
41
+ *
42
+ * <en/> Get the original visibility of the shape
43
+ * @param shape - <zh/> 图形实例 | <en/> shape instance
44
+ * @returns <zh/> 可见性 | <en/> visibility
45
+ */
46
+ function getOriginalValue(shape) {
47
+ if (ORIGINAL_MAP.has(shape))
48
+ return ORIGINAL_MAP.get(shape);
49
+ return shape.style.visibility;
33
50
  }
34
51
  //# sourceMappingURL=visibility.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"visibility.js","sourceRoot":"src/","sources":["utils/visibility.ts"],"names":[],"mappings":";;AAkBA,sCAmBC;AApCD,mCAAqE;AACrE,mCAA8C;AAE9C,MAAM,WAAW,GAAG,YAAY,CAAC;AAEjC;;;;;;;;;;;GAWG;AACH,SAAgB,aAAa,CAC3B,KAAoB,EACpB,UAAwC,EACxC,MAAqD;IAErD,IAAI,MAAM,GAAG,CAAC,KAAK,EAAE,GAAG,IAAA,2BAAmB,EAAC,KAAK,CAAC,CAAC,CAAC;IAEpD,IAAI,MAAM;QAAE,MAAM,GAAG,MAAM,EAAE,CAAC,MAAM,CAAC,CAAC;IAEtC,MAAM,CAAC,OAAO,CAAC,CAAC,EAAE,EAAE,EAAE;QACpB,IAAI,CAAC,IAAA,sBAAc,EAAC,EAAE,EAAE,WAAW,CAAC;YAAE,IAAA,kBAAU,EAAC,EAAE,EAAE,WAAW,CAAC,CAAC;QAClE,MAAM,gBAAgB,GAAG,IAAA,sBAAc,EAAC,EAAE,EAAE,WAAW,CAAC,CAAC;QAEzD,sBAAsB;QACtB,+CAA+C;QAC/C,IAAI,KAAK,KAAK,EAAE,IAAI,gBAAgB,KAAK,QAAQ;YAAE,OAAO;QAE1D,EAAE,CAAC,KAAK,CAAC,UAAU,GAAG,UAAU,CAAC;IACnC,CAAC,CAAC,CAAC;AACL,CAAC"}
1
+ {"version":3,"file":"visibility.js","sourceRoot":"src/","sources":["utils/visibility.ts"],"names":[],"mappings":";;AAgBA,sCA0BC;AAxCD,MAAM,YAAY,GAAG,IAAI,OAAO,EAA+C,CAAC;AAEhF;;;;;;;;;;;GAWG;AACH,SAAgB,aAAa,CAC3B,KAAoB,EACpB,KAAmC,EACnC,MAA0C;IAE1C,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO;IAEhC,MAAM,QAAQ,GAAG,CAAC,OAAsB,EAAE,KAAK,GAAG,KAAK,EAAE,EAAE;QACzD,MAAM,IAAI,GAAG,CAAC,GAAG,GAAG,KAAK,EAAE,EAAE,CAAE,OAAO,CAAC,UAA8B,CAAC,OAAO,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,EAAE,GAAG,CAAC,CAAC,CAAC;QAE7G,IAAI,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC;YAAE,OAAO,IAAI,EAAE,CAAC;QAE9C,IAAI,OAAO,KAAK,KAAK,EAAE,CAAC;YACtB,KAAK,CAAC,KAAK,CAAC,UAAU,GAAG,KAAK,CAAC;YAC/B,YAAY,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;YAC3B,IAAI,CAAC,KAAK,CAAC,CAAC;QACd,CAAC;aAAM,CAAC;YACN,IAAI,CAAC,YAAY,CAAC,GAAG,CAAC,OAAO,CAAC;gBAAE,YAAY,CAAC,GAAG,CAAC,OAAO,EAAE,OAAO,CAAC,KAAK,CAAC,UAAU,CAAC,CAAC;YAEpF,MAAM,aAAa,GAAG,KAAK,KAAK,QAAQ,IAAI,gBAAgB,CAAC,OAAO,CAAC,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,SAAS,CAAC;YAC1G,OAAO,CAAC,KAAK,CAAC,UAAU,GAAG,aAAa,CAAC;YACzC,IAAI,CAAC,aAAa,CAAC,CAAC;QACtB,CAAC;IACH,CAAC,CAAC;IAEF,QAAQ,CAAC,KAAK,CAAC,CAAC;AAClB,CAAC;AAED;;;;;;GAMG;AACH,SAAS,gBAAgB,CAAC,KAAoB;IAC5C,IAAI,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC;QAAE,OAAO,YAAY,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAC5D,OAAO,KAAK,CAAC,KAAK,CAAC,UAAU,CAAC;AAChC,CAAC"}
package/lib/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "5.0.13";
1
+ export declare const version = "5.0.14";
package/lib/version.js CHANGED
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.version = void 0;
4
- exports.version = '5.0.13';
4
+ exports.version = '5.0.14';
5
5
  //# sourceMappingURL=version.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@antv/g6",
3
- "version": "5.0.13",
3
+ "version": "5.0.14",
4
4
  "description": "A Graph Visualization Framework in JavaScript",
5
5
  "keywords": [
6
6
  "antv",
@@ -188,7 +188,7 @@ export class ClickSelect extends BaseBehavior<ClickSelectOptions> {
188
188
  Object.assign(states, this.getClearStates(!!unselectedState));
189
189
  const addState = (list: ID[], state: State) => {
190
190
  list.forEach((id) => {
191
- if (!states[id]) states[id] = [];
191
+ if (!states[id]) states[id] = graph.getElementState(id);
192
192
  states[id].push(state);
193
193
  });
194
194
  };
@@ -206,8 +206,7 @@ export class ClickSelect extends BaseBehavior<ClickSelectOptions> {
206
206
  if (type === 'select') {
207
207
  const addState = (list: ID[], state: State) => {
208
208
  list.forEach((id) => {
209
- const datum = graph.getElementData(id);
210
- const dataStatesSet = new Set(statesOf(datum));
209
+ const dataStatesSet = new Set(graph.getElementState(id));
211
210
  dataStatesSet.add(state);
212
211
  dataStatesSet.delete(unselectedState);
213
212
  states[id] = Array.from(dataStatesSet);
@@ -1,6 +1,6 @@
1
1
  import type { Cursor } from '@antv/g';
2
2
  import { debounce, isObject } from '@antv/util';
3
- import { CanvasEvent } from '../constants';
3
+ import { CommonEvent } from '../constants';
4
4
  import type { RuntimeContext } from '../runtime/types';
5
5
  import type { IKeyboardEvent, IPointerEvent, Vector2, ViewportAnimationEffectTiming } from '../types';
6
6
  import type { ShortcutKey } from '../utils/shortcut';
@@ -72,10 +72,6 @@ export class DragCanvas extends BaseBehavior<DragCanvasOptions> {
72
72
 
73
73
  private defaultCursor: Cursor;
74
74
 
75
- private get canvas() {
76
- return this.context.canvas.getLayer();
77
- }
78
-
79
75
  constructor(context: RuntimeContext, options: DragCanvasOptions) {
80
76
  super(context, Object.assign({}, DragCanvas.defaultOptions, options));
81
77
 
@@ -109,9 +105,9 @@ export class DragCanvas extends BaseBehavior<DragCanvasOptions> {
109
105
  this.shortcut.bind(right, (event) => this.onTranslate([-1, 0], event));
110
106
  } else {
111
107
  const { graph } = this.context;
112
- graph.on(CanvasEvent.DRAG_START, this.onDragStart);
113
- graph.on(CanvasEvent.DRAG, this.onDrag);
114
- graph.on(CanvasEvent.DRAG_END, this.onDragEnd);
108
+ graph.on(CommonEvent.DRAG_START, this.onDragStart);
109
+ graph.on(CommonEvent.DRAG, this.onDrag);
110
+ graph.on(CommonEvent.DRAG_END, this.onDragEnd);
115
111
  }
116
112
  }
117
113
 
@@ -172,9 +168,9 @@ export class DragCanvas extends BaseBehavior<DragCanvasOptions> {
172
168
  private unbindEvents() {
173
169
  this.shortcut.unbindAll();
174
170
  const { graph } = this.context;
175
- graph.off(CanvasEvent.DRAG_START, this.onDragStart);
176
- graph.off(CanvasEvent.DRAG, this.onDrag);
177
- graph.off(CanvasEvent.DRAG_END, this.onDragEnd);
171
+ graph.off(CommonEvent.DRAG_START, this.onDragStart);
172
+ graph.off(CommonEvent.DRAG, this.onDrag);
173
+ graph.off(CommonEvent.DRAG_END, this.onDragEnd);
178
174
  }
179
175
 
180
176
  public destroy(): void {
@@ -72,7 +72,7 @@ export class OptimizeViewportTransform extends BaseBehavior<OptimizeViewportTran
72
72
  setVisibility(
73
73
  element,
74
74
  visibility,
75
- excludedClassnames && ((shapes) => this.filterShapes(shapes, excludedClassnames)),
75
+ (shape) => !!shape.className && !excludedClassnames?.includes(shape.className),
76
76
  );
77
77
  });
78
78
  };
@@ -124,7 +124,10 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
124
124
  */
125
125
  public getGraphicStyle<T extends Record<string, any>>(
126
126
  style: T,
127
- ): Omit<T, 'x' | 'y' | 'z' | 'transform' | 'transformOrigin' | 'className' | 'class' | 'context' | 'zIndex'> {
127
+ ): Omit<
128
+ T,
129
+ 'x' | 'y' | 'z' | 'transform' | 'transformOrigin' | 'className' | 'class' | 'context' | 'zIndex' | 'visibility'
130
+ > {
128
131
  return getSubShapeStyle(style);
129
132
  }
130
133
 
package/src/exports.ts CHANGED
@@ -89,6 +89,7 @@ export { omitStyleProps, subStyleProps } from './utils/prefix';
89
89
  export { Shortcut } from './utils/shortcut';
90
90
  export { parseSize } from './utils/size';
91
91
  export { treeToGraphData } from './utils/tree';
92
+ export { setVisibility } from './utils/visibility';
92
93
 
93
94
  export type { BaseStyleProps } from '@antv/g';
94
95
  export type {
@@ -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
  }
@@ -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.14';