@antv/g6 5.0.35 → 5.0.37

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 (146) hide show
  1. package/dist/g6.min.js +11 -11
  2. package/dist/lib/elements/shapes/base-shape.d.ts +0 -7
  3. package/dist/lib/layouts/types.d.ts +20 -0
  4. package/dist/lib/plugins/tooltip.d.ts +1 -1
  5. package/dist/lib/runtime/data.d.ts +2 -8
  6. package/dist/lib/runtime/element.d.ts +5 -2
  7. package/dist/lib/runtime/layout.d.ts +18 -1
  8. package/dist/lib/runtime/options.d.ts +9 -0
  9. package/dist/lib/runtime/transform.d.ts +2 -1
  10. package/dist/lib/spec/data.d.ts +0 -14
  11. package/dist/lib/spec/theme.d.ts +1 -1
  12. package/dist/lib/transforms/base-transform.d.ts +2 -1
  13. package/dist/lib/utils/element.d.ts +9 -1
  14. package/dist/lib/utils/layout.d.ts +13 -1
  15. package/dist/lib/utils/transform.d.ts +2 -2
  16. package/dist/lib/version.d.ts +1 -1
  17. package/esm/animations/executor.js +3 -2
  18. package/esm/animations/executor.js.map +1 -1
  19. package/esm/behaviors/drag-element.js +9 -2
  20. package/esm/behaviors/drag-element.js.map +1 -1
  21. package/esm/elements/combos/base-combo.js +1 -1
  22. package/esm/elements/combos/base-combo.js.map +1 -1
  23. package/esm/elements/edges/base-edge.js +8 -2
  24. package/esm/elements/edges/base-edge.js.map +1 -1
  25. package/esm/elements/shapes/base-shape.d.ts +0 -7
  26. package/esm/elements/shapes/base-shape.js +22 -19
  27. package/esm/elements/shapes/base-shape.js.map +1 -1
  28. package/esm/layouts/types.d.ts +20 -0
  29. package/esm/plugins/background/index.js +1 -0
  30. package/esm/plugins/background/index.js.map +1 -1
  31. package/esm/plugins/grid-line.js +1 -0
  32. package/esm/plugins/grid-line.js.map +1 -1
  33. package/esm/plugins/tooltip.d.ts +1 -1
  34. package/esm/plugins/tooltip.js +18 -16
  35. package/esm/plugins/tooltip.js.map +1 -1
  36. package/esm/runtime/data.d.ts +2 -8
  37. package/esm/runtime/data.js +14 -18
  38. package/esm/runtime/data.js.map +1 -1
  39. package/esm/runtime/element.d.ts +5 -2
  40. package/esm/runtime/element.js +45 -18
  41. package/esm/runtime/element.js.map +1 -1
  42. package/esm/runtime/graph.js +19 -7
  43. package/esm/runtime/graph.js.map +1 -1
  44. package/esm/runtime/layout.d.ts +18 -1
  45. package/esm/runtime/layout.js +85 -20
  46. package/esm/runtime/layout.js.map +1 -1
  47. package/esm/runtime/options.d.ts +9 -0
  48. package/esm/runtime/options.js +49 -0
  49. package/esm/runtime/options.js.map +1 -0
  50. package/esm/runtime/transform.d.ts +2 -1
  51. package/esm/runtime/transform.js.map +1 -1
  52. package/esm/spec/data.d.ts +0 -14
  53. package/esm/spec/theme.d.ts +1 -1
  54. package/esm/transforms/base-transform.d.ts +2 -1
  55. package/esm/transforms/base-transform.js +1 -1
  56. package/esm/transforms/base-transform.js.map +1 -1
  57. package/esm/utils/animation.js +2 -2
  58. package/esm/utils/animation.js.map +1 -1
  59. package/esm/utils/element.d.ts +9 -1
  60. package/esm/utils/element.js +35 -1
  61. package/esm/utils/element.js.map +1 -1
  62. package/esm/utils/layout.d.ts +13 -1
  63. package/esm/utils/layout.js +14 -0
  64. package/esm/utils/layout.js.map +1 -1
  65. package/esm/utils/transform.d.ts +2 -2
  66. package/esm/utils/transform.js +13 -8
  67. package/esm/utils/transform.js.map +1 -1
  68. package/esm/version.d.ts +1 -1
  69. package/esm/version.js +1 -1
  70. package/lib/animations/executor.js +3 -2
  71. package/lib/animations/executor.js.map +1 -1
  72. package/lib/behaviors/drag-element.js +9 -2
  73. package/lib/behaviors/drag-element.js.map +1 -1
  74. package/lib/elements/combos/base-combo.js +1 -1
  75. package/lib/elements/combos/base-combo.js.map +1 -1
  76. package/lib/elements/edges/base-edge.js +7 -1
  77. package/lib/elements/edges/base-edge.js.map +1 -1
  78. package/lib/elements/shapes/base-shape.d.ts +0 -7
  79. package/lib/elements/shapes/base-shape.js +21 -18
  80. package/lib/elements/shapes/base-shape.js.map +1 -1
  81. package/lib/layouts/types.d.ts +20 -0
  82. package/lib/plugins/background/index.js +1 -0
  83. package/lib/plugins/background/index.js.map +1 -1
  84. package/lib/plugins/grid-line.js +1 -0
  85. package/lib/plugins/grid-line.js.map +1 -1
  86. package/lib/plugins/tooltip.d.ts +1 -1
  87. package/lib/plugins/tooltip.js +18 -16
  88. package/lib/plugins/tooltip.js.map +1 -1
  89. package/lib/runtime/data.d.ts +2 -8
  90. package/lib/runtime/data.js +14 -18
  91. package/lib/runtime/data.js.map +1 -1
  92. package/lib/runtime/element.d.ts +5 -2
  93. package/lib/runtime/element.js +45 -18
  94. package/lib/runtime/element.js.map +1 -1
  95. package/lib/runtime/graph.js +21 -9
  96. package/lib/runtime/graph.js.map +1 -1
  97. package/lib/runtime/layout.d.ts +18 -1
  98. package/lib/runtime/layout.js +84 -19
  99. package/lib/runtime/layout.js.map +1 -1
  100. package/lib/runtime/options.d.ts +9 -0
  101. package/lib/runtime/options.js +52 -0
  102. package/lib/runtime/options.js.map +1 -0
  103. package/lib/runtime/transform.d.ts +2 -1
  104. package/lib/runtime/transform.js.map +1 -1
  105. package/lib/spec/data.d.ts +0 -14
  106. package/lib/spec/theme.d.ts +1 -1
  107. package/lib/transforms/base-transform.d.ts +2 -1
  108. package/lib/transforms/base-transform.js +1 -1
  109. package/lib/transforms/base-transform.js.map +1 -1
  110. package/lib/utils/animation.js +2 -2
  111. package/lib/utils/animation.js.map +1 -1
  112. package/lib/utils/element.d.ts +9 -1
  113. package/lib/utils/element.js +35 -0
  114. package/lib/utils/element.js.map +1 -1
  115. package/lib/utils/layout.d.ts +13 -1
  116. package/lib/utils/layout.js +15 -0
  117. package/lib/utils/layout.js.map +1 -1
  118. package/lib/utils/transform.d.ts +2 -2
  119. package/lib/utils/transform.js +13 -8
  120. package/lib/utils/transform.js.map +1 -1
  121. package/lib/version.d.ts +1 -1
  122. package/lib/version.js +1 -1
  123. package/package.json +1 -1
  124. package/src/animations/executor.ts +4 -4
  125. package/src/behaviors/drag-element.ts +9 -2
  126. package/src/elements/combos/base-combo.ts +1 -1
  127. package/src/elements/edges/base-edge.ts +9 -5
  128. package/src/elements/shapes/base-shape.ts +21 -19
  129. package/src/layouts/types.ts +20 -0
  130. package/src/plugins/background/index.ts +1 -0
  131. package/src/plugins/grid-line.ts +1 -0
  132. package/src/plugins/tooltip.ts +21 -15
  133. package/src/runtime/data.ts +14 -19
  134. package/src/runtime/element.ts +69 -18
  135. package/src/runtime/graph.ts +19 -8
  136. package/src/runtime/layout.ts +79 -23
  137. package/src/runtime/options.ts +52 -0
  138. package/src/runtime/transform.ts +2 -0
  139. package/src/spec/data.ts +0 -14
  140. package/src/spec/theme.ts +1 -1
  141. package/src/transforms/base-transform.ts +3 -1
  142. package/src/utils/animation.ts +1 -1
  143. package/src/utils/element.ts +21 -2
  144. package/src/utils/layout.ts +16 -1
  145. package/src/utils/transform.ts +13 -9
  146. package/src/version.ts +1 -1
