// SPDX-License-Identifier: AGPL-3.0-or-later import { autoUpdate, computePosition, flip, offset, shift } from "@floating-ui/dom"; /** * A small floating editor for a block's source text (a LaTeX formula, a * Mermaid diagram), anchored under the block. Plain DOM, so it works in * every host. Ctrl/Cmd+Enter (or Enter on one line) saves, Escape cancels. */ export interface SourceEditorOptions { anchor: HTMLElement; value: string; label: string; multiline?: boolean; monospace?: boolean; labels: { done: string; cancel: string; remove: string }; onSave: (value: string) => void; /** Offers a Remove button. */ onRemove?: () => void; /** Called on every keystroke, e.g. for a live preview. */ onInput?: (value: string) => void; /** Called when the editor closes without saving or removing. */ onCancel?: () => void; } let open: (() => void) | null = null; export function editSource(o: SourceEditorOptions): () => void { open?.(); const box = document.createElement("div"); box.className = "dw-source"; box.setAttribute("role", "dialog"); box.setAttribute("aria-label", o.label); const field = document.createElement(o.multiline ? "textarea" : "input") as HTMLTextAreaElement | HTMLInputElement; field.className = "dw-source__field" + (o.monospace ? " dw-source__field--mono" : ""); field.value = o.value; field.setAttribute("aria-label", o.label); field.spellcheck = false; if (field instanceof HTMLTextAreaElement) field.rows = Math.min(16, Math.max(4, o.value.split("\n").length + 1)); const actions = document.createElement("div"); actions.className = "dw-source__actions"; const button = (text: string, cls: string, onClick: () => void) => { const b = document.createElement("button"); b.type = "button"; b.className = `dw-source__btn ${cls}`; b.textContent = text; b.addEventListener("mousedown", (e) => e.preventDefault()); b.addEventListener("click", onClick); return b; }; if (o.onRemove) { actions.append( button(o.labels.remove, "dw-source__btn--danger", () => { close(true); o.onRemove!(); }), ); } const spacer = document.createElement("span"); spacer.style.flex = "1"; actions.append( spacer, button(o.labels.cancel, "", () => close()), button(o.labels.done, "dw-source__btn--primary", () => save()), ); box.append(field, actions); document.body.append(box); const save = () => { const v = field.value; close(true); o.onSave(v); }; field.addEventListener("input", () => o.onInput?.(field.value)); field.addEventListener("keydown", (e) => { const ev = e as KeyboardEvent; if (ev.key === "Escape") { ev.preventDefault(); close(); } else if (ev.key === "Enter" && (ev.ctrlKey || ev.metaKey || !o.multiline)) { ev.preventDefault(); save(); } ev.stopPropagation(); }); const outside = (e: MouseEvent) => { if (!box.contains(e.target as Node) && !o.anchor.contains(e.target as Node)) close(); }; setTimeout(() => document.addEventListener("mousedown", outside), 0); const stop = autoUpdate(o.anchor, box, () => { void computePosition(o.anchor, box, { placement: "bottom-start", middleware: [offset(6), flip(), shift({ padding: 8 })], }).then(({ x, y }) => Object.assign(box.style, { left: `${x}px`, top: `${y}px` })); }); let closed = false; function close(done = false) { if (closed) return; closed = true; stop(); document.removeEventListener("mousedown", outside); box.remove(); if (open === cancel) open = null; if (!done) o.onCancel?.(); } const cancel = () => close(); open = cancel; field.focus(); if (!o.multiline) field.select(); return cancel; }