Files
Docwell/Packages/editor/src/extensions.ts
T
tajniak81andClaude Opus 5.5 84043ba05b Add date mentions with reminders
- "@today", "@friday", "@2026-12-01" insert a date; "@remind friday"
  or the date's popover adds a reminder for the person who set it.
- docwell-server keeps a reminders row per person, date and time zone
  in step with each page and sends a "reminder" notification at 9:00
  on the day in that time zone (job notify.reminders). Dates already
  past when added do not notify.
- Exports write dates out; the web and phone inboxes show reminders.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-02 14:55:46 +01:00

149 lines
6.0 KiB
TypeScript

// SPDX-License-Identifier: AGPL-3.0-or-later
import { Extension, type AnyExtension, type Editor } from "@tiptap/core";
import Collaboration from "@tiptap/extension-collaboration";
import CollaborationCaret from "@tiptap/extension-collaboration-caret";
import { Placeholder } from "@tiptap/extensions";
import StarterKit from "@tiptap/starter-kit";
import { BlockLinks } from "./blocklinks";
import { CommentHighlights } from "./comments";
import { DateMention } from "./dates";
import { DrawioBlock, ExcalidrawBlock, type ExcalidrawLoader } from "./diagrams";
import { Embed, LinkCard, type LinkHost } from "./embeds";
import { DocwellImage, FileAttachment, FileDrop, type FileHost } from "./files";
import { FindReplace } from "./find";
import { englishLabels, type EditorLabels } from "./labels";
import { docwellMath } from "./math";
import { docwellMention, type MentionHost } from "./mention";
import { MermaidBlock, type MermaidLoader } from "./mermaid";
import { PageLinks, type PageHost } from "./pagelinks";
import { SlashMenu, slashCommands } from "./slash";
import type { CollabProvider } from "./provider";
import { TableOfContents } from "./toc";
import { docwellToggles } from "./toggle";
export interface CollabUser {
name: string;
color: string;
}
export interface ExtensionOptions {
placeholder?: string;
/** Edits the provider's shared document and shows the other editors' carets. */
collab?: { provider: CollabProvider; user: CollabUser };
/** Loads and uploads attachments; without it images use their src as is. */
files?: FileHost;
/** Finds people for "@" mentions. */
mentions?: MentionHost;
/** Finds pages for "[[" links. */
pages?: PageHost;
/** Fetches previews for link cards; without it cards show the address only. */
links?: LinkHost;
/**
* The full address of a link to a block of this page, from its "#b-<id>"
* fragment. Without it the margin has no "copy link" button.
*/
blockLink?: (hash: string) => string;
/** Called after a block link was copied. */
onBlockLinkCopied?: (url: string) => void;
/** Opens an app route, e.g. "/p/<id>" when a page link is followed. */
onNavigate?: (path: string) => void;
/** The Draw.io embed address (default https://embed.diagrams.net). */
drawioUrl?: string;
/** Loads Mermaid; without it diagrams show as their text. */
loadMermaid?: MermaidLoader;
/** Loads the Excalidraw editor; without it drawings show but cannot be edited. */
loadExcalidraw?: ExcalidrawLoader;
labels?: EditorLabels;
/** Clicking highlighted comment text calls this with the comment id. */
onCommentSelect?: (id: string) => void;
onError?: (e: unknown) => void;
}
/**
* The Docwell document schema. Every client (web, desktop, mobile WebView)
* must build its editor from this list so documents stay compatible.
*/
export function docwellExtensions(opts: ExtensionOptions = {}): AnyExtension[] {
const labels = opts.labels ?? englishLabels;
// Node views and click handlers that need the editor find it here.
let editor: Editor | null = null;
const editorRef = Extension.create({
name: "editorRef",
onCreate() {
editor = this.editor;
},
});
const diagrams = {
host: opts.files,
labels,
drawioUrl: opts.drawioUrl,
loadExcalidraw: opts.loadExcalidraw,
onError: opts.onError,
};
const list: AnyExtension[] = [
editorRef,
StarterKit.configure({
heading: { levels: [1, 2, 3] },
link: { openOnClick: false, autolink: true },
// Collaboration brings its own undo history, scoped to the local user.
undoRedo: opts.collab ? false : undefined,
}),
Placeholder.configure({ placeholder: opts.placeholder ?? "Type '/' for commands…" }),
DocwellImage.configure({ host: opts.files }),
FileAttachment.configure({ host: opts.files }),
FileDrop.configure({ host: opts.files, onError: opts.onError }),
docwellMention(labels, opts.mentions),
DateMention.configure({ labels, me: () => opts.mentions?.me }),
SlashMenu.configure({
commands: slashCommands(labels, opts.files, opts.onError, opts.pages, !!opts.loadExcalidraw, opts.links),
labels,
}),
CommentHighlights.configure({ onSelect: opts.onCommentSelect }),
docwellMath(labels, () => editor),
MermaidBlock.configure({ labels, load: opts.loadMermaid }),
TableOfContents.configure({ labels }),
DrawioBlock.configure(diagrams),
ExcalidrawBlock.configure(diagrams),
PageLinks.configure({ host: opts.pages, labels, onNavigate: opts.onNavigate }),
...docwellToggles(labels),
Embed.configure({ labels }),
LinkCard.configure({ labels }),
BlockLinks.configure({ labels, link: opts.blockLink, onCopied: opts.onBlockLinkCopied }),
FindReplace,
];
if (opts.collab) {
list.push(
Collaboration.configure({ document: opts.collab.provider.doc }),
CollaborationCaret.configure({ provider: opts.collab.provider, user: opts.collab.user, render: renderCaret }),
);
}
return list;
}
/**
* Another editor's caret with their name. The name is hidden from assistive
* tech, which would otherwise read it as part of the text.
*/
function renderCaret(user: Record<string, unknown>): HTMLElement {
const color = typeof user.color === "string" && /^#[0-9a-f]{3,8}$/i.test(user.color) ? user.color : "#888";
const caret = document.createElement("span");
caret.classList.add("collaboration-carets__caret");
caret.style.borderColor = color;
const label = document.createElement("div");
label.classList.add("collaboration-carets__label");
label.setAttribute("aria-hidden", "true");
label.style.backgroundColor = color;
label.textContent = typeof user.name === "string" ? user.name : "";
caret.append(label);
return caret;
}
/** A stable caret colour for a user id, from the design system's accent hues. */
export function userColor(id: string): string {
const hues = ["#2f7d6d", "#7c5cc4", "#c2571a", "#2f6fbf", "#b8327a", "#5f8a1f", "#a0761c", "#1f8a9a"];
let h = 0;
for (const ch of id) h = (h * 31 + ch.charCodeAt(0)) >>> 0;
return hues[h % hues.length]!;
}