@antv/g6 5.0.51 → 5.1.0-beta.0

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.
@@ -1,22 +1,22 @@
1
1
  import type { IAnimation } from '@antv/g';
2
2
  import { Graph as Graphlib } from '@antv/graphlib';
3
- import { Supervisor, isLayoutWithIterations } from '@antv/layout';
3
+ import { isLayoutWithIterations } from '@antv/layout';
4
4
  import { deepMix } from '@antv/util';
5
5
  import { COMBO_KEY, GraphEvent, TREE_KEY } from '../constants';
6
6
  import { BaseLayout } from '../layouts';
7
- import type { AntVLayout } from '../layouts/types';
7
+ import { AntVLayout } from '../layouts/types';
8
8
  import { getExtension } from '../registry/get';
9
9
  import type { GraphData, LayoutOptions, NodeData } from '../spec';
10
10
  import type { STDLayoutOptions } from '../spec/layout';
11
11
  import type { DrawData } from '../transforms/types';
12
- import type { AdaptiveLayout, ID, TreeData } from '../types';
12
+ import type { ID, TreeData } from '../types';
13
13
  import { getAnimationOptions } from '../utils/animation';
14
14
  import { isCollapsed } from '../utils/collapsibility';
15
15
  import { isToBeDestroyed } from '../utils/element';
16
- import { GraphLifeCycleEvent, emit } from '../utils/event';
16
+ import { emit, GraphLifeCycleEvent } from '../utils/event';
17
17
  import { createTreeStructure } from '../utils/graphlib';
18
18
  import { idOf } from '../utils/id';
19
- import { isTreeLayout, layoutAdapter, layoutMapping2GraphData } from '../utils/layout';
19
+ import { isLegacyAntVLayout, isTreeLayout, layoutAdapter, legacyLayoutAdapter } from '../utils/layout';
20
20
  import { print } from '../utils/print';
21
21
  import { dfs } from '../utils/traverse';
22
22
  import type { RuntimeContext } from './types';
