mirror of
https://github.com/langgenius/dify.git
synced 2026-05-04 01:18:05 +08:00
layout
This commit is contained in:
@ -3,6 +3,7 @@ import {
|
||||
getConnectedEdges,
|
||||
getOutgoers,
|
||||
} from 'reactflow'
|
||||
import dagre from 'dagre'
|
||||
import type {
|
||||
Edge,
|
||||
Node,
|
||||
@ -118,3 +119,26 @@ export const getNodesPositionMap = (nodes: Node[], edges: Edge[]) => {
|
||||
|
||||
return positionMap
|
||||
}
|
||||
|
||||
export const getLayoutByDagre = (nodes: Node[], edges: Edge[]) => {
|
||||
const dagreGraph = new dagre.graphlib.Graph()
|
||||
dagreGraph.setGraph({
|
||||
rankdir: 'LR',
|
||||
align: 'UL',
|
||||
nodesep: 40,
|
||||
ranksep: 64,
|
||||
})
|
||||
nodes.forEach((node) => {
|
||||
dagreGraph.setNode(node.id, { width: node.width, height: node.height })
|
||||
})
|
||||
|
||||
edges.forEach((edge) => {
|
||||
dagreGraph.setEdge(edge.source, edge.target, {
|
||||
weight: edge?.data?.weight || 1,
|
||||
})
|
||||
})
|
||||
|
||||
dagre.layout(dagreGraph)
|
||||
|
||||
return dagreGraph
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user