// The host layer of the Project Management interface: the shared action menu, the shared tooltip, and the shared action // icons of RuneStone. The interface builds plain DOM. A Svelte version is a later card. import { showActionMenu, type MenuOptions } from "../../../ui/managedActionMenu.ts"; import { attachTooltip } from "../../../ui/tooltip.ts"; export { actionIcons, type ActionIcon } from "../../../ui/actionIcons.ts"; export type { ActionMenuItem } from "../../../ui/actionMenu.ts"; /** The menus that the interface opens. Tests can give their own. */ export interface MenuHost { showActionMenu(options: MenuOptions): { close(): void }; } export const hostMenus: MenuHost = { showActionMenu: options => showActionMenu(options) }; /** The attribute that gives a control its tooltip text. */ export const TOOLTIP = "data-pm-tooltip"; /** Give a control the shared tooltip. The text can change later through the attribute. */ export function tooltip(control: T, text: string): T { control.setAttribute(TOOLTIP, text); return control; } /** * Attach the shared tooltip to each control in `root` with `data-pm-tooltip`, also to controls that a render adds later. * A removed control loses its tooltip. Returns the function that removes all tooltips. */ export function mountTooltips(root: HTMLElement): () => void { const attached = new Map void>(); const attach = (node: Node) => { if (!(node instanceof HTMLElement)) return; const controls = node.matches(`[${TOOLTIP}]`) ? [node, ...node.querySelectorAll(`[${TOOLTIP}]`)] : node.querySelectorAll(`[${TOOLTIP}]`); for (const control of controls) { if (attached.has(control)) continue; attached.set(control, attachTooltip(control, () => control.getAttribute(TOOLTIP) ?? "")); } }; const sweep = () => { for (const [control, detach] of attached) { if (control.isConnected && root.contains(control)) continue; detach(); attached.delete(control); } }; const observer = new MutationObserver(records => { sweep(); for (const record of records) for (const node of record.addedNodes) attach(node); }); observer.observe(root, { childList: true, subtree: true }); attach(root); return () => { observer.disconnect(); for (const detach of attached.values()) detach(); attached.clear(); }; }