// fixtures/schema.mjs — the editor's ProseMirror schema, on its own so
// that Node can load it: the font gate's tests and the random-document
// generator (generate-random-docs.mjs) derive what they cover from it,
// node by node and mark by mark, rather than from a hand list that a new
// node type could slip past. editor-entry.js mounts the editor with it.
import { Schema } from 'prosemirror-model'
import { schema as basic } from 'prosemirror-schema-basic'
import { addListNodes } from 'prosemirror-schema-list'
import { withIndent } from './indent.mjs'
import { SIZE_MARKS } from './sizes.mjs'
import { alignedParagraph } from './align.mjs'
import { hardBreakIgnoringInterchange } from './paste.mjs'
import { FAMILIES, DEFAULT_FONT, CODE_FONT, familyForStack } from './fonts.mjs'
// spanMark builds one of the font/size marks: attribute-free, rendered
// as a span with a fixed class.
//
// The class names, and the mark names, are a contract with
// core/src/fragment.rs's MARKS table — the server renders the same
// document to the same markup for a reader with no JavaScript, and
// internal/export reads the same mark names to lay out a PDF. All
// three have to agree, and none of them can carry a value: see that
// table's own comment for why a mark with an attribute would spend
// the property that lets the read view emit its markup unescaped.
//
// group/excludes make each set mutually exclusive, so text cannot end
// up both sans and mono, or both small and large. The toolbar's own
// select controls rely on that as well as on their own clearing —
// belt and braces, because the schema is what also governs a paste.
function spanMark(cls, group) {
return {
group,
excludes: group,
parseDOM: [{ tag: 'span.' + cls }],
toDOM: () => ['span', { class: cls }, 0],
}
}
// addToStart, applied in reverse, leaves the font and size marks ahead
// of prosemirror-schema-basic's own — ProseMirror nests marks in
// schema order, so this makes the browser produce
// …, which is the
// order fragment.rs's MARKS table renders server-side.
//
// Every family is a mark, the default's included. The default
// (fixtures/fonts.mjs's DEFAULT_FONT, Carlito) is what the toolbar
// CLEARS to rather than sets, so no document gains a span for the face
// it was already in. The others have to go on parsing for ever: font_sans
// and font_serif were each the default once, and documents carry them;
// dropping any family's mark from this schema would strip it out of
// every document carrying it on the next edit.
//
// The size marks are added from SIZE_MARKS rather than written out,
// which is a departure from the three faces below and needs saying.
//
// The usual argument for writing names out is that internal/docs's
// tests grep this file's source for a literal, so a loop would leave
// them nothing to find. That argument still holds for anything they
// grep — and it is why fixtures/sizes.mjs spells the ladder out as
// numbers, where they can be read. Here there are fourteen of them
// (eleven points and three legacy names), the class is mechanically the
// mark name with its prefix swapped, and fourteen hand-written lines
// would be fourteen chances to typo a class that then renders as
// nothing. The vocabulary lives in sizes.mjs; this is its application.
const marks = SIZE_MARKS.reduce(
(spec, mark) => spec.addToStart(mark, spanMark('docs-' + mark.replace('_', '-'), 'fontSize')),
basic.spec.marks,
)
// The font marks, one per family in internal/fonts/fonts.json, in
// manifest order: added in reverse, so the first family ends up first,
// outermost, as fragment.rs's MARKS table renders them.
//
// A pasted run's own font-family stack is read too, by familyForStack,
// the rule the native app uses: Georgia, "Times New Roman" is Caladea.
// A stack that maps to the default, or to nothing, gets no mark.
const fontMarks = [...FAMILIES]
.reverse()
.reduce((spec, f) => {
const mark = spanMark('docs-font-' + f.key, 'fontFamily')
if (f.key !== DEFAULT_FONT) {
mark.parseDOM = [...mark.parseDOM, { style: 'font-family', getAttrs: (value) => (familyForStack(value) === f.key ? null : false) }]
} else {
// An explicit font-family that is the default, or that maps to
// nothing, is the default: it clears a font mark inherited from an
// outer element, so Calibri inside an Inter paragraph is Carlito
// (branch review 3). No font-family style at all inherits as before.
mark.parseDOM = [...mark.parseDOM, {
style: 'font-family',
getAttrs: (value) => (familyForStack(value) === DEFAULT_FONT || familyForStack(value) === null ? null : false),
clearMark: (m) => m.type.spec.group === 'fontFamily',
}]
}
return spec.addToStart('font_' + f.key, mark)
}, marks)
// The paragraph gains an `align` attribute — the first block-level
// formatting, and the first thing in this schema that is not a mark.
//
// It cannot be one: a mark applies to a run and alignment applies to
// the whole block, so it lives on the node. That means it crosses the
// wire differently too — y-prosemirror stores a node's attributes as
// Yjs XML attributes, so an aligned paragraph genuinely changes what
// the document CONTAINS, where a mark only changes how a run is
// annotated. fixtures/align.mjs has the vocabulary and the reasoning,
// and fixtures/align.test.mjs pins it; the short version is that the
// value is only ever a key into a closed table, in this schema, in
// core/src/fragment.rs and in internal/export/pdf.go alike.
//
// Applied before addListNodes so the list nodes are built against the
// paragraph that already has the attribute — a list item's paragraph is
// an ordinary paragraph and gets alignment like any other.
//
// hard_break is patched at the same time, and only on the way in: the
// rule ignores WebKit's Apple-interchange-newline marker, which is a
// copy boundary rather than a line the author left. See paste.mjs.
// Indent is the second block attribute, and it wraps the paragraph
// AFTER alignment so a paragraph carries both. withIndent composes
// rather than replaces — it keeps whatever parseDOM rules the spec it
// wraps already had and merges its own getAttrs into them, which is
// what stops a pasted paragraph that is centred AND indented from
// losing one of the two.
//
// Headings get it too, and do not get alignment. Every word processor
// indents a heading; none of the reference applications this one
// follows offers alignment on a heading through its own toolbar, and
// adding it would be a change to fragment.rs and pdf.go as well as to
// this line.
// sizedImage is the basic image with the two layout hints an upload's
// reply gives (Docs images spec §1 decision 5): width and height, read by
// parseDOM and written on insert. The node stays inline.
//
// Both need default: null. y-prosemirror builds each node with
// schema.node() and deletes the Yjs element, for everyone, when that
// throws, so an attr with no default would delete every image written
// without it, such as the data: images native paste writes today.
// validate is not run on that path either, so a value may arrive as a
// number or as junk: image-view.mjs, fragment.rs and node_json each take
// only 1 to 5 digits, clamped to 10,000.
function sizedImage(spec) {
const size = (dom, name) => {
const value = dom.getAttribute(name)
return value && /^\d{1,5}$/.test(value) && Number(value) > 0 ? Math.min(Number(value), 10000) : null
}
return {
...spec,
attrs: { ...spec.attrs, width: { default: null }, height: { default: null } },
parseDOM: [{
tag: 'img[src]',
getAttrs: (dom) => ({
src: dom.getAttribute('src'),
alt: dom.getAttribute('alt'),
title: dom.getAttribute('title'),
width: size(dom, 'width'),
height: size(dom, 'height'),
}),
}],
toDOM(node) {
const { src, alt, title, width, height } = node.attrs
return ['img', { src, alt, title, width, height }]
},
}
}
const nodes = addListNodes(
basic.spec.nodes
.update(
'paragraph',
withIndent(alignedParagraph(basic.spec.nodes.get('paragraph')), 'p'),
)
.update('heading', withIndent(basic.spec.nodes.get('heading'), 'h1'))
.update('hard_break', hardBreakIgnoringInterchange(basic.spec.nodes.get('hard_break')))
.update('image', sizedImage(basic.spec.nodes.get('image'))),
'paragraph block*',
'block',
)
const schema = new Schema({ nodes, marks: fontMarks })
export { schema }
// familyOfMark and familyOfNode are the families a mark or a node type
// puts text in, read off the schema: a font_ mark is its family;
// a spec with code: true (the code mark, the code_block node) is the
// code family. Everything else is in its surroundings' family. The font
// gate (font-session.mjs, editor-entry.js's transaction filter) and its
// tests take every family from these two.
export function familyOfMark(markType) {
const name = markType.name || markType
if (typeof name === 'string' && name.startsWith('font_')) return name.slice(5)
const t = typeof markType === 'string' ? schema.marks[markType] : markType
return t && t.spec.code ? CODE_FONT : null
}
export function familyOfNode(nodeType) {
const t = typeof nodeType === 'string' ? schema.nodes[nodeType] : nodeType
return t && t.spec.code ? CODE_FONT : null
}
// familiesOfFragment is every family a ProseMirror fragment's content is
// in, the default always among them.
export function familiesOfFragment(fragment, out = new Set([DEFAULT_FONT])) {
fragment.descendants((node) => {
const n = familyOfNode(node.type)
if (n) out.add(n)
for (const m of node.marks) {
const f = familyOfMark(m.type)
if (f) out.add(f)
}
})
return out
}