(
blocks: Record<string, BlockState>,
edges: Edge[],
loops: Record<string, Loop>,
parallels: Record<string, Parallel>,
subBlockValues: Record<string, Record<string, unknown>>,
positionOffset: { x: number; y: number },
existingBlockNames: Record<string, BlockState>,
uniqueNameFn: (name: string, blocks: Record<string, BlockState>) => string
)
| 447 | } |
| 448 | |
| 449 | export function regenerateBlockIds( |
| 450 | blocks: Record<string, BlockState>, |
| 451 | edges: Edge[], |
| 452 | loops: Record<string, Loop>, |
| 453 | parallels: Record<string, Parallel>, |
| 454 | subBlockValues: Record<string, Record<string, unknown>>, |
| 455 | positionOffset: { x: number; y: number }, |
| 456 | existingBlockNames: Record<string, BlockState>, |
| 457 | uniqueNameFn: (name: string, blocks: Record<string, BlockState>) => string |
| 458 | ): RegeneratedState & { subBlockValues: Record<string, Record<string, unknown>> } { |
| 459 | const blockIdMap = new Map<string, string>() |
| 460 | const nameMap = new Map<string, string>() |
| 461 | const newBlocks: Record<string, BlockState> = {} |
| 462 | const newSubBlockValues: Record<string, Record<string, unknown>> = {} |
| 463 | |
| 464 | // Track all blocks for name uniqueness (existing + newly processed) |
| 465 | const allBlocksForNaming = { ...existingBlockNames } |
| 466 | |
| 467 | // First pass: generate new IDs and names for all blocks |
| 468 | Object.entries(blocks).forEach(([oldId, block]) => { |
| 469 | const newId = generateId() |
| 470 | blockIdMap.set(oldId, newId) |
| 471 | |
| 472 | const oldNormalizedName = normalizeName(block.name) |
| 473 | const nameConflicts = Object.values(allBlocksForNaming).some( |
| 474 | (existing) => normalizeName(existing.name) === oldNormalizedName |
| 475 | ) |
| 476 | const newName = nameConflicts ? uniqueNameFn(block.name, allBlocksForNaming) : block.name |
| 477 | const newNormalizedName = normalizeName(newName) |
| 478 | nameMap.set(oldNormalizedName, newNormalizedName) |
| 479 | |
| 480 | // Determine position offset based on parent relationship: |
| 481 | // 1. Parent also being copied: keep exact relative position (parent itself will be offset) |
| 482 | // 2. Parent exists in existing workflow: use provided offset, but cap large viewport-based |
| 483 | // offsets since they don't make sense for relative positions |
| 484 | // 3. Top-level block (no parent): apply full paste offset |
| 485 | const hasParentInPasteSet = block.data?.parentId && blocks[block.data.parentId] |
| 486 | const hasParentInExistingWorkflow = |
| 487 | block.data?.parentId && existingBlockNames[block.data.parentId] |
| 488 | |
| 489 | let newPosition: Position |
| 490 | if (hasParentInPasteSet) { |
| 491 | // Parent also being copied - keep exact relative position |
| 492 | newPosition = { x: block.position.x, y: block.position.y } |
| 493 | } else if (hasParentInExistingWorkflow) { |
| 494 | // Block stays in existing subflow - use provided offset unless it's viewport-based (large) |
| 495 | const isLargeOffset = |
| 496 | Math.abs(positionOffset.x) > LARGE_OFFSET_THRESHOLD || |
| 497 | Math.abs(positionOffset.y) > LARGE_OFFSET_THRESHOLD |
| 498 | const effectiveOffset = isLargeOffset ? DEFAULT_DUPLICATE_OFFSET : positionOffset |
| 499 | newPosition = { |
| 500 | x: block.position.x + effectiveOffset.x, |
| 501 | y: block.position.y + effectiveOffset.y, |
| 502 | } |
| 503 | } else { |
| 504 | // Top-level block - apply full paste offset |
| 505 | newPosition = { |
| 506 | x: block.position.x + positionOffset.x, |
no test coverage detected