diff --git a/src/scripts/changeTracker.test.ts b/src/scripts/changeTracker.test.ts index 13c2539fd1d..6c1bc211b83 100644 --- a/src/scripts/changeTracker.test.ts +++ b/src/scripts/changeTracker.test.ts @@ -314,6 +314,78 @@ describe('ChangeTracker', () => { expect(undo).toHaveBeenCalledTimes(undoCalls) } ) + + it.for([ + { editor: 'INPUT', createElement: () => document.createElement('input') }, + { + editor: 'TEXTAREA', + createElement: () => document.createElement('textarea') + }, + { + editor: 'contenteditable', + createElement: () => { + const element = document.createElement('div') + Object.defineProperty(element, 'isContentEditable', { value: true }) + return element + } + } + ])( + 'leaves $editor history to the editor without scanning modals', + ({ createElement }) => { + vi.spyOn(document, 'activeElement', 'get').mockReturnValue( + createElement() + ) + useQueueSettingsStore().mode = 'disabled' + const querySelectorAll = vi.spyOn(document, 'querySelectorAll') + const frames: FrameRequestCallback[] = [] + vi.spyOn(window, 'requestAnimationFrame').mockImplementation((frame) => + frames.push(frame) + ) + const addEventListener = vi + .spyOn(window, 'addEventListener') + .mockImplementation(() => {}) + ChangeTracker.init() + const keydown = addEventListener.mock.calls.find( + ([type]) => type === 'keydown' + )?.[1] + if (typeof keydown !== 'function') + throw new Error('keydown listener missing') + + keydown(new KeyboardEvent('keydown', { key: 'a' })) + + expect(querySelectorAll).not.toHaveBeenCalled() + expect(frames).toHaveLength(0) + } + ) + + it('captures editor changes when store-backed auto-queue uses change mode', async () => { + const editor = document.createElement('input') + vi.spyOn(document, 'activeElement', 'get').mockReturnValue(editor) + useQueueSettingsStore().mode = 'change' + app.ui.autoQueueEnabled = false + const tracker = createTracker(createState(1)) + mockCanvasState(createState(2)) + const frames: FrameRequestCallback[] = [] + vi.spyOn(window, 'requestAnimationFrame').mockImplementation((frame) => + frames.push(frame) + ) + const addEventListener = vi + .spyOn(window, 'addEventListener') + .mockImplementation(() => {}) + ChangeTracker.init() + const keydown = addEventListener.mock.calls.find( + ([type]) => type === 'keydown' + )?.[1] + if (typeof keydown !== 'function') + throw new Error('keydown listener missing') + + keydown(new KeyboardEvent('keydown', { key: 'a' })) + expect(frames).toHaveLength(1) + await frames[0](0) + + expect(dispatchedEventNames()).toContain('autoQueueGraphChanged') + expect(tracker.activeState.nodes).toHaveLength(2) + }) }) describe('captureCanvasState', () => { diff --git a/src/scripts/changeTracker.ts b/src/scripts/changeTracker.ts index 77f1b181aff..b2814b7a42c 100644 --- a/src/scripts/changeTracker.ts +++ b/src/scripts/changeTracker.ts @@ -531,24 +531,28 @@ export class ChangeTracker { // If the mask editor is opened, we don't want to trigger on key events const comfyApp = app.constructor as typeof ComfyApp if (comfyApp.maskeditor_is_opended?.()) return + + const activeEl = document.activeElement + if ( + !isAutoQueueOnChange() && + (activeEl?.tagName === 'INPUT' || + activeEl?.tagName === 'TEXTAREA' || + (activeEl instanceof HTMLElement && activeEl.isContentEditable)) + ) { + // Text editors own their history. Bail out before the document-wide + // modal probes, whose cost otherwise scales with transcript size. + return + } if (isModalOpen(dialogStore.dialogStack.length)) return // The layer editor has its own session-local undo history if (useDialogStore().isDialogOpen(LAYER_EDITOR_DIALOG_KEY)) return - const activeEl = document.activeElement const selectOnlyAtKeydown = isSelectOnly(app.canvas) requestAnimationFrame(async () => { let bindInputEl: Element | null = null // If we are auto queue in change mode then we do want to trigger on inputs - if (!app.ui.autoQueueEnabled || app.ui.autoQueueMode === 'instant') { - if ( - activeEl?.tagName === 'INPUT' || - (activeEl && 'type' in activeEl && activeEl.type === 'textarea') - ) { - // Ignore events on inputs, they have their native history - return - } + if (!isAutoQueueOnChange()) { bindInputEl = activeEl }