Something went wrong. Try again.
A monorepo for writing specs and other serious text on the AT Protocol.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164// Block behaviour: Enter/Backspace/Delete/Tab as a document of blocks wants// them, section anchors that follow titles, and `$x$` for inline math.import { Extension, InputRule } from '@tiptap/core';import { Plugin, TextSelection } from '@tiptap/pm/state';import type { Node as PMNode } from '@tiptap/pm/model';import { slugify } from '../text.ts';
export const DraftKeymap = Extension.create({ name: 'draftKeymap', priority: 1000, addKeyboardShortcuts() { return { Enter: ({ editor }) => { const { state } = editor; const { $from, empty } = state.selection; if (!empty) return false; const parent = $from.parent; const name = parent.type.name; if (name === 'definitionTerm') { // term → its description return editor.commands.setTextSelection($from.after() + 1); } if (name === 'definitionDescription') { const item = $from.node(-1); const term = item.child(0); if (term.content.size === 0 && parent.content.size === 0) { // an empty pair: leave the list into a paragraph after it const list = $from.node(-2); const listPos = $from.before(-2); const single = list.childCount === 1; return editor.commands.command(({ tr, dispatch }) => { const p = state.schema.nodes.paragraph!.create(); let at: number; if (single) { tr.replaceWith(listPos, listPos + list.nodeSize, p); at = listPos + 1; } else { tr.delete($from.before(-1), $from.after(-1)); const after = tr.mapping.map(listPos + list.nodeSize); tr.insert(after, p); at = after + 1; } tr.setSelection(TextSelection.create(tr.doc, Math.min(at, tr.doc.content.size))); dispatch?.(tr.scrollIntoView()); return true; }); } // a new pair after this one return editor.commands.command(({ tr, dispatch }) => { const after = $from.after(-1); const fresh = state.schema.nodes.definitionItem!.createAndFill(); if (!fresh) return false; tr.insert(after, fresh); tr.setSelection(TextSelection.create(tr.doc, after + 2)); dispatch?.(tr.scrollIntoView()); return true; }); } if (name !== 'paragraph') return false; if ($from.node(-1).type.name === 'listItem') return false; // the list keymap if (parent.content.size === 0) return true; // an empty block stays alone if ($from.parentOffset === parent.content.size) { // at the end: an empty next block is reused const container = $from.node(-1); const idx = $from.indexAfter(-1); const next = idx < container.childCount ? container.child(idx) : null; if (next && next.type.name === 'paragraph' && next.content.size === 0) { return editor.commands.setTextSelection($from.after() + 1); } } return false; }, 'Shift-Enter': ({ editor }) => { // out of a code or math block: a paragraph after it const { state } = editor; const sel = state.selection; const $from = sel.$from; const inCode = $from.parent.type.name === 'codeBlock'; const onMath = 'node' in sel && (sel as { node: PMNode }).node.type.name === 'blockMath'; if (!inCode && !onMath) return false; const after = inCode ? $from.after() : sel.to; return editor.commands.command(({ tr, dispatch }) => { tr.insert(after, state.schema.nodes.paragraph!.create()); tr.setSelection(TextSelection.create(tr.doc, after + 1)); dispatch?.(tr.scrollIntoView()); return true; }); }, Backspace: ({ editor }) => { const { $from, empty } = editor.state.selection; if (!empty || $from.parentOffset !== 0) return false; const parent = $from.parent; if (parent.type.name !== 'paragraph' || parent.content.size === 0) return false; if ($from.node(-1).type.name === 'listItem') return false; const idx = $from.index(-1); const prev = idx > 0 ? $from.node(-1).child(idx - 1) : null; // merge only into a paragraph; otherwise stay put return !!prev && prev.type.name !== 'paragraph'; }, Delete: ({ editor }) => { const { $from, empty } = editor.state.selection; const parent = $from.parent; if (!empty || parent.type.name !== 'paragraph' || $from.parentOffset !== parent.content.size || parent.content.size === 0) return false; if ($from.node(-1).type.name === 'listItem') return false; const container = $from.node(-1); const idx = $from.indexAfter(-1); const next = idx < container.childCount ? container.child(idx) : null; return !!next && next.type.name !== 'paragraph'; }, Tab: ({ editor }) => { if (editor.state.selection.$from.parent.type.name !== 'codeBlock') return false; return editor.commands.insertContent(' '); }, }; },});
/** Section anchors follow their titles until edited by hand. */export const SectionAnchors = Extension.create({ name: 'sectionAnchors', addProseMirrorPlugins() { return [ new Plugin({ appendTransaction: (trs, _old, state) => { if (!trs.some((tr) => tr.docChanged)) return null; const tr = state.tr; let changed = false; state.doc.descendants((node, pos) => { if (node.type.name !== 'section') return true; if (node.attrs.anchorTouched) return true; const title = node.firstChild?.textContent ?? ''; const anchor = slugify(title, 'section'); if (node.attrs.anchor !== anchor) { tr.setNodeMarkup(pos, undefined, { ...node.attrs, anchor }); changed = true; } return true; }); return changed ? tr : null; }, }), ]; },});
/** `$x$` makes inline math (the extension only has `$$x$$`). */export const InlineMathDollar = Extension.create({ name: 'inlineMathDollar', addInputRules() { const type = this.editor.schema.nodes.inlineMath; if (!type) return []; return [ new InputRule({ find: /(?<!\$)\$([^$\n]+?)\$$/, handler: ({ state, range, match }) => { const latex = match[1]?.trim(); if (!latex) return; state.tr.replaceWith(range.from, range.to, type.create({ latex })); }, }), ]; },});