Skip to content

Commit 1cd9c41

Browse files
committed
refactor: adjust types
1 parent 4dd01d3 commit 1cd9c41

File tree

9 files changed

+43
-79
lines changed

9 files changed

+43
-79
lines changed

packages/layout/src/d3Force/index.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { isFunction, isNumber, isObject } from '@antv/util';
22
import * as d3Force from 'd3-force';
3-
import {
3+
import type {
44
D3ForceLayoutOptions,
55
Edge,
66
Graph,
77
LayoutMapping,
88
LayoutWithIterations,
99
Node,
10+
OutEdge,
1011
OutNode,
1112
} from '../types';
1213
import { cloneFormatData, isArray } from '../util';
@@ -62,7 +63,7 @@ export class D3ForceLayout
6263
private forceSimulation: d3Force.Simulation<any, any>;
6364

6465
private lastLayoutNodes: CalcNode[];
65-
private lastLayoutEdges: Edge[];
66+
private lastLayoutEdges: OutEdge[];
6667
private lastAssign: boolean;
6768
private lastGraph: Graph;
6869

@@ -146,7 +147,7 @@ export class D3ForceLayout
146147
y: node.data?.y,
147148
} as CalcNode),
148149
);
149-
const layoutEdges: Edge[] = edges.map((edge) => cloneFormatData(edge));
150+
const layoutEdges: OutEdge[] = edges.map((edge) => cloneFormatData(edge));
150151

151152
// Use them later in `tick`.
152153
this.lastLayoutNodes = layoutNodes;

