Something went wrong. Try again.
A monorepo for writing specs and other serious text on the AT Protocol.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364// The formatting bubble over a selection: plain DOM, shared by the body and// the single-line editors, styled by draft.css.import type { Editor } from '@tiptap/core';
export type InlineFeature = 'bold' | 'italic' | 'code' | 'sub' | 'sup' | 'link' | 'math' | 'dfn' | 'dfnRef' | 'ref' | 'comment';
export const SIMPLE_FEATURES: readonly InlineFeature[] = ['bold', 'italic', 'code', 'sub', 'sup'];export const FULL_FEATURES: readonly InlineFeature[] = [...SIMPLE_FEATURES, 'link', 'math', 'dfn', 'dfnRef', 'ref'];
type Button = { kind: InlineFeature; label: string; title: string; run: (editor: Editor) => void; active?: (editor: Editor) => boolean };
export type BubbleHooks = { onLink: (editor: Editor) => void; onRef: (editor: Editor) => void; onComment?: (editor: Editor) => void;};
export function bubbleButtons(hooks: BubbleHooks): Button[] { return [ { kind: 'bold', label: 'B', title: 'Bold (⌘B, **…**)', run: (e) => e.chain().focus().toggleBold().run(), active: (e) => e.isActive('bold') }, { kind: 'italic', label: 'I', title: 'Italic (⌘I, _…_)', run: (e) => e.chain().focus().toggleItalic().run(), active: (e) => e.isActive('italic') }, { kind: 'code', label: '</>', title: 'Code (⌘E, `…`)', run: (e) => e.chain().focus().toggleCode().run(), active: (e) => e.isActive('code') }, { kind: 'sub', label: 'x₂', title: 'Subscript (⌘,)', run: (e) => e.chain().focus().toggleSubscript().run(), active: (e) => e.isActive('subscript') }, { kind: 'sup', label: 'x²', title: 'Superscript (⌘.)', run: (e) => e.chain().focus().toggleSuperscript().run(), active: (e) => e.isActive('superscript') }, { kind: 'link', label: '🔗', title: 'Link (⌘K)', run: (e) => hooks.onLink(e), active: (e) => e.isActive('link') }, { kind: 'math', label: '∑', title: 'Inline math (⌘M, $…$)', run: (e) => { const { from, to } = e.state.selection; const latex = e.state.doc.textBetween(from, to, ' '); if (latex) e.chain().focus().deleteRange({ from, to }).insertInlineMath({ latex }).run(); }, }, { kind: 'dfn', label: 'dfn', title: 'Define this term (⌘D)', run: (e) => e.chain().focus().toggleMark('dfn').run(), active: (e) => e.isActive('dfn') }, { kind: 'dfnRef', label: '→dfn', title: 'Reference a defined term (⌘⇧D, @)', run: (e) => e.chain().focus().toggleMark('dfnRef').run(), active: (e) => e.isActive('dfnRef') }, { kind: 'ref', label: '[ref]', title: 'Bibliographic reference (⌘⇧R, [key])', run: (e) => hooks.onRef(e), active: (e) => e.isActive('ref') }, { kind: 'comment', label: '💬', title: 'Comment on the selection (⌘⇧M)', run: (e) => hooks.onComment?.(e), active: (e) => e.isActive('comment') }, ];}
/** Build the bubble's buttons into `element`; `update()` refreshes active states. */export function fillBubble(element: HTMLElement, editor: Editor, kinds: readonly InlineFeature[], hooks: BubbleHooks): { update: () => void } { element.className = 'bubble'; element.addEventListener('mousedown', (e) => e.preventDefault()); const buttons = bubbleButtons(hooks).filter((b) => kinds.includes(b.kind)); const els = buttons.map((b) => { const btn = document.createElement('button'); btn.type = 'button'; btn.className = 'btn btn-quiet btn-bare btn-small'; btn.title = b.title; btn.textContent = b.label; btn.addEventListener('click', () => b.run(editor)); element.append(btn); return { b, btn }; }); return { update: () => { for (const { b, btn } of els) btn.classList.toggle('active', !!b.active?.(editor)); }, };}