From a5cc7537818936fe034405c3f6b5a914d732e801 Mon Sep 17 00:00:00 2001 From: roberttwang Date: Fri, 27 Nov 2026 13:45:56 +0000 Subject: [PATCH 1/2] fix(schedule): Esc closes the AI job model menu, not the whole editor Pressing Esc while the model menu of the AI schedule editor was open closed the menu and the editor in one keystroke, discarding the unsaved draft. The menu now handles Esc in the capture phase, closes itself, returns focus to the model button and stops the page-level handler; a second Esc still closes the editor. Co-Authored-By: Claude Code --- web/app/sync-schedule.js | 9 ++++++- webtest/tests/sync-schedule.test.mjs | 37 ++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/web/app/sync-schedule.js b/web/app/sync-schedule.js index fe57516b5..e3fa61d23 100644 --- a/web/app/sync-schedule.js +++ b/web/app/sync-schedule.js @@ -328,7 +328,14 @@ function bindScheduleModelPicker(){ if(next)paintScheduleModel(next); }; document.addEventListener('click',e=>{if(!host.contains(e.target))closeScheduleModelMenu()}); - document.addEventListener('keydown',e=>{if(e.key==='Escape')closeScheduleModelMenu()}); + // Esc closes only the open menu. Capture runs before the page-level Esc + // handler that would otherwise close the whole editor and drop the draft. + document.addEventListener('keydown',e=>{ + if(e.key!=='Escape'||menu.hidden)return; + e.preventDefault();e.stopImmediatePropagation(); + closeScheduleModelMenu(); + toggle.focus({preventScroll:true}); + },true); // A fixed menu would drift from its toggle; close it when anything else scrolls. document.addEventListener('scroll',e=>{if(!menu.contains(e.target))closeScheduleModelMenu()},true); window.addEventListener('resize',closeScheduleModelMenu); diff --git a/webtest/tests/sync-schedule.test.mjs b/webtest/tests/sync-schedule.test.mjs index 03d25d72d..dc32528ff 100644 --- a/webtest/tests/sync-schedule.test.mjs +++ b/webtest/tests/sync-schedule.test.mjs @@ -733,3 +733,40 @@ test('AI schedule model picker loads account login CLI models for its project an assert.equal(engine.value, 'codex'); assert.equal(form.elements.assistantModel.value, 'codex:gpt-6-sol'); }); + +test('Escape closes the AI job model menu first and keeps the editor and its draft open', async (t) => { + const app = await loadApp(); + t.after(app.close); + const $ = (s) => app.document.querySelector(s); + app.router.get('/api/assistant/models', { + default: 'glm-test', + models: [{ id: 'glm-test', name: 'GLM Test', family: 'glm' }, { id: 'claude-test', name: 'Claude Test', family: 'claude' }], + engines: [{ id: 'codebuddy', name: 'CodeBuddy', configured: true }], + }); + app.evaluate("state.projectFilter='project-1'"); + await app.evaluate("openScheduleJobModal('assistant')"); + await settle(4); + const form = $('#scheduleJobForm'); + assert.ok($('#scheduleModal').classList.contains('open')); + form.elements.name.value = '未保存的草稿'; + const toggle = $('#scheduleModelPicker [data-model-toggle]'); + const menu = $('#scheduleModelPicker .ai-model-menu'); + toggle.click(); + assert.equal(menu.hidden, false); + assert.equal(toggle.getAttribute('aria-expanded'), 'true'); + const esc = () => { + const event = new app.window.KeyboardEvent('keydown', { key: 'Escape', bubbles: true, cancelable: true }); + (app.document.activeElement || app.document.body).dispatchEvent(event); + return event; + }; + toggle.focus(); + assert.equal(esc().defaultPrevented, true); + assert.equal(menu.hidden, true, 'the first Escape closes the model menu'); + assert.equal(toggle.getAttribute('aria-expanded'), 'false'); + assert.ok($('#scheduleModal').classList.contains('open'), 'the editor stays open'); + assert.equal(form.elements.name.value, '未保存的草稿'); + assert.equal(app.document.activeElement, toggle, 'focus returns to the model button'); + esc(); + await settle(2); + assert.equal($('#scheduleModal').classList.contains('open'), false, 'a second Escape closes the editor as before'); +}); From f397e987f071adaeeb3c108b5e22a8a330aa55ab Mon Sep 17 00:00:00 2001 From: roberttwang Date: Fri, 27 Nov 2026 15:12:11 +0000 Subject: [PATCH 2/2] fix(schedule): keep the model menu Esc handler from stealing other layers' Esc - Close the model menu together with the editor. A menu left open when the editor closes without a click (save, failed load, project switch) kept the capture-phase handler armed, so the next Esc on another modal only closed the hidden menu. - Skip the handler while a confirm dialog or the command palette sits above the editor. The confirm binds its capture handler lazily, after the model picker, so stopImmediatePropagation kept the confirm open and moved focus behind it. Co-Authored-By: Claude Code --- web/app/sync-schedule.js | 7 ++- webtest/tests/sync-schedule.test.mjs | 73 ++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/web/app/sync-schedule.js b/web/app/sync-schedule.js index e3fa61d23..72f78955f 100644 --- a/web/app/sync-schedule.js +++ b/web/app/sync-schedule.js @@ -330,8 +330,11 @@ function bindScheduleModelPicker(){ document.addEventListener('click',e=>{if(!host.contains(e.target))closeScheduleModelMenu()}); // Esc closes only the open menu. Capture runs before the page-level Esc // handler that would otherwise close the whole editor and drop the draft. + // A confirm dialog or the command palette above the editor keeps its Esc: + // the confirm binds its capture handler lazily, so it may run after this one. document.addEventListener('keydown',e=>{ - if(e.key!=='Escape'||menu.hidden)return; + if(e.key!=='Escape'||menu.hidden||e.defaultPrevented)return; + if($('#confirmModal')?.classList.contains('open')||$('#commandPalette')?.hidden===false)return; e.preventDefault();e.stopImmediatePropagation(); closeScheduleModelMenu(); toggle.focus({preventScroll:true}); @@ -483,6 +486,8 @@ function closeScheduleJobModal(){ scheduleModalVersion++; const modal=$('#scheduleModal'); if(!modal.classList.contains('open'))return; + // A menu left open behind a closed editor would swallow the next Esc. + closeScheduleModelMenu(); modal.classList.remove('open'); schedulePromptEditor?.destroy();schedulePromptEditor=null; const editId=scheduleModalOpener?.dataset?.editSchedule; diff --git a/webtest/tests/sync-schedule.test.mjs b/webtest/tests/sync-schedule.test.mjs index dc32528ff..3af9cb55f 100644 --- a/webtest/tests/sync-schedule.test.mjs +++ b/webtest/tests/sync-schedule.test.mjs @@ -770,3 +770,76 @@ test('Escape closes the AI job model menu first and keeps the editor and its dra await settle(2); assert.equal($('#scheduleModal').classList.contains('open'), false, 'a second Escape closes the editor as before'); }); + +test('closing the AI job editor with the model menu open does not swallow the next Escape', async (t) => { + const app = await loadApp(); + t.after(app.close); + const $ = (s) => app.document.querySelector(s); + app.router.get('/api/assistant/models', { + default: 'glm-test', + models: [{ id: 'glm-test', name: 'GLM Test', family: 'glm' }], + engines: [{ id: 'codebuddy', name: 'CodeBuddy', configured: true }], + }); + app.evaluate("state.projectFilter='project-1'"); + await app.evaluate("openScheduleJobModal('assistant')"); + await settle(4); + const toggle = $('#scheduleModelPicker [data-model-toggle]'); + const menu = $('#scheduleModelPicker .ai-model-menu'); + toggle.click(); + assert.equal(menu.hidden, false); + // Save, a failed load and a project switch close the editor without a click. + app.evaluate('closeScheduleJobModal()'); + assert.equal(menu.hidden, true, 'the model menu closes with the editor'); + assert.equal(toggle.getAttribute('aria-expanded'), 'false'); + const other = $('#customMcpModal'); + other.classList.add('open'); + const event = new app.window.KeyboardEvent('keydown', { key: 'Escape', bubbles: true, cancelable: true }); + app.document.body.dispatchEvent(event); + await settle(2); + assert.equal(other.classList.contains('open'), false, 'the first Escape reaches the modal that is actually open'); +}); + +test('a confirm dialog or the command palette above the AI job editor keeps its Escape while the model menu is open', async (t) => { + const app = await loadApp(); + t.after(app.close); + const $ = (s) => app.document.querySelector(s); + app.router.get('/api/assistant/models', { + default: 'glm-test', + models: [{ id: 'glm-test', name: 'GLM Test', family: 'glm' }], + engines: [{ id: 'codebuddy', name: 'CodeBuddy', configured: true }], + }); + app.evaluate("state.projectFilter='project-1'"); + await app.evaluate("openScheduleJobModal('assistant')"); + await settle(4); + const toggle = $('#scheduleModelPicker [data-model-toggle]'); + const menu = $('#scheduleModelPicker .ai-model-menu'); + const esc = () => { + const event = new app.window.KeyboardEvent('keydown', { key: 'Escape', bubbles: true, cancelable: true }); + (app.document.activeElement || app.document.body).dispatchEvent(event); + }; + toggle.click(); + assert.equal(menu.hidden, false); + // The confirm dialog binds its own capture-phase Esc handler on first use, + // after the model picker has bound its handler. + app.evaluate("window.__confirm=afConfirm({title:'确认',message:'继续?'})"); + await settle(2); + assert.ok($('#confirmModal').classList.contains('open')); + esc(); + await settle(2); + assert.equal($('#confirmModal').classList.contains('open'), false, 'Escape cancels the confirm dialog on top'); + assert.equal(await app.evaluate('window.__confirm'), false); + assert.ok($('#scheduleModal').classList.contains('open'), 'the editor stays open'); + toggle.click(); + if (menu.hidden) toggle.click(); + assert.equal(menu.hidden, false); + app.evaluate('openCommandPalette()'); + await settle(1); + assert.equal($('#commandPalette').hidden, false); + esc(); + await settle(1); + assert.equal($('#commandPalette').hidden, true, 'Escape closes the command palette on top'); + assert.ok($('#scheduleModal').classList.contains('open'), 'the editor stays open'); + esc(); + assert.equal(menu.hidden, true, 'the next Escape closes the model menu'); + assert.ok($('#scheduleModal').classList.contains('open')); +});