|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | import type { |
10 | | - Flow, |
11 | 10 | IEdge, |
12 | 11 | INode, |
13 | 12 | IPort, |
14 | 13 | SerializedEdge, |
15 | 14 | } from '@badaitech/chaingraph-types' |
| 15 | +import { |
| 16 | + Flow, |
| 17 | +} from '@badaitech/chaingraph-types' |
16 | 18 | import { |
17 | 19 | FlowMigration, |
18 | 20 | } from '@badaitech/chaingraph-types' |
@@ -81,10 +83,12 @@ export const pasteNodes = flowContextProcedure |
81 | 83 | throw new Error(`Flow ${flowId} not found`) |
82 | 84 | } |
83 | 85 |
|
| 86 | + const emptyFlow = new Flow() |
| 87 | + |
84 | 88 | await ctx.flowStore.lockFlow(flowId) |
85 | 89 |
|
86 | 90 | try { |
87 | | - flow.setIsDisabledPropagationEvents(true) |
| 91 | + emptyFlow.setIsDisabledPropagationEvents(true) |
88 | 92 |
|
89 | 93 | // Step 1: Clone nodes with new IDs using cloneWithNewId() |
90 | 94 | const nodeIdMapping = new Map<string, string>() |
@@ -192,7 +196,7 @@ export const pasteNodes = flowContextProcedure |
192 | 196 | } |
193 | 197 | } |
194 | 198 |
|
195 | | - const addedNodes = await flow.addNodes(createdNodes, false) |
| 199 | + const addedNodes = await emptyFlow.addNodes(createdNodes, true) |
196 | 200 |
|
197 | 201 | // Step 2: Recreate edges using new IDs |
198 | 202 | const createdEdges: SerializedEdge[] = [] |
@@ -241,10 +245,13 @@ export const pasteNodes = flowContextProcedure |
241 | 245 | }) |
242 | 246 | .filter((edge): edge is NonNullable<typeof edge> => edge !== null) |
243 | 247 |
|
244 | | - await flow.addEdges(edgesToAdd, false) |
| 248 | + await emptyFlow.addEdges(edgesToAdd, true) |
245 | 249 |
|
246 | 250 | // Force migrate flow to v2 |
247 | | - FlowMigration.migrateFlowFromV1ToV2(flow) |
| 251 | + const flowWithPastedNodes = FlowMigration.migrateFlowFromV1ToV2(emptyFlow) |
| 252 | + |
| 253 | + await flow.addNodes(Array.from(flowWithPastedNodes.nodes.values()), false) |
| 254 | + await flow.addEdges(Array.from(flowWithPastedNodes.edges.values()), false) |
248 | 255 |
|
249 | 256 | // Save the updated flow |
250 | 257 | await ctx.flowStore.updateFlow(flow as Flow) |
|
0 commit comments