|
1 | | -import type { Node } from 'reactflow' |
| 1 | +import type { Edge, Node } from 'reactflow' |
2 | 2 | import { BLOCK_DIMENSIONS, CONTAINER_DIMENSIONS } from '@/lib/workflows/blocks/block-dimensions' |
3 | 3 | import { TriggerUtils } from '@/lib/workflows/triggers/triggers' |
4 | 4 | import { clampPositionToContainer } from '@/app/workspace/[workspaceId]/w/[workflowId]/hooks/use-node-utilities' |
@@ -139,3 +139,43 @@ export function computeClampedPositionUpdates( |
139 | 139 | position: getClampedPositionForNode(node.id, node.position, blocks, allNodes), |
140 | 140 | })) |
141 | 141 | } |
| 142 | + |
| 143 | +interface ParentUpdateEntry { |
| 144 | + blockId: string |
| 145 | + newParentId: string |
| 146 | + affectedEdges: Edge[] |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * Computes parent update entries for nodes being moved into a subflow. |
| 151 | + * Only includes "boundary edges" - edges that cross the selection boundary |
| 152 | + * (one end inside selection, one end outside). Edges between nodes in the |
| 153 | + * selection are preserved. |
| 154 | + */ |
| 155 | +export function computeParentUpdateEntries( |
| 156 | + validNodes: Node[], |
| 157 | + allEdges: Edge[], |
| 158 | + targetParentId: string |
| 159 | +): ParentUpdateEntry[] { |
| 160 | + const movingNodeIds = new Set(validNodes.map((n) => n.id)) |
| 161 | + |
| 162 | + // Find edges that cross the boundary (one end inside selection, one end outside) |
| 163 | + // Edges between nodes in the selection should stay intact |
| 164 | + const boundaryEdges = allEdges.filter((e) => { |
| 165 | + const sourceInSelection = movingNodeIds.has(e.source) |
| 166 | + const targetInSelection = movingNodeIds.has(e.target) |
| 167 | + // Only remove if exactly one end is in the selection (crosses boundary) |
| 168 | + return sourceInSelection !== targetInSelection |
| 169 | + }) |
| 170 | + |
| 171 | + // Build updates for all valid nodes |
| 172 | + return validNodes.map((n) => { |
| 173 | + // Only include boundary edges connected to this specific node |
| 174 | + const edgesForThisNode = boundaryEdges.filter((e) => e.source === n.id || e.target === n.id) |
| 175 | + return { |
| 176 | + blockId: n.id, |
| 177 | + newParentId: targetParentId, |
| 178 | + affectedEdges: edgesForThisNode, |
| 179 | + } |
| 180 | + }) |
| 181 | +} |
0 commit comments