fix(editor): keep the keyboard in the editor after closing the focused tab

Closing the focused editor tab (⌘W) showed the next tab but left the
keyboard focus on the page body, so arrow keys and typing did nothing
until the editor was clicked. The tab that takes its place now gets the
focus when the closed one had it.
Цей коміт міститься в:
l0ng-ai committed 2026-11-21 23:03:25 +08:00
1 джерело 83364c5423
коміт 0ee28f7783
2 files changed
+35 -2

No files matched your search

+23
Переглянути файл
@@ -203,6 +203,29 @@ describe("EditorController", () => {
}
});
it("gives the keyboard to the next tab when the focused one closes", async () => {
ctrl = new EditorController(createMemoryBackend({ "/p/a.ts": "a\n", "/p/b.ts": "b\n" }));
ctrl.start();
await ctrl.openFile("/p/a.ts", { focus: false });
const first = ctrl.activeSlot!;
await ctrl.openFile("/p/b.ts", { focus: false });
const second = ctrl.activeSlot!;
const hosts = [document.createElement("div"), document.createElement("div")];
document.body.append(...hosts);
try {
const a = first.mount(hosts[0]!);
ctrl.onMounted(first);
const b = second.mount(hosts[1]!);
ctrl.onMounted(second);
b.focus();
expect(b.hasFocus).toBe(true);
await ctrl.closeActiveTab();
expect(a.hasFocus).toBe(true);
} finally {
hosts.forEach((h) => h.remove());
}
});
it("autosaves on blur, but not while focus is in a popup over the editor", async () => {
const backend = createMemoryBackend({ "/p/a.c": "int main() {\n \n}\n" });
const save = vi.spyOn(backend, "save");
+12 -2
Переглянути файл
@@ -1735,8 +1735,18 @@ export class EditorController implements SlotHost {
});
}
closeTab(paneId: string, tabId: string): Promise<boolean> {
return this.removeGuarded((s) => (s.panes[paneId] ? closeTab(s, paneId, tabId) : s));
async closeTab(paneId: string, tabId: string): Promise<boolean> {
const hadFocus = this.slots.get(tabId)?.view?.hasFocus ?? false;
const closed = await this.removeGuarded((s) => (s.panes[paneId] ? closeTab(s, paneId, tabId) : s));
// The closed editor had the keyboard: hand it to the tab that took its place.
if (closed && hadFocus) await this.focusActiveEditor();
return closed;
}
private async focusActiveEditor() {
const tab = this.activeTabOf();
if (tab) await this.ensureSlot(tab.id);
if (this.activeTabOf()?.id === tab?.id) this.focus();
}
closeOthers(paneId: string, tabId: string): Promise<boolean> {
return this.removeGuarded((s) => (s.panes[paneId] ? closeOthers(s, paneId, tabId) : s), {