@@ -2,14 +2,16 @@ import type { IAnimation } from '@antv/g';
2
2
  import { Graph as Graphlib } from '@antv/graphlib';
3
3
  import { Supervisor, isLayoutWithIterations } from '@antv/layout';
4
4
  import { deepMix } from '@antv/util';
5
- import { GraphEvent, TREE_KEY } from '../constants';
5
+ import { COMBO_KEY, GraphEvent, TREE_KEY } from '../constants';
6
6
  import { BaseLayout } from '../layouts';
7
7
  import type { AntVLayout } from '../layouts/types';
8
8
  import { getExtension } from '../registry/get';
9
9
  import type { GraphData, NodeData } from '../spec';
10
10
  import type { STDLayoutOptions } from '../spec/layout';
11
- import type { AdaptiveLayout, Combo, ID, Node, TreeData } from '../types';
11
+ import type { DrawData } from '../transforms/types';
12
+ import type { AdaptiveLayout, ID, TreeData } from '../types';
12
13
  import { getAnimationOptions } from '../utils/animation';
14
+ import { isCollapsed } from '../utils/collapsibility';
13
15
  import { isToBeDestroyed } from '../utils/element';
14
16
  import { GraphLifeCycleEvent, emit } from '../utils/event';
15
17
  import { createTreeStructure } from '../utils/graphlib';
