Skip to content

Commit ce93065

Browse files
committed
cleanup code
1 parent 72801ca commit ce93065

File tree

1 file changed

+22
-31
lines changed
  • apps/sim/app/workspace/[workspaceId]/w/[workflowId]

1 file changed

+22
-31
lines changed

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

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,26 @@ const logger = createLogger('Workflow')
8888

8989
const DEFAULT_PASTE_OFFSET = { x: 50, y: 50 }
9090

91+
/**
92+
* Gets the center of the current viewport in flow coordinates
93+
*/
94+
function getViewportCenter(
95+
screenToFlowPosition: (pos: { x: number; y: number }) => { x: number; y: number }
96+
): { x: number; y: number } {
97+
const flowContainer = document.querySelector('.react-flow')
98+
if (!flowContainer) {
99+
return screenToFlowPosition({
100+
x: window.innerWidth / 2,
101+
y: window.innerHeight / 2,
102+
})
103+
}
104+
const rect = flowContainer.getBoundingClientRect()
105+
return screenToFlowPosition({
106+
x: rect.width / 2,
107+
y: rect.height / 2,
108+
})
109+
}
110+
91111
/**
92112
* Calculates the offset to paste blocks at viewport center
93113
*/
@@ -124,14 +144,7 @@ function calculatePasteOffset(
124144
)
125145
const clipboardCenter = { x: (minX + maxX) / 2, y: (minY + maxY) / 2 }
126146

127-
const flowContainer = document.querySelector('.react-flow')
128-
if (!flowContainer) return DEFAULT_PASTE_OFFSET
129-
130-
const rect = flowContainer.getBoundingClientRect()
131-
const viewportCenter = screenToFlowPosition({
132-
x: rect.width / 2,
133-
y: rect.height / 2,
134-
})
147+
const viewportCenter = getViewportCenter(screenToFlowPosition)
135148

136149
return {
137150
x: viewportCenter.x - clipboardCenter.x,
@@ -1311,30 +1324,13 @@ const WorkflowContent = React.memo(() => {
13111324
if (!type) return
13121325
if (type === 'connectionBlock') return
13131326

1314-
const getViewportCenterPosition = (): { x: number; y: number } => {
1315-
const flowContainer = document.querySelector('.react-flow')
1316-
if (!flowContainer) {
1317-
return screenToFlowPosition({
1318-
x: window.innerWidth / 2,
1319-
y: window.innerHeight / 2,
1320-
})
1321-
}
1322-
const rect = flowContainer.getBoundingClientRect()
1323-
return screenToFlowPosition({
1324-
x: rect.width / 2,
1325-
y: rect.height / 2,
1326-
})
1327-
}
1328-
1329-
const basePosition = getViewportCenterPosition()
1327+
const basePosition = getViewportCenter(screenToFlowPosition)
13301328

1331-
// Special handling for container nodes (loop or parallel)
13321329
if (type === 'loop' || type === 'parallel') {
13331330
const id = crypto.randomUUID()
13341331
const baseName = type === 'loop' ? 'Loop' : 'Parallel'
13351332
const name = getUniqueBlockName(baseName, blocks)
13361333

1337-
// No auto-connect edge for click/Cmd+K - only drag-and-drop gets auto-connect
13381334
addBlock(
13391335
id,
13401336
type,
@@ -1359,18 +1355,13 @@ const WorkflowContent = React.memo(() => {
13591355
return
13601356
}
13611357

1362-
// Check trigger constraints first
13631358
if (checkTriggerConstraints(type)) return
13641359

1365-
// Create a new block with a unique ID
13661360
const id = crypto.randomUUID()
1367-
// Prefer semantic default names for triggers; then ensure unique numbering centrally
13681361
const defaultTriggerName = TriggerUtils.getDefaultTriggerName(type)
13691362
const baseName = defaultTriggerName || blockConfig.name
13701363
const name = getUniqueBlockName(baseName, blocks)
13711364

1372-
// No auto-connect edge for click/Cmd+K - only drag-and-drop gets auto-connect
1373-
// Enable trigger mode if this is a trigger-capable block from the triggers tab
13741365
addBlock(
13751366
id,
13761367
type,

0 commit comments

Comments
 (0)