Something went wrong. Try again.
A monorepo for writing specs and other serious text on the AT Protocol.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159// The document's shape: sections (a title, blocks, subsections) holding// paragraphs, lists, code, math, definition lists and figures. Every block// and section carries a stable id.import { Node, mergeAttributes } from '@tiptap/core';import { BLOCK_MARKS, SIMPLE_MARKS } from './marks.ts';import { FigureView, SectionTitleView, SectionView, type ViewHooks } from './views.ts';
export const Doc = Node.create({ name: 'doc', topNode: true, content: 'section*', marks: BLOCK_MARKS });
/** A single paragraph, for title and abstract fields. */export const LineDoc = Node.create({ name: 'doc', topNode: true, content: 'paragraph' });
export const Section = Node.create<{ hooks: ViewHooks }>({ name: 'section', content: 'sectionTitle block+ section*', marks: BLOCK_MARKS, defining: true, isolating: true, draggable: true, selectable: true, addOptions() { return { hooks: {} as ViewHooks }; }, addAttributes() { return { /** The anchor (a slug of the title until edited by hand). */ anchor: { default: '', parseHTML: (el) => el.getAttribute('id') ?? '', renderHTML: (a) => (a.anchor ? { id: a.anchor } : {}) }, anchorTouched: { default: false, parseHTML: () => false, renderHTML: () => ({}) }, /** How many words the section is meant to run to, when the writer (or the assistant) set a target. */ target: { default: null, parseHTML: (el) => { const n = Number(el.getAttribute('data-target')); return n > 0 ? Math.round(n) : null; }, renderHTML: (a) => (a.target ? { 'data-target': String(a.target) } : {}), }, }; }, parseHTML() { return [{ tag: 'section' }]; }, renderHTML({ HTMLAttributes }) { return ['section', mergeAttributes(HTMLAttributes, { class: 'sec' }), 0]; }, addNodeView() { return (props) => new SectionView(props, this.options.hooks); },});
export const SectionTitle = Node.create<{ hooks: ViewHooks }>({ name: 'sectionTitle', content: 'inline*', marks: SIMPLE_MARKS, defining: true, addOptions() { return { hooks: {} as ViewHooks }; }, parseHTML() { return [{ tag: 'h2' }, { tag: 'h3' }, { tag: 'h4' }, { tag: 'h5' }, { tag: 'h6' }]; }, renderHTML({ HTMLAttributes }) { return ['h2', mergeAttributes(HTMLAttributes), 0]; }, addNodeView() { return (props) => new SectionTitleView(props, this.options.hooks); }, addKeyboardShortcuts() { return { // Enter in a title goes to the first block Enter: ({ editor }) => { const { $from } = editor.state.selection; if ($from.parent.type.name !== this.name) return false; return editor.commands.setTextSelection($from.after() + 1); }, }; },});
export const DefinitionList = Node.create({ name: 'definitionList', group: 'block', content: 'definitionItem+', marks: BLOCK_MARKS, parseHTML() { return [{ tag: 'dl' }]; }, renderHTML({ HTMLAttributes }) { return ['dl', mergeAttributes(HTMLAttributes), 0]; },});
export const DefinitionItem = Node.create({ name: 'definitionItem', content: 'definitionTerm definitionDescription', marks: BLOCK_MARKS, defining: true, parseHTML() { return [{ tag: 'dl > div' }]; }, renderHTML({ HTMLAttributes }) { return ['div', mergeAttributes(HTMLAttributes, { class: 'ditem' }), 0]; },});
export const DefinitionTerm = Node.create({ name: 'definitionTerm', content: 'inline*', marks: SIMPLE_MARKS, parseHTML() { return [{ tag: 'dt' }]; }, renderHTML({ HTMLAttributes }) { return ['dt', mergeAttributes(HTMLAttributes), 0]; },});
export const DefinitionDescription = Node.create({ name: 'definitionDescription', content: 'inline*', parseHTML() { return [{ tag: 'dd' }]; }, renderHTML({ HTMLAttributes }) { return ['dd', mergeAttributes(HTMLAttributes), 0]; },});
/** An image with a caption; the file itself lives wherever the host puts it. */export const Figure = Node.create<{ hooks: ViewHooks }>({ name: 'figure', group: 'block', content: 'inline*', draggable: true, selectable: true, isolating: true, addOptions() { return { hooks: {} as ViewHooks }; }, addAttributes() { return { cid: { default: '' }, mimeType: { default: '' }, size: { default: 0 }, alt: { default: '' }, local: { default: null, renderHTML: () => ({}) }, }; }, parseHTML() { return [{ tag: 'figure[data-cid]' }]; }, renderHTML({ HTMLAttributes, node }) { return ['figure', mergeAttributes({ 'data-cid': node.attrs.cid }), ['img', { src: HTMLAttributes.src ?? '' }], ['figcaption', 0]]; }, addNodeView() { return (props) => new FigureView(props, this.options.hooks); },});