2024-08-22 00:56:58 +02:00
|
|
|
import dagre from "dagre"
|
2024-08-16 13:56:52 +02:00
|
|
|
|
2024-08-22 00:56:58 +02:00
|
|
|
export const getLayoutedElements = (nodes: any, edges: any, direction = 'TB') => {
|
|
|
|
|
const dagreGraph = new dagre.graphlib.Graph()
|
|
|
|
|
dagreGraph.setDefaultEdgeLabel(() => ({}))
|
2024-08-16 13:56:52 +02:00
|
|
|
|
2024-08-22 00:56:58 +02:00
|
|
|
const nodeWidth = 172
|
|
|
|
|
const nodeHeight = 200
|
2024-08-16 13:56:52 +02:00
|
|
|
|
|
|
|
|
const isHorizontal = direction === 'LR';
|
|
|
|
|
dagreGraph.setGraph({rankdir: direction});
|
|
|
|
|
|
|
|
|
|
nodes.forEach((node: any) => {
|
|
|
|
|
dagreGraph.setNode(node.id, {width: nodeWidth, height: nodeHeight});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
edges.forEach((edge: any) => {
|
|
|
|
|
dagreGraph.setEdge(edge.source, edge.target);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
dagre.layout(dagreGraph);
|
|
|
|
|
|
|
|
|
|
const newNodes = nodes.map((node: any) => {
|
|
|
|
|
const nodeWithPosition = dagreGraph.node(node.id)
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
...node,
|
|
|
|
|
targetPosition: isHorizontal ? 'left' : 'top',
|
|
|
|
|
sourcePosition: isHorizontal ? 'right' : 'bottom',
|
|
|
|
|
position: {
|
|
|
|
|
x: nodeWithPosition.x - nodeWidth / 2,
|
|
|
|
|
y: nodeWithPosition.y - nodeHeight / 2
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return {nodes: newNodes, edges};
|
|
|
|
|
}
|