feat(editor): Add undo/redo for importing workflow data on new canvas (no-changelog) (#11101)

This commit is contained in:
Alex Grozav 2024-10-04 15:57:09 +03:00 committed by GitHub
parent d537cea363
commit 71e75e8a68
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1376,26 +1376,40 @@ export function useCanvasOperations({ router }: { router: ReturnType<typeof useR
return targetNodeHasInputConnectionOfType; return targetNodeHasInputConnectionOfType;
} }
function addConnections(connections: CanvasConnectionCreateData[] | CanvasConnection[]) { function addConnections(
connections: CanvasConnectionCreateData[] | CanvasConnection[],
{ trackBulk = true, trackHistory = false } = {},
) {
if (trackBulk) {
historyStore.startRecordingUndo();
}
for (const { source, target, data } of connections) { for (const { source, target, data } of connections) {
createConnection({ createConnection(
source, {
sourceHandle: createCanvasConnectionHandleString({ source,
mode: CanvasConnectionMode.Output, sourceHandle: createCanvasConnectionHandleString({
type: isValidNodeConnectionType(data?.source.type) mode: CanvasConnectionMode.Output,
? data?.source.type type: isValidNodeConnectionType(data?.source.type)
: NodeConnectionType.Main, ? data?.source.type
index: data?.source.index ?? 0, : NodeConnectionType.Main,
}), index: data?.source.index ?? 0,
target, }),
targetHandle: createCanvasConnectionHandleString({ target,
mode: CanvasConnectionMode.Input, targetHandle: createCanvasConnectionHandleString({
type: isValidNodeConnectionType(data?.target.type) mode: CanvasConnectionMode.Input,
? data?.target.type type: isValidNodeConnectionType(data?.target.type)
: NodeConnectionType.Main, ? data?.target.type
index: data?.target.index ?? 0, : NodeConnectionType.Main,
}), index: data?.target.index ?? 0,
}); }),
},
{ trackHistory },
);
}
if (trackBulk) {
historyStore.stopRecordingUndo();
} }
} }
@ -1462,6 +1476,7 @@ export function useCanvasOperations({ router }: { router: ReturnType<typeof useR
async function addImportedNodesToWorkflow( async function addImportedNodesToWorkflow(
data: IWorkflowDataUpdate, data: IWorkflowDataUpdate,
{ trackBulk = true, trackHistory = false } = {},
): Promise<IWorkflowDataUpdate> { ): Promise<IWorkflowDataUpdate> {
// Because nodes with the same name maybe already exist, it could // Because nodes with the same name maybe already exist, it could
// be needed that they have to be renamed. Also could it be possible // be needed that they have to be renamed. Also could it be possible
@ -1602,17 +1617,23 @@ export function useCanvasOperations({ router }: { router: ReturnType<typeof useR
} }
// Add the nodes with the changed node names, expressions and connections // Add the nodes with the changed node names, expressions and connections
historyStore.startRecordingUndo(); if (trackBulk) {
historyStore.startRecordingUndo();
}
await addNodes(Object.values(tempWorkflow.nodes)); await addNodes(Object.values(tempWorkflow.nodes), { trackBulk: false, trackHistory: true });
addConnections( addConnections(
mapLegacyConnectionsToCanvasConnections( mapLegacyConnectionsToCanvasConnections(
tempWorkflow.connectionsBySourceNode, tempWorkflow.connectionsBySourceNode,
Object.values(tempWorkflow.nodes), Object.values(tempWorkflow.nodes),
), ),
{ trackBulk: false, trackHistory },
); );
historyStore.stopRecordingUndo(); if (trackBulk) {
historyStore.stopRecordingUndo();
}
uiStore.stateIsDirty = true; uiStore.stateIsDirty = true;
return { return {