From 0ee28f778370fd950421cf1f97609fa5e4b4e4c6 Mon Sep 17 00:00:00 2001 From: l0ng-ai <24760907+l0ng-ai@users.noreply.github.com> Date: Sat, 21 Nov 2026 23:03:25 +0800 Subject: [PATCH] fix(editor): keep the keyboard in the editor after closing the focused tab MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- src/editor/core/controller.test.ts | 23 +++++++++++++++++++++++ src/editor/core/controller.ts | 14 ++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/editor/core/controller.test.ts b/src/editor/core/controller.test.ts index cb2e72390..799c1370d 100644 --- a/src/editor/core/controller.test.ts +++ b/src/editor/core/controller.test.ts @@ -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"); diff --git a/src/editor/core/controller.ts b/src/editor/core/controller.ts index 5fd984242..671e9cba5 100644 --- a/src/editor/core/controller.ts +++ b/src/editor/core/controller.ts @@ -1735,8 +1735,18 @@ export class EditorController implements SlotHost { }); } - closeTab(paneId: string, tabId: string): Promise { - return this.removeGuarded((s) => (s.panes[paneId] ? closeTab(s, paneId, tabId) : s)); + async closeTab(paneId: string, tabId: string): Promise { + 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 { return this.removeGuarded((s) => (s.panes[paneId] ? closeOthers(s, paneId, tabId) : s), {