Skip to content

Commit 542c95c

Browse files
Now initial flow layout is the same as the built in react flow auto layout (#508)
1 parent 15eb36a commit 542c95c

1 file changed

Lines changed: 35 additions & 12 deletions

File tree

  • src/main/frontend/app/routes/studio/canvas

src/main/frontend/app/routes/studio/canvas/flow.tsx

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
type OnConnectEnd,
1111
ReactFlow,
1212
ReactFlowProvider,
13+
useNodesInitialized,
1314
useReactFlow,
1415
useUpdateNodeInternals,
1516
} from '@xyflow/react'
@@ -221,8 +222,10 @@ function FlowCanvas({ onOpenInEditor }: { onOpenInEditor: () => void }) {
221222
const reactFlow = useReactFlow()
222223
const reactFlowRef = useRef(reactFlow)
223224
reactFlowRef.current = reactFlow
225+
const nodesInitialized = useNodesInitialized()
224226
const canvasRef = useRef<HTMLDivElement>(null)
225227
const fitAfterLayoutRef = useRef<{ id: string }[] | null>(null)
228+
const pendingInitialRelayoutRef = useRef<{ pendingSelection: { subtype: string; name: string } | null } | null>(null)
226229

227230
const applySelectionToNodes = useCallback((pendingSelection: { subtype: string; name: string }) => {
228231
const currentNodes = useFlowStore.getState().nodes
@@ -574,6 +577,36 @@ function FlowCanvas({ onOpenInEditor }: { onOpenInEditor: () => void }) {
574577
flowStore.setNodes(laidOut)
575578
}, [layoutGraph])
576579

580+
useEffect(() => {
581+
if (!nodesInitialized || !pendingInitialRelayoutRef.current) return
582+
583+
const { pendingSelection } = pendingInitialRelayoutRef.current
584+
pendingInitialRelayoutRef.current = null
585+
586+
const flowStore = useFlowStore.getState()
587+
const nodesWithResetPositions = flowStore.nodes.map((node) =>
588+
node.type === 'frankNode' || node.type === 'exitNode' ? { ...node, position: { x: 0, y: 0 } } : node,
589+
)
590+
const laidOutNodes = layoutGraph(nodesWithResetPositions, flowStore.edges, 'LR')
591+
flowStore.setNodes(laidOutNodes)
592+
593+
if (pendingSelection) {
594+
applySelectionToNodes(pendingSelection)
595+
} else {
596+
waitForStableCanvasDimensions((canvasWidth, canvasHeight) => {
597+
const freshViewport = computeAdapterCenteredViewport(laidOutNodes, canvasWidth, canvasHeight)
598+
useFlowStore.getState().setViewport(freshViewport)
599+
reactFlowRef.current?.setViewport(freshViewport)
600+
})
601+
}
602+
}, [
603+
nodesInitialized,
604+
layoutGraph,
605+
waitForStableCanvasDimensions,
606+
computeAdapterCenteredViewport,
607+
applySelectionToNodes,
608+
])
609+
577610
const getFullySelectedGroupIds = useCallback(
578611
(parentIds: (string | undefined)[], selectedNodes: FlowNode[]) => {
579612
return parentIds.filter((parentId) => {
@@ -1285,20 +1318,10 @@ function FlowCanvas({ onOpenInEditor }: { onOpenInEditor: () => void }) {
12851318
const adapterJson = await convertAdapterXmlToJson(adapter)
12861319

12871320
flowStore.setEdges(adapterJson.edges)
1288-
const laidOutNodes = layoutGraph(adapterJson.nodes, adapterJson.edges, 'LR')
1289-
flowStore.setNodes(laidOutNodes)
1321+
flowStore.setNodes(adapterJson.nodes)
12901322
flowStore.setHistory([])
12911323
flowStore.setFuture([])
1292-
1293-
if (pendingSelection) {
1294-
applySelectionToNodes(pendingSelection)
1295-
} else {
1296-
waitForStableCanvasDimensions((canvasWidth, canvasHeight) => {
1297-
const freshViewport = computeAdapterCenteredViewport(laidOutNodes, canvasWidth, canvasHeight)
1298-
useFlowStore.getState().setViewport(freshViewport)
1299-
reactFlowRef.current?.setViewport(freshViewport)
1300-
})
1301-
}
1324+
pendingInitialRelayoutRef.current = { pendingSelection }
13021325
}
13031326

13041327
async function loadFlowFromTab(tab: TabData) {

0 commit comments

Comments
 (0)