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:
Knotty Wyvern
2026-10-28 11:09:09 -07:00
parent 724402729e
commit 6cd9195370
17 changed files with 152 additions and 135 deletions
+9 -10
View File
@@ -2,7 +2,8 @@ import { tr } from '../i18n/index.ts';
import { actionIcon, type ActionIcon } from "../ui/actionIcons";
import { attachTooltip } from "../ui/tooltip";
const tooltipCleanups = new WeakMap<HTMLElement, () => void>();
import { actionMenu, type ActionMenuItem } from "../ui/actionMenu";
import type { ActionMenuItem } from "../ui/actionMenu";
import { showActionMenu, type MenuHandle } from "../ui/managedActionMenu";
export type ImageActions = {
edit?: () => void;
@@ -107,9 +108,10 @@ export function imageControls(root: HTMLElement, actions: ImageActions = {}, mea
if (actions.edit) toolbar.append(button(actions.editing ? tr("Hide image source") : tr("Edit image source"), "code", actions.editing && actions.done ? actions.done : actions.edit));
frame.append(toolbar);
cleanup.push(() => toolbar.querySelectorAll<HTMLButtonElement>("button").forEach(button => tooltipCleanups.get(button)?.()));
let menu: HTMLElement | undefined;
let menu: MenuHandle | undefined;
let openingMenu = false;
function closeMenu(focus = true) { menu?.remove(); menu = undefined; if (focus && frame.isConnected) frame.focus({ preventScroll: true }); }
const restore = () => { if (frame.isConnected) frame.focus({ preventScroll: true }); };
function closeMenu(focus = true) { menu?.close(); menu = undefined; if (focus) restore(); }
async function run(action: () => void | Promise<void>) {
closeMenu(false);
try { await action(); } catch (error) {
@@ -138,7 +140,7 @@ export function imageControls(root: HTMLElement, actions: ImageActions = {}, mea
...(actions.reveal && !/^https?:/i.test(image.dataset.mdSrc || "") ? [{ label: tr("Show in Explorer"), icon: "external" as const, run: () => void run(() => actions.reveal!(image)) }] : []),
];
openingMenu = true;
try { menu = actionMenu(x, y, items, closeMenu, tr("Image actions")); }
try { menu = showActionMenu({ x, y, items, label: tr("Image actions") }, undefined, restore); }
finally { openingMenu = false; }
}
frame.addEventListener("mousedown", event => { if (!(event.target as Element).closest("button")) { event.preventDefault(); event.stopPropagation(); select(); } });
@@ -155,15 +157,12 @@ export function imageControls(root: HTMLElement, actions: ImageActions = {}, mea
});
const outside = (event: Event) => {
if (openingMenu) return;
if (event.target instanceof Node && !frame.contains(event.target) && !menu?.contains(event.target)) { closeMenu(false); frame.classList.remove("is-selected"); }
if (event.target instanceof Node && !frame.contains(event.target) && !menu?.element.contains(event.target)) { closeMenu(false); frame.classList.remove("is-selected"); }
};
const dismiss = () => closeMenu(false);
const scroll = (event: Event) => { if (menu && event.target instanceof Node && !menu.contains(event.target)) closeMenu(false); };
// The shared menu closes itself. This handler also clears the image selection.
document.addEventListener("mousedown", outside, true);
document.addEventListener("focusin", outside, true);
window.addEventListener("resize", dismiss);
document.addEventListener("scroll", scroll, true);
cleanup.push(() => { closeMenu(false); document.removeEventListener("mousedown", outside, true); document.removeEventListener("focusin", outside, true); window.removeEventListener("resize", dismiss); document.removeEventListener("scroll", scroll, true); });
cleanup.push(() => { closeMenu(false); document.removeEventListener("mousedown", outside, true); document.removeEventListener("focusin", outside, true); });
if (measure) {
image.addEventListener("load", measure);
image.addEventListener("error", measure);