forked from idootop/reactflow-auto-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathd3-dag.ts
More file actions
125 lines (110 loc) · 3.57 KB
/
d3-dag.ts
File metadata and controls
125 lines (110 loc) · 3.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import { getIncomers, type Node } from '@xyflow/react';
import { graphStratify, sugiyama } from 'd3-dag';
import type { ReactflowNodeWithData } from '@/data/types';
import { getEdgeLayouted, getNodeLayouted, getNodeSize } from '../../metadata';
import type { LayoutAlgorithm, LayoutAlgorithmProps } from '..';
type NodeWithPosition = ReactflowNodeWithData & { x: number; y: number };
// Since d3-dag layout algorithm does not support multiple root nodes,
// we attach the sub-workflows to the global rootNode.
const rootNode: NodeWithPosition = {
id: '#root',
x: 0,
y: 0,
position: { x: 0, y: 0 },
data: {} as any,
};
const algorithms = {
'd3-dag': 'd3-dag',
'ds-dag(s)': 'ds-dag(s)',
};
export type D3DAGLayoutAlgorithms = 'd3-dag' | 'ds-dag(s)';
export const layoutD3DAG = async (
props: LayoutAlgorithmProps & { algorithm?: D3DAGLayoutAlgorithms },
) => {
const {
nodes,
edges,
direction,
visibility,
spacing,
algorithm = 'd3-dag',
} = props;
const isHorizontal = direction === 'horizontal';
const initialNodes = [] as NodeWithPosition[];
let maxNodeWidth = 0;
let maxNodeHeight = 0;
for (const node of nodes) {
const { widthWithDefault, heightWithDefault } = getNodeSize(node);
initialNodes.push({
...node,
...node.position,
width: widthWithDefault,
height: heightWithDefault,
});
maxNodeWidth = Math.max(maxNodeWidth, widthWithDefault);
maxNodeHeight = Math.max(maxNodeHeight, heightWithDefault);
}
// Since d3-dag does not support horizontal layout,
// we swap the width and height of nodes and interchange x and y mappings based on the layout direction.
const nodeSize: any = isHorizontal
? [maxNodeHeight + spacing.y, maxNodeWidth + spacing.x]
: [maxNodeWidth + spacing.x, maxNodeHeight + spacing.y];
const getParentIds = (node: Node) => {
if (node.id === rootNode.id) {
return undefined;
}
// Node without input is the root node of sub-workflow, and we should connect it to the rootNode
const incomers = getIncomers(node, nodes, edges);
if (incomers.length < 1) {
return [rootNode.id];
}
return algorithm === 'd3-dag'
? [incomers[0]?.id]
: incomers.map((e) => e.id);
};
const stratify = graphStratify();
const dag = stratify(
[rootNode, ...initialNodes].map((node) => {
return {
id: node.id,
parentIds: getParentIds(node),
};
}),
);
const layout = sugiyama().nodeSize(nodeSize);
layout(dag);
const layoutNodes = new Map<string, any>();
for (const node of dag.nodes()) {
layoutNodes.set(node.data.id, node);
}
return {
nodes: nodes.map((node) => {
const { x, y } = layoutNodes.get(node.id);
// Interchange x and y mappings based on the layout direction.
const position = isHorizontal ? { x: y, y: x } : { x, y };
return getNodeLayouted({
node,
position,
direction,
visibility,
fixPosition: ({ x, y, width, height }) => {
// This algorithm uses the center coordinate of the node as the reference point,
// which needs adjustment for ReactFlow's topLeft coordinate system.
return {
x: x - width / 2,
y: y - height / 2,
};
},
});
}),
edges: edges.map((edge) => getEdgeLayouted({ edge, visibility })),
};
};
export const kD3DAGAlgorithms: Record<string, LayoutAlgorithm> = Object.keys(
algorithms,
).reduce((pre, algorithm) => {
pre[algorithm] = (props: any) => {
return layoutD3DAG({ ...props, algorithm });
};
return pre;
}, {} as any);