Use one menu lifecycle and shared menu rows in the editors
Phase 4c. The Markdown, code, Read, and image menus open through showActionMenu. It closes a menu on an outside press, an outside scroll, a resize, and window blur. A host surface now gives its own focus return, so four copies of the listener code are gone. - menuItems.ts builds Undo, Redo, the clipboard rows, and the search row. The Markdown, code, Read, table cell, and table range menus use it. The Markdown and table menus now show the shortcuts for Cut, Copy, Paste, and Select all, as the other menus did. - writerSearch.ts is now editorSearch.ts, because it serves Write and Source. - Plugins still get only close() from api.ui.showActionMenu. The SDK menu types do not change. - The table range menu and the native spelling menu keep their own lifecycle, because their open state drives table editing and the native spelling protocol. - docs/ui-design.md names the shared rows and the search modules. The menu inventory has the new count for markdownMenu.ts. pnpm check, pnpm test (341 of 341), and the full smoke suite pass, except tables/formulas, which failed one time under load and passed on retry.
This commit is contained in:
@@ -1,8 +1,13 @@
|
||||
import { actionMenu, type ActionMenuItem } from "./actionMenu.ts";
|
||||
|
||||
export type MenuOptions = { x: number; y: number; items: ActionMenuItem[]; label?: string; parent?: HTMLElement };
|
||||
export type MenuHandle = { close(restore?: boolean): void; readonly element: HTMLElement; readonly open: boolean };
|
||||
|
||||
export function showActionMenu(options: MenuOptions, signal?: AbortSignal): { close(): void } {
|
||||
/**
|
||||
* Show the shared action menu. It closes on a press outside it, a scroll outside it, a window resize, and window blur.
|
||||
* A choice and Escape return focus to the element that had focus. A host surface can give `restore` to return focus itself.
|
||||
*/
|
||||
export function showActionMenu(options: MenuOptions, signal?: AbortSignal, restore?: () => void): MenuHandle {
|
||||
const origin = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
||||
// Focus can scroll a trigger before its queued scroll event reaches this menu.
|
||||
const scrollPositions = new Map<EventTarget, [number, number]>();
|
||||
@@ -10,20 +15,24 @@ export function showActionMenu(options: MenuOptions, signal?: AbortSignal): { cl
|
||||
scrollPositions.set(node, [node.scrollLeft, node.scrollTop]);
|
||||
}
|
||||
scrollPositions.set(document, [window.scrollX, window.scrollY]);
|
||||
let root: HTMLElement | undefined;
|
||||
const close = (restore = false) => {
|
||||
root?.remove();
|
||||
let open = true;
|
||||
const close = (back = false) => {
|
||||
if (!open) return;
|
||||
open = false;
|
||||
root.remove();
|
||||
window.removeEventListener("pointerdown", outside, true);
|
||||
window.removeEventListener("resize", dismiss);
|
||||
window.removeEventListener("blur", dismiss);
|
||||
document.removeEventListener("scroll", scroll, true);
|
||||
signal?.removeEventListener("abort", dismiss);
|
||||
if (restore && origin?.isConnected) origin.focus({ preventScroll: true });
|
||||
if (!back) return;
|
||||
if (restore) restore();
|
||||
else if (origin?.isConnected) origin.focus({ preventScroll: true });
|
||||
};
|
||||
const dismiss = () => close();
|
||||
const outside = (event: PointerEvent) => { if (!root?.contains(event.target as Node)) close(); };
|
||||
const outside = (event: PointerEvent) => { if (!root.contains(event.target as Node)) close(); };
|
||||
const scroll = (event: Event) => {
|
||||
if (root?.contains(event.target as Node)) return;
|
||||
if (root.contains(event.target as Node)) return;
|
||||
const before = event.target && scrollPositions.get(event.target);
|
||||
const current = event.target === document ? [window.scrollX, window.scrollY]
|
||||
: event.target instanceof HTMLElement ? [event.target.scrollLeft, event.target.scrollTop] : undefined;
|
||||
@@ -34,12 +43,12 @@ export function showActionMenu(options: MenuOptions, signal?: AbortSignal): { cl
|
||||
...(item.children ? { children: wrap(item.children) } : {}),
|
||||
...(!item.children || item.run ? { run: () => { close(true); if (!signal?.aborted) item.run?.(); } } : {}),
|
||||
});
|
||||
root = actionMenu(options.x, options.y, wrap(options.items), () => close(true), options.label ?? "Actions", options.parent);
|
||||
const root = actionMenu(options.x, options.y, wrap(options.items), () => close(true), options.label ?? "Actions", options.parent);
|
||||
window.addEventListener("pointerdown", outside, true);
|
||||
window.addEventListener("resize", dismiss);
|
||||
window.addEventListener("blur", dismiss);
|
||||
document.addEventListener("scroll", scroll, true);
|
||||
signal?.addEventListener("abort", dismiss, { once: true });
|
||||
if (signal?.aborted) close();
|
||||
return { close: () => close() };
|
||||
return { close, element: root, get open() { return open; } };
|
||||
}
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
import { tr } from "../i18n/index.ts";
|
||||
import type { ActionMenuItem } from "./actionMenu.ts";
|
||||
|
||||
// The common text actions of the editor and reading menus. Each menu supplies only its commands.
|
||||
|
||||
/** Undo and Redo, then a separator. */
|
||||
export function historyItems(history: { canUndo: boolean; canRedo: boolean; undo(): void; redo(): void }): ActionMenuItem[] {
|
||||
return [
|
||||
{ label: tr("Undo"), icon: "undo", shortcut: "Ctrl+Z", disabled: !history.canUndo, run: history.undo },
|
||||
{ label: tr("Redo"), icon: "redo", shortcut: "Ctrl+Shift+Z", disabled: !history.canRedo, run: history.redo },
|
||||
{ separator: true },
|
||||
];
|
||||
}
|
||||
|
||||
/** Search the workspace for the selected text. The label quotes at most 32 characters. */
|
||||
export function searchItem(text: string, search?: (text: string) => void): ActionMenuItem {
|
||||
return {
|
||||
label: text ? tr("Search for “{text}”", { text: text.length > 32 ? text.slice(0, 32) + "…" : text }) : tr("Search selection"),
|
||||
icon: "search", disabled: !text || !search, run: () => search?.(text),
|
||||
};
|
||||
}
|
||||
|
||||
export type ClipboardActions = {
|
||||
selected: boolean;
|
||||
copy(): void;
|
||||
cut?(): void;
|
||||
paste?(): void;
|
||||
pastePlain?(): void;
|
||||
selectAll?(): void;
|
||||
};
|
||||
|
||||
/** The clipboard actions in their shared order. A menu leaves out an action that it does not supply. */
|
||||
export function clipboardItems(actions: ClipboardActions): ActionMenuItem[] {
|
||||
const items: ActionMenuItem[] = [];
|
||||
if (actions.cut) items.push({ label: tr("Cut"), icon: "cut", shortcut: "Ctrl+X", disabled: !actions.selected, run: actions.cut });
|
||||
items.push({ label: tr("Copy"), icon: "copy", shortcut: "Ctrl+C", disabled: !actions.selected, run: actions.copy });
|
||||
if (actions.paste) items.push({ label: tr("Paste"), icon: "paste", shortcut: "Ctrl+V", run: actions.paste });
|
||||
if (actions.pastePlain) items.push({ label: tr("Paste as plain text"), icon: "paste-text", run: actions.pastePlain });
|
||||
if (actions.selectAll) items.push({ label: tr("Select all"), icon: "select", shortcut: "Ctrl+A", run: actions.selectAll });
|
||||
return items;
|
||||
}
|
||||
Reference in New Issue
Block a user