packages/layout/src/dagre.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export class DagreLayout implements Layout<DagreLayoutOptions> {
192192
if (minY > node.data.y!) minY = node.data.y!;
193193
});
194194
g.getAllEdges().forEach((edge) => {
195-
edge.data.points?.forEach((point) => {
195+
edge.data.points?.forEach((point: Point) => {
196196
if (minX > point.x) minX = point.x;
197197
if (minY > point.y) minY = point.y;
198198
});
@@ -460,7 +460,7 @@ export class DagreLayout implements Layout<DagreLayoutOptions> {
460460
// if (i <= -1) return;
461461
if (edgeLabelSpace && controlPoints && edge.data.type !== 'loop') {
462462
edge.data.controlPoints = getControlPoints(
463-
edge.data.points?.map(({ x, y }) => ({
463+
edge.data.points?.map(({ x, y }: Point) => ({
464464
x: x + layoutTopLeft[0],
465465
y: y + layoutTopLeft[1],
466466
})),

packages/layout/src/dagre/coordinate-system.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Node } from '@antv/graphlib';
2-
import { DagreRankdir, Graph, NodeData } from '../types';
2+
import type { DagreRankdir, Graph, NodeData, Point } from '../types';
33

44
const adjust = (g: Graph, rankdir: DagreRankdir) => {
55
const rd = rankdir.toLowerCase();
@@ -41,14 +41,14 @@ const reverseY = (g: Graph) => {
4141
});
4242

4343
g.getAllEdges().forEach((edge) => {
44-
edge.data.points?.forEach((point) => reverseYOne(point));
44+
edge.data.points?.forEach((point: Point) => reverseYOne(point));
4545
if (edge.data.hasOwnProperty('y')) {
4646
reverseYOne(edge.data);
4747
}
4848
});
4949
};
5050

51-
const reverseYOne = (node: { x?: number; y?: number }) => {
51+
const reverseYOne = (node: any) => {
5252
if (node?.y) {
5353
node.y = -node.y;
5454
}
@@ -60,14 +60,14 @@ const swapXY = (g: Graph) => {
6060
});
6161

6262
g.getAllEdges().forEach((edge) => {
63-
edge.data.points?.forEach((point) => swapXYOne(point));
63+
edge.data.points?.forEach((point: Point) => swapXYOne(point));
6464
if (edge.data.hasOwnProperty('x')) {
6565
swapXYOne(edge.data);
6666
}
6767
});
6868
};
6969

70-
const swapXYOne = (node: { x?: number; y?: number }) => {
70+
const swapXYOne = (node: any) => {
7171
const x = node.x;
7272
node.x = node.y;
7373
node.y = x;

packages/layout/src/dagre/layout.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { Graph, ID } from '@antv/graphlib';
1+
import { Edge as IEdge, Graph, ID } from '@antv/graphlib';
22
import { isNil } from '@antv/util';
33
import {
44
DagreAlign,
55
DagreRankdir,
6+
EdgeData,
67
Graph as IGraph,
78
NodeData,
89
Point,
@@ -438,7 +439,7 @@ const translateGraph = (
438439
});
439440

440441
g.getAllEdges().forEach((edge) => {
441-
edge.data.points?.forEach((p) => {
442+
edge.data.points?.forEach((p: Point) => {
442443
p.x -= minX;
443444
p.y -= minY;
444445
});
@@ -553,7 +554,7 @@ const insertSelfEdges = (g: IGraph) => {
553554
layer?.forEach((v: ID, i: number) => {
554555
const node = g.getNode(v)!;
555556
node.data.order = i + orderShift;
556-
node.data.selfEdges?.forEach((selfEdge) => {
557+
node.data.selfEdges?.forEach((selfEdge: IEdge<EdgeData>) => {
557558
addDummyNode(
558559
g,
559560
'selfedge',

packages/layout/src/force/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Graph as IGraph } from '@antv/graphlib';
22
import { isFunction, isNumber, isObject } from '@antv/util';
3-
import {
3+
import type {
44
Edge,
55
ForceLayoutOptions,
66
Graph,
77
LayoutMapping,
88
LayoutWithIterations,
99
Node,
10+
OutEdge,
1011
OutNode,
1112
Point,
1213
} from '../types';
@@ -67,7 +68,7 @@ export class ForceLayout implements LayoutWithIterations<ForceLayoutOptions> {
6768

6869
private running: boolean = false;
6970
private lastLayoutNodes: OutNode[];
70-
private lastLayoutEdges: Edge[];
71+
private lastLayoutEdges: OutEdge[];
7172
private lastAssign: boolean;
7273
private lastCalcGraph: IGraph<CalcNodeData, CalcEdgeData>;
7374
private lastGraph: Graph;

packages/layout/src/forceAtlas2/index.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import type {
88
Layout,
99
LayoutMapping,
1010
Node,
11+
OutEdge,
12+
OutEdgeData,
1113
OutNode,
1214
OutNodeData,
1315
PointTuple,
@@ -125,8 +127,8 @@ export class ForceAtlas2Layout implements Layout<ForceAtlas2LayoutOptions> {
125127
const calcEdges = edges.filter((edge: Edge) => {
126128
const { source, target } = edge;
127129
return source !== target;
128-
});
129-
const calcGraph = new GGraph<OutNodeData, EdgeData>({
130+
}) as OutEdge[];
131+
const calcGraph = new GGraph<OutNodeData, OutEdgeData>({
130132
nodes: calcNodes,
131133
edges: calcEdges,
132134
});

packages/layout/src/fruchterman.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { Graph as IGraph } from '@antv/graphlib';
22
import { isNumber } from '@antv/util';
33
import type {
4-
Edge,
54
EdgeData,
65
FruchtermanLayoutOptions,
76
Graph,
87
LayoutMapping,
98
LayoutWithIterations,
9+
OutEdge,
10+
OutEdgeData,
1011
OutNode,
1112
OutNodeData,
1213
Point,
13-
PointTuple,
14+
PointTuple
1415
} from './types';
1516
import { cloneFormatData } from './util';
1617

@@ -75,7 +76,7 @@ export class FruchtermanLayout
7576

7677
private running: boolean = false;
7778
private lastLayoutNodes: OutNode[];
78-
private lastLayoutEdges: Edge[];
79+
private lastLayoutEdges: OutEdge[];
7980
private lastAssign: boolean;
8081
private lastGraph: IGraph<OutNodeData, EdgeData>;
8182
private lastOptions: FormattedOptions;
@@ -212,7 +213,7 @@ export class FruchtermanLayout
212213
const layoutNodes: OutNode[] = nodes.map(
213214
(node) => cloneFormatData(node, [width, height]) as OutNode,
214215
);
215-
const calcGraph = new IGraph<OutNodeData, EdgeData>({
216+
const calcGraph = new IGraph<OutNodeData, OutEdgeData>({
216217
nodes: layoutNodes,
217218
edges,
218219
});

packages/layout/src/types.ts

+12-52
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,46 @@
11
import {
22
Edge as IEdge,
33
Graph as IGraph,
4-
GraphView as IGraphView,
54
ID,
65
Node as INode,
7-
PlainObject,
86
} from '@antv/graphlib';
97

10-
export interface NodeData extends PlainObject {
11-
size?: number | number[];
12-
bboxSize?: number[];
13-
borderLeft?: ID | ID[];
14-
borderRight?: ID | ID[];
8+
export interface NodeData {
159
x?: number;
1610
y?: number;
1711
z?: number;
18-
height?: number;
19-
width?: number;
20-
e?: IEdge<EdgeData>;
21-
selfEdges?: IEdge<EdgeData>[];
22-
rank?: number;
23-
_rank?: number;
24-
order?: number;
25-
fixorder?: number;
26-
minRank?: number;
27-
maxRank?: number;
28-
layout?: boolean;
29-
layer?: number;
30-
low?: number;
31-
lim?: number;
12+
size?: number | number[];
13+
[key: string]: any;
14+
}
15+
16+
export interface EdgeData {
17+
weight?: number;
18+
[keys: string]: any;
3219
}
3320

3421
export interface OutNodeData extends NodeData {
3522
x: number;
3623
y: number;
37-
z?: number;
3824
}
3925

40-
export interface EdgeData extends PlainObject {
41-
// temp edges e.g. the edge generated for releated collapsed combo
42-
virtual?: boolean;
43-
weight?: number;
44-
x?: number;
45-
y?: number;
46-
height?: number;
47-
width?: number;
48-
points?: Point[];
49-
controlPoints?: Point[];
50-
minlen?: number;
51-
cutvalue?: number;
52-
labeloffset?: number;
53-
}
26+
export interface OutEdgeData extends EdgeData {}
5427

55-
/** input node */
5628
export type Node = INode<NodeData>;
57-
/** output node */
58-
export type OutNode = INode<OutNodeData>;
59-
/** input and output edge */
6029
export type Edge = IEdge<EdgeData>;
61-
62-
export type Degree = {
63-
in: number;
64-
out: number;
65-
all: number;
66-
};
30+
export type OutNode = INode<OutNodeData>;
31+
export type OutEdge = IEdge<OutEdgeData>;
6732

6833
// maps node's id and its index in the nodes array
6934
export type IndexMap = {
7035
[nodeId: string]: number;
7136
};
7237

7338
export type Graph = IGraph<NodeData, EdgeData>;
74-
export type GraphView = IGraphView<NodeData, EdgeData>;
7539

7640
export type PointTuple = [number, number] | [number, number, number];
7741
export type Point = { x: number; y: number; z?: number };
7842
export type Matrix = number[];
79-
export type LayoutMapping = { nodes: OutNode[]; edges: Edge[] };
43+
export type LayoutMapping = { nodes: OutNode[]; edges: OutEdge[] };
8044

8145
export interface Layout<LayoutOptions> {
8246
/**
@@ -119,10 +83,6 @@ export interface LayoutWithIterations<LayoutOptions>
11983
tick: (iterations?: number) => LayoutMapping;
12084
}
12185

122-
export interface LayoutConstructor<LayoutOptions> {
123-
new (options?: LayoutOptions): Layout<LayoutOptions>;
124-
}
125-
12686
export interface LayoutSupervisor {
12787
execute(): Promise<LayoutMapping>;
12888
stop(): void;

packages/layout/src/util/index.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
export * from './array';
2-
export * from './function';
3-
export * from './math';
4-
export * from './number';
5-
export * from './object';
6-
export * from './string';
1+
export * from "./array";
2+
export * from "./math";
3+
export * from "./object";
4+
export * from "./function";

0 commit comments

Comments
 (0)