Skip to content

Commit cc6ea4e

Browse files
ihaichaochinnsenn
authored and
chinnsenn
committed
fix: the edges between the nodes inside the copied iteration node are… (langgenius#12692)
1 parent 52421cb commit cc6ea4e

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

web/app/components/workflow/hooks/use-nodes-interactions.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1117,9 +1117,12 @@ export const useNodesInteractions = () => {
11171117
const {
11181118
getNodes,
11191119
setNodes,
1120+
edges,
1121+
setEdges,
11201122
} = store.getState()
11211123

11221124
const nodesToPaste: Node[] = []
1125+
const edgesToPaste: Edge[] = []
11231126
const nodes = getNodes()
11241127

11251128
if (clipboardElements.length) {
@@ -1128,6 +1131,7 @@ export const useNodesInteractions = () => {
11281131
const currentPosition = screenToFlowPosition({ x: mousePosition.pageX, y: mousePosition.pageY })
11291132
const offsetX = currentPosition.x - x
11301133
const offsetY = currentPosition.y - y
1134+
let idMapping: Record<string, string> = {}
11311135
clipboardElements.forEach((nodeToPaste, index) => {
11321136
const nodeType = nodeToPaste.data.type
11331137

@@ -1159,7 +1163,13 @@ export const useNodesInteractions = () => {
11591163
newIterationStartNode!.parentId = newNode.id;
11601164
(newNode.data as IterationNodeType).start_node_id = newIterationStartNode!.id
11611165

1162-
newChildren = handleNodeIterationChildrenCopy(nodeToPaste.id, newNode.id)
1166+
const oldIterationStartNode = nodes
1167+
.find(n => n.parentId === nodeToPaste.id && n.type === CUSTOM_ITERATION_START_NODE)
1168+
idMapping[oldIterationStartNode!.id] = newIterationStartNode!.id
1169+
1170+
const { copyChildren, newIdMapping } = handleNodeIterationChildrenCopy(nodeToPaste.id, newNode.id, idMapping)
1171+
newChildren = copyChildren
1172+
idMapping = newIdMapping
11631173
newChildren.forEach((child) => {
11641174
newNode.data._children?.push(child.id)
11651175
})
@@ -1172,7 +1182,27 @@ export const useNodesInteractions = () => {
11721182
nodesToPaste.push(...newChildren)
11731183
})
11741184

1185+
edges.forEach((edge) => {
1186+
const sourceId = idMapping[edge.source]
1187+
const targetId = idMapping[edge.target]
1188+
1189+
if (sourceId && targetId) {
1190+
const newEdge: Edge = {
1191+
...edge,
1192+
id: `${sourceId}-${edge.sourceHandle}-${targetId}-${edge.targetHandle}`,
1193+
source: sourceId,
1194+
target: targetId,
1195+
data: {
1196+
...edge.data,
1197+
_connectedNodeIsSelected: false,
1198+
},
1199+
}
1200+
edgesToPaste.push(newEdge)
1201+
}
1202+
})
1203+
11751204
setNodes([...nodes, ...nodesToPaste])
1205+
setEdges([...edges, ...edgesToPaste])
11761206
saveStateToHistory(WorkflowHistoryEvent.NodePaste)
11771207
handleSyncWorkflowDraft()
11781208
}

web/app/components/workflow/nodes/iteration/use-interactions.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,13 @@ export const useNodeIterationInteractions = () => {
105105
handleNodeIterationRerender(parentId)
106106
}, [store, handleNodeIterationRerender])
107107

108-
const handleNodeIterationChildrenCopy = useCallback((nodeId: string, newNodeId: string) => {
108+
const handleNodeIterationChildrenCopy = useCallback((nodeId: string, newNodeId: string, idMapping: Record<string, string>) => {
109109
const { getNodes } = store.getState()
110110
const nodes = getNodes()
111111
const childrenNodes = nodes.filter(n => n.parentId === nodeId && n.type !== CUSTOM_ITERATION_START_NODE)
112+
const newIdMapping = { ...idMapping }
112113

113-
return childrenNodes.map((child, index) => {
114+
const copyChildren = childrenNodes.map((child, index) => {
114115
const childNodeType = child.data.type as BlockEnum
115116
const nodesWithSameType = nodes.filter(node => node.data.type === childNodeType)
116117
const { newNode } = generateNewNode({
@@ -131,8 +132,14 @@ export const useNodeIterationInteractions = () => {
131132
zIndex: child.zIndex,
132133
})
133134
newNode.id = `${newNodeId}${newNode.id + index}`
135+
newIdMapping[child.id] = newNode.id
134136
return newNode
135137
})
138+
139+
return {
140+
copyChildren,
141+
newIdMapping,
142+
}
136143
}, [store, t])
137144

138145
return {

0 commit comments

Comments
 (0)