@antv/g6 5.0.34 → 5.0.35-beta.1

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 (139) 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 +6 -0
  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/transforms/base-transform.d.ts +2 -1
  12. package/dist/lib/utils/element.d.ts +9 -1
  13. package/dist/lib/utils/layout.d.ts +13 -1
  14. package/dist/lib/utils/transform.d.ts +2 -2
  15. package/dist/lib/version.d.ts +1 -1
  16. package/esm/animations/executor.js +3 -2
  17. package/esm/animations/executor.js.map +1 -1
  18. package/esm/behaviors/drag-element.js +9 -2
  19. package/esm/behaviors/drag-element.js.map +1 -1
  20. package/esm/elements/combos/base-combo.js +1 -1
  21. package/esm/elements/combos/base-combo.js.map +1 -1
  22. package/esm/elements/edges/base-edge.js +8 -2
  23. package/esm/elements/edges/base-edge.js.map +1 -1
  24. package/esm/elements/shapes/base-shape.d.ts +0 -7
  25. package/esm/elements/shapes/base-shape.js +22 -19
  26. package/esm/elements/shapes/base-shape.js.map +1 -1
  27. package/esm/layouts/types.d.ts +20 -0
  28. package/esm/plugins/background/index.js +1 -0
  29. package/esm/plugins/background/index.js.map +1 -1
  30. package/esm/plugins/grid-line.js +1 -0
  31. package/esm/plugins/grid-line.js.map +1 -1
  32. package/esm/plugins/tooltip.d.ts +6 -0
  33. package/esm/plugins/tooltip.js +8 -6
  34. package/esm/plugins/tooltip.js.map +1 -1
  35. package/esm/runtime/data.d.ts +2 -8
  36. package/esm/runtime/data.js +14 -18
  37. package/esm/runtime/data.js.map +1 -1
  38. package/esm/runtime/element.d.ts +5 -2
  39. package/esm/runtime/element.js +39 -15
  40. package/esm/runtime/element.js.map +1 -1
  41. package/esm/runtime/graph.js +19 -7
  42. package/esm/runtime/graph.js.map +1 -1
  43. package/esm/runtime/layout.d.ts +18 -1
  44. package/esm/runtime/layout.js +85 -20
  45. package/esm/runtime/layout.js.map +1 -1
  46. package/esm/runtime/options.d.ts +9 -0
  47. package/esm/runtime/options.js +49 -0
  48. package/esm/runtime/options.js.map +1 -0
  49. package/esm/runtime/transform.d.ts +2 -1
  50. package/esm/runtime/transform.js.map +1 -1
  51. package/esm/spec/data.d.ts +0 -14
  52. package/esm/transforms/base-transform.d.ts +2 -1
  53. package/esm/transforms/base-transform.js +1 -1
  54. package/esm/transforms/base-transform.js.map +1 -1
  55. package/esm/utils/element.d.ts +9 -1
  56. package/esm/utils/element.js +35 -1
  57. package/esm/utils/element.js.map +1 -1
  58. package/esm/utils/layout.d.ts +13 -1
  59. package/esm/utils/layout.js +14 -0
  60. package/esm/utils/layout.js.map +1 -1
  61. package/esm/utils/transform.d.ts +2 -2
  62. package/esm/utils/transform.js +13 -8
  63. package/esm/utils/transform.js.map +1 -1
  64. package/esm/version.d.ts +1 -1
  65. package/esm/version.js +1 -1
  66. package/esm/version.js.map +1 -1
  67. package/lib/animations/executor.js +3 -2
  68. package/lib/animations/executor.js.map +1 -1
  69. package/lib/behaviors/drag-element.js +9 -2
  70. package/lib/behaviors/drag-element.js.map +1 -1
  71. package/lib/elements/combos/base-combo.js +1 -1
  72. package/lib/elements/combos/base-combo.js.map +1 -1
  73. package/lib/elements/edges/base-edge.js +7 -1
  74. package/lib/elements/edges/base-edge.js.map +1 -1
  75. package/lib/elements/shapes/base-shape.d.ts +0 -7
  76. package/lib/elements/shapes/base-shape.js +21 -18
  77. package/lib/elements/shapes/base-shape.js.map +1 -1
  78. package/lib/layouts/types.d.ts +20 -0
  79. package/lib/plugins/background/index.js +1 -0
  80. package/lib/plugins/background/index.js.map +1 -1
  81. package/lib/plugins/grid-line.js +1 -0
  82. package/lib/plugins/grid-line.js.map +1 -1
  83. package/lib/plugins/tooltip.d.ts +6 -0
  84. package/lib/plugins/tooltip.js +8 -6
  85. package/lib/plugins/tooltip.js.map +1 -1
  86. package/lib/runtime/data.d.ts +2 -8
  87. package/lib/runtime/data.js +14 -18
  88. package/lib/runtime/data.js.map +1 -1
  89. package/lib/runtime/element.d.ts +5 -2
  90. package/lib/runtime/element.js +39 -15
  91. package/lib/runtime/element.js.map +1 -1
  92. package/lib/runtime/graph.js +21 -9
  93. package/lib/runtime/graph.js.map +1 -1
  94. package/lib/runtime/layout.d.ts +18 -1
  95. package/lib/runtime/layout.js +84 -19
  96. package/lib/runtime/layout.js.map +1 -1
  97. package/lib/runtime/options.d.ts +9 -0
  98. package/lib/runtime/options.js +52 -0
  99. package/lib/runtime/options.js.map +1 -0
  100. package/lib/runtime/transform.d.ts +2 -1
  101. package/lib/runtime/transform.js.map +1 -1
  102. package/lib/spec/data.d.ts +0 -14
  103. package/lib/transforms/base-transform.d.ts +2 -1
  104. package/lib/transforms/base-transform.js +1 -1
  105. package/lib/transforms/base-transform.js.map +1 -1
  106. package/lib/utils/element.d.ts +9 -1
  107. package/lib/utils/element.js +35 -0
  108. package/lib/utils/element.js.map +1 -1
  109. package/lib/utils/layout.d.ts +13 -1
  110. package/lib/utils/layout.js +15 -0
  111. package/lib/utils/layout.js.map +1 -1
  112. package/lib/utils/transform.d.ts +2 -2
  113. package/lib/utils/transform.js +13 -8
  114. package/lib/utils/transform.js.map +1 -1
  115. package/lib/version.d.ts +1 -1
  116. package/lib/version.js +1 -1
  117. package/lib/version.js.map +1 -1
  118. package/package.json +2 -2
  119. package/src/animations/executor.ts +4 -4
  120. package/src/behaviors/drag-element.ts +9 -2
  121. package/src/elements/combos/base-combo.ts +1 -1
  122. package/src/elements/edges/base-edge.ts +9 -5
  123. package/src/elements/shapes/base-shape.ts +21 -19
  124. package/src/layouts/types.ts +20 -0
  125. package/src/plugins/background/index.ts +1 -0
  126. package/src/plugins/grid-line.ts +1 -0
  127. package/src/plugins/tooltip.ts +11 -4
  128. package/src/runtime/data.ts +14 -19
  129. package/src/runtime/element.ts +62 -13
  130. package/src/runtime/graph.ts +19 -8
  131. package/src/runtime/layout.ts +79 -23
  132. package/src/runtime/options.ts +52 -0
  133. package/src/runtime/transform.ts +2 -0
  134. package/src/spec/data.ts +0 -14
  135. package/src/transforms/base-transform.ts +3 -1
  136. package/src/utils/element.ts +21 -2
  137. package/src/utils/layout.ts +16 -1
  138. package/src/utils/transform.ts +13 -9
  139. package/src/version.ts +1 -1
