project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching0 forksJavaScript3.6 MiBe100df4Tahinli1 month ago1 commit
| 1 | // Formatting toolbar: plain DOM, rendered once. A ProseMirror plugin view keeps the |
| 2 | // active/disabled state of every button in sync with the editor. Icons only, with |
| 3 | // keycap tooltips; roving tabindex so the bar is one Tab stop. |
| 4 | import type { Schema } from "prosemirror-model"; |
| 5 | import { Plugin, type Command, type EditorState } from "prosemirror-state"; |
| 6 | import type { EditorView } from "prosemirror-view"; |
| 7 | import { findAncestor, isBlockActive, isMarkActive, type Editing } from "./editing"; |
| 8 | import { t } from "./i18n"; |
| 9 | import { ALT, MOD, SHIFT, h, tooltip } from "./dom"; |
| 10 | |
| 11 | |
| 12 | // Lucide-style glyphs (24x24, stroke based). Static strings, safe for innerHTML. |
| 13 | const ICONS = { |
| 14 | undo: '<path d="M9 14 4 9l5-5"/><path d="M4 9h10.5a5.5 5.5 0 0 1 0 11H11"/>', |
| 15 | redo: '<path d="m15 14 5-5-5-5"/><path d="M20 9H9.5a5.5 5.5 0 0 0 0 11H13"/>', |
| 16 | paragraph: '<path d="M13 4v16"/><path d="M17 4v16"/><path d="M19 4H9.5a4.5 4.5 0 0 0 0 9H13"/>', |
| 17 | h1: '<path d="M4 12h8"/><path d="M4 18V6"/><path d="M12 18V6"/><path d="m17 12 3-2v8"/>', |
| 18 | h2: '<path d="M4 12h8"/><path d="M4 18V6"/><path d="M12 18V6"/><path d="M21 18h-4c0-4 4-3 4-6 0-1.5-2-2.5-4-1"/>', |
| 19 | h3: '<path d="M4 12h8"/><path d="M4 18V6"/><path d="M12 18V6"/><path d="M17.5 10.5c1.7-1 3.5 0 3.5 1.5a2 2 0 0 1-2 2"/><path d="M17 17.5c2 1.5 4 .3 4-1.5a2 2 0 0 0-2-2"/>', |
| 20 | bold: '<path d="M6 12h9a4 4 0 0 1 0 8H7a1 1 0 0 1-1-1V5a1 1 0 0 1 1-1h7a4 4 0 0 1 0 8"/>', |
| 21 | italic: '<line x1="19" x2="10" y1="4" y2="4"/><line x1="14" x2="5" y1="20" y2="20"/><line x1="15" x2="9" y1="4" y2="20"/>', |
| 22 | code: '<polyline points="16 18 22 12 16 6"/><polyline points="8 6 2 12 8 18"/>', |
| 23 | link: '<path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/>', |
| 24 | bullets: |
| 25 | '<line x1="8" x2="21" y1="6" y2="6"/><line x1="8" x2="21" y1="12" y2="12"/><line x1="8" x2="21" y1="18" y2="18"/><line x1="3" x2="3.01" y1="6" y2="6"/><line x1="3" x2="3.01" y1="12" y2="12"/><line x1="3" x2="3.01" y1="18" y2="18"/>', |
| 26 | ordered: |
| 27 | '<line x1="10" x2="21" y1="6" y2="6"/><line x1="10" x2="21" y1="12" y2="12"/><line x1="10" x2="21" y1="18" y2="18"/><path d="M4 6h1v4"/><path d="M4 10h2"/><path d="M6 18H4c0-1 2-2 2-3s-1-1.5-2-1"/>', |
| 28 | quote: '<path d="M17 6H3"/><path d="M21 12H8"/><path d="M21 18H8"/><path d="M3 12v6"/>', |
| 29 | codeBlock: '<rect x="3" y="3" width="18" height="18" rx="2"/><path d="m10 9-3 3 3 3"/><path d="m14 15 3-3-3-3"/>', |
| 30 | rule: '<line x1="5" x2="19" y1="12" y2="12"/>', |
| 31 | } as const; |
| 32 | |
| 33 | interface Item { |
| 34 | id: string; |
| 35 | label: string; |
| 36 | keys: string[]; |
| 37 | icon: string; |
| 38 | run: Command; |
| 39 | /** Present for toggles: drives aria-pressed and the active look. */ |
| 40 | isActive?: (state: EditorState) => boolean; |
| 41 | } |
| 42 | |
| 43 | type Entry = Item | "|"; |
| 44 | |
| 45 | function buildItems(schema: Schema, editing: Editing, history: { undo: Command; redo: Command }): Entry[] { |
| 46 | const { paragraph, heading, code_block, blockquote, bullet_list, ordered_list } = schema.nodes; |
| 47 | const { strong, em, code, link } = schema.marks; |
| 48 | const headingItem = (level: 1 | 2 | 3): Item => ({ |
| 49 | id: `h${level}`, |
| 50 | label: t(`heading${level}` as "heading1"), |
| 51 | keys: [MOD, ALT, String(level)], |
| 52 | icon: level === 1 ? ICONS.h1 : level === 2 ? ICONS.h2 : ICONS.h3, |
| 53 | run: editing.heading(level), |
| 54 | isActive: (s) => isBlockActive(s, heading, { level }), |
| 55 | }); |
| 56 | const inList = (type: typeof bullet_list) => (s: EditorState) => findAncestor(s, (n) => n.type === type) !== null; |
| 57 | |
| 58 | return [ |
| 59 | { id: "undo", label: t("undo"), keys: [MOD, "Z"], icon: ICONS.undo, run: history.undo }, |
| 60 | { id: "redo", label: t("redo"), keys: [MOD, SHIFT, "Z"], icon: ICONS.redo, run: history.redo }, |
| 61 | "|", |
| 62 | { |
| 63 | id: "paragraph", |
| 64 | label: t("paragraph"), |
| 65 | keys: [MOD, ALT, "0"], |
| 66 | icon: ICONS.paragraph, |
| 67 | run: editing.paragraph, |
| 68 | isActive: (s) => isBlockActive(s, paragraph), |
| 69 | }, |
| 70 | headingItem(1), |
| 71 | headingItem(2), |
| 72 | headingItem(3), |
| 73 | "|", |
| 74 | { |
| 75 | id: "bold", |
| 76 | label: t("bold"), |
| 77 | keys: [MOD, "B"], |
| 78 | icon: ICONS.bold, |
| 79 | run: editing.bold, |
| 80 | isActive: (s) => isMarkActive(s, strong), |
| 81 | }, |
| 82 | { |
| 83 | id: "italic", |
| 84 | label: t("italic"), |
| 85 | keys: [MOD, "I"], |
| 86 | icon: ICONS.italic, |
| 87 | run: editing.italic, |
| 88 | isActive: (s) => isMarkActive(s, em), |
| 89 | }, |
| 90 | { |
| 91 | id: "code", |
| 92 | label: t("inlineCode"), |
| 93 | keys: [MOD, "E"], |
| 94 | icon: ICONS.code, |
| 95 | run: editing.inlineCode, |
| 96 | isActive: (s) => isMarkActive(s, code), |
| 97 | }, |
| 98 | { |
| 99 | id: "link", |
| 100 | label: t("link"), |
| 101 | keys: [MOD, "K"], |
| 102 | icon: ICONS.link, |
| 103 | run: editing.link, |
| 104 | isActive: (s) => isMarkActive(s, link), |
| 105 | }, |
| 106 | "|", |
| 107 | { |
| 108 | id: "bullets", |
| 109 | label: t("bulletList"), |
| 110 | keys: [MOD, SHIFT, "8"], |
| 111 | icon: ICONS.bullets, |
| 112 | run: editing.bulletList, |
| 113 | isActive: inList(bullet_list), |
| 114 | }, |
| 115 | { |
| 116 | id: "ordered", |
| 117 | label: t("numberedList"), |
| 118 | keys: [MOD, SHIFT, "9"], |
| 119 | icon: ICONS.ordered, |
| 120 | run: editing.orderedList, |
| 121 | isActive: inList(ordered_list), |
| 122 | }, |
| 123 | { |
| 124 | id: "quote", |
| 125 | label: t("quote"), |
| 126 | keys: [">"], |
| 127 | icon: ICONS.quote, |
| 128 | run: editing.blockquote, |
| 129 | isActive: (s) => findAncestor(s, (n) => n.type === blockquote) !== null, |
| 130 | }, |
| 131 | { |
| 132 | id: "codeblock", |
| 133 | label: t("codeBlock"), |
| 134 | keys: ["```"], |
| 135 | icon: ICONS.codeBlock, |
| 136 | run: editing.codeBlock, |
| 137 | isActive: (s) => isBlockActive(s, code_block), |
| 138 | }, |
| 139 | { |
| 140 | id: "rule", |
| 141 | label: t("divider"), |
| 142 | keys: ["---"], |
| 143 | icon: ICONS.rule, |
| 144 | run: editing.horizontalRule, |
| 145 | }, |
| 146 | ]; |
| 147 | } |
| 148 | |
| 149 | // data-group names, in order: one per run of buttons between the separators of buildItems. |
| 150 | const GROUP_NAMES = ["history", "headings", "marks", "blocks"] as const; |
| 151 | |
| 152 | /** |
| 153 | * Creates the toolbar DOM (into #editor-toolbar, or just before `mount`) and returns |
| 154 | * the ProseMirror plugin that drives it. Buttons stay disabled until the view exists. |
| 155 | */ |
| 156 | export function createToolbar( |
| 157 | mount: HTMLElement, |
| 158 | schema: Schema, |
| 159 | editing: Editing, |
| 160 | history: { undo: Command; redo: Command }, |
| 161 | ): Plugin { |
| 162 | let el = document.getElementById("editor-toolbar"); |
| 163 | if (!el) { |
| 164 | el = document.createElement("div"); |
| 165 | el.id = "editor-toolbar"; |
| 166 | mount.before(el); |
| 167 | } |
| 168 | el.classList.add("editor-toolbar"); |
| 169 | el.setAttribute("role", "toolbar"); |
| 170 | el.setAttribute("aria-label", t("toolbarLabel")); |
| 171 | |
| 172 | let current: EditorView | null = null; |
| 173 | const buttons: { btn: HTMLButtonElement; item: Item; enabled: boolean }[] = []; |
| 174 | |
| 175 | // Each run of buttons between separators is a group; CSS hides low-priority groups |
| 176 | // and draws the dividers between them (see editor.css, container queries). |
| 177 | let group = h("div", { class: "editor-toolbar-group", "data-group": GROUP_NAMES[0] }); |
| 178 | el.append(group); |
| 179 | let groupIndex = 0; |
| 180 | |
| 181 | for (const entry of buildItems(schema, editing, history)) { |
| 182 | if (entry === "|") { |
| 183 | groupIndex += 1; |
| 184 | group = h("div", { class: "editor-toolbar-group", "data-group": GROUP_NAMES[groupIndex] }); |
| 185 | el.append(group); |
| 186 | continue; |
| 187 | } |
| 188 | const btn = h( |
| 189 | "button", |
| 190 | { |
| 191 | type: "button", |
| 192 | class: "editor-toolbar-btn", |
| 193 | "aria-label": entry.label, |
| 194 | "aria-keyshortcuts": ariaKeys(entry.keys), |
| 195 | "data-command": entry.id, |
| 196 | tabindex: "-1", |
| 197 | }, |
| 198 | h("span", { class: "editor-toolbar-icon", "aria-hidden": "true" }), |
| 199 | tooltip(entry.label, entry.keys, t("unavailable")), |
| 200 | ); |
| 201 | // Static icon paths only (see ICONS above). |
| 202 | btn.querySelector(".editor-toolbar-icon")!.innerHTML = `<svg viewBox="0 0 24 24" width="16" height="16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${entry.icon}</svg>`; |
| 203 | btn.disabled = true; |
| 204 | btn.addEventListener("click", () => { |
| 205 | if (!current) return; |
| 206 | entry.run(current.state, current.dispatch, current); |
| 207 | current.focus(); |
| 208 | }); |
| 209 | group.append(btn); |
| 210 | buttons.push({ btn, item: entry, enabled: false }); |
| 211 | } |
| 212 | |
| 213 | // Keep the editor's selection and focus when pressing toolbar buttons. |
| 214 | el.addEventListener("mousedown", (e) => e.preventDefault()); |
| 215 | |
| 216 | // Roving tabindex: arrows, Home and End move among enabled buttons. |
| 217 | const enabledButtons = () => buttons.map((b) => b.btn).filter((b) => !b.disabled); |
| 218 | const setStop = (target: HTMLButtonElement) => { |
| 219 | for (const { btn } of buttons) btn.tabIndex = btn === target ? 0 : -1; |
| 220 | }; |
| 221 | el.addEventListener("keydown", (e) => { |
| 222 | const list = enabledButtons(); |
| 223 | if (!list.length) return; |
| 224 | const index = list.indexOf(document.activeElement as HTMLButtonElement); |
| 225 | let next: HTMLButtonElement | undefined; |
| 226 | if (e.key === "ArrowRight") next = list[(index + 1 + list.length) % list.length]; |
| 227 | else if (e.key === "ArrowLeft") next = list[(index - 1 + list.length) % list.length]; |
| 228 | else if (e.key === "Home") next = list[0]; |
| 229 | else if (e.key === "End") next = list[list.length - 1]; |
| 230 | if (!next) return; |
| 231 | e.preventDefault(); |
| 232 | setStop(next); |
| 233 | next.focus(); |
| 234 | }); |
| 235 | |
| 236 | const sync = (state: EditorState) => { |
| 237 | for (const entry of buttons) { |
| 238 | // Commands without a dispatch only report whether they would apply. |
| 239 | entry.enabled = !!entry.item.run(state); |
| 240 | entry.btn.disabled = !entry.enabled; |
| 241 | if (entry.item.isActive) { |
| 242 | const active = entry.item.isActive(state); |
| 243 | entry.btn.dataset.active = String(active); |
| 244 | entry.btn.setAttribute("aria-pressed", String(active)); |
| 245 | } |
| 246 | } |
| 247 | // Keep one enabled button in the tab order, preferring the last one focused. |
| 248 | const stop = buttons.find((b) => b.btn.tabIndex === 0 && !b.btn.disabled)?.btn ?? enabledButtons()[0]; |
| 249 | if (stop) setStop(stop); |
| 250 | }; |
| 251 | |
| 252 | return new Plugin({ |
| 253 | view(view) { |
| 254 | current = view; |
| 255 | sync(view.state); |
| 256 | return { |
| 257 | update(v) { |
| 258 | sync(v.state); |
| 259 | }, |
| 260 | destroy() { |
| 261 | current = null; |
| 262 | }, |
| 263 | }; |
| 264 | }, |
| 265 | }); |
| 266 | } |
| 267 | |
| 268 | /** Keyboard shortcut in aria-keyshortcuts form, e.g. "Control+B" or "Meta+Shift+8". */ |
| 269 | function ariaKeys(keys: string[]): string { |
| 270 | const names: Record<string, string> = { |
| 271 | Ctrl: "Control", |
| 272 | "⌘": "Meta", |
| 273 | Shift: "Shift", |
| 274 | "⇧": "Shift", |
| 275 | Alt: "Alt", |
| 276 | "⌥": "Alt", |
| 277 | }; |
| 278 | return keys.map((k) => names[k] ?? k).join("+"); |
| 279 | } |