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:
+14
-28
@@ -3,21 +3,17 @@ import { EditorSelection } from "@codemirror/state";
|
||||
import type { EditorView } from "@codemirror/view";
|
||||
import { redo, redoDepth, undo, undoDepth } from "@codemirror/commands";
|
||||
import { selectSelectionMatches } from "@codemirror/search";
|
||||
import { actionMenu, type ActionMenuItem } from "../ui/actionMenu";
|
||||
import { showActionMenu, type MenuHandle } from "../ui/managedActionMenu";
|
||||
import { clipboardItems, historyItems } from "../ui/menuItems";
|
||||
|
||||
/** Keep source editing commands in the host's shared action menu. */
|
||||
export function codeMenu(view: EditorView, report: (message: string) => void) {
|
||||
let root: HTMLElement | undefined;
|
||||
let menu: MenuHandle | undefined;
|
||||
const restore = () => view.focus();
|
||||
function close(focus = true) {
|
||||
root?.remove(); root = undefined;
|
||||
if (focus) view.focus();
|
||||
menu?.close(); menu = undefined;
|
||||
if (focus) restore();
|
||||
}
|
||||
const outside = (event: MouseEvent) => { if (root && !root.contains(event.target as Node)) close(false); };
|
||||
const scroll = (event: Event) => { if (root && event.target instanceof Node && !root.contains(event.target)) close(false); };
|
||||
const resize = () => close(false);
|
||||
document.addEventListener("mousedown", outside, true);
|
||||
document.addEventListener("scroll", scroll, true);
|
||||
window.addEventListener("resize", resize);
|
||||
function open(x: number, y: number) {
|
||||
close(false);
|
||||
const state = view.state, main = state.selection.main;
|
||||
@@ -30,35 +26,25 @@ export function codeMenu(view: EditorView, report: (message: string) => void) {
|
||||
view.focus();
|
||||
};
|
||||
const clipboard = async (kind: "cut" | "copy" | "paste") => {
|
||||
close();
|
||||
try {
|
||||
if (kind === "paste") replace(await navigator.clipboard.readText());
|
||||
else { await navigator.clipboard.writeText(selected); if (kind === "cut") replace(""); }
|
||||
} catch { report("Clipboard unavailable. Use the keyboard shortcut to copy, cut, or paste."); }
|
||||
};
|
||||
const changeAll = () => {
|
||||
close();
|
||||
if (!valid() || !word) return;
|
||||
if (main.empty) view.dispatch({ selection: EditorSelection.single(word.from, word.to) });
|
||||
selectSelectionMatches(view);
|
||||
};
|
||||
const items: ActionMenuItem[] = [
|
||||
{ label: tr("Undo"), icon: "undo", shortcut: "Ctrl+Z", disabled: !undoDepth(state), run: () => { close(); undo(view); } },
|
||||
{ label: tr("Redo"), icon: "redo", shortcut: "Ctrl+Shift+Z", disabled: !redoDepth(state), run: () => { close(); redo(view); } },
|
||||
{ separator: true },
|
||||
menu = showActionMenu({ x, y, label: tr("Code text actions"), items: [
|
||||
...historyItems({ canUndo: undoDepth(state) > 0, canRedo: redoDepth(state) > 0, undo: () => undo(view), redo: () => redo(view) }),
|
||||
{ label: tr("Change all occurrences"), icon: "select", shortcut: "Ctrl+Shift+L", disabled: !word || word.empty, run: changeAll },
|
||||
{ separator: true },
|
||||
{ label: tr("Cut"), icon: "cut", shortcut: "Ctrl+X", disabled: !selected, run: () => void clipboard("cut") },
|
||||
{ label: tr("Copy"), icon: "copy", shortcut: "Ctrl+C", disabled: !selected, run: () => void clipboard("copy") },
|
||||
{ label: tr("Paste"), icon: "paste", shortcut: "Ctrl+V", run: () => void clipboard("paste") },
|
||||
{ label: tr("Select all"), icon: "select", shortcut: "Ctrl+A", run: () => { close(); view.dispatch({ selection: { anchor: 0, head: view.state.doc.length } }); } },
|
||||
];
|
||||
root = actionMenu(x, y, items, close, tr("Code text actions"));
|
||||
...clipboardItems({
|
||||
selected: !!selected, cut: () => void clipboard("cut"), copy: () => void clipboard("copy"), paste: () => void clipboard("paste"),
|
||||
selectAll: () => view.dispatch({ selection: { anchor: 0, head: view.state.doc.length } }),
|
||||
}),
|
||||
] }, undefined, restore);
|
||||
}
|
||||
return { open, close, destroy() {
|
||||
close(false);
|
||||
document.removeEventListener("mousedown", outside, true);
|
||||
document.removeEventListener("scroll", scroll, true);
|
||||
window.removeEventListener("resize", resize);
|
||||
} };
|
||||
return { open, close, destroy() { close(false); } };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user