@antv/g6 5.0.51 → 5.1.0-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.
@@ -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.1';