@@ -49,11 +51,54 @@ export class LayoutController {
49
51
  return this.instances;
50
52
  }
51
53
 
52
- public async layout() {
54
+ /**
55
+ * <zh/> 前布局,即在绘制前执行布局
56
+ *
57
+ * <en/> Pre-layout, that is, perform layout before drawing
58
+ * @param data - <zh/> 绘制数据 | <en/> Draw data
59
+ * @remarks
60
+ * <zh/> 前布局应该只在首次绘制前执行,后续更新不会触发
61
+ *
62
+ * <en/> Pre-layout should only be executed before the first drawing, and subsequent updates will not trigger
63
+ */
64
+ public async preLayout(data: DrawData) {
65
+ const { graph, model } = this.context;
66
+
67
+ const { add } = data;
68
+ emit(graph, new GraphLifeCycleEvent(GraphEvent.BEFORE_LAYOUT, { type: 'pre' }));
69
+ const simulate = await this.context.layout?.simulate();
70
+ simulate?.nodes?.forEach((l) => {
71
+ const id = idOf(l);
72
+ const node = add.nodes.get(id);
73
+ model.syncNodeLikeDatum(l);
74
+ if (node) Object.assign(node.style!, l.style);
75
+ });
76
+ simulate?.edges?.forEach((l) => {
77
+ const id = idOf(l);
78
+ const edge = add.edges.get(id);
79
+ model.syncEdgeDatum(l);
80
+ if (edge) Object.assign(edge.style!, l.style);
81
+ });
82
+ simulate?.combos?.forEach((l) => {
83
+ const id = idOf(l);
84
+ const combo = add.combos.get(id);
85
+ model.syncNodeLikeDatum(l);
86
+ if (combo) Object.assign(combo.style!, l.style);
87
+ });
88
+ emit(graph, new GraphLifeCycleEvent(GraphEvent.AFTER_LAYOUT, { type: 'pre' }));
89
+ this.transformDataAfterLayout('pre', data);
90
+ }
91
+
92
+ /**
93
+ * <zh/> 后布局,即在完成绘制后执行布局
94
+ *
95
+ * <en/> Post layout, that is, perform layout after drawing
96
+ */
97
+ public async postLayout() {
53
98
  if (!this.options) return;
54
99
  const pipeline = Array.isArray(this.options) ? this.options : [this.options];
55
100
  const { graph } = this.context;
56
- emit(graph, new GraphLifeCycleEvent(GraphEvent.BEFORE_LAYOUT));
101
+ emit(graph, new GraphLifeCycleEvent(GraphEvent.BEFORE_LAYOUT, { type: 'post' }));
57
102
  for (const options of pipeline) {
58
103
  const index = pipeline.indexOf(options);
59
104
  const data = this.getLayoutData(options);
@@ -67,14 +112,14 @@ export class LayoutController {
67
112
  this.updateElementPosition(result, false);
68
113
  }
69
114
  }
70
- emit(graph, new GraphLifeCycleEvent(GraphEvent.AFTER_LAYOUT));
71
- this.transformDataAfterLayout();
115
+ emit(graph, new GraphLifeCycleEvent(GraphEvent.AFTER_LAYOUT, { type: 'post' }));
116
+ this.transformDataAfterLayout('post');
72
117
  }
73
118
 
74
- private transformDataAfterLayout() {
119
+ private transformDataAfterLayout(type: 'pre' | 'post', data?: DrawData) {
75
120
  const transforms = this.context.transform.getTransformInstance();
76
-
77
- Object.values(transforms).forEach((transform) => transform.afterLayout());
121
+ // @ts-expect-error skip type check
122
+ Object.values(transforms).forEach((transform) => transform.afterLayout(type, data));
78
123
  }
79
124
 
80
125
  /**
@@ -250,18 +295,30 @@ export class LayoutController {
250
295
  }
251
296
 
252
297
  public getLayoutData(options: STDLayoutOptions): GraphData {
253
- const { nodeFilter = () => true } = options;
298
+ const { nodeFilter = () => true, preLayout = false, isLayoutInvisibleNodes = false } = options;
254
299
  const { nodes, edges, combos } = this.context.model.getData();
255
300
 
256
- const getElement = (id: ID) => this.context.element!.getElement(id);
257
-
258
- const nodesToLayout = nodes.filter((node) => {
259
- const id = idOf(node);
260
- const element = getElement(id);
261
- if (!element) return false;
262
- if (isToBeDestroyed(element)) return false;
263
- return nodeFilter(node);
264
- });
301
+ const { element, model } = this.context;
302
+ const getElement = (id: ID) => element!.getElement(id);
303
+
304
+ const filterFn = preLayout
305
+ ? (node: NodeData) => {
306
+ if (!isLayoutInvisibleNodes) {
307
+ if (node.style?.visibility === 'hidden') return false;
308
+ if (model.getAncestorsData(node.id, TREE_KEY).some(isCollapsed)) return false;
309
+ if (model.getAncestorsData(node.id, COMBO_KEY).some(isCollapsed)) return false;
310
+ }
311
+ return nodeFilter(node);
312
+ }
313
+ : (node: NodeData) => {
314
+ const id = idOf(node);
315
+ const element = getElement(id);
316
+ if (!element) return false;
317
+ if (isToBeDestroyed(element)) return false;
318
+ return nodeFilter(node);
319
+ };
320
+
321
+ const nodesToLayout = nodes.filter(filterFn);
265
322
 
266
323
  const nodesIdMap = new Map<ID, NodeData>(nodesToLayout.map((node) => [idOf(node), node]));
267
324
 
@@ -295,10 +352,9 @@ export class LayoutController {
295
352
  const nodeSize: number | ((node: NodeData) => number) =
296
353
  (options?.nodeSize as number) ??
297
354
  ((node) => {
298
- const nodeElement = element?.getElement<Node | Combo>(node.id as string);
299
- const { size } = nodeElement?.attributes || {};
300
-
301
- return size;
355
+ const nodeElement = element?.getElement(node.id);
356
+ if (nodeElement) return nodeElement.attributes.size;
357
+ return element?.getElementComputedStyle('node', node).size;
302
358
  });
303
359
 
304
360
  const Ctor = getExtension('layout', type);
@@ -0,0 +1,52 @@
1
+ import type { GraphOptions } from '../spec';
2
+
3
+ /**
4
+ * <zh/> 基于用户传入的配置,推断出最终的配置
5
+ *
6
+ * <en/> Infer the final configuration based on the configuration passed by the user
7
+ * @param options - <zh/> 用户传入的配置 | <en/> Configuration passed by the user
8
+ * @returns <zh/> 最终的配置 | <en/> Final configuration
9
+ */
10
+ export function inferOptions(options: GraphOptions): GraphOptions {
11
+ const flow = [inferLayoutOptions];
12
+ return flow.reduce((acc, infer) => infer(acc), options);
13
+ }
14
+
15
+ /**
16
+ * <zh/> 推断布局配置
17
+ *
18
+ * <en/> Infer layout configuration
19
+ * @param options - <zh/> 用户传入的配置 | <en/> Configuration passed by the user
20
+ * @returns <zh/> 最终的配置 | <en/> Final configuration
21
+ */
22
+ function inferLayoutOptions(options: GraphOptions): GraphOptions {
23
+ if (!options.layout) return options;
24
+ if (Array.isArray(options.layout)) return options;
25
+ if ('preLayout' in options.layout) return options;
26
+
27
+ if (
28
+ [
29
+ 'antv-dagre',
30
+ 'combo-combined',
31
+ 'compact-box',
32
+ 'circular',
33
+ 'concentric',
34
+ 'dagre',
35
+ 'fishbone',
36
+ 'grid',
37
+ 'indented',
38
+ 'mds',
39
+ 'radial',
40
+ 'random',
41
+ 'snake',
42
+ // <zh/> 下列布局的标签位置待适配,需要手动配置 preLayout false
43
+ // <en/> The label position of the following layouts needs to be adapted, and preLayout needs to be manually configured as false
44
+ 'dendrogram',
45
+ 'mindmap',
46
+ ].includes(options.layout.type)
47
+ ) {
48
+ options.layout.preLayout = true;
49
+ }
50
+
51
+ return options;
52
+ }
@@ -29,6 +29,8 @@ export class TransformController extends ExtensionController<BaseTransform<Custo
29
29
  ]);
