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,9 +1376,17 @@ 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, source,
sourceHandle: createCanvasConnectionHandleString({ sourceHandle: createCanvasConnectionHandleString({
mode: CanvasConnectionMode.Output, mode: CanvasConnectionMode.Output,
@ -1395,7 +1403,13 @@ export function useCanvasOperations({ router }: { router: ReturnType<typeof useR
: NodeConnectionType.Main, : NodeConnectionType.Main,
index: data?.target.index ?? 0, 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
if (trackBulk) {
historyStore.startRecordingUndo(); 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 },
); );
if (trackBulk) {
historyStore.stopRecordingUndo(); historyStore.stopRecordingUndo();
}
uiStore.stateIsDirty = true; uiStore.stateIsDirty = true;
return { return {