Birch UI v0.1
Pricing

Editing engineDocument

How the rich-text editing core inside Birch UI Document Editor is organised, from the immutable document tree and steps to the DOM view, and how it is tested.

Open .md

The editing engine is the part of the Document Editor that owns the document tree, turns every edit into a reversible step, and keeps the browser DOM in sync with that tree. It lives in packages/document/src/core and has no runtime dependencies.

The engine is internal. It is not exported from @birchui/document, and its modules may change between releases. This page is for contributors and for anyone who wants to understand what happens between a keystroke and a saved document.

Modules#

Module Responsibility
core/model Schema, nodes, fragments, slices, marks, resolved positions, HTML parsing and serialising
core/transform Steps, position maps, and helpers that replace, wrap, lift, split and join content
core/state Editor state, selections, transactions and plugins
core/view DOM rendering, selection sync, keyboard, composition, clipboard, drag and drop, coordinates, decorations
core/commands Editing commands and the base key bindings
core/keymap Key-name normalisation and the keymap plugin
core/history Undo and redo with grouping and rebasing over remote changes
core/list List wrapping, splitting, lifting and sinking
core/tables Table node specs, the table map, cell selections, table commands, column resizing and table repair

Everything except core/view, the HTML parser and serialiser, and the two table plugins is free of DOM access, so it runs unchanged in the headless entry and in the collaboration authority.

Document model#

A document is an immutable tree. An edit never changes a node; it builds a new tree that shares every untouched branch with the old one. That makes comparing two versions cheap and lets undo, collaboration and change tracking keep old documents without copying them.

A schema declares which nodes and marks exist. Each node type has a content expression such as paragraph+ or (text | image)*, compiled once into an automaton. The automaton answers three questions the rest of the engine keeps asking: is this content valid here, what may come next, and which nodes must be added to make the content valid.

Positions are plain integers that count tokens from the start of the document. A resolved position adds the path of ancestors, with the index and offset at each level.

Documents are stored as JSON in the form { type, attrs, content, marks, text }. The shape is unchanged from earlier releases, so saved documents open as they are.

Steps and mapping#

Every change is a step: replace a range, replace around a gap, add or remove a mark, or set an attribute. A step can be applied, inverted, mapped over other steps and written to JSON. The JSON identifiers (replace, replaceAround, addMark, removeMark, addNodeMark, removeNodeMark, attr, docAttr) are the ones the collaboration wire format and stored step logs already use.

Each step produces a position map. Maps carry positions forward through a change and report when a position was deleted. A mapping is a list of maps that can also record mirror pairs, which is how a local step is rebased over remote steps without losing positions that were removed and put back.

Inserting a slice into a range whose surroundings have a different structure is the hardest part of this layer. The fitter places each node of the slice at the deepest open level that accepts it, wraps or fills where the schema requires it, and closes what is left, so the result is always valid under the schema.

State and transactions#

The editor state holds the document, the selection, the stored marks and one value per plugin. A transaction is a transform plus a selection, stored marks, metadata and a timestamp. Applying a transaction returns a new state. Plugins can reject a transaction, append further transactions, and keep their own state.

View#

The view renders the document into a contenteditable element and updates it incrementally: a node that is the same object as before keeps its DOM untouched, and only changed nodes are rendered again. Node views let a feature take over the DOM of a node type. Decorations add classes, wrappers and widgets without changing the document, and their set is mapped through each change instead of being rebuilt.

Input arrives three ways. Key presses run through the key bindings. beforeinput events are handled where browsers report them reliably. Everything else, including input methods, spellcheck replacements, autocorrect and dictation, is read back from the DOM by a mutation observer and turned into the smallest step that explains the change.

The view also maps between document positions and screen coordinates, which pagination and text wrapping depend on.

CSS hooks#

The view marks the elements it manages with classes under the product prefix. Style them from your own stylesheet; do not add or remove them yourself.

Class Where it appears
lq-doc-editable The editable root element, next to lq-doc-content
lq-doc-focused The editable root while it has focus
lq-doc-hide-selection The editable root while a node, not text, is selected
lq-doc-selected-node A node that is selected as a whole, such as a picture, a field or a break
lq-doc-widget The root of every widget the editor inserts between content
lq-doc-trailing-break The filler line break of an empty paragraph
lq-doc-separator The filler image after a trailing non-editable inline node
lq-doc-table-wrapper The scroll container around a table
lq-doc-cell-selected A cell inside a cell selection
lq-doc-column-resize-handle The column resize handle shown at a cell edge
lq-doc-column-resize-dragging The cell whose edge is being dragged
lq-doc-resize-cursor The editable root while the pointer is over a column edge

Migrating from 0.x#

Version 1 renamed these hooks. If your stylesheet or tests target the old names, replace them:

Before Now
ProseMirror lq-doc-editable
ProseMirror-focused lq-doc-focused
ProseMirror-hideselection lq-doc-hide-selection
ProseMirror-selectednode lq-doc-selected-node
ProseMirror-widget lq-doc-widget
ProseMirror-trailingBreak lq-doc-trailing-break
ProseMirror-separator lq-doc-separator
tableWrapper lq-doc-table-wrapper
selectedCell lq-doc-cell-selected
column-resize-handle lq-doc-column-resize-handle
column-resize-dragging lq-doc-column-resize-dragging
resize-cursor lq-doc-resize-cursor

HTML copied from the editor now carries data-lq-doc-slice on its first element instead of data-pm-slice. The prosemirror-* packages are no longer installed with @birchui/document; if your application imported them only because the editor brought them in, add them to your own dependencies or remove the imports.

How it is tested#

  • Unit tests for every module: npx vitest run packages/document/test/core-.
  • Property tests generate random valid documents and operations from a fixed seed and assert that results stay valid, that inverted steps restore the original, and that positions and JSON survive a round trip.
  • Recorded transcripts under packages/document/test/fixtures/core hold inputs and expected results for transforms, steps, mappings, selections, history, commands, tables and HTML parsing.
  • Browser tests in packages/document/test/core-browser run in Chrome and Firefox and cover typing, selection, composition, clipboard, drag and drop, coordinates and cleanup.
  • The package's own suite: every other unit test and end-to-end spec of the Document Editor runs on the engine, because the editor imports it directly.

↑ ↓ to move, Enter to open, Esc to close