Skip to content

Commit d30e116

Browse files
authored
fix(autolayout): fixed autolayout with sockets (#546)
1 parent 7182f35 commit d30e116

2 files changed

Lines changed: 44 additions & 24 deletions

File tree

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/utils.ts

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -986,9 +986,15 @@ export const applyAutoLayoutSmooth = (
986986
options: LayoutOptions & {
987987
animationDuration?: number
988988
isSidebarCollapsed?: boolean
989+
onComplete?: (finalPositions: Map<string, { x: number; y: number }>) => void
989990
} = {}
990991
): void => {
991-
const { animationDuration = 500, isSidebarCollapsed = false, ...layoutOptions } = options
992+
const {
993+
animationDuration = 500,
994+
isSidebarCollapsed = false,
995+
onComplete,
996+
...layoutOptions
997+
} = options
992998

993999
if (!layoutOptions.handleOrientation || layoutOptions.handleOrientation === 'auto') {
9941000
layoutOptions.handleOrientation = detectHandleOrientation(blocks)
@@ -1066,13 +1072,18 @@ export const applyAutoLayoutSmooth = (
10661072
if (progress < 1) {
10671073
requestAnimationFrame(animate)
10681074
} else {
1069-
await resizeLoopNodes()
1075+
resizeLoopNodes()
10701076

10711077
const padding = isSidebarCollapsed ? 0.35 : 0.55
1072-
await fitView({
1078+
fitView({
10731079
padding,
10741080
duration: 400,
10751081
})
1082+
1083+
// Call completion callback with final positions
1084+
if (onComplete) {
1085+
onComplete(allPositions)
1086+
}
10761087
}
10771088
}
10781089

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/workflow.tsx

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -231,38 +231,39 @@ const WorkflowContent = React.memo(() => {
231231
[getNodes]
232232
)
233233

234+
// Helper function to get orientation config
235+
const getOrientationConfig = useCallback((orientation: string) => {
236+
return orientation === 'vertical'
237+
? {
238+
// Vertical handles: optimize for top-to-bottom flow
239+
horizontalSpacing: 400,
240+
verticalSpacing: 300,
241+
startX: 200,
242+
startY: 200,
243+
}
244+
: {
245+
// Horizontal handles: optimize for left-to-right flow
246+
horizontalSpacing: 600,
247+
verticalSpacing: 200,
248+
startX: 150,
249+
startY: 300,
250+
}
251+
}, [])
252+
234253
// Auto-layout handler
235254
const handleAutoLayout = useCallback(() => {
236255
if (Object.keys(blocks).length === 0) return
237256

238257
// Detect the predominant handle orientation in the workflow
239258
const detectedOrientation = detectHandleOrientation(blocks)
240259

241-
// Optimize spacing based on handle orientation
242-
const orientationConfig = useMemo(
243-
() =>
244-
detectedOrientation === 'vertical'
245-
? {
246-
// Vertical handles: optimize for top-to-bottom flow
247-
horizontalSpacing: 400,
248-
verticalSpacing: 300,
249-
startX: 200,
250-
startY: 200,
251-
}
252-
: {
253-
// Horizontal handles: optimize for left-to-right flow
254-
horizontalSpacing: 600,
255-
verticalSpacing: 200,
256-
startX: 150,
257-
startY: 300,
258-
},
259-
[detectedOrientation]
260-
)
260+
// Get spacing configuration based on handle orientation
261+
const orientationConfig = getOrientationConfig(detectedOrientation)
261262

262263
applyAutoLayoutSmooth(
263264
blocks,
264265
edges,
265-
collaborativeUpdateBlockPosition,
266+
storeUpdateBlockPosition,
266267
fitView,
267268
resizeLoopNodesWrapper,
268269
{
@@ -271,6 +272,12 @@ const WorkflowContent = React.memo(() => {
271272
animationDuration: 500, // Smooth 500ms animation
272273
isSidebarCollapsed,
273274
handleOrientation: detectedOrientation, // Explicitly set the detected orientation
275+
onComplete: (finalPositions) => {
276+
// Emit collaborative updates for final positions after animation completes
277+
finalPositions.forEach((position, blockId) => {
278+
collaborativeUpdateBlockPosition(blockId, position)
279+
})
280+
},
274281
}
275282
)
276283

@@ -286,10 +293,12 @@ const WorkflowContent = React.memo(() => {
286293
}, [
287294
blocks,
288295
edges,
296+
storeUpdateBlockPosition,
289297
collaborativeUpdateBlockPosition,
290298
fitView,
291299
isSidebarCollapsed,
292300
resizeLoopNodesWrapper,
301+
getOrientationConfig,
293302
])
294303

295304
const debouncedAutoLayout = useCallback(() => {

0 commit comments

Comments
 (0)