@@ -4,7 +4,7 @@ import { isEmpty, isFunction, upperFirst } from '@antv/util';
4
4
  import { ExtensionCategory } from '../../constants';
5
5
  import type { Keyframe } from '../../types';
6
6
  import { createAnimationsProxy, preprocessKeyframes } from '../../utils/animation';
7
- import { updateStyle } from '../../utils/element';
7
+ import { setAttributes, updateStyle } from '../../utils/element';
8
8
  import { subObject } from '../../utils/prefix';
9
9
  import { format } from '../../utils/print';
10
10
  import { getSubShapeStyle } from '../../utils/style';
@@ -21,8 +21,8 @@ export interface BaseShapeStyleProps extends BaseStyleProps {}
21
21
  */
22
22
  export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends CustomElement<StyleProps> {
23
23
  constructor(options: DisplayObjectConfig<StyleProps>) {
24
+ applyTransform(options.style);
24
25
  super(options);
25
- this.transformPosition(this.attributes);
26
26
  this.render(this.attributes as Required<StyleProps>, this);
27
27
  this.setVisibility();
28
28
  this.bindEvents();
@@ -116,26 +116,11 @@ export abstract class BaseShape<StyleProps extends BaseShapeStyleProps> extends
116
116
  return target;
117
117
  }
118
118
 
119
- /**
120
- * <zh/> 使用 transform 更新图形位置
121
- *
122
- * <en/> Update the position of the shape using transform
123
- * @param attributes - <zh/> 样式属性 | <en/> style attributes
124
- */
125
- protected transformPosition(attributes: Partial<StyleProps>) {
126
- // Use `transform: translate3d()` instead of `x/y/z`
127
- if ('x' in attributes || 'y' in attributes || 'z' in attributes) {
128
- const { x = 0, y = 0, z = 0, transform } = attributes as any;
129
- const newTransform = replaceTranslateInTransform(+x, +y, +z, transform);
130
- if (newTransform) this.style.transform = newTransform;
131
- }
132
- }
133
-
134
119
  public update(attr: Partial<StyleProps> = {}): void {
135
120
  const attributes = Object.assign({}, this.attributes, attr) as Required<StyleProps>;
136
- this.attr(attributes);
121
+ applyTransform(attributes);
122
+ setAttributes(this, attributes);
137
123
  this.render(attributes, this);
138
- this.transformPosition(attributes);
139
124
  this.setVisibility();
140
125
  }
141
126
 
@@ -331,3 +316,20 @@ export interface UpsertHooks {
331
316
  */
332
317
  afterDestroy?: (instance: DisplayObject) => void;
333
318
  }
319
+
320
+ /**
321
+ * <zh/> 应用 transform
322
+ *
323
+ * <en/> Apply transform
324
+ * @param style - <zh/> 样式 | <en/> style
325
+ * @returns <zh/> 样式 | <en/> style
326
+ */
327
+ function applyTransform(style?: BaseShapeStyleProps) {
328
+ if (!style) return {};
329
+ if ('x' in style || 'y' in style || 'z' in style) {
330
+ const { x = 0, y = 0, z, transform } = style as any;
331
+ const newTransform = replaceTranslateInTransform(x, y, z, transform);
332
+ if (newTransform) style.transform = newTransform;
333
+ }
334
+ return style;
335
+ }
@@ -52,6 +52,26 @@ export interface BaseLayoutOptions extends AnimationOptions, WebWorkerLayoutOpti
52
52
  * @returns <zh/> 是否参与布局 | <en/> Whether to participate in the layout
53
53
  */
54
54
  nodeFilter?: (node: NodeData) => boolean;
55
+ /**
56
+ * <zh/> 使用前布局,在初始化元素前计算布局
57
+ *
58
+ * <en/> Use pre-layout to calculate the layout before initializing the elements
59
+ * @remarks
60
+ * <zh/> 不适用于流水线布局
61
+ *
62
+ * <en/> Not applicable to pipeline layout
63
+ */
64
+ preLayout?: boolean;
65
+ /**
66
+ * <zh/> 不可见节点是否参与布局
67
+ *
68
+ * <en/> Whether invisible nodes participate in the layout
69
+ * @remarks
70
+ * <zh/> 当 preLayout 为 true 时生效
71
+ *
72
+ * <en/> Takes effect when preLayout is true
73
+ */
74
+ isLayoutInvisibleNodes?: boolean;
55
75
  }
56
76
 
57
77
  interface CircularLayout extends BaseLayoutOptions, CircularLayoutOptions {
@@ -24,6 +24,7 @@ export class Background extends BasePlugin<BackgroundOptions> {
24
24
  static defaultOptions: Partial<BackgroundOptions> = {
25
25
  transition: 'background 0.5s',
26
26
  backgroundSize: 'cover',
27
+ zIndex: '-1', // aviod to cover the other plugin's dom, eg: grid-line.
27
28
  };
28
29
 
29
30
  private $element: HTMLElement = createPluginContainer('background');
@@ -136,6 +136,7 @@ export class GridLine extends BasePlugin<GridLineOptions> {
136
136
  border: border ? `${borderLineWidth}px ${borderStyle} ${borderStroke}` : 'none',
137
137
  backgroundImage: `linear-gradient(${stroke} ${lineWidth}px, transparent ${lineWidth}px), linear-gradient(90deg, ${stroke} ${lineWidth}px, transparent ${lineWidth}px)`,
138
138
  backgroundSize: `${size}px ${size}px`,
139
+ backgroundRepeat: 'repeat',
139
140
  });
140
141
  }
141
142
 
@@ -39,6 +39,12 @@ export interface TooltipOptions
39
39
  * @defaultValue true
40
40
  */
41
41
  enable?: boolean | ((event: IElementEvent) => boolean);
42
+ /**
43
+ * <zh/> 显示隐藏的回调
44
+ *
45
+ * <en/> Callback executed when visibility of the tooltip card is changed
46
+ */
47
+ onOpenChange: (open: boolean) => void;
42
48
  }
43
49
 
44
50
  /**
@@ -162,9 +168,7 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
162
168
  // click the same item twice, tooltip will be hidden
163
169
  if (this.currentTarget === id) {
164
170
  this.hide(event);
165
- this.currentTarget = null;
166
171
  } else {
167
- this.currentTarget = id;
168
172
  this.show(event);
169
173
  }
170
174
  };
@@ -190,7 +194,6 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
190
194
  */
191
195
  public onPointerLeave = (event: IElementEvent) => {
192
196
  this.hide(event);
193
- this.currentTarget = null;
194
197
  };
195
198
  /**
196
199
  * <zh/> 移动画布
@@ -200,7 +203,6 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
200
203
  */
201
204
  public onCanvasMove = (event: IElementEvent) => {
202
205
  this.hide(event);
203
- this.currentTarget = null;
204
206
  };
205
207
 
206
208
  private onPointerEnter = (event: IElementEvent) => {
@@ -280,6 +282,7 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
280
282
  }),
281
283
  };
282
284
  }
285
+ this.options.onOpenChange?.(true);
283
286
  this.tooltipElement.update({
284
287
  ...this.tooltipStyleProps,
285
288
  x,
@@ -301,7 +304,9 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
301
304
  public hide = (event?: IElementEvent) => {
302
305
  // if e is undefined, hide the tooltip, external call
303
306
  if (!event) {
307
+ this.options.onOpenChange?.(false);
304
308
  this.tooltipElement?.hide();
309
+ this.currentTarget = null;
305
310
  return;
306
311
  }
307
312
  if (!this.tooltipElement) return;
@@ -310,7 +315,9 @@ export class Tooltip extends BasePlugin<TooltipOptions> {
310
315
  const {
311
316
  client: { x, y },
312
317
  } = event;
318
+ this.options.onOpenChange?.(false);
313
319
  this.tooltipElement.hide(x, y);
320
+ this.currentTarget = null;
314
321
  };
315
322
 
316
323
  private get tooltipStyleProps() {
@@ -608,15 +608,26 @@ export class DataController {
608
608
  });
609
609
  }
610
610
 
611
- public syncNodeDatum(datum: PartialNodeLikeData<NodeData>) {
611
+ public syncNodeLikeDatum(datum: PartialNodeLikeData<NodeData>) {
612
612
  const { model } = this;
613
613
 
614
614
  const id = idOf(datum);
615
+ if (!model.hasNode(id)) return;
615
616
  const original = toG6Data(model.getNode(id));
616
617
  const value = mergeElementsData(original, datum);
617
618
  model.mergeNodeData(id, value);
618
619
  }
619
620
 
621
+ public syncEdgeDatum(datum: PartialEdgeData<EdgeData>) {
622
+ const { model } = this;
623
+
624
+ const id = idOf(datum);
625
+ if (!model.hasEdge(id)) return;
626
+ const original = toG6Data(model.getEdge(id));
627
+ const value = mergeElementsData(original, datum);
628
+ model.mergeEdgeData(id, value);
629
+ }
630
+
620
631
  public updateEdgeData(edges: PartialEdgeData<EdgeData>[] = []) {
621
632
  if (!edges.length) return;
622
633
  const { model } = this;
@@ -679,8 +690,8 @@ export class DataController {
679
690
 
680
691
  if (originalParentId !== parent && hierarchyKey === COMBO_KEY) {
681
692
  const modifiedDatum = { id, combo: parent };
682
- if (this.isCombo(id)) this.syncComboDatum(modifiedDatum);
683
- else this.syncNodeDatum(modifiedDatum);
693
+ if (this.isCombo(id)) this.syncNodeLikeDatum(modifiedDatum);
694
+ else this.syncNodeLikeDatum(modifiedDatum);
684
695
  }
685
696
 
686
697
  this.model.setParent(id, parent, hierarchyKey);
@@ -713,22 +724,6 @@ export class DataController {
713
724
  });
714
725
  }
715
726
 
716
- /**
717
- * <zh/> 将 combo 数据同步到 model 中
718
- *
719
- * <en/> Synchronize combo data to the model
720
- * @param datum - <zh/> combo 数据 | <en/> combo data
721
- */
722
- public syncComboDatum(datum: PartialNodeLikeData<ComboData>) {
723
- const { model } = this;
724
-
725
- const id = idOf(datum);
726
- if (!model.hasNode(id)) return;
727
- const original = toG6Data(model.getNode(id));
728
- const value = mergeElementsData(original, datum);
729
- model.mergeNodeData(id, value);
730
- }
731
-
732
727
  public getElementPosition(id: ID): Point {
733
728
  const datum = this.getElementDataById(id) as NodeLikeData;
734
729
  return positionOf(datum);
@@ -262,28 +262,68 @@ export class ElementController {
262
262
  return style;
263
263
  }
264
264
 
265
+ private getDrawData(context: DrawContext): DrawPayload | null {
266
+ this.init();
267
+
268
+ const data = this.computeChangesAndDrawData(context);
269
+ if (!data) return null;
270
+
271
+ const { type = 'draw', stage = type } = context;
272
+ this.markDestroyElement(data.drawData);
273
+ // 计算样式 / Calculate style
274
+ this.computeStyle(stage);
275
+
276
+ return { type, stage, data };
277
+ }
278
+
265
279
  /**
266
280
  * <zh/> 开始绘制流程
267
281
  *
268
282
  * <en/> start render process
269
283
  */
270
284
  public draw(context: DrawContext = { animation: true }) {
271
- this.init();
285
+ const drawData = this.getDrawData(context);
286
+ if (!drawData) return;
272
287
 
273
- const data = this.computeChangesAndDrawData(context);
274
- if (!data) return null;
288
+ const {
289
+ data: {
290
+ drawData: { add, update, remove },
291
+ },
292
+ } = drawData;
293
+
294
+ this.destroyElements(remove, context);
295
+ this.createElements(add, context);
296
+ this.updateElements(update, context);
297
+
298
+ return this.setAnimationTask(context, drawData);
299
+ }
300
+
301
+ public async preLayoutDraw(context: DrawContext = { animation: true }) {
302
+ const preResult = this.getDrawData(context);
303
+ if (!preResult) return;
304
+
305
+ const {
306
+ data: { drawData },
307
+ } = preResult;
308
+
309
+ await this.context.layout?.preLayout?.(drawData);
275
310
 
276
- const { type = 'draw', stage = type, animation, silence } = context;
277
- const { dataChanges, drawData } = data;
278
- this.markDestroyElement(drawData);
279
- // 计算样式 / Calculate style
280
- this.computeStyle(stage);
281
- // 创建渲染任务 / Create render task
282
311
  const { add, update, remove } = drawData;
283
312
  this.destroyElements(remove, context);
284
313
  this.createElements(add, context);
285
314
  this.updateElements(update, context);
286
315
 
316
+ return this.setAnimationTask(context, preResult);
317
+ }
318
+
319
+ private setAnimationTask(context: DrawContext, data: DrawPayload) {
320
+ const { animation, silence } = context;
321
+ const {
322
+ data: { dataChanges, drawData },
323
+ stage,
324
+ type,
325
+ } = data;
326
+
287
327
  return this.context.animation!.animate(
288
328
  animation,
289
329
  silence
@@ -618,17 +658,17 @@ export class ElementController {
618
658
  *
619
659
  * <en/> collapse node
620
660
  * @param id - <zh/> 元素 ID | <en/> element ID
621
- * @param animation - <zh/> 是否使用动画,默认为 true | <en/> Whether to use animation, default is true
661
+ * @param options - <zh/> 选项 | <en/> options
622
662
  */
623
663
  public async collapseNode(id: ID, options: CollapseExpandNodeOptions): Promise<void> {
624
664
  const { animation, align } = options;
625
665
 
626
666
  const { model, layout } = this.context;
627
667
 
628
- const preLayoutData = this.computeChangesAndDrawData({ stage: 'collapse', animation });
629
- if (!preLayoutData) return;
668
+ const simulateLayoutData = this.computeChangesAndDrawData({ stage: 'collapse', animation });
669
+ if (!simulateLayoutData) return;
630
670
 
631
- this.markDestroyElement(preLayoutData.drawData);
671
+ this.markDestroyElement(simulateLayoutData.drawData);
632
672
 
633
673
  // 进行预布局,计算出所有元素的位置
634
674
  // Perform pre-layout to calculate the position of all elements
@@ -837,6 +877,15 @@ export interface DrawContext {
837
877
  target?: ID;
838
878
  }
839
879
 
880
+ interface DrawPayload {
881
+ data: {
882
+ dataChanges: DataChange[];
883
+ drawData: DrawData;
884
+ };
885
+ stage: AnimationStage;
886
+ type: 'render' | 'draw';
887
+ }
888
+
840
889
  /**
841
890
  * <zh/> 展开/收起节点选项
842
891
  *
@@ -44,6 +44,7 @@ import { sizeOf } from '../utils/dom';
44
44
  import { getSubgraphRelatedEdges } from '../utils/edge';
45
45
  import { GraphLifeCycleEvent, emit } from '../utils/event';
46
46
  import { idOf } from '../utils/id';
47
+ import { isPreLayout } from '../utils/layout';
47
48
  import { format } from '../utils/print';
48
49
  import { subtract } from '../utils/vector';
49
50
  import { getZIndexOf } from '../utils/z-index';
@@ -56,13 +57,14 @@ import { DataController } from './data';
56
57
  import type { CollapseExpandNodeOptions } from './element';
57
58
  import { ElementController } from './element';
58
59
  import { LayoutController } from './layout';
60
+ import { inferOptions } from './options';
59
61
  import { PluginController } from './plugin';
60
62
  import { TransformController } from './transform';
61
63
  import { RuntimeContext } from './types';
62
64
  import { ViewportController } from './viewport';
63
65
 
64
66
  export class Graph extends EventEmitter {
65
- private options: GraphOptions;
67
+ private options: GraphOptions = {};
66
68
 
67
69
  /**
68
70
  * @internal
@@ -96,8 +98,7 @@ export class Graph extends EventEmitter {
96
98
 
97
99
  constructor(options: GraphOptions) {
98
100
  super();
99
- this.options = Object.assign({}, Graph.defaultOptions, options);
100
- this._setOptions(this.options, true);
101
+ this._setOptions(Object.assign({}, Graph.defaultOptions, options), true);
101
102
  this.context.graph = this;
102
103
 
103
104
  // Listening resize to autoResize.
@@ -132,13 +133,13 @@ export class Graph extends EventEmitter {
132
133
 
133
134
  private _setOptions(options: GraphOptions, isInit: boolean) {
134
135
  this.updateCanvas(options);
136
+ Object.assign(this.options, inferOptions(options));
135
137
 
136
138
  if (isInit) {
137
139
  const { data } = options;
138
140
  if (data) this.addData(data);
139
141
  return;
140
142
  }
141
- Object.assign(this.options, options);
142
143
  const { behaviors, combo, data, edge, layout, node, plugins, theme, transforms } = options;
143
144
  if (behaviors) this.setBehaviors(behaviors);
144
145
  if (data) this.setData(data);
@@ -1131,9 +1132,19 @@ export class Graph extends EventEmitter {
1131
1132
  public async render(): Promise<void> {
1132
1133
  await this.prepare();
1133
1134
  emit(this, new GraphLifeCycleEvent(GraphEvent.BEFORE_RENDER));
1134
- const animation = this.context.element!.draw({ type: 'render' });
1135
- await Promise.all([animation?.finished, this.context.layout!.layout()]);
1136
- await this.autoFit();
1135
+
1136
+ if (!this.options.layout) {
1137
+ const animation = this.context.element!.draw({ type: 'render' });
1138
+ await Promise.all([animation?.finished, this.autoFit()]);
1139
+ } else if (!this.rendered && isPreLayout(this.options.layout)) {
1140
+ const animation = await this.context.element!.preLayoutDraw({ type: 'render' });
1141
+ await Promise.all([animation?.finished, this.autoFit()]);
1142
+ } else {
1143
+ const animation = this.context.element!.draw({ type: 'render' });
1144
+ await Promise.all([animation?.finished, this.context.layout!.postLayout()]);
1145
+ await this.autoFit();
1146
+ }
1147
+
1137
1148
  this.rendered = true;
1138
1149
  emit(this, new GraphLifeCycleEvent(GraphEvent.AFTER_RENDER));
1139
1150
  }
@@ -1165,7 +1176,7 @@ export class Graph extends EventEmitter {
1165
1176
  * @apiCategory layout
1166
1177
  */
1167
1178
  public async layout() {
1168
- await this.context.layout!.layout();
1179
+ await this.context.layout!.postLayout();
1169
1180
  }
1170
1181
 
1171
1182
  /**
@@ -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
  *
@@ -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
  }