Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion web/app/sync-schedule.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,17 @@ 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.
// 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||e.defaultPrevented)return;
if($('#confirmModal')?.classList.contains('open')||$('#commandPalette')?.hidden===false)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);
Expand Down Expand Up @@ -476,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;
Expand Down
110 changes: 110 additions & 0 deletions webtest/tests/sync-schedule.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -733,3 +733,113 @@ 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');
});

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'));
});
Loading