Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

fix(workflow): make node value edits undoable

Josh Corbett33a0f1ecf489
diff --git a/invokeai/frontend/webv2/src/features/workflow/core/document.test.ts b/invokeai/frontend/webv2/src/features/workflow/core/document.test.ts
index 1f1a6ca160..5e51a1f860 100644
--- a/invokeai/frontend/webv2/src/features/workflow/core/document.test.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/core/document.test.ts
@@ -6,6 +6,7 @@ import {
buildInvocationNode,
createProjectGraph,
getFormChildren,
+ getProjectGraphUndoEntry,
isFieldExposed,
normalizeProjectGraph,
projectGraphReducer,
@@ -409,3 +410,23 @@ describe('seed modes', () => {
expect(held).toBe(refixed);
});
});
+
+describe('getProjectGraphUndoEntry', () => {
+ it('keys streamed edits per field so a burst folds into one step, and leaves moves out of history', () => {
+ expect(getProjectGraphUndoEntry({ fieldName: 'a', nodeId: 'n', type: 'setFieldValue', value: 1 })).toEqual({
+ label: 'Edit workflow field value',
+ mergeKey: 'setFieldValue:n:a',
+ });
+ expect(getProjectGraphUndoEntry({ fieldName: 'b', nodeId: 'n', type: 'setFieldValue', value: 1 })?.mergeKey).toBe(
+ 'setFieldValue:n:b'
+ );
+ expect(getProjectGraphUndoEntry({ fieldName: 'a', nodeId: 'n', type: 'setFieldValue', value: true })).toEqual({
+ label: 'Edit workflow field value',
+ });
+ expect(getProjectGraphUndoEntry({ nodeId: 'n', type: 'setNodeUseCache', useCache: false })).toEqual({
+ label: 'Change workflow node caching',
+ });
+ expect(getProjectGraphUndoEntry({ nodeId: 'n', position: { x: 1, y: 1 }, type: 'setNodePosition' })).toBeNull();
+ expect(getProjectGraphUndoEntry({ isOpen: false, nodeId: 'n', type: 'setNodeIsOpen' })).toBeNull();
+ });
+});
diff --git a/invokeai/frontend/webv2/src/features/workflow/core/document.ts b/invokeai/frontend/webv2/src/features/workflow/core/document.ts
index 8d7f315116..73384f1f81 100644
--- a/invokeai/frontend/webv2/src/features/workflow/core/document.ts
+++ b/invokeai/frontend/webv2/src/features/workflow/core/document.ts
@@ -339,13 +339,61 @@ const undoLabels: Partial<Record<ProjectGraphAction['type'], string>> = {
removeFormElement: 'Edit workflow form',
removeNodes: 'Delete workflow nodes',
setContainerLayout: 'Edit workflow form',
+ setFieldDescription: 'Edit workflow field description',
+ setFieldLabel: 'Rename workflow field',
+ setFieldSeedMode: 'Change workflow seed mode',
+ setFieldValue: 'Edit workflow field value',
+ setFormElementContent: 'Edit workflow form',
+ setMetadata: 'Edit workflow details',
setNodeFieldShowDescription: 'Edit workflow form',
setNodeFieldShowShuffle: 'Edit workflow form',
+ setNodeIsIntermediate: 'Change workflow node output saving',
+ setNodeLabel: 'Rename workflow node',
+ setNodeNotes: 'Edit workflow node notes',
+ setNodeUseCache: 'Change workflow node caching',
unexposeField: 'Remove workflow field from form',
};
-/** Returns the project-undo label for an action, or null when the edit should not create an undo entry. */
-export const getProjectGraphUndoLabel = (action: ProjectGraphAction): string | null => undoLabels[action.type] ?? null;
+export interface ProjectGraphUndoEntry {
+ label: string;
+ /** Present for edits that arrive as a stream (typing, dragging): consecutive edits with one key fold into one undo step. */
+ mergeKey?: string;
+}
+
+const getUndoMergeKey = (action: ProjectGraphAction): string | undefined => {
+ switch (action.type) {
+ case 'setFieldDescription':
+ case 'setFieldLabel':
+ return `${action.type}:${action.nodeId}:${action.fieldName}`;
+ case 'setFieldValue':
+ // Typed text and dragged numbers stream; a pick (model, board, switch) is one step of its own.
+ return typeof action.value === 'string' || typeof action.value === 'number'
+ ? `${action.type}:${action.nodeId}:${action.fieldName}`
+ : undefined;
+ case 'setNodeLabel':
+ case 'setNodeNotes':
+ return `${action.type}:${action.nodeId}`;
+ case 'setFormElementContent':
+ return `${action.type}:${action.elementId}`;
+ case 'setMetadata':
+ return `${action.type}:${Object.keys(action.patch).sort().join(',')}`;
+ default:
+ return undefined;
+ }
+};
+
+/** The undo entry an action earns, or null when the edit should not create one (positions, disclosure state, seed advances). */
+export const getProjectGraphUndoEntry = (action: ProjectGraphAction): ProjectGraphUndoEntry | null => {
+ const label = undoLabels[action.type];
+
+ if (!label) {
+ return null;
+ }
+
+ const mergeKey = getUndoMergeKey(action);
+
+ return mergeKey ? { label, mergeKey } : { label };
+};
const updateNode = (
document: ProjectGraphState,
diff --git a/invokeai/frontend/webv2/src/workbench/hotkeys/keys.browser.test.ts b/invokeai/frontend/webv2/src/workbench/hotkeys/keys.browser.test.ts
new file mode 100644
index 0000000000..63a17ed8e4
--- /dev/null
+++ b/invokeai/frontend/webv2/src/workbench/hotkeys/keys.browser.test.ts
@@ -0,0 +1,36 @@
+import { afterEach, describe, expect, it } from 'vitest';
+
+import { isEditableHotkeyTarget } from './keys';
+
+describe('isEditableHotkeyTarget', () => {
+ const host = document.createElement('div');
+
+ document.body.append(host);
+
+ afterEach(() => {
+ host.replaceChildren();
+ });
+
+ const mount = (html: string): HTMLElement => {
+ host.innerHTML = html;
+
+ return host.querySelector<HTMLElement>('[data-target]')!;
+ };
+
+ it('protects the native shortcuts of anything that takes typed text', () => {
+ expect(isEditableHotkeyTarget(mount('<input data-target />'))).toBe(true);
+ expect(isEditableHotkeyTarget(mount('<input data-target type="number" />'))).toBe(true);
+ expect(isEditableHotkeyTarget(mount('<textarea data-target></textarea>'))).toBe(true);
+ expect(isEditableHotkeyTarget(mount('<select data-target></select>'))).toBe(true);
+ expect(isEditableHotkeyTarget(mount('<div contenteditable="true"><span data-target>x</span></div>'))).toBe(true);
+ });
+
+ it('lets shortcuts through from a focused switch, checkbox, or button', () => {
+ // A workflow node's boolean switch is a hidden checkbox: Ctrl+Z from it must reach the editor's undo.
+ expect(isEditableHotkeyTarget(mount('<label><input data-target type="checkbox" /></label>'))).toBe(false);
+ expect(isEditableHotkeyTarget(mount('<input data-target type="radio" />'))).toBe(false);
+ expect(isEditableHotkeyTarget(mount('<input data-target type="range" />'))).toBe(false);
+ expect(isEditableHotkeyTarget(mount('<button data-target type="button">x</button>'))).toBe(false);
+ expect(isEditableHotkeyTarget(null)).toBe(false);
+ });
+});
diff --git a/invokeai/frontend/webv2/src/workbench/hotkeys/keys.ts b/invokeai/frontend/webv2/src/workbench/hotkeys/keys.ts
index e74b9ad6d1..30f1ba9c8f 100644
--- a/invokeai/frontend/webv2/src/workbench/hotkeys/keys.ts
+++ b/invokeai/frontend/webv2/src/workbench/hotkeys/keys.ts
@@ -96,6 +96,19 @@ export const formatHotkeyForPlatform = (hotkey: string): string[] =>
.filter(Boolean)
.map((part) => (IS_MAC_OS ? part.replace('mod', 'cmd').replace('alt', 'option') : part.replace('mod', 'ctrl')));
+/** Inputs that take no typed text: a switch or checkbox owning focus has no native undo/shortcut to protect. */
+const NON_TEXT_INPUT_TYPES = new Set([
+ 'button',
+ 'checkbox',
+ 'color',
+ 'file',
+ 'image',
+ 'radio',
+ 'range',
+ 'reset',
+ 'submit',
+]);
+
export const isEditableHotkeyTarget = (target: EventTarget | null): boolean => {
if (typeof HTMLElement === 'undefined') {
return false;
@@ -105,5 +118,10 @@ export const isEditableHotkeyTarget = (target: EventTarget | null): boolean => {
return false;
}
- return Boolean(target.closest('input, textarea, select, [contenteditable="true"]'));
+ const editable = target.closest('input, textarea, select, [contenteditable="true"]');
+
+ return (
+ editable !== null &&
+ !(editable.tagName === 'INPUT' && NON_TEXT_INPUT_TYPES.has((editable as HTMLInputElement).type))
+ );
};
diff --git a/invokeai/frontend/webv2/src/workbench/projectContracts.ts b/invokeai/frontend/webv2/src/workbench/projectContracts.ts
index 605b22f940..ba0d7d3579 100644
--- a/invokeai/frontend/webv2/src/workbench/projectContracts.ts
+++ b/invokeai/frontend/webv2/src/workbench/projectContracts.ts
@@ -120,6 +120,10 @@ export interface UndoRedoEntry {
createdAt: string;
label: string;
project: ProjectUndoSnapshot;
+ /** Edits that arrive as a stream (typing, dragging) share a key so they fold into one step. */
+ mergeKey?: string;
+ /** When the entry last absorbed a same-key edit; the merge window runs from here. */
+ mergedAt?: string;
}
/**
diff --git a/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts b/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
index a61075fab5..0018f44d1d 100644
--- a/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/workbenchState.test.ts
@@ -4746,6 +4746,102 @@ describe('workbenchReducer Phase 5 generation flow', () => {
expect(getActiveProject(state).queue.items[0]?.snapshot.galleryBoardId).toBe('project-board');
});
+ it('folds a stream of workflow field edits into one undo step and starts another after a pause', () => {
+ vi.useFakeTimers({ now: new Date('2026-06-10T00:00:00.000Z') });
+
+ try {
+ let state = createInitialWorkbenchState();
+ const setValue = (fieldName: string, value: number) =>
+ workbenchReducer(state, {
+ action: { fieldName, nodeId: 'node-1', type: 'setFieldValue', value },
+ type: 'applyProjectGraphAction',
+ });
+ const fieldValue = (fieldName: string) => {
+ const node = getActiveProject(state).projectGraph.nodes[0];
+
+ return node?.type === 'invocation' ? node.data.inputs[fieldName]?.value : undefined;
+ };
+ const past = () => getActiveProject(state).undoRedo.past;
+
+ state = workbenchReducer(state, {
+ action: {
+ node: {
+ data: {
+ inputs: {},
+ isIntermediate: true,
+ isOpen: true,
+ label: '',
+ nodePack: 'invokeai',
+ notes: '',
+ type: 'add',
+ useCache: true,
+ version: '1.0.0',
+ },
+ id: 'node-1',
+ position: { x: 0, y: 0 },
+ type: 'invocation',
+ },
+ type: 'addNode',
+ },
+ type: 'applyProjectGraphAction',
+ });
+ const stepsAfterAdd = past().length;
+
+ // Keystrokes a second apart keep one edit going: the window slides with each keystroke.
+ state = setValue('a', 1);
+ vi.setSystemTime(new Date('2026-06-10T00:00:01.000Z'));
+ state = setValue('a', 12);
+ vi.setSystemTime(new Date('2026-06-10T00:00:02.000Z'));
+ state = setValue('a', 123);
+
+ expect(past()).toHaveLength(stepsAfterAdd + 1);
+ expect(past().at(-1)?.label).toBe('Edit workflow field value');
+
+ // A pause, or another field, opens a new step.
+ vi.setSystemTime(new Date('2026-06-10T00:00:05.000Z'));
+ state = setValue('a', 3);
+ state = setValue('b', 7);
+
+ expect(past()).toHaveLength(stepsAfterAdd + 3);
+
+ state = workbenchReducer(state, { type: 'undoProjectChange' });
+ expect(fieldValue('b')).toBeUndefined();
+ expect(fieldValue('a')).toBe(3);
+
+ state = workbenchReducer(state, { type: 'undoProjectChange' });
+ expect(fieldValue('a')).toBe(123);
+
+ state = workbenchReducer(state, { type: 'undoProjectChange' });
+ expect(fieldValue('a')).toBeUndefined();
+
+ // Redo brings the whole burst back at once, and a fresh edit after it starts its own step.
+ state = workbenchReducer(state, { type: 'redoProjectChange' });
+ expect(fieldValue('a')).toBe(123);
+
+ state = setValue('a', 4);
+ expect(past()).toHaveLength(stepsAfterAdd + 2);
+ expect(getActiveProject(state).undoRedo.future).toHaveLength(0);
+
+ // An undo inside the window ends the burst: the step the user undid back to stays its own step.
+ state = setValue('b', 8);
+ state = workbenchReducer(state, { type: 'undoProjectChange' });
+ state = setValue('a', 5);
+
+ expect(past()).toHaveLength(stepsAfterAdd + 3);
+ state = workbenchReducer(state, { type: 'undoProjectChange' });
+ expect(fieldValue('a')).toBe(4);
+
+ // Node moves are not history: dragging a node around leaves the steps alone.
+ state = workbenchReducer(state, {
+ action: { nodeId: 'node-1', position: { x: 10, y: 10 }, type: 'setNodePosition' },
+ type: 'applyProjectGraphAction',
+ });
+ expect(past()).toHaveLength(stepsAfterAdd + 2);
+ } finally {
+ vi.useRealTimers();
+ }
+ });
+
it('stores full selected gallery image data for Preview widget', () => {
let state = createInitialWorkbenchState();
const image = createGalleryImageItem('backend-selected.png');
diff --git a/invokeai/frontend/webv2/src/workbench/workbenchState.ts b/invokeai/frontend/webv2/src/workbench/workbenchState.ts
index 1b3fa5a26a..9b23529d00 100644
--- a/invokeai/frontend/webv2/src/workbench/workbenchState.ts
+++ b/invokeai/frontend/webv2/src/workbench/workbenchState.ts
@@ -144,7 +144,7 @@ import { getInvocationTemplatesSnapshot } from '@features/workflow/react';
import {
cloneProjectGraph,
createProjectGraph,
- getProjectGraphUndoLabel,
+ getProjectGraphUndoEntry,
normalizeProjectGraph,
projectGraphReducer,
type ProjectGraphAction,
@@ -452,6 +452,8 @@ type WorkbenchReducerAction =
| { type: 'recordNotice'; kind: WorkbenchNotificationKind; title: string; message?: string };
const HISTORY_LIMIT = 40;
+/** A pause this long between same-key edits (typing, dragging) starts a new undo step. */
+const UNDO_MERGE_WINDOW_MS = 1500;
const NOTIFICATION_LIMIT = 100;
// Side panels host real widget UIs (gallery grid, generate form); below
// ~350px their toolbars and grids collapse into unusable slivers, so that is
@@ -1166,21 +1168,50 @@ const restoreUndoSnapshot = (project: Project, snapshot: ProjectUndoSnapshot): P
widgetRegions: cloneWidgetRegions(snapshot.widgetRegions),
});
-const pushUndo = (project: Project, label: string, projectGraph?: ProjectGraphState): Project => ({
- ...project,
- undoRedo: {
- future: [],
- past: [
- ...project.undoRedo.past,
- {
- createdAt: now(),
- id: createId('undo'),
- label,
- project: createUndoSnapshot(project, projectGraph),
+/**
+ * Records the project as it is *before* an edit. A `mergeKey` folds a stream
+ * of edits (each keystroke in a field, each move of a drag) into the entry
+ * that opened the stream while they keep arriving within the merge window,
+ * so one undo reverts the whole burst.
+ */
+const pushUndo = (project: Project, label: string, projectGraph?: ProjectGraphState, mergeKey?: string): Project => {
+ const previous = project.undoRedo.past.at(-1);
+ const timestamp = now();
+
+ // An undo in between (`future` non-empty) ends the burst: the state the user
+ // just stood on must stay reachable as its own step.
+ if (
+ mergeKey &&
+ previous?.mergeKey === mergeKey &&
+ project.undoRedo.future.length === 0 &&
+ Date.parse(timestamp) - Date.parse(previous.mergedAt ?? previous.createdAt) <= UNDO_MERGE_WINDOW_MS
+ ) {
+ return {
+ ...project,
+ undoRedo: {
+ future: [],
+ past: [...project.undoRedo.past.slice(0, -1), { ...previous, mergedAt: timestamp }],
},
- ].slice(-HISTORY_LIMIT),
- },
-});
+ };
+ }
+
+ return {
+ ...project,
+ undoRedo: {
+ future: [],
+ past: [
+ ...project.undoRedo.past,
+ {
+ createdAt: timestamp,
+ id: createId('undo'),
+ label,
+ ...(mergeKey ? { mergeKey } : {}),
+ project: createUndoSnapshot(project, projectGraph),
+ },
+ ].slice(-HISTORY_LIMIT),
+ },
+ };
+};
const createWidgetStates = (): WidgetStateMap => ({
'autosave-status': { id: 'autosave-status', label: 'Autosave', values: {}, version: 1 },
@@ -4268,8 +4299,10 @@ export const __workbenchReducerInternal = (
const routedProject = isHighConfidenceGraphEdit(action.action)
? applyAutoRouteForEdit(project, 'workflow', context)
: project;
- const undoLabel = getProjectGraphUndoLabel(action.action);
- const nextProject = undoLabel ? pushUndo(routedProject, undoLabel) : routedProject;
+ const undoEntry = getProjectGraphUndoEntry(action.action);
+ const nextProject = undoEntry
+ ? pushUndo(routedProject, undoEntry.label, undefined, undoEntry.mergeKey)
+ : routedProject;
const updated = { ...nextProject, projectGraph };
return updated;