| 1 | // fixtures/schema.mjs — the editor's ProseMirror schema, on its own so |
| 2 | // that Node can load it: the font gate's tests and the random-document |
| 3 | // generator (generate-random-docs.mjs) derive what they cover from it, |
| 4 | // node by node and mark by mark, rather than from a hand list that a new |
| 5 | // node type could slip past. editor-entry.js mounts the editor with it. |
| 6 | |
| 7 | import { Schema } from 'prosemirror-model' |
| 8 | import { schema as basic } from 'prosemirror-schema-basic' |
| 9 | import { addListNodes } from 'prosemirror-schema-list' |
| 10 | import { withIndent } from './indent.mjs' |
| 11 | import { SIZE_MARKS } from './sizes.mjs' |
| 12 | import { alignedParagraph } from './align.mjs' |
| 13 | import { hardBreakIgnoringInterchange } from './paste.mjs' |
| 14 | import { FAMILIES, DEFAULT_FONT, CODE_FONT, familyForStack } from './fonts.mjs' |
| 15 | |
| 16 | // spanMark builds one of the font/size marks: attribute-free, rendered |
| 17 | // as a span with a fixed class. |
| 18 | // |
| 19 | // The class names, and the mark names, are a contract with |
| 20 | // core/src/fragment.rs's MARKS table — the server renders the same |
| 21 | // document to the same markup for a reader with no JavaScript, and |
| 22 | // internal/export reads the same mark names to lay out a PDF. All |
| 23 | // three have to agree, and none of them can carry a value: see that |
| 24 | // table's own comment for why a mark with an attribute would spend |
| 25 | // the property that lets the read view emit its markup unescaped. |
| 26 | // |
| 27 | // group/excludes make each set mutually exclusive, so text cannot end |
| 28 | // up both sans and mono, or both small and large. The toolbar's own |
| 29 | // select controls rely on that as well as on their own clearing — |
| 30 | // belt and braces, because the schema is what also governs a paste. |
| 31 | function spanMark(cls, group) { |
| 32 | return { |
| 33 | group, |
| 34 | excludes: group, |
| 35 | parseDOM: [{ tag: 'span.' + cls }], |
| 36 | toDOM: () => ['span', { class: cls }, 0], |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // addToStart, applied in reverse, leaves the font and size marks ahead |
| 41 | // of prosemirror-schema-basic's own — ProseMirror nests marks in |
| 42 | // schema order, so this makes the browser produce |
| 43 | // <span class="docs-font-inter"><strong>…</strong></span>, which is the |
| 44 | // order fragment.rs's MARKS table renders server-side. |
| 45 | // |
| 46 | // Every family is a mark, the default's included. The default |
| 47 | // (fixtures/fonts.mjs's DEFAULT_FONT, Carlito) is what the toolbar |
| 48 | // CLEARS to rather than sets, so no document gains a span for the face |
| 49 | // it was already in. The others have to go on parsing for ever: font_sans |
| 50 | // and font_serif were each the default once, and documents carry them; |
| 51 | // dropping any family's mark from this schema would strip it out of |
| 52 | // every document carrying it on the next edit. |
| 53 | // |
| 54 | // The size marks are added from SIZE_MARKS rather than written out, |
| 55 | // which is a departure from the three faces below and needs saying. |
| 56 | // |
| 57 | // The usual argument for writing names out is that internal/docs's |
| 58 | // tests grep this file's source for a literal, so a loop would leave |
| 59 | // them nothing to find. That argument still holds for anything they |
| 60 | // grep — and it is why fixtures/sizes.mjs spells the ladder out as |
| 61 | // numbers, where they can be read. Here there are fourteen of them |
| 62 | // (eleven points and three legacy names), the class is mechanically the |
| 63 | // mark name with its prefix swapped, and fourteen hand-written lines |
| 64 | // would be fourteen chances to typo a class that then renders as |
| 65 | // nothing. The vocabulary lives in sizes.mjs; this is its application. |
| 66 | const marks = SIZE_MARKS.reduce( |
| 67 | (spec, mark) => spec.addToStart(mark, spanMark('docs-' + mark.replace('_', '-'), 'fontSize')), |
| 68 | basic.spec.marks, |
| 69 | ) |
| 70 | |
| 71 | // The font marks, one per family in internal/fonts/fonts.json, in |
| 72 | // manifest order: added in reverse, so the first family ends up first, |
| 73 | // outermost, as fragment.rs's MARKS table renders them. |
| 74 | // |
| 75 | // A pasted run's own font-family stack is read too, by familyForStack, |
| 76 | // the rule the native app uses: Georgia, "Times New Roman" is Caladea. |
| 77 | // A stack that maps to the default, or to nothing, gets no mark. |
| 78 | const fontMarks = [...FAMILIES] |
| 79 | .reverse() |
| 80 | .reduce((spec, f) => { |
| 81 | const mark = spanMark('docs-font-' + f.key, 'fontFamily') |
| 82 | if (f.key !== DEFAULT_FONT) { |
| 83 | mark.parseDOM = [...mark.parseDOM, { style: 'font-family', getAttrs: (value) => (familyForStack(value) === f.key ? null : false) }] |
| 84 | } else { |
| 85 | // An explicit font-family that is the default, or that maps to |
| 86 | // nothing, is the default: it clears a font mark inherited from an |
| 87 | // outer element, so Calibri inside an Inter paragraph is Carlito |
| 88 | // (branch review 3). No font-family style at all inherits as before. |
| 89 | mark.parseDOM = [...mark.parseDOM, { |
| 90 | style: 'font-family', |
| 91 | getAttrs: (value) => (familyForStack(value) === DEFAULT_FONT || familyForStack(value) === null ? null : false), |
| 92 | clearMark: (m) => m.type.spec.group === 'fontFamily', |
| 93 | }] |
| 94 | } |
| 95 | return spec.addToStart('font_' + f.key, mark) |
| 96 | }, marks) |
| 97 | |
| 98 | // The paragraph gains an `align` attribute — the first block-level |
| 99 | // formatting, and the first thing in this schema that is not a mark. |
| 100 | // |
| 101 | // It cannot be one: a mark applies to a run and alignment applies to |
| 102 | // the whole block, so it lives on the node. That means it crosses the |
| 103 | // wire differently too — y-prosemirror stores a node's attributes as |
| 104 | // Yjs XML attributes, so an aligned paragraph genuinely changes what |
| 105 | // the document CONTAINS, where a mark only changes how a run is |
| 106 | // annotated. fixtures/align.mjs has the vocabulary and the reasoning, |
| 107 | // and fixtures/align.test.mjs pins it; the short version is that the |
| 108 | // value is only ever a key into a closed table, in this schema, in |
| 109 | // core/src/fragment.rs and in internal/export/pdf.go alike. |
| 110 | // |
| 111 | // Applied before addListNodes so the list nodes are built against the |
| 112 | // paragraph that already has the attribute — a list item's paragraph is |
| 113 | // an ordinary paragraph and gets alignment like any other. |
| 114 | // |
| 115 | // hard_break is patched at the same time, and only on the way in: the |
| 116 | // rule ignores WebKit's Apple-interchange-newline marker, which is a |
| 117 | // copy boundary rather than a line the author left. See paste.mjs. |
| 118 | // Indent is the second block attribute, and it wraps the paragraph |
| 119 | // AFTER alignment so a paragraph carries both. withIndent composes |
| 120 | // rather than replaces — it keeps whatever parseDOM rules the spec it |
| 121 | // wraps already had and merges its own getAttrs into them, which is |
| 122 | // what stops a pasted paragraph that is centred AND indented from |
| 123 | // losing one of the two. |
| 124 | // |
| 125 | // Headings get it too, and do not get alignment. Every word processor |
| 126 | // indents a heading; none of the reference applications this one |
| 127 | // follows offers alignment on a heading through its own toolbar, and |
| 128 | // adding it would be a change to fragment.rs and pdf.go as well as to |
| 129 | // this line. |
| 130 | const nodes = addListNodes( |
| 131 | basic.spec.nodes |
| 132 | .update( |
| 133 | 'paragraph', |
| 134 | withIndent(alignedParagraph(basic.spec.nodes.get('paragraph')), 'p'), |
| 135 | ) |
| 136 | .update('heading', withIndent(basic.spec.nodes.get('heading'), 'h1')) |
| 137 | .update('hard_break', hardBreakIgnoringInterchange(basic.spec.nodes.get('hard_break'))), |
| 138 | 'paragraph block*', |
| 139 | 'block', |
| 140 | ) |
| 141 | |
| 142 | const schema = new Schema({ nodes, marks: fontMarks }) |
| 143 | |
| 144 | export { schema } |
| 145 | |
| 146 | // familyOfMark and familyOfNode are the families a mark or a node type |
| 147 | // puts text in, read off the schema: a font_<key> mark is its family; |
| 148 | // a spec with code: true (the code mark, the code_block node) is the |
| 149 | // code family. Everything else is in its surroundings' family. The font |
| 150 | // gate (font-session.mjs, editor-entry.js's transaction filter) and its |
| 151 | // tests take every family from these two. |
| 152 | export function familyOfMark(markType) { |
| 153 | const name = markType.name || markType |
| 154 | if (typeof name === 'string' && name.startsWith('font_')) return name.slice(5) |
| 155 | const t = typeof markType === 'string' ? schema.marks[markType] : markType |
| 156 | return t && t.spec.code ? CODE_FONT : null |
| 157 | } |
| 158 | |
| 159 | export function familyOfNode(nodeType) { |
| 160 | const t = typeof nodeType === 'string' ? schema.nodes[nodeType] : nodeType |
| 161 | return t && t.spec.code ? CODE_FONT : null |
| 162 | } |
| 163 | |
| 164 | // familiesOfFragment is every family a ProseMirror fragment's content is |
| 165 | // in, the default always among them. |
| 166 | export function familiesOfFragment(fragment, out = new Set([DEFAULT_FONT])) { |
| 167 | fragment.descendants((node) => { |
| 168 | const n = familyOfNode(node.type) |
| 169 | if (n) out.add(n) |
| 170 | for (const m of node.marks) { |
| 171 | const f = familyOfMark(m.type) |
| 172 | if (f) out.add(f) |
| 173 | } |
| 174 | }) |
| 175 | return out |
| 176 | } |
| 177 | |