// SPDX-License-Identifier: AGPL-3.0-or-later import { Node, type Editor } from "@tiptap/core"; import type { Node as PMNode } from "@tiptap/pm/model"; import type { EditorLabels } from "./labels"; declare module "@tiptap/core" { interface Commands { tableOfContents: { insertTableOfContents: () => ReturnType; }; } } export interface Heading { level: number; text: string; pos: number; } /** The document's headings, in order. */ export function headingsOf(doc: PMNode): Heading[] { const out: Heading[] = []; doc.descendants((node, pos) => { if (node.type.name === "heading") { out.push({ level: Number(node.attrs.level) || 1, text: node.textContent.trim(), pos }); return false; } return true; }); return out; } /** Scrolls heading at pos into view and puts the caret at its start. */ function goTo(editor: Editor, pos: number) { const dom = editor.view.nodeDOM(pos); if (dom instanceof HTMLElement) dom.scrollIntoView({ behavior: "smooth", block: "start" }); if (editor.isEditable) editor.commands.setTextSelection(pos + 1); } /** * A table of contents block. It stores nothing: each client lists the * document's headings and keeps the list current while people edit. */ export const TableOfContents = Node.create<{ labels: EditorLabels | null }>({ name: "tableOfContents", group: "block", atom: true, draggable: true, addOptions() { return { labels: null }; }, parseHTML() { return [{ tag: "nav[data-type='toc']" }]; }, renderHTML() { return ["nav", { "data-type": "toc" }]; }, addCommands() { return { insertTableOfContents: () => ({ commands }) => commands.insertContent({ type: this.name }), }; }, addNodeView() { return ({ editor }) => { const labels = this.options.labels!.blocks; const dom = document.createElement("nav"); dom.className = "dw-toc"; dom.setAttribute("data-type", "toc"); dom.setAttribute("aria-label", labels.toc); dom.contentEditable = "false"; const title = document.createElement("p"); title.className = "dw-toc__title"; title.textContent = labels.toc; const list = document.createElement("ol"); list.className = "dw-toc__list"; dom.append(title, list); let last = ""; const render = () => { const headings = headingsOf(editor.state.doc); const key = JSON.stringify(headings.map((h) => [h.level, h.text])); if (key === last) { // Same headings, maybe moved: refresh positions only. list.querySelectorAll("a").forEach((a, i) => (a.dataset.pos = String(headings[i]?.pos ?? 0))); return; } last = key; list.replaceChildren(); if (!headings.length) { const empty = document.createElement("li"); empty.className = "dw-toc__empty"; empty.textContent = labels.tocEmpty; list.append(empty); return; } const top = Math.min(...headings.map((h) => h.level)); for (const h of headings) { const li = document.createElement("li"); li.style.paddingLeft = `${(h.level - top) * 1}rem`; const a = document.createElement("a"); a.href = "#"; a.textContent = h.text || "…"; a.dataset.pos = String(h.pos); a.addEventListener("click", (e) => { e.preventDefault(); goTo(editor, Number(a.dataset.pos)); }); li.append(a); list.append(li); } }; render(); editor.on("update", render); return { dom, update: (n) => n.type.name === "tableOfContents", ignoreMutation: () => true, // Links handle their own clicks; elsewhere the block selects and drags. stopEvent: (e) => e.target instanceof Element && !!e.target.closest("a"), destroy: () => editor.off("update", render), }; }; }, });