-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp
More file actions
116 lines (106 loc) · 2.74 KB
/
temp
File metadata and controls
116 lines (106 loc) · 2.74 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
import ELK from 'elkjs/lib/elk.bundled.js';
import React, { useCallback } from 'react';
import {
ReactFlow,
ReactFlowProvider,
Panel,
useNodesState,
useEdgesState,
useReactFlow,
} from '@xyflow/react';
import { initialNodes, initialEdges } from './nodes-edges.js';
import '@xyflow/react/dist/style.css';
const elk = new ELK();
const useLayoutedElements = () => {
const { getNodes, setNodes, getEdges, fitView } = useReactFlow();
const defaultOptions = {
'elk.algorithm': 'layered',
'elk.layered.spacing.nodeNodeBetweenLayers': 100,
'elk.spacing.nodeNode': 80,
};
const getLayoutedElements = useCallback((options) => {
const layoutOptions = { ...defaultOptions, ...options };
const graph = {
id: 'root',
layoutOptions: layoutOptions,
children: getNodes().map((node) => ({
...node,
width: node.measured.width,
height: node.measured.height,
})),
edges: getEdges(),
};
elk.layout(graph).then(({ children }) => {
// By mutating the children in-place we saves ourselves from creating a
// needless copy of the nodes array.
children.forEach((node) => {
node.position = { x: node.x, y: node.y };
});
setNodes(children);
fitView();
});
}, []);
return { getLayoutedElements };
};
const LayoutFlow = () => {
const [nodes, , onNodesChange] = useNodesState(initialNodes);
const [edges, , onEdgesChange] = useEdgesState(initialEdges);
const { getLayoutedElements } = useLayoutedElements();
return (
<ReactFlow
nodes={nodes}
edges={edges}
onNodesChange={onNodesChange}
onEdgesChange={onEdgesChange}
fitView
>
<Panel position="top-right">
<button
onClick={() =>
getLayoutedElements({
'elk.algorithm': 'layered',
'elk.direction': 'DOWN',
})
}
>
vertical layout
</button>
<button
onClick={() =>
getLayoutedElements({
'elk.algorithm': 'layered',
'elk.direction': 'RIGHT',
})
}
>
horizontal layout
</button>
<button
onClick={() =>
getLayoutedElements({
'elk.algorithm': 'org.eclipse.elk.radial',
})
}
>
radial layout
</button>
<button
onClick={() =>
getLayoutedElements({
'elk.algorithm': 'org.eclipse.elk.force',
})
}
>
force layout
</button>
</Panel>
</ReactFlow>
);
};
export default function () {
return (
<ReactFlowProvider>
<LayoutFlow />
</ReactFlowProvider>
);
}