-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Use
dagre
library for auto-layout in React Flow trees
The way we were previously implementing layout was messy, and contained arbitrary hardcoded values. The file `useLayout.ts` came almost directly from the React Flow Pro example at https://reactflow.dev/docs/examples/layout/auto-layout/. But we had to add two `as never` annotation to get it accepted by TypeScript's strict mode. Note that the dodgy dynamic typing is confined to within the module, so the API is safe. Also note that we can't just turn off strict mode for a single module or definition: microsoft/TypeScript#28306.
- Loading branch information
Showing
4 changed files
with
95 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useMemo } from "react"; | ||
import { Node, Edge, Position } from "react-flow-renderer"; | ||
import dagre from "dagre"; | ||
|
||
// the layout direction (T = top, R = right, B = bottom, L = left, TB = top to bottom, ...) | ||
export type Direction = "TB" | "LR" | "RL" | "BT"; | ||
|
||
export type Options = { | ||
direction: Direction; | ||
}; | ||
|
||
const dagreGraph = new dagre.graphlib!.Graph(); | ||
dagreGraph.setDefaultEdgeLabel(() => ({})); | ||
|
||
const nodeWidth = 100; | ||
const nodeHeight = 50; | ||
|
||
const positionMap = { | ||
T: Position.Top, | ||
L: Position.Left, | ||
R: Position.Right, | ||
B: Position.Bottom, | ||
}; | ||
|
||
function layoutGraph( | ||
nodes: Node[], | ||
edges: Edge[], | ||
{ direction = "TB" }: Options | ||
) { | ||
dagreGraph.setGraph({ rankdir: direction }); | ||
|
||
nodes.forEach((el) => { | ||
dagreGraph.setNode(el.id, { width: nodeWidth, height: nodeHeight }); | ||
}); | ||
|
||
edges.forEach((el) => { | ||
dagreGraph.setEdge(el.source, el.target); | ||
}); | ||
|
||
dagre.layout(dagreGraph); | ||
|
||
const layoutNodes = nodes.map((node) => { | ||
const nodeWithPosition = dagreGraph.node(node.id); | ||
node.targetPosition = positionMap[direction[0] as never]; | ||
node.sourcePosition = positionMap[direction[1] as never]; | ||
|
||
node.position = { | ||
x: nodeWithPosition.x - nodeWidth / 2, | ||
y: nodeWithPosition.y - nodeHeight / 2, | ||
}; | ||
|
||
return node; | ||
}); | ||
|
||
return layoutNodes; | ||
} | ||
|
||
function useLayout(nodes: Node[], edges: Edge[], options: Options) { | ||
return useMemo( | ||
() => layoutGraph(nodes, edges, options), | ||
[nodes, edges, options] | ||
); | ||
} | ||
|
||
export default useLayout; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.