diff --git a/solvway-admin-portal/app/components/pages/visual/VisualEditor.test.tsx b/solvway-admin-portal/app/components/pages/visual/VisualEditor.test.tsx index 36c06cb..f890d67 100644 --- a/solvway-admin-portal/app/components/pages/visual/VisualEditor.test.tsx +++ b/solvway-admin-portal/app/components/pages/visual/VisualEditor.test.tsx @@ -131,6 +131,33 @@ describe("VisualEditor", () => { expect(canvas.getByRole("textbox", { name: "Question 1" })).toHaveTextContent("Parking?"); }); + it("undoes with Ctrl+Z when nothing is focused (a button that just went disabled lets focus fall to the page)", async () => { + const canvas = await open(); + fireEvent.click(section(2)); + fireEvent.click(canvas.getByRole("button", { name: "Remove block" })); + fireEvent.keyDown(document.body, { key: "z", ctrlKey: true }); + expect(canvas.getByRole("textbox", { name: "Question 1" })).toHaveTextContent("Parking?"); + }); + + it("undoes with Ctrl+Z right after a toolbar delete, when focus falls to the canvas", async () => { + const canvas = await open(); + fireEvent.click(section(2)); + fireEvent.click(canvas.getByRole("button", { name: "Remove block" })); + fireEvent.keyDown(frame().contentDocument!.body, { key: "z", ctrlKey: true }); + expect(canvas.getByRole("textbox", { name: "Question 1" })).toHaveTextContent("Parking?"); + }); + + it("undoes one step per Ctrl+Z from the editor's own buttons", async () => { + const canvas = await open(); + fireEvent.click(section(1)); + fireEvent.click(canvas.getByRole("button", { name: "Remove block" })); + fireEvent.click(section(1)); + fireEvent.click(canvas.getByRole("button", { name: "Remove block" })); + fireEvent.keyDown(screen.getByRole("button", { name: "Save" }), { key: "z", ctrlKey: true }); + expect(canvas.getByText("Parking?")).toBeInTheDocument(); + expect(canvas.queryAllByRole("textbox", { name: "Title" })).toHaveLength(0); + }); + it("selects, outlines and explains a refused form: the message is at the Event field", async () => { mocks.savePage.mockRejectedValueOnce(new ApiError("x", 422, { code: "PagesEventRequired", details: { field: "blocks[3].eventUuid" } })); await open(); diff --git a/solvway-admin-portal/app/components/pages/visual/VisualEditor.tsx b/solvway-admin-portal/app/components/pages/visual/VisualEditor.tsx index c339443..b3945cc 100644 --- a/solvway-admin-portal/app/components/pages/visual/VisualEditor.tsx +++ b/solvway-admin-portal/app/components/pages/visual/VisualEditor.tsx @@ -47,13 +47,25 @@ export function VisualEditor({ p, ask }: { p: Draft; ask: Ask }) { focused()?.blur(); if (redo) p.redo(); else p.undo(); }; - const onKeyDown = (e: KeyboardEvent) => { + const onKeyDown = (e: KeyboardEvent | globalThis.KeyboardEvent) => { if (!(e.ctrlKey || e.metaKey) || e.key.toLowerCase() !== "z") return; // Inside rich text, its own undo works on the text until it loses focus. if ((e.target as Element).closest?.(".ProseMirror")) return; e.preventDefault(); history(e.shiftKey); }; + // Focus that fell out of the editor (a toolbar button removed with its section drops it on the canvas body; a + // button that just went disabled drops it on the page): the key still undoes, unless the editor already handled it + // or it is typing in some other field. + useEffect(() => { + const onBody = (e: globalThis.KeyboardEvent) => { + const el = e.target as HTMLElement; + if (!e.defaultPrevented && !el.isContentEditable && !el.closest?.("input, textarea, select")) onKeyDown(e); + }; + const docs = [document, canvas.current?.querySelector("iframe")?.contentDocument].filter((d): d is Document => !!d); + docs.forEach((d) => d.addEventListener("keydown", onBody)); + return () => docs.forEach((d) => d.removeEventListener("keydown", onBody)); + }); const addMenu = (at: number) => (