Skip to content

Commit 8775e76

Browse files
improvement(subflow): resize vertical height estimate (#2428)
* improvement(node-dims): share constants for node padding * fix vertical height estimation
1 parent 9a6c687 commit 8775e76

File tree

3 files changed

+38
-55
lines changed

3 files changed

+38
-55
lines changed

apps/sim/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-node-utilities.ts

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -252,45 +252,34 @@ export function useNodeUtilities(blocks: Record<string, any>) {
252252
*/
253253
const calculateLoopDimensions = useCallback(
254254
(nodeId: string): { width: number; height: number } => {
255-
const minWidth = CONTAINER_DIMENSIONS.DEFAULT_WIDTH
256-
const minHeight = CONTAINER_DIMENSIONS.DEFAULT_HEIGHT
257-
258-
// Match styling in subflow-node.tsx:
259-
// - Header section: 50px total height
260-
// - Content area: px-[16px] pb-[0px] pt-[16px] pr-[70px]
261-
// Left padding: 16px, Right padding: 64px, Top padding: 16px, Bottom padding: -6px (reduced by additional 6px from 0 to achieve 14px total reduction from original 8px)
262-
// - Children are positioned relative to the content area (after header, inside padding)
263-
const headerHeight = 50
264-
const leftPadding = 16
265-
const rightPadding = 80
266-
const topPadding = 16
267-
const bottomPadding = 16
268-
269255
const childNodes = getNodes().filter((node) => node.parentId === nodeId)
270256
if (childNodes.length === 0) {
271-
return { width: minWidth, height: minHeight }
257+
return {
258+
width: CONTAINER_DIMENSIONS.DEFAULT_WIDTH,
259+
height: CONTAINER_DIMENSIONS.DEFAULT_HEIGHT,
260+
}
272261
}
273262

274263
let maxRight = 0
275264
let maxBottom = 0
276265

277266
childNodes.forEach((node) => {
278267
const { width: nodeWidth, height: nodeHeight } = getBlockDimensions(node.id)
279-
280-
// Child positions are relative to content area's inner top-left (inside padding)
281-
// Calculate the rightmost and bottommost edges of children
282-
const rightEdge = node.position.x + nodeWidth
283-
const bottomEdge = node.position.y + nodeHeight
284-
285-
maxRight = Math.max(maxRight, rightEdge)
286-
maxBottom = Math.max(maxBottom, bottomEdge)
268+
maxRight = Math.max(maxRight, node.position.x + nodeWidth)
269+
maxBottom = Math.max(maxBottom, node.position.y + nodeHeight)
287270
})
288271

289-
// Container dimensions = header + padding + children bounds + padding
290-
// Width: left padding + max child right edge + right padding (64px)
291-
const width = Math.max(minWidth, leftPadding + maxRight + rightPadding)
292-
// Height: header + top padding + max child bottom edge + bottom padding (8px)
293-
const height = Math.max(minHeight, headerHeight + topPadding + maxBottom + bottomPadding)
272+
const width = Math.max(
273+
CONTAINER_DIMENSIONS.DEFAULT_WIDTH,
274+
CONTAINER_DIMENSIONS.LEFT_PADDING + maxRight + CONTAINER_DIMENSIONS.RIGHT_PADDING
275+
)
276+
const height = Math.max(
277+
CONTAINER_DIMENSIONS.DEFAULT_HEIGHT,
278+
CONTAINER_DIMENSIONS.HEADER_HEIGHT +
279+
CONTAINER_DIMENSIONS.TOP_PADDING +
280+
maxBottom +
281+
CONTAINER_DIMENSIONS.BOTTOM_PADDING
282+
)
294283

295284
return { width, height }
296285
},

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

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { useShallow } from 'zustand/react/shallow'
1818
import type { OAuthConnectEventDetail } from '@/lib/copilot/tools/client/other/oauth-request-access'
1919
import { createLogger } from '@/lib/logs/console/logger'
2020
import type { OAuthProvider } from '@/lib/oauth'
21-
import { BLOCK_DIMENSIONS, CONTAINER_DIMENSIONS } from '@/lib/workflows/blocks/block-dimensions'
21+
import { CONTAINER_DIMENSIONS } from '@/lib/workflows/blocks/block-dimensions'
2222
import { TriggerUtils } from '@/lib/workflows/triggers/triggers'
2323
import { useWorkspacePermissionsContext } from '@/app/workspace/[workspaceId]/providers/workspace-permissions-provider'
2424
import {
@@ -177,6 +177,7 @@ const WorkflowContent = React.memo(() => {
177177
resizeLoopNodes,
178178
updateNodeParent: updateNodeParentUtil,
179179
getNodeAnchorPosition,
180+
getBlockDimensions,
180181
} = useNodeUtilities(blocks)
181182

182183
/** Triggers immediate subflow resize without delays. */
@@ -1512,43 +1513,32 @@ const WorkflowContent = React.memo(() => {
15121513
if (!parentId) return
15131514

15141515
setDisplayNodes((currentNodes) => {
1515-
// Find all children of this container from current displayNodes
15161516
const childNodes = currentNodes.filter((n) => n.parentId === parentId)
15171517
if (childNodes.length === 0) return currentNodes
15181518

1519-
// Calculate dimensions using current positions from displayNodes
1520-
// Match padding values from use-node-utilities.ts calculateLoopDimensions
1521-
const headerHeight = 50
1522-
const leftPadding = 16
1523-
const rightPadding = 80
1524-
const topPadding = 16
1525-
const bottomPadding = 16
1526-
const minWidth = CONTAINER_DIMENSIONS.DEFAULT_WIDTH
1527-
const minHeight = CONTAINER_DIMENSIONS.DEFAULT_HEIGHT
1528-
15291519
let maxRight = 0
15301520
let maxBottom = 0
15311521

15321522
childNodes.forEach((node) => {
1533-
// Use the dragged node's live position, others from displayNodes
15341523
const nodePosition = node.id === draggedNodeId ? draggedNodePosition : node.position
1524+
const { width: nodeWidth, height: nodeHeight } = getBlockDimensions(node.id)
15351525

1536-
// Get dimensions - use block store for height estimates
1537-
const blockData = blocks[node.id]
1538-
const nodeWidth = BLOCK_DIMENSIONS.FIXED_WIDTH
1539-
const nodeHeight = blockData?.height || node.height || BLOCK_DIMENSIONS.MIN_HEIGHT
1540-
1541-
const rightEdge = nodePosition.x + nodeWidth
1542-
const bottomEdge = nodePosition.y + nodeHeight
1543-
1544-
maxRight = Math.max(maxRight, rightEdge)
1545-
maxBottom = Math.max(maxBottom, bottomEdge)
1526+
maxRight = Math.max(maxRight, nodePosition.x + nodeWidth)
1527+
maxBottom = Math.max(maxBottom, nodePosition.y + nodeHeight)
15461528
})
15471529

1548-
const newWidth = Math.max(minWidth, leftPadding + maxRight + rightPadding)
1549-
const newHeight = Math.max(minHeight, headerHeight + topPadding + maxBottom + bottomPadding)
1530+
const newWidth = Math.max(
1531+
CONTAINER_DIMENSIONS.DEFAULT_WIDTH,
1532+
CONTAINER_DIMENSIONS.LEFT_PADDING + maxRight + CONTAINER_DIMENSIONS.RIGHT_PADDING
1533+
)
1534+
const newHeight = Math.max(
1535+
CONTAINER_DIMENSIONS.DEFAULT_HEIGHT,
1536+
CONTAINER_DIMENSIONS.HEADER_HEIGHT +
1537+
CONTAINER_DIMENSIONS.TOP_PADDING +
1538+
maxBottom +
1539+
CONTAINER_DIMENSIONS.BOTTOM_PADDING
1540+
)
15501541

1551-
// Update the container node's dimensions in displayNodes
15521542
return currentNodes.map((node) => {
15531543
if (node.id === parentId) {
15541544
const currentWidth = node.data?.width || CONTAINER_DIMENSIONS.DEFAULT_WIDTH
@@ -1570,7 +1560,7 @@ const WorkflowContent = React.memo(() => {
15701560
})
15711561
})
15721562
},
1573-
[blocks]
1563+
[blocks, getBlockDimensions]
15741564
)
15751565

15761566
/**

apps/sim/lib/workflows/blocks/block-dimensions.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ export const CONTAINER_DIMENSIONS = {
2424
MIN_WIDTH: 400,
2525
MIN_HEIGHT: 200,
2626
HEADER_HEIGHT: 50,
27+
LEFT_PADDING: 16,
28+
RIGHT_PADDING: 80,
29+
TOP_PADDING: 16,
30+
BOTTOM_PADDING: 16,
2731
} as const
2832

2933
/**

0 commit comments

Comments
 (0)