Skip to content

Commit fc4859c

Browse files
committed
refactor: adjust types
1 parent 9d243da commit fc4859c

File tree

9 files changed

+119
-151
lines changed

9 files changed

+119
-151
lines changed

packages/layout/src/d3Force/index.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
Node,
66
LayoutMapping,
77
OutNode,
8+
OutEdge,
89
D3ForceLayoutOptions,
910
Edge,
1011
LayoutWithIterations,
@@ -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

+10-10
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,17 @@ export class DagreLayout implements Layout<DagreLayoutOptions> {
6868
private async genericDagreLayout(
6969
assign: false,
7070
graph: IGraph,
71-
options?: DagreLayoutOptions
71+
options?: DagreLayoutOptions,
7272
): Promise<LayoutMapping>;
7373
private async genericDagreLayout(
7474
assign: true,
7575
graph: IGraph,
76-
options?: DagreLayoutOptions
76+
options?: DagreLayoutOptions,
7777
): Promise<void>;
7878
private async genericDagreLayout(
7979
assign: boolean,
8080
graph: IGraph,
81-
options?: DagreLayoutOptions
81+
options?: DagreLayoutOptions,
8282
): Promise<LayoutMapping | void> {
8383
const mergedOptions = { ...this.options, ...options };
8484
const {
@@ -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
})),
@@ -469,7 +469,7 @@ export class DagreLayout implements Layout<DagreLayoutOptions> {
469469
layerCoordsArr,
470470
isHorizontal,
471471
isDifferentLayer,
472-
filterControlPointsOutOfBoundary
472+
filterControlPointsOutOfBoundary,
473473
);
474474
}
475475
});
@@ -525,8 +525,8 @@ const getControlPoints = (
525525
filterControlPointsOutOfBoundary: (
526526
ps: Point[],
527527
point1: Point,
528-
point2: Point
529-
) => Point[]
528+
point2: Point,
529+
) => Point[],
530530
) => {
531531
let controlPoints = points?.slice(1, points.length - 1) || []; // 去掉头尾
532532
// 酌情增加控制点,使折线不穿过跨层的节点
@@ -575,7 +575,7 @@ const getControlPoints = (
575575
controlPoints = filterControlPointsOutOfBoundary(
576576
controlPoints,
577577
sourceNode.data as Point,
578-
targetNode.data as Point
578+
targetNode.data as Point,
579579
);
580580
// one controlPoint at least
581581
if (!controlPoints.length) {
@@ -588,7 +588,7 @@ const getControlPoints = (
588588
: {
589589
x: sourceX,
590590
y: (sourceY! + targetY!) / 2,
591-
}) as Point
591+
}) as Point,
592592
);
593593
}
594594
} else if (layerDiff > 1) {

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

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

44
const adjust = (g: Graph, rankdir: DagreRankdir) => {
55
const rd = rankdir.toLowerCase();
6-
if (rd === "lr" || rd === "rl") {
6+
if (rd === 'lr' || rd === 'rl') {
77
swapWidthHeight(g);
88
}
99
};
1010

1111
const undo = (g: Graph, rankdir: DagreRankdir) => {
1212
const rd = rankdir.toLowerCase();
13-
if (rd === "bt" || rd === "rl") {
13+
if (rd === 'bt' || rd === 'rl') {
1414
reverseY(g);
1515
}
1616

17-
if (rd === "lr" || rd === "rl") {
17+
if (rd === 'lr' || rd === 'rl') {
1818
swapXY(g);
1919
swapWidthHeight(g);
2020
}
@@ -41,14 +41,14 @@ const reverseY = (g: Graph) => {
4141
});
4242

4343
g.getAllEdges().forEach((edge) => {
44-
edge.data.points?.forEach((point) => reverseYOne(point));
45-
if (edge.data.hasOwnProperty("y")) {
44+
edge.data.points?.forEach((point: Point) => reverseYOne(point));
45+
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));
64-
if (edge.data.hasOwnProperty("x")) {
63+
edge.data.points?.forEach((point: Point) => swapXYOne(point));
64+
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Point,
1010
OutNode,
1111
LayoutWithIterations,
12+
OutEdge,
1213
} from '../types';
1314
import { formatNumberFn, isArray } from '../util';
1415
import { forceNBody } from './forceNBody';
@@ -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;

0 commit comments

Comments
 (0)