schyma
Version:
JSON Schemas Visualizer React component
30 lines • 1.1 kB
JavaScript
import { Position } from 'reactflow';
import { nodeHeight, nodeWidth } from '../constants/node';
import dagre from '@dagrejs/dagre';
export const getLayoutedElements = (nodes, edges, direction = 'LR') => {
const dagreGraph = new dagre.graphlib.Graph();
dagreGraph.setDefaultEdgeLabel(() => ({}));
dagreGraph.setGraph({ rankdir: direction });
nodes.forEach((node) => {
dagreGraph.setNode(node.id, { width: nodeWidth, height: nodeHeight });
});
edges.forEach((edge) => {
dagreGraph.setEdge(edge.source, edge.target);
});
dagre.layout(dagreGraph, {
disableOptimalOrderHeuristic: true,
});
nodes.forEach((node) => {
const nodeId = node.id;
const nodeWithPosition = dagreGraph.node(nodeId);
node.sourcePosition = Position.Right;
node.targetPosition = Position.Left;
node.position = {
x: nodeWithPosition.x - nodeWidth / 3,
y: nodeWithPosition.y - nodeHeight / 3,
};
return node;
});
return { nodes, edges };
};
//# sourceMappingURL=dagreUtils.js.map