From a34c1a5dce58a9f13339c830b69ca3953e32a5d9 Mon Sep 17 00:00:00 2001 From: Knotty Wyvern Date: Wed, 28 Oct 2026 16:53:03 -0700 Subject: [PATCH] Keep an open context menu when its items change The file pane menu took its items from live state: the template list, the selection, and the clipboard. A template reload while the menu was open rebuilt the menu and closed its open submenu. The workspace/manageTemplates smoke test failed in about half of its runs because of this race. ContextMenu.svelte now reads the items once when the menu opens. It builds the menu again only when its position or label changes. manageTemplates has a new check: a template reload keeps the open submenu. The check fails without the fix. With the fix, the test passed 6 of 6 runs. The tree, plugins, menus, and workspace smoke groups pass (23 of 23), and pnpm check passes. --- src/lib/layout/ContextMenu.svelte | 3 ++- tests/smoke/workspace/manageTemplates.smoke.mjs | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/lib/layout/ContextMenu.svelte b/src/lib/layout/ContextMenu.svelte index d46ea9d..fc53b8a 100644 --- a/src/lib/layout/ContextMenu.svelte +++ b/src/lib/layout/ContextMenu.svelte @@ -14,7 +14,8 @@ let { x, y, items, onClose, label = "Context actions" }: Props = $props(); $effect(() => { - const menuX = x, menuY = y, entries = items, name = label; + // Read the items once. A change of state behind an open menu, such as a template reload, must not rebuild it and close its submenu. + const menuX = x, menuY = y, name = label, entries = untrack(() => items); const previous = document.activeElement instanceof HTMLElement ? document.activeElement : null; function close(restoreFocus = false) { root.remove(); diff --git a/tests/smoke/workspace/manageTemplates.smoke.mjs b/tests/smoke/workspace/manageTemplates.smoke.mjs index b8643d2..391c34a 100644 --- a/tests/smoke/workspace/manageTemplates.smoke.mjs +++ b/tests/smoke/workspace/manageTemplates.smoke.mjs @@ -33,6 +33,13 @@ await runSmoke(name, async ({ page }) => { await row("a.md").waitFor(); await page.getByRole("navigation", { name: "Workspace files" }).click({ button: "right", position: { x: 90, y: 360 } }); await item("New note").hover(); + // A template reload behind the open menu keeps the menu and its open submenu (src/lib/layout/ContextMenu.svelte). + const submenu = page.getByRole("menu", { name: "New note", exact: true }); + await submenu.waitFor(); + await submenu.evaluate(node => { node.dataset.smokeMark = "open"; }); + await page.evaluate(() => window.dispatchEvent(new Event("runestone-templates-changed"))); + await page.waitForTimeout(300); + assert.equal(await page.locator('[data-smoke-mark="open"]:popover-open').count(), 1, "A template reload keeps the open submenu"); await item("Daily").click(); await row("Daily.md").waitFor(); assert.equal(await file("Daily.md"), "# Daily\n\n## Tasks");