From ff4d1d4a4ad701adb1980cc07a8db4dd3819c9fd Mon Sep 17 00:00:00 2001 From: Muse Sweep Date: Sat, 24 Oct 2026 00:16:23 -0500 Subject: [PATCH] fix(id/web): rebind Find panel and Go to Line dialog when the editor is recreated (#718) Both modules keep their DOM in a module-level singleton bound to the first editor's view/container. Their destroy helpers have no production caller (only test afterEach), so after disconnect/reopen the panel and dialog were detached nodes bound to a destroyed view: Ctrl+F/Ctrl+G returned true but nothing ever appeared. openPanel/openGotoLine now destroy and recreate the singleton when it belongs to a different view/container. Regression tests recreate the editor without calling the destroy helpers, mirroring production teardown: both fail pre-fix, all 43 search-panel + goto-line tests pass post-fix, typecheck clean. --- pkgs/id/web/src/goto-line.test.ts | 22 ++++++++++++++++++++++ pkgs/id/web/src/goto-line.ts | 9 +++++++++ pkgs/id/web/src/search-panel.test.ts | 22 ++++++++++++++++++++++ pkgs/id/web/src/search-panel.ts | 9 +++++++++ 4 files changed, 62 insertions(+) diff --git a/pkgs/id/web/src/goto-line.test.ts b/pkgs/id/web/src/goto-line.test.ts index 4e2a5a47df..bfb9ef36d3 100644 --- a/pkgs/id/web/src/goto-line.test.ts +++ b/pkgs/id/web/src/goto-line.test.ts @@ -303,3 +303,25 @@ describe("dialog input interaction", () => { expect(posAfter).toBeLessThanOrEqual(2); }); }); + +// ── Editor recreation (#718) ─────────────────────────────────────── + +describe("goto-line dialog across editor recreation", () => { + it("Ctrl+G in a recreated editor shows a dialog in the new container without destroyGotoLineDialog()", () => { + const first = createViewWithGotoLine("first\nsecond"); + triggerGotoLine(first.view); + expect(first.container.querySelector(".goto-line-dialog")).not.toBeNull(); + + // Production teardown (main.ts disconnect) destroys only the view — + // it never calls destroyGotoLineDialog(). Mirror that exactly. + first.view.destroy(); + first.container.remove(); + + const second = createViewWithGotoLine("first\nsecond"); + views.push(second.view); + containers.push(second.container); + triggerGotoLine(second.view); + expect(second.container.querySelector(".goto-line-dialog")).not.toBeNull(); + expect(second.container.querySelector(".goto-line-input")).not.toBeNull(); + }); +}); diff --git a/pkgs/id/web/src/goto-line.ts b/pkgs/id/web/src/goto-line.ts index 97ea145417..879e66e0db 100644 --- a/pkgs/id/web/src/goto-line.ts +++ b/pkgs/id/web/src/goto-line.ts @@ -77,6 +77,15 @@ function openGotoLine(view: EditorView): void { const container = view.dom.parentElement; if (!container) return; + // The dialog is a module singleton attached to one container. When the + // editor is recreated (disconnect / open another file) the old dialog is + // detached with the old container — and no production code calls + // destroyGotoLineDialog() on teardown. Rebind instead of "opening" a + // detached node (#718). + if (dialogEl && dialogEl.parentElement !== container) { + destroyGotoLineDialog(); + } + if (!dialogEl) { dialogEl = createDialog(container); dialogInput = dialogEl.querySelector("#goto-line-input"); diff --git a/pkgs/id/web/src/search-panel.test.ts b/pkgs/id/web/src/search-panel.test.ts index 4af75bb958..3505a7550a 100644 --- a/pkgs/id/web/src/search-panel.test.ts +++ b/pkgs/id/web/src/search-panel.test.ts @@ -278,3 +278,25 @@ describe("search panel integration", () => { expect(panels.length).toBe(1); }); }); + +// ── Editor recreation (#718) ─────────────────────────────────────── + +describe("search panel across editor recreation", () => { + it("Ctrl+F in a recreated editor shows a panel in the new container without destroySearchPanel()", () => { + const first = createViewWithSearch(); + triggerSearchKey(first.view, "f"); + expect(first.container.querySelector(".search-panel")).not.toBeNull(); + + // Production teardown (main.ts disconnect) destroys only the view — + // it never calls destroySearchPanel(). Mirror that exactly. + first.view.destroy(); + first.container.remove(); + + const second = createViewWithSearch(); + activeView = second.view; + activeContainer = second.container; + triggerSearchKey(second.view, "f"); + expect(second.container.querySelector(".search-panel")).not.toBeNull(); + expect(second.container.querySelector("#search-input")).not.toBeNull(); + }); +}); diff --git a/pkgs/id/web/src/search-panel.ts b/pkgs/id/web/src/search-panel.ts index cb15e45458..73f26d323c 100644 --- a/pkgs/id/web/src/search-panel.ts +++ b/pkgs/id/web/src/search-panel.ts @@ -198,6 +198,15 @@ function openPanel(view: EditorView, showReplace: boolean): void { const container = view.dom.parentElement; if (!container) return; + // The panel is a module singleton bound to one view and container. When + // the editor is recreated (disconnect / open another file) the old panel + // is detached with the old container and its handlers still point at the + // destroyed view — and no production code calls destroySearchPanel() on + // teardown. Rebind instead of "opening" a detached node (#718). + if (panelState.panelEl && (panelState.view !== view || panelState.panelEl.parentElement !== container)) { + destroySearchPanel(); + } + if (!panelState.panelEl) { panelState.panelEl = createPanel(container); panelState.searchInput = panelState.panelEl.querySelector("#search-input");