diff --git a/app/src/routes/canvas.tsx b/app/src/routes/canvas.tsx index 36256474..ba14dfa4 100644 --- a/app/src/routes/canvas.tsx +++ b/app/src/routes/canvas.tsx @@ -828,45 +828,50 @@ const CanvasRoute: VoidComponent = () => { } }} /> - { - setDiscardDialogOpen(open); - if (!open) setPendingSelectedId(undefined); - }} - title={ - activeEditor()?.api.error() - ? "This note didn't save" - : "Discard unsaved changes?" - } - description={ - activeEditor()?.api.error() - ? "Your latest changes aren't saved. If you stay, this note keeps retrying. If you leave now, they will be lost." - : "Your unsaved changes will be lost if you leave this note." - } - confirmLabel={ - activeEditor()?.api.error() ? "Leave" : "Discard changes" - } - cancelLabel={activeEditor()?.api.error() ? "Stay" : "Keep editing"} - onConfirm={() => { - setSelectedId(pendingSelectedId()); - setPendingSelectedId(undefined); - }} - /> - setSaveInProgressDialogOpen(false)} - > - Keep editing - - } - /> + {/* Mounted on demand so their portals land above the panel's. */} + + { + setDiscardDialogOpen(open); + if (!open) setPendingSelectedId(undefined); + }} + title={ + activeEditor()?.api.error() + ? "This note didn't save" + : "Discard unsaved changes?" + } + description={ + activeEditor()?.api.error() + ? "Your latest changes aren't saved. If you stay, this note keeps retrying. If you leave now, they will be lost." + : "Your unsaved changes will be lost if you leave this note." + } + confirmLabel={ + activeEditor()?.api.error() ? "Leave" : "Discard changes" + } + cancelLabel={activeEditor()?.api.error() ? "Stay" : "Keep editing"} + onConfirm={() => { + setSelectedId(pendingSelectedId()); + setPendingSelectedId(undefined); + }} + /> + + + setSaveInProgressDialogOpen(false)} + > + Keep editing + + } + /> + ); diff --git a/app/tests/e2e/canvas-note-panel.spec.ts b/app/tests/e2e/canvas-note-panel.spec.ts index e87328fe..c005a374 100644 --- a/app/tests/e2e/canvas-note-panel.spec.ts +++ b/app/tests/e2e/canvas-note-panel.spec.ts @@ -9,9 +9,10 @@ import { waitForAppReady } from "./helpers/readiness"; import { holdNextServerAction, settled } from "./helpers/server-actions"; // Regressions on the real /canvas route and its keyed note panel. -// PR #44: closing the panel (close button, backdrop, or Escape) used to drop -// unsaved edits and could close mid-save; Escape inside the guard dialog must -// not close the panel. +// PR #44: leaving the note (close button, Escape, or opening another note) used +// to drop unsaved edits and could happen mid-save; Escape inside the guard +// dialog must not close the panel. The panel is non-modal, so it has no +// backdrop and the map and search stay usable beside it. // PR #43: a disposed panel viewer must not report its late failure over the // note that replaced it. // @@ -37,6 +38,19 @@ const openFromRail = async (page: Page, title: string) => { await page.waitForLoadState("networkidle"); }; +// DocumentEditor saves 1.5s after the last keystroke, which races any case that +// needs an unsaved draft. This drops that one timer; if the delay changes, the +// unsaved case fails on its dialog or its no-save check. +const AUTOSAVE_DELAY_MS = 1500; +const holdAutosave = (page: Page) => + page.evaluate((autosaveMs) => { + const schedule = window.setTimeout.bind(window); + window.setTimeout = ((handler: TimerHandler, ms?: number, ...args) => + ms === autosaveMs + ? 0 + : schedule(handler, ms, ...args)) as typeof window.setTimeout; + }, AUTOSAVE_DELAY_MS); + const typeIntoEditor = async (page: Page, text: string) => { await editor(page).click(); await expect(editor(page)).toBeFocused(); @@ -48,8 +62,13 @@ const typeIntoEditor = async (page: Page, text: string) => { ).toBeVisible(); }; -const clickBackdrop = (page: Page) => - panel(page).click({ position: { x: 10, y: 200 } }); +// The panel covers the rail, so search is how to ask for another note by name: +// Enter on a single match opens it. +const openFromSearch = async (page: Page, title: string) => { + const search = page.getByRole("searchbox", { name: /^Search notes/ }); + await search.fill(title); + await search.press("Enter"); +}; test.describe("canvas note panel", () => { // The canvas reads the newest UMAP run, so these tests run one at a time in @@ -88,6 +107,9 @@ test.describe("canvas note panel", () => { }); test("unsaved edits ask before the panel closes", async ({ page }) => { + // In Chromium, `request.postData()` is null for server-action requests + // unless the page has a route, so the no-save check below needs one. + await page.route("**/_server*", (route) => route.continue()); const posts: string[] = []; page.on("request", (request) => { if (request.method() === "POST" && request.url().includes("/_server")) { @@ -98,6 +120,7 @@ test.describe("canvas note panel", () => { }); await openFromRail(page, title); + await holdAutosave(page); await typeIntoEditor(page, " unsaved canvas edit"); await panel(page).getByRole("button", { name: "Close panel" }).click(); @@ -118,14 +141,15 @@ test.describe("canvas note panel", () => { await expect(panel(page)).toBeVisible(); await expect(editor(page)).toContainText("unsaved canvas edit"); - await clickBackdrop(page); + await openFromSearch(page, otherTitle); await expect(discardDialog(page)).toBeVisible(); await discardDialog(page) .getByRole("button", { name: "Discard changes" }) .click(); - await expect(panel(page)).toBeHidden(); + await expect(editor(page)).toContainText(`Body of ${otherTitle}`); - await openFromRail(page, title); + await openFromSearch(page, title); + await expect(editor(page)).toContainText(`Body of ${title}`); await expect(editor(page)).not.toContainText("unsaved canvas edit"); expect(posts, "discarding must not save the edit").toEqual([]); }); @@ -153,9 +177,10 @@ test.describe("canvas note panel", () => { page, }) => { await openFromRail(page, title); + await holdAutosave(page); await typeIntoEditor(page, " saved canvas edit"); - const save = await holdNextServerAction(page, "updateDoc", [noteId]); + const save = await holdNextServerAction(page, "saveDocContent", [noteId]); await panel(page) .getByRole("button", { name: "Save note changes" }) .click(); @@ -169,7 +194,7 @@ test.describe("canvas note panel", () => { await expect(savingDialog(page)).toBeHidden(); await expect(panel(page)).toBeVisible(); - await clickBackdrop(page); + await openFromSearch(page, otherTitle); await expect(savingDialog(page)).toBeVisible(); save.release(); @@ -182,7 +207,8 @@ test.describe("canvas note panel", () => { await expect(discardDialog(page)).toBeHidden(); await expect(panel(page)).toBeHidden(); - await openFromRail(page, title); + // The rail still lists the search above, so reopen through search too. + await openFromSearch(page, title); await expect(editor(page)).toContainText("saved canvas edit"); });