30
30
  }
31
31
 
32
+ public getTransformInstance(): Record<string, BaseTransform>;
33
+ public getTransformInstance(key: string): BaseTransform;
32
34
  public getTransformInstance(key?: string) {
33
35
  return key ? this.extensionMap[key] : this.extensionMap;
34
36
  }
package/src/spec/data.ts CHANGED
@@ -189,20 +189,6 @@ export interface EdgeData {
189
189
  * <en/> Target node ID
190
190
  */
191
191
  target: ID;
192
- /**
193
- * <zh/> 边的起点 shape
194
- *
195
- * <en/> The source shape. Represents the start of the edge
196
- * @internal
197
- */
198
- sourceNode?: ID;
199
- /**
200
- * <zh/> 边的终点 shape
201
- *
202
- * <en/> The source shape. Represents the start of the edge
203
- * @internal
204
- */
205
- targetNode?: ID;
206
192
  /**
207
193
  * <zh/> 边类型
208
194
  *
package/src/spec/theme.ts CHANGED
@@ -4,4 +4,4 @@
4
4
  * <en/> Theme Options
5
5
  * @public
6
6
  */
7
- export type ThemeOptions = 'light' | 'dark' | string;
7
+ export type ThemeOptions = false | 'light' | 'dark' | string;
@@ -15,5 +15,7 @@ export abstract class BaseTransform<T extends BaseTransformOptions = BaseTransfo
15
15
  return data;
16
16
  }
