fix(pages): Ctrl+Z undoes when focus fell out of the editor
A toolbar button removed with its section drops focus on the canvas body, and a button that just went disabled drops it on the page body; neither reached the editor's key handler. Listen on both documents as a fallback. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
1 parent
d977467182
commit
cd3c2e1987
2 files changed
+40
-1
No files matched your search
@@ -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();
|
||||
|
||||
@@ -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) => (
|
||||
<div role="menu" aria-label={t("PagesAddSection")} className="absolute top-full left-1/2 z-30 mt-1 grid w-56 -translate-x-1/2 gap-0.5 rounded-md border bg-popover p-1 text-popover-foreground shadow-md">
|
||||
|
||||
Reference in new issue
Block a user