Skip to content

Commit 07e0baa

Browse files
icecrasher321Sg312
authored andcommitted
fix(embedded): block layout should not be dependent on viewport (#3621)
* fix(embedded): block layout should not be dependent on viewport * address comments
1 parent 8d82415 commit 07e0baa

File tree

1 file changed

+75
-3
lines changed
  • apps/sim/app/workspace/[workspaceId]/w/[workflowId]

1 file changed

+75
-3
lines changed

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

Lines changed: 75 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ const reactFlowStyles = [
208208
'[&_.react-flow__node-subflowNode.selected]:!shadow-none',
209209
].join(' ')
210210
const reactFlowFitViewOptions = { padding: 0.6, maxZoom: 1.0 } as const
211-
const embeddedFitViewOptions = { padding: 0.15, maxZoom: 0.85, minZoom: 0.35 } as const
211+
const embeddedFitViewOptions = { padding: 0.15, maxZoom: 0.85, minZoom: 0.1 } as const
212+
const embeddedResizeFitViewOptions = { ...embeddedFitViewOptions, duration: 0 } as const
212213
const reactFlowProOptions = { hideAttribution: true } as const
213214

214215
/**
@@ -244,7 +245,10 @@ const WorkflowContent = React.memo(
244245
const [potentialParentId, setPotentialParentId] = useState<string | null>(null)
245246
const [selectedEdges, setSelectedEdges] = useState<SelectedEdgesMap>(new Map())
246247
const [isErrorConnectionDrag, setIsErrorConnectionDrag] = useState(false)
248+
const canvasContainerRef = useRef<HTMLDivElement>(null)
247249
const selectedIdsRef = useRef<string[] | null>(null)
250+
const embeddedFitFrameRef = useRef<number | null>(null)
251+
const hasCompletedInitialEmbeddedFitRef = useRef(false)
248252
const canvasMode = useCanvasModeStore((state) => state.mode)
249253
const isHandMode = embedded ? true : canvasMode === 'hand'
250254
const { handleCanvasMouseDown, selectionProps } = useShiftSelectionLock({ isHandMode })
@@ -373,6 +377,34 @@ const WorkflowContent = React.memo(
373377
]
374378
)
375379

380+
const scheduleEmbeddedFit = useCallback(() => {
381+
if (!embedded || !isWorkflowReady) return
382+
383+
if (embeddedFitFrameRef.current !== null) {
384+
cancelAnimationFrame(embeddedFitFrameRef.current)
385+
}
386+
387+
embeddedFitFrameRef.current = requestAnimationFrame(() => {
388+
embeddedFitFrameRef.current = null
389+
390+
const container = canvasContainerRef.current
391+
if (!container) return
392+
393+
const rect = container.getBoundingClientRect()
394+
if (rect.width <= 0 || rect.height <= 0) return
395+
396+
const nodes = reactFlowInstance.getNodes()
397+
if (nodes.length > 0) {
398+
void reactFlowInstance.fitView(embeddedResizeFitViewOptions)
399+
}
400+
401+
if (!hasCompletedInitialEmbeddedFitRef.current) {
402+
hasCompletedInitialEmbeddedFitRef.current = true
403+
setIsCanvasReady(true)
404+
}
405+
})
406+
}, [embedded, isWorkflowReady, reactFlowInstance])
407+
376408
const {
377409
getNodeDepth,
378410
getNodeAbsolutePosition,
@@ -3750,10 +3782,46 @@ const WorkflowContent = React.memo(
37503782
activeWorkflowId,
37513783
])
37523784

3785+
useEffect(() => {
3786+
if (!embedded || !isWorkflowReady) {
3787+
return
3788+
}
3789+
3790+
const container = canvasContainerRef.current
3791+
if (!container) {
3792+
return
3793+
}
3794+
3795+
scheduleEmbeddedFit()
3796+
3797+
const resizeObserver = new ResizeObserver(() => {
3798+
scheduleEmbeddedFit()
3799+
})
3800+
3801+
resizeObserver.observe(container)
3802+
3803+
return () => {
3804+
resizeObserver.disconnect()
3805+
3806+
if (embeddedFitFrameRef.current !== null) {
3807+
cancelAnimationFrame(embeddedFitFrameRef.current)
3808+
embeddedFitFrameRef.current = null
3809+
}
3810+
}
3811+
}, [embedded, isWorkflowReady, scheduleEmbeddedFit])
3812+
3813+
useEffect(() => {
3814+
if (!embedded || !isWorkflowReady) {
3815+
return
3816+
}
3817+
3818+
scheduleEmbeddedFit()
3819+
}, [blocksStructureHash, embedded, isWorkflowReady, scheduleEmbeddedFit])
3820+
37533821
return (
37543822
<div className='flex h-full w-full overflow-hidden'>
37553823
<div className='flex min-w-0 flex-1 flex-col'>
3756-
<div className='relative flex-1 overflow-hidden'>
3824+
<div ref={canvasContainerRef} className='relative flex-1 overflow-hidden'>
37573825
{!isWorkflowReady && (
37583826
<div className='absolute inset-0 z-[5] flex items-center justify-center bg-[var(--bg)]'>
37593827
<div
@@ -3791,8 +3859,12 @@ const WorkflowContent = React.memo(
37913859
onDrop={effectivePermissions.canEdit ? onDrop : undefined}
37923860
onDragOver={effectivePermissions.canEdit ? onDragOver : undefined}
37933861
onInit={(instance) => {
3862+
if (embedded) {
3863+
return
3864+
}
3865+
37943866
requestAnimationFrame(() => {
3795-
instance.fitView(embedded ? embeddedFitViewOptions : reactFlowFitViewOptions)
3867+
instance.fitView(reactFlowFitViewOptions)
37963868
setIsCanvasReady(true)
37973869
})
37983870
}}

0 commit comments

Comments
 (0)