17
17
 
18
- public afterLayout() {}
18
+ public afterLayout(type: 'pre', data: DrawData): void;
19
+ public afterLayout(type: 'post', data?: undefined): void;
20
+ public afterLayout(type: 'pre' | 'post', data?: DrawData) {}
19
21
  }
@@ -189,12 +189,12 @@ export function getElementAnimationOptions(
189
189
  localAnimation?: AnimationEffectTiming | boolean,
190
190
  ): STDAnimation {
191
191
  const { animation: globalAnimation } = options;
192
+ if (globalAnimation === false || localAnimation === false) return [];
192
193
 
193
194
  const userElementAnimation = options?.[elementType]?.animation;
194
195
  if (userElementAnimation === false) return [];
195
196
  const useElementStageAnimation = userElementAnimation?.[stage];
196
197
  if (useElementStageAnimation === false) return [];
197
- if (globalAnimation === false || localAnimation === false) return [];
198
198
 
199
199
  // 优先级:用户局部动画配置 > 用户动画配置 > 全局动画配置 > 主题动画配置 > 默认动画配置
200
200
  // Priority: user local animation configuration > user animation configuration > global animation configuration > theme animation configuration > default animation configuration
@@ -1,5 +1,5 @@
1
- import type { AABB, DisplayObject, TextStyleProps } from '@antv/g';
2
- import { get, isString, set } from '@antv/util';
1
+ import type { AABB, BaseStyleProps, DisplayObject, TextStyleProps } from '@antv/g';
2
+ import { get, isNumber, isString, set } from '@antv/util';
3
3
  import { BaseCombo, BaseEdge, BaseNode } from '../elements';
4
4
  import type { Combo, Edge, Element, Node, NodePortStyleProps, Placement, Point, TriangleDirection } from '../types';
5
5
  import type { NodeLabelStyleProps, Port } from '../types/node';
@@ -467,6 +467,25 @@ export function isVisible(element: DisplayObject) {
467
467
  return get(element, ['style', 'visibility']) !== 'hidden';
468
468
  }
469
469
 
470
+ /**
471
+ * <zh/> 设置元素属性(优化性能)
472
+ *
473
+ * <en/> Set element attributes (optimize performance)
474
+ * @param element - <zh/> 元素 | <en/> element
475
+ * @param style - <zh/> 样式 | <en/> style
476
+ */
477
+ export function setAttributes(element: DisplayObject, style: BaseStyleProps) {
478
+ const { zIndex, transform, transformOrigin, visibility, cursor, clipPath, ...rest } = style;
479
+ Object.assign(element.attributes, rest);
480
+
481
+ if (transform) element.setAttribute('transform', transform);
482
+ if (isNumber(zIndex)) element.setAttribute('zIndex', zIndex);
483
+ if (transformOrigin) element.setAttribute('transformOrigin', transformOrigin);
484
+ if (visibility) element.setAttribute('visibility', visibility);
485
+ if (cursor) element.setAttribute('cursor', cursor);
486
+ if (clipPath) element.setAttribute('clipPath', clipPath);
487
+ }
488
+
470
489
  /**
471
490
  * <zh/> 更新图形样式
472
491
  *
@@ -9,7 +9,7 @@ import type { LayoutMapping, Graph as LayoutModel, Node as LayoutNodeData } from
9
9
  import type { AntVLayout } from '../layouts/types';
10
10
  import type { RuntimeContext } from '../runtime/types';
11
11
  import type { GraphData } from '../spec/data';
12
- import type { STDLayoutOptions } from '../spec/layout';
12
+ import type { LayoutOptions, STDLayoutOptions } from '../spec/layout';
13
13
  import type { AdaptiveLayout, ID } from '../types';
14
14
 
15
15
  /**
@@ -49,6 +49,21 @@ export function isPositionSpecified(data: Record<string, unknown>) {
49
49
  return isNumber(data.x) && isNumber(data.y);
50
50
  }
51
51
 
52
+ /**
53
+ * <zh/> 是否是前布局
54
+ *
55
+ * <en/> Is pre-layout
56
+ * @remarks
57
+ * <zh/> 前布局是指在初始化元素前计算布局,适用于一些布局需要提前计算位置的场景。
58
+ *
59
+ * <en/> Pre-layout refers to calculating the layout before initializing the elements, which is suitable for some layouts that need to calculate the position in advance.
60
+ * @param options - <zh/> 布局配置项 | <en/> Layout options
61
+ * @returns <zh/> 是否是前布局 | <en/> Is it a pre-layout
62
+ */
63
+ export function isPreLayout(options?: LayoutOptions) {
64
+ return !Array.isArray(options) && options?.preLayout;
65
+ }
66
+
52
67
  /**
53
68
  * <zh/> 将图布局结果转换为 G6 数据
54
69
  *
@@ -1,4 +1,5 @@
1
1
  import type { TransformArray } from '@antv/g';
2
+ import { isNumber } from '@antv/util';
2
3
 
3
4
  /**
4
5
  * <zh/> 从 transform 字符串中替换 translate 部分
@@ -8,36 +9,39 @@ import type { TransformArray } from '@antv/g';
8
9
  * @param y - <zh/> y | <en/> y
9
10
  * @param z - <zh/> z | <en/> z
10
11
  * @param transform - <zh/> transform 字符串 | <en/> transform string
11
- * @returns <zh/> 替换后的 transform 字符串 | <en/> replaced transform string
12
+ * @returns <zh/> 替换后的 transform 字符串,返回 null 表示无需替换 | <en/> the replaced transform string, return null means no need to replace
12
13
  */
13
14
  export function replaceTranslateInTransform(
14
15
  x: number,
15
16
  y: number,
16
- z: number,
17
- transform: string | TransformArray,
17
+ z?: number,
18
+ transform: string | TransformArray = [],
18
19
  ): string | TransformArray | null {
20
+ if (!transform && x === 0 && y === 0 && z === 0) return null;
21
+
19
22
  if (Array.isArray(transform)) {
20
- let hasTranslate = false;
23
+ let translateIndex = -1;
21
24
  const newTransform: TransformArray = [];
22
25
 
23
26
  for (let i = 0; i < transform.length; i++) {
24
27
  const t = transform[i];
25
28
  if (t[0] === 'translate') {
26
29
  if (t[1] === x && t[2] === y) return null;
27
- hasTranslate = true;
30
+ translateIndex = i;
28
31
  newTransform.push(['translate', x, y]);
29
32
  } else if (t[0] === 'translate3d') {
30
33
  if (t[1] === x && t[2] === y && t[3] === z) return null;
31
- hasTranslate = true;
32
- newTransform.push(['translate3d', x, y, z]);
34
+ translateIndex = i;
35
+ newTransform.push(['translate3d', x, y, z ?? 0]);
33
36
  } else {
34
37
  newTransform.push(t);
35
38
  }
36
39
  }
37
40
 
38
- if (!hasTranslate) {
39
- newTransform.splice(0, 0, z === 0 ? ['translate', x, y] : ['translate3d', x, y, z]);
41
+ if (translateIndex === -1) {
42
+ newTransform.splice(0, 0, isNumber(z) ? ['translate3d', x, y, z ?? 0] : ['translate', x, y]);
40
43
  }
44
+ if (newTransform.length === 0) return null;
41
45
  return newTransform;
42
46
  }
43
47
 
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '5.0.35';
1
+ export const version = '5.0.37';