forked from idootop/reactflow-auto-layout
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelk.ts
More file actions
134 lines (120 loc) · 3.67 KB
/
elk.ts
File metadata and controls
134 lines (120 loc) · 3.67 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
126
127
128
129
130
131
132
133
134
import { getIncomers } from '@xyflow/react';
import ELK from 'elkjs/lib/elk.bundled.js';
import type { ReactflowNodeWithData } from '@/data/types';
import { getEdgeLayouted, getNodeLayouted, getNodeSize } from '../../metadata';
import type { LayoutAlgorithm, LayoutAlgorithmProps } from '..';
const algorithms = {
'elk-layered': 'layered',
'elk-mr-tree': 'mrtree',
};
const elk = new ELK({ algorithms: Object.values(algorithms) });
export type ELKLayoutAlgorithms = 'elk-layered' | 'elk-mr-tree';
export const layoutELK = async (
props: LayoutAlgorithmProps & { algorithm?: ELKLayoutAlgorithms },
) => {
const {
nodes,
edges,
direction,
visibility,
spacing,
algorithm = 'elk-mr-tree',
} = props;
const isHorizontal = direction === 'horizontal';
const subWorkflowRootNodes: ReactflowNodeWithData[] = [];
const layoutNodes = nodes.map((node) => {
const incomers = getIncomers(node, nodes, edges);
if (incomers.length < 1) {
// Node without input is the root node of sub-workflow
subWorkflowRootNodes.push(node);
}
const { widthWithDefault, heightWithDefault } = getNodeSize(node);
const sourcePorts = node.data.sourceHandles.map((id) => ({
id,
properties: {
side: isHorizontal ? 'EAST' : 'SOUTH',
},
}));
const targetPorts = node.data.targetHandles.map((id) => ({
id,
properties: {
side: isHorizontal ? 'WEST' : 'NORTH',
},
}));
return {
id: node.id,
width: widthWithDefault,
height: heightWithDefault,
ports: [...targetPorts, ...sourcePorts],
properties: {
'org.eclipse.elk.portConstraints': 'FIXED_ORDER',
},
};
});
const layoutEdges = edges.map((edge) => {
return {
id: edge.id,
sources: [edge.sourceHandle || edge.source],
targets: [edge.targetHandle || edge.target],
};
});
// Connect sub-workflows' root nodes to the rootNode
const rootNode: any = { id: '#root', width: 1, height: 1 };
layoutNodes.push(rootNode);
for (const subWorkflowRootNode of subWorkflowRootNodes) {
layoutEdges.push({
id: `${rootNode.id}-${subWorkflowRootNode.id}`,
sources: [rootNode.id],
targets: [subWorkflowRootNode.id],
});
}
const layouted = await elk
.layout({
id: '@root',
children: layoutNodes,
edges: layoutEdges,
layoutOptions: {
// - https://www.eclipse.org/elk/reference/algorithms.html
'elk.algorithm': algorithms[algorithm],
'elk.direction': isHorizontal ? 'RIGHT' : 'DOWN',
// - https://www.eclipse.org/elk/reference/options.html
'elk.spacing.nodeNode': isHorizontal
? spacing.y.toString()
: spacing.x.toString(),
'elk.layered.spacing.nodeNodeBetweenLayers': isHorizontal
? spacing.x.toString()
: spacing.y.toString(),
},
})
.catch((e) => {
console.log('❌ ELK layout failed', e);
});
if (!layouted?.children) {
return;
}
const layoutedNodePositions = layouted.children.reduce(
(pre, v) => {
pre[v.id] = {
x: v.x ?? 0,
y: v.y ?? 0,
};
return pre;
},
{} as Record<string, { x: number; y: number }>,
);
return {
nodes: nodes.map((node) => {
const position = layoutedNodePositions[node.id];
return getNodeLayouted({ node, position, direction, visibility });
}),
edges: edges.map((edge) => getEdgeLayouted({ edge, visibility })),
};
};
export const kElkAlgorithms: Record<string, LayoutAlgorithm> = Object.keys(
algorithms,
).reduce((pre, algorithm) => {
pre[algorithm] = (props: any) => {
return layoutELK({ ...props, algorithm });
};
return pre;
}, {} as any);