@@ -24,8 +24,6 @@ import type { RuntimeContext } from './types';
24
24
  export class LayoutController {
25
25
  private context: RuntimeContext;
26
26
 
27
- private supervisor?: Supervisor;
28
-
29
27
  private instance?: BaseLayout;
30
28
 
31
29
  private instances: BaseLayout[] = [];
@@ -100,6 +98,7 @@ export class LayoutController {
100
98
  const pipeline = Array.isArray(layoutOptions) ? layoutOptions : [layoutOptions];
101
99
  const { graph } = this.context;
102
100
  emit(graph, new GraphLifeCycleEvent(GraphEvent.BEFORE_LAYOUT, { type: 'post' }));
101
+
103
102
  for (let index = 0; index < pipeline.length; index++) {
104
103
  const options = pipeline[index];
105
104
  const data = this.getLayoutData(options);
@@ -154,7 +153,7 @@ export class LayoutController {
154
153
  }
155
154
 
156
155
  private async graphLayout(data: GraphData, options: STDLayoutOptions, index: number): Promise<GraphData> {
157
- const { animation, enableWorker, iterations = 300 } = options;
156
+ const { animation, iterations = 300 } = options;
158
157
 
159
158
  const layout = this.initGraphLayout(options);
160
159
  if (!layout) return {};
@@ -162,20 +161,13 @@ export class LayoutController {
162
161
  this.instances[index] = layout;
163
162
  this.instance = layout;
164
163
 
165
- // 使用 web worker 执行布局 / Use web worker to execute layout
166
- if (enableWorker) {
167
- const rawLayout = layout as unknown as AdaptiveLayout;
168
- this.supervisor = new Supervisor(rawLayout.graphData2LayoutModel(data), rawLayout.instance, { iterations });
169
- return layoutMapping2GraphData(await this.supervisor.execute());
170
- }
171
-
172
164
  if (isLayoutWithIterations(layout)) {
173
165
  // 有动画,基于布局迭代 tick 更新位置 / Update position based on layout iteration tick
174
166
  if (animation) {
175
167
  return await layout.execute(data, {
176
- onTick: (tickData: GraphData) => {
177
- this.updateElementPosition(tickData, false);
178
- },
168
+ animate: true,
169
+ maxIteration: iterations,
170
+ onTick: (tickData: GraphData) => this.updateElementPosition(tickData, false),
179
171
  });
180
172
  }
181
173
 
@@ -187,6 +179,7 @@ export class LayoutController {
187
179
 
188
180
  // 无迭代的布局,直接返回终态位置 / Layout without iteration, return final position directly
189
181
  const layoutResult = await layout.execute(data);
182
+
190
183
  if (animation) {
191
184
  const animationResult = this.updateElementPosition(layoutResult, animation);
192
185
  await animationResult?.finished;
@@ -285,11 +278,6 @@ export class LayoutController {
285
278
  this.instance = undefined;
286
279
  }
287
280
 
288
- if (this.supervisor) {
289
- this.supervisor.stop();
290
- this.supervisor = undefined;
291
- }
292
-
293
281
  if (this.animationResult) {
294
282
  this.animationResult.finish();
295
283
  this.animationResult = undefined;
@@ -351,7 +339,7 @@ export class LayoutController {
351
339
  */
352
340
  private initGraphLayout(options: STDLayoutOptions) {
353
341
  const { element, viewport } = this.context;
354
- const { type, enableWorker, animation, iterations, ...restOptions } = options;
342
+ const { type, animation, iterations, ...restOptions } = options;
355
343
 
356
344
  const [width, height] = viewport!.getCanvasSize();
357
345
  const center = [width / 2, height / 2];
@@ -370,9 +358,12 @@ export class LayoutController {
370
358
  const STDCtor =
371
359
  Object.getPrototypeOf(Ctor.prototype) === BaseLayout.prototype
372
360
  ? Ctor
373
- : layoutAdapter(Ctor as new (options?: Record<string, unknown>) => AntVLayout, this.context);
361
+ : isLegacyAntVLayout(Ctor)
362
+ ? legacyLayoutAdapter(Ctor, this.context)
363
+ : layoutAdapter(Ctor as new (options?: Record<string, unknown>) => AntVLayout, this.context);
374
364
 
375
365
  const layout = new STDCtor(this.context);
366
+
376
367
  const config = { nodeSize, width, height, center };
377
368
 
378
369
  switch (layout.id) {
@@ -402,8 +393,6 @@ export class LayoutController {
402
393
  this.stopLayout();
403
394
  // @ts-expect-error force delete
404
395
  this.context = {};
405
- this.supervisor?.kill();
406
- this.supervisor = undefined;
407
396
  this.instance = undefined;
408
397
  this.instances = [];
409
398
  this.animationResult = undefined;
@@ -1,12 +1,9 @@
1
- import type { Graph as LayoutModel } from '@antv/layout';
2
- import type { AntVLayout } from '../layouts/types';
1
+ import type { AntVLayout, LegacyAntVLayout } from '../layouts/types';
3
2
  import type { GraphData } from '../spec/data';
4
3
  import type { STDLayoutOptions } from '../spec/layout';
5
4
 
6
5
  export interface AdaptiveLayout {
7
- instance: AntVLayout;
6
+ instance: AntVLayout | LegacyAntVLayout;
8
7
 
9
8
  execute(model: GraphData, options?: STDLayoutOptions): Promise<GraphData>;
10
-
11
- graphData2LayoutModel(data: GraphData): LayoutModel;
12
9
  }
@@ -1,16 +1,16 @@
1
- import { Graph as Graphlib } from '@antv/graphlib';
1
+ import { Edge, Graph as Graphlib, Node } from '@antv/graphlib';
2
2
  import { deepMix, isNumber } from '@antv/util';
3
3
  import { COMBO_KEY } from '../constants';
4
4
  import { BaseLayout } from '../layouts/base-layout';
5
5
  import { idOf } from './id';
6
- import { parsePoint } from './point';
7
6
 
8
- import type { LayoutMapping, Graph as LayoutModel, Node as LayoutNodeData } from '@antv/layout';
9
- import type { AntVLayout } from '../layouts/types';
7
+ import type { AntVGraphData, AntVLayout, LegacyAntVLayout, LegacyGraph } from '../layouts/types';
10
8
  import type { RuntimeContext } from '../runtime/types';
11
- import type { GraphData } from '../spec/data';
9
+ import type { EdgeData, GraphData, NodeData } from '../spec/data';
10
+ import type { NodeStyle } from '../spec/element/node';
12
11
  import type { LayoutOptions, STDLayoutOptions } from '../spec/layout';
13
12
  import type { AdaptiveLayout, ID } from '../types';
13
+ import { parsePoint } from './point';
14
14
 
15
15
  /**
16
16
  * <zh/> 判断是否是 combo 布局
@@ -64,6 +64,111 @@ export function isPreLayout(options?: LayoutOptions) {
64
64
  return !Array.isArray(options) && options?.preLayout;
65
65
  }
66
66
 
67
+ /**
68
+ * <zh/> 将 @antv/layout 布局适配为 G6 布局
69
+ *
70
+ * <en/> Adapt @antv/layout layout to G6 layout
71
+ * @param Ctor - <zh/> 布局类 | <en/> Layout class
72
+ * @param context - <zh/> 运行时上下文 | <en/> Runtime context
73
+ * @returns <zh/> G6 布局类 | <en/> G6 layout class
74
+ */
75
+ export function layoutAdapter(
76
+ Ctor: new (options: Record<string, unknown>) => AntVLayout,
77
+ context: RuntimeContext,
78
+ ): new (context: RuntimeContext, options?: Record<string, unknown>) => BaseLayout {
79
+ class AdaptLayout extends BaseLayout implements AdaptiveLayout {
80
+ public instance: AntVLayout;
81
+
82
+ public id: string;
83
+
84
+ constructor(context: RuntimeContext, options?: Record<string, unknown>) {
85
+ super(context, options);
86
+ this.instance = new Ctor({});
87
+ this.id = this.instance.id;
88
+
89
+ if ('stop' in this.instance && 'tick' in this.instance) {
90
+ const instance = this.instance;
91
+ this.stop = instance.stop.bind(instance);
92
+ this.tick = (iterations?: number) => {
93
+ instance.tick(iterations);
94
+ return this.getLayoutResult(instance);
95
+ };
96
+ }
97
+ }
98
+
99
+ public async execute(model: GraphData, options?: STDLayoutOptions): Promise<GraphData> {
100
+ await this.instance.execute(
101
+ this.graphData2LayoutModel(model),
102
+ this.transformOptions(deepMix({}, this.options, options)),
103
+ );
104
+ return this.getLayoutResult(this.instance);
105
+ }
106
+
107
+ private graphData2LayoutModel(data: GraphData): AntVGraphData {
108
+ const { nodes = [], edges = [], combos = [] } = data;
109
+
110
+ return {
111
+ nodes: [...nodes, ...combos],
112
+ edges,
113
+ };
114
+ }
115
+
116
+ private transformOptions(options: STDLayoutOptions) {
117
+ const isCombo = (id: string) => context.model.isCombo(id);
118
+
119
+ const defaultNode = (datum: NodeData) => {
120
+ const { style } = datum || {};
121
+ const parentId = 'combo' in (datum || {}) ? (datum.combo ?? null) : null;
122
+ const id = idOf(datum);
123
+
124
+ return {
125
+ id,
126
+ ...(isNumber(style?.x) ? { x: style.x } : {}),
127
+ ...(isNumber(style?.y) ? { y: style.y } : {}),
128
+ ...(isNumber(style?.z) ? { z: style.z } : {}),
129
+ parentId,
130
+ ...(isCombo(id) ? { isCombo: true } : {}),
131
+ };
132
+ };
133
+
134
+ const defaultEdge = (datum: EdgeData) => ({ id: idOf(datum), source: datum.source, target: datum.target });
135
+
136
+ options.node = defaultNode;
137
+ options.edge = defaultEdge;
138
+
139
+ if (!('onTick' in options)) return options;
140
+
141
+ const onTick = options.onTick as (data: GraphData) => void;
142
+ options.onTick = (layout: AntVLayout) => onTick(this.getLayoutResult(layout));
143
+ return options;
144
+ }
145
+
146
+ private getLayoutResult(layout: AntVLayout): GraphData {
147
+ const { model } = this.context;
148
+
149
+ const result: GraphData = { nodes: [], edges: [], combos: [] };
150
+
151
+ layout.forEachNode((node) => {
152
+ const nodeId = String(node.id);
153
+ const target = model.isCombo(nodeId) ? result.combos : result.nodes;
154
+
155
+ const style = { x: node.x, y: node.y, z: node.z ?? 0, ...(node.size ? { size: node.size } : {}) } as NodeStyle;
156
+
157
+ target?.push({ id: nodeId, style });
158
+ });
159
+
160
+ layout.forEachEdge((edge) => {
161
+ const style = { controlPoints: edge.points || [] };
162
+ result.edges!.push({ id: String(edge.id), source: String(edge.source), target: String(edge.target), style });
163
+ });
164
+
165
+ return result;
166
+ }
167
+ }
168
+
169
+ return AdaptLayout;
170
+ }
171
+
67
172
  /**
68
173
  * <zh/> 将图布局结果转换为 G6 数据
69
174
  *
@@ -71,8 +176,8 @@ export function isPreLayout(options?: LayoutOptions) {
71
176
  * @param layoutMapping - <zh/> 布局映射 | <en/> Layout mapping
72
177
  * @returns <zh/> G6 数据 | <en/> G6 data
73
178
  */
74
- export function layoutMapping2GraphData(layoutMapping: LayoutMapping): GraphData {
75
- const { nodes, edges } = layoutMapping;
179
+ export function layoutMapping2GraphData(layoutMapping: AntVGraphData): GraphData {
180
+ const { nodes, edges = [] } = layoutMapping;
76
181
  const data: GraphData = { nodes: [], edges: [], combos: [] };
77
182
 
78
183
  nodes.forEach((nodeLike) => {
@@ -109,6 +214,19 @@ export function layoutMapping2GraphData(layoutMapping: LayoutMapping): GraphData
109
214
  return data;
110
215
  }
111
216
 
217
+ /**
218
+ * <zh/> 判断是否为 AntV Layout 1.x
219
+ *
220
+ * <en/> Determine if it is AntV Layout 1.x
221
+ * @param Ctor - <zh/> 布局类 | <en/> Layout class
222
+ * @returns <zh/> 是否为 AntV Layout 1.x | <en/> Whether it is AntV Layout 1.x
223
+ */
224
+ export function isLegacyAntVLayout(
225
+ Ctor: new (options: Record<string, unknown>) => unknown,
226
+ ): Ctor is new (options: Record<string, unknown>) => LegacyAntVLayout {
227
+ return !('forEachNode' in Ctor.prototype) && !('forEachEdge' in Ctor.prototype);
228
+ }
229
+
112
230
  /**
113
231
  * <zh/> 将 @antv/layout 布局适配为 G6 布局
114
232
  *
@@ -117,12 +235,12 @@ export function layoutMapping2GraphData(layoutMapping: LayoutMapping): GraphData
117
235
  * @param context - <zh/> 运行时上下文 | <en/> Runtime context
118
236
  * @returns <zh/> G6 布局类 | <en/> G6 layout class
119
237
  */
120
- export function layoutAdapter(
121
- Ctor: new (options: Record<string, unknown>) => AntVLayout,
238
+ export function legacyLayoutAdapter(
239
+ Ctor: new (options: Record<string, unknown>) => LegacyAntVLayout,
122
240
  context: RuntimeContext,
123
241
  ): new (context: RuntimeContext, options?: Record<string, unknown>) => BaseLayout {
124
242
  class AdaptLayout extends BaseLayout implements AdaptiveLayout {
125
- public instance: AntVLayout;
243
+ public instance: LegacyAntVLayout;
126
244
 
127
245
  public id: string;
128
246
 
@@ -135,7 +253,7 @@ export function layoutAdapter(
135
253
  const instance = this.instance;
136
254
  this.stop = instance.stop.bind(instance);
137
255
  this.tick = (iterations?: number) => {
138
- const tickResult = instance.tick(iterations);
256
+ const tickResult = instance.tick?.(iterations);
139
257
  return layoutMapping2GraphData(tickResult);
140
258
  };
141
259
  }
@@ -153,13 +271,13 @@ export function layoutAdapter(
153
271
  private transformOptions(options: STDLayoutOptions) {
154
272
  if (!('onTick' in options)) return options;
155
273
  const onTick = options.onTick as (data: GraphData) => void;
156
- options.onTick = (data: LayoutMapping) => onTick(layoutMapping2GraphData(data));
274
+ options.onTick = (data: AntVGraphData) => onTick(layoutMapping2GraphData(data));
157
275
  return options;
158
276
  }
159
277
 
160
- public graphData2LayoutModel(data: GraphData): LayoutModel {
278
+ private graphData2LayoutModel(data: GraphData): LegacyGraph {
161
279
  const { nodes = [], edges = [], combos = [] } = data;
162
- const nodesToLayout: LayoutNodeData[] = nodes.map((datum) => {
280
+ const nodesToLayout: Node<NodeData>[] = nodes.map((datum) => {
163
281
  const id = idOf(datum);
164
282
  const { data, style, combo, ...rest } = datum;
165
283
 
@@ -193,14 +311,24 @@ export function layoutAdapter(
193
311
  })
194
312
  .map((edge) => {
195
313
  const { source, target, data, style } = edge;
196
- return { id: idOf(edge), source, target, data: { ...data }, style: { ...style } };
314
+ return {
315
+ id: idOf(edge),
316
+ source,
317
+ target,
318
+ data: { ...data },
319
+ style: { ...style },
320
+ } as unknown as Edge<EdgeData>;
197
321
  });
198
322
 
199
- const combosToLayout: LayoutNodeData[] = combos.map((combo) => {
200
- return { id: idOf(combo), data: { _isCombo: true, ...combo.data }, style: { ...combo.style } };
323
+ const combosToLayout: Node<NodeData>[] = combos.map((combo) => {
324
+ return {
325
+ id: idOf(combo),
326
+ data: { _isCombo: true, ...combo.data },
327
+ style: { ...combo.style },
328
+ } as unknown as Node<NodeData>;
201
329
  });
202
330
 
203
- const layoutModel = new Graphlib({
331
+ const layoutModel = new Graphlib<NodeData, EdgeData>({
204
332
  nodes: [...nodesToLayout, ...combosToLayout],
205
333
  edges: edgesToLayout,
206
334
  });
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const version = '5.0.51';
1
+ export const version = '5.1.0-beta.0';
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2018 Alipay.inc
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.