// Formatting toolbar: plain DOM, rendered once. A ProseMirror plugin view keeps the // active/disabled state of every button in sync with the editor. Icons only, with // keycap tooltips; roving tabindex so the bar is one Tab stop. import type { Schema } from "prosemirror-model"; import { Plugin, type Command, type EditorState } from "prosemirror-state"; import type { EditorView } from "prosemirror-view"; import { findAncestor, isBlockActive, isMarkActive, type Editing } from "./editing"; import { t } from "./i18n"; import { ALT, MOD, SHIFT, h, tooltip } from "./dom"; // Lucide-style glyphs (24x24, stroke based). Static strings, safe for innerHTML. const ICONS = { undo: '', redo: '', paragraph: '', h1: '', h2: '', h3: '', bold: '', italic: '', code: '', link: '', bullets: '', ordered: '', quote: '', codeBlock: '', rule: '', } as const; interface Item { id: string; label: string; keys: string[]; icon: string; run: Command; /** Present for toggles: drives aria-pressed and the active look. */ isActive?: (state: EditorState) => boolean; } type Entry = Item | "|"; function buildItems(schema: Schema, editing: Editing, history: { undo: Command; redo: Command }): Entry[] { const { paragraph, heading, code_block, blockquote, bullet_list, ordered_list } = schema.nodes; const { strong, em, code, link } = schema.marks; const headingItem = (level: 1 | 2 | 3): Item => ({ id: `h${level}`, label: t(`heading${level}` as "heading1"), keys: [MOD, ALT, String(level)], icon: level === 1 ? ICONS.h1 : level === 2 ? ICONS.h2 : ICONS.h3, run: editing.heading(level), isActive: (s) => isBlockActive(s, heading, { level }), }); const inList = (type: typeof bullet_list) => (s: EditorState) => findAncestor(s, (n) => n.type === type) !== null; return [ { id: "undo", label: t("undo"), keys: [MOD, "Z"], icon: ICONS.undo, run: history.undo }, { id: "redo", label: t("redo"), keys: [MOD, SHIFT, "Z"], icon: ICONS.redo, run: history.redo }, "|", { id: "paragraph", label: t("paragraph"), keys: [MOD, ALT, "0"], icon: ICONS.paragraph, run: editing.paragraph, isActive: (s) => isBlockActive(s, paragraph), }, headingItem(1), headingItem(2), headingItem(3), "|", { id: "bold", label: t("bold"), keys: [MOD, "B"], icon: ICONS.bold, run: editing.bold, isActive: (s) => isMarkActive(s, strong), }, { id: "italic", label: t("italic"), keys: [MOD, "I"], icon: ICONS.italic, run: editing.italic, isActive: (s) => isMarkActive(s, em), }, { id: "code", label: t("inlineCode"), keys: [MOD, "E"], icon: ICONS.code, run: editing.inlineCode, isActive: (s) => isMarkActive(s, code), }, { id: "link", label: t("link"), keys: [MOD, "K"], icon: ICONS.link, run: editing.link, isActive: (s) => isMarkActive(s, link), }, "|", { id: "bullets", label: t("bulletList"), keys: [MOD, SHIFT, "8"], icon: ICONS.bullets, run: editing.bulletList, isActive: inList(bullet_list), }, { id: "ordered", label: t("numberedList"), keys: [MOD, SHIFT, "9"], icon: ICONS.ordered, run: editing.orderedList, isActive: inList(ordered_list), }, { id: "quote", label: t("quote"), keys: [">"], icon: ICONS.quote, run: editing.blockquote, isActive: (s) => findAncestor(s, (n) => n.type === blockquote) !== null, }, { id: "codeblock", label: t("codeBlock"), keys: ["```"], icon: ICONS.codeBlock, run: editing.codeBlock, isActive: (s) => isBlockActive(s, code_block), }, { id: "rule", label: t("divider"), keys: ["---"], icon: ICONS.rule, run: editing.horizontalRule, }, ]; } // data-group names, in order: one per run of buttons between the separators of buildItems. const GROUP_NAMES = ["history", "headings", "marks", "blocks"] as const; /** * Creates the toolbar DOM (into #editor-toolbar, or just before `mount`) and returns * the ProseMirror plugin that drives it. Buttons stay disabled until the view exists. */ export function createToolbar( mount: HTMLElement, schema: Schema, editing: Editing, history: { undo: Command; redo: Command }, ): Plugin { let el = document.getElementById("editor-toolbar"); if (!el) { el = document.createElement("div"); el.id = "editor-toolbar"; mount.before(el); } el.classList.add("editor-toolbar"); el.setAttribute("role", "toolbar"); el.setAttribute("aria-label", t("toolbarLabel")); let current: EditorView | null = null; const buttons: { btn: HTMLButtonElement; item: Item; enabled: boolean }[] = []; // Each run of buttons between separators is a group; CSS hides low-priority groups // and draws the dividers between them (see editor.css, container queries). let group = h("div", { class: "editor-toolbar-group", "data-group": GROUP_NAMES[0] }); el.append(group); let groupIndex = 0; for (const entry of buildItems(schema, editing, history)) { if (entry === "|") { groupIndex += 1; group = h("div", { class: "editor-toolbar-group", "data-group": GROUP_NAMES[groupIndex] }); el.append(group); continue; } const btn = h( "button", { type: "button", class: "editor-toolbar-btn", "aria-label": entry.label, "aria-keyshortcuts": ariaKeys(entry.keys), "data-command": entry.id, tabindex: "-1", }, h("span", { class: "editor-toolbar-icon", "aria-hidden": "true" }), tooltip(entry.label, entry.keys, t("unavailable")), ); // Static icon paths only (see ICONS above). btn.querySelector(".editor-toolbar-icon")!.innerHTML = ``; btn.disabled = true; btn.addEventListener("click", () => { if (!current) return; entry.run(current.state, current.dispatch, current); current.focus(); }); group.append(btn); buttons.push({ btn, item: entry, enabled: false }); } // Keep the editor's selection and focus when pressing toolbar buttons. el.addEventListener("mousedown", (e) => e.preventDefault()); // Roving tabindex: arrows, Home and End move among enabled buttons. const enabledButtons = () => buttons.map((b) => b.btn).filter((b) => !b.disabled); const setStop = (target: HTMLButtonElement) => { for (const { btn } of buttons) btn.tabIndex = btn === target ? 0 : -1; }; el.addEventListener("keydown", (e) => { const list = enabledButtons(); if (!list.length) return; const index = list.indexOf(document.activeElement as HTMLButtonElement); let next: HTMLButtonElement | undefined; if (e.key === "ArrowRight") next = list[(index + 1 + list.length) % list.length]; else if (e.key === "ArrowLeft") next = list[(index - 1 + list.length) % list.length]; else if (e.key === "Home") next = list[0]; else if (e.key === "End") next = list[list.length - 1]; if (!next) return; e.preventDefault(); setStop(next); next.focus(); }); const sync = (state: EditorState) => { for (const entry of buttons) { // Commands without a dispatch only report whether they would apply. entry.enabled = !!entry.item.run(state); entry.btn.disabled = !entry.enabled; if (entry.item.isActive) { const active = entry.item.isActive(state); entry.btn.dataset.active = String(active); entry.btn.setAttribute("aria-pressed", String(active)); } } // Keep one enabled button in the tab order, preferring the last one focused. const stop = buttons.find((b) => b.btn.tabIndex === 0 && !b.btn.disabled)?.btn ?? enabledButtons()[0]; if (stop) setStop(stop); }; return new Plugin({ view(view) { current = view; sync(view.state); return { update(v) { sync(v.state); }, destroy() { current = null; }, }; }, }); } /** Keyboard shortcut in aria-keyshortcuts form, e.g. "Control+B" or "Meta+Shift+8". */ function ariaKeys(keys: string[]): string { const names: Record = { Ctrl: "Control", "⌘": "Meta", Shift: "Shift", "⇧": "Shift", Alt: "Alt", "⌥": "Alt", }; return keys.map((k) => names[k] ?? k).join("+"); }