Reference How it works
How it works
Four decisions explain most of the code, and a fifth explains how several people work on one document. Each has a small demo, made the way the engine is.
The path of a keystroke
One letter typed, from the key to the screen of everyone on the document. Each step is one of the decisions below.
- Hidden textarea
- Operation
- New Doc
- Lines and pages
- Placed spans
- The others
Step 1 of 6
A key, in a hidden textarea
The focus is in a textarea that follows the caret. There is no contenteditable: the browser edits nothing, and what is typed arrives there.
// what arrives in the textarea, at the caret
'x' → paragraph 3, offset 12The document is a value
A Doc is plain immutable data: paragraphs of runs, tables, the sections and their pages, headers and footers, footnotes, text boxes, styles, numbering, media. Headers, footers, footnotes and text boxes are stories: paragraphs of their own, edited the same way as the body. Nothing mutates one. An operation takes a document and returns another, sharing everything it did not touch.
const next = insertText(doc, { para: 3, offset: 0 }, 'Hello').doc;
next.paragraphs[0] === doc.paragraphs[0]; // true: untouched, so the very same objectTry it
Click a paragraph to type in it. The number is which object it is.
doc
What the engine sees
Green is the very same object in both documents: nothing to copy, nothing to lay out again, nothing to send. Yellow is the one paragraph made again — the only one the layout below breaks into lines again, and the only one the change sent to the others is about.
That one rule gives a lot for free
Undo
Is keeping the previous document.
Change detection
Is ===.
Layout caching
A paragraph that is the same object as last time breaks into the same lines.
Collaboration
The difference between two documents is found by comparing references.
Layout is ours
The browser is asked one thing: how wide is this piece of text in this font. Everything else is worked out by the engine. Because the rules are ours, they can be the ones Word documents are written for, and a file looks the same here as there — which a browser’s own line breaking would never give. The aim is compatibility, not imitation: the same document, the same pages.
Try it
What the engine did
Drag the width: the lines change, but after the first time no width is asked again — each word is known. Here the browser measures; in Node, a font file does, and the same code makes the PDF.
What the engine works out
- Lines, with justification, tab stops, kerning from the font’s own table of pairs, and Word’s rule for how far spaces may shrink to take one more word.
- Chinese and Japanese, broken between any two characters, never before a closing mark nor after an opening one, and justified between their characters.
- Pages: widow and orphan control, keep-with-next, keep-lines-together, page breaks.
- Sections, each with its own page size, margins, columns, header and footer.
- Tables, with rows that split across pages, tables inside cells, and text on its side.
- Headers and footers that take room from the text when they are tall.
- Footnotes at the foot of the page that refers to them, and endnotes at the end of the document or of each section, numbered as each section says, again on each page if it asks.
- Floating pictures and text boxes, and the text that goes round them.
- Lengths in points throughout, as in Word.
Lines then go into pages, by Word’s rules for where a page may end.
Try it: pages
Where the lines went
Drag the text above until the last paragraph meets the foot of the page. With the rules on, no line is left alone and the heading never ends a page; turn them off to see what they prevent. These are the rules of the engine, made small: the real ones also keep lines together, break before a paragraph and split table rows.
And because the only thing asked from outside is a width, the same code runs in Node with font files in place of a browser: that is how PDFs are made on a server.
Painting is placing
The editor does not hand text to the browser to lay out. Each piece of text is a span placed at the coordinates the layout gave; the browser never wraps a line, so what is on screen is exactly what was computed, and what will be printed. There is no contenteditable: a hidden textarea follows the caret and has the focus; what is typed arrives there and becomes an operation. Movement keys and shortcuts are taken in keydown, and the caret and the selection are drawn by the editor.
Try it
On the page
Click in the text and type: the keys go to a real textarea at the caret, each becomes a change to the text, and the lines are broken and placed again. Zoom scales pages laid out at their real size, so no line breaks differently.
DOCX without loss
A DOCX is a ZIP of XML with far more in it than any editor models. To not destroy what is not understood:
- Every imported paragraph remembers the XML it came from.
- A paragraph that was not touched is written back byte for byte.
- One that was changed is rewritten, keeping the properties the model does not know about.
- Parts of the file the editor never reads — themes, custom XML, settings — are copied through.
Try it
Mark the paragraphs you changed, then save.
<w:pPr>w:suppressLineNumbers…<w:pPr>w:snapToGrid…<w:pPr>w:kinsoku…<w:pPr>w:textAlignment…
Parts it never reads
word/theme/theme1.xmlword/settings.xmlcustomXml/item1.xmlNot saved yet
Each paragraph remembers the XML it came from. Change none and save: the file comes out the same — that is tested on a set of real documents.
Collaboration
Paragraphs get an id while a document is shared, and a change names paragraphs by id, so it still means the same after someone added a paragraph above. Headers, footers, footnotes and text boxes are sent the same way, and tracked changes go along as part of the formatting of their text.
Step 1 of 6
The same document, shared
While a document is shared, each paragraph has an id. Ana and Luis have the same three.
Ana’s screen
k1Receiptk2Payment is due within 15 days.k3Late payments add a fee.
Luis’s screen
k1Receiptk2Payment is due within 15 days.k3Late payments add a fee.
The hard cases
A deletion
Carries the paragraph it takes out, so that what someone was writing in it meanwhile stays.
Formatting
Says it is formatting, and is made letter by letter.
Enter
Carries the formatting of the new paragraph.
Undo
Sends the inverse of your own step, transformed against what others did since: it never takes back someone else’s work.
What it costs
Without contenteditable, everything the browser gives an editable page for free has to be done again. This is what was, and what was not.
Screen readersHandled
The textarea holds none of the text, so the editor says in a live region what the caret moved over, what was selected or deleted, and the page, table or list it is in. Screen reader mode (⌘⌥Z, Ctrl+Alt+Z elsewhere) puts the paragraph in the field, so the reader’s own commands and a braille display work.
Accessibility →Keyboard and high contrastHandled
F6 goes round the parts of the editor, Alt+F10 to the toolbar, Escape back to the document. With forced colours, the caret, the selection and what is on keep the system’s colours.
The keyboard →Other scripts and phone keyboardsHandled
A word being composed — Japanese, Chinese, a phone that suggests — is shown as it is written and changed when the keyboard corrects it.
FindHandled
⌘F and ⌘H (Ctrl elsewhere) open the editor’s own find bar, which searches the document, not the screen, and replaces.
Copy and pasteIn part
Copying gives plain text, HTML and the editor’s own format. Pasting from Word, Google Docs or a page keeps the formatting, links, lists, headings, tables, with their merged cells, colours, borders and row heights, and pictures; a table inside a cell comes as lines with tabs, and a picture a web server does not let the page read stays behind.
Status →Selecting by touchHandled
The selection is drawn by the editor, with handles to drag on a phone, and pages zoom with two fingers.
Mobile →Spell checkHandled
A browser checks spelling only in text it edits, and here it edits none: the editor checks it itself, with Hunspell dictionaries in a worker, and draws the red wave and the suggestions.
Spelling →Dragging textNot yet
Text cannot be dragged to another place yet; cut and paste does it.
What is announced was read in Chrome, not yet listened to with VoiceOver, NVDA or JAWS. The status page keeps the list of what is missing.
Where things live
To read the code from here: what each file holds, by package.
@rebodocs/core
- packages/core/src/model.ts
- The shape of a document.
- packages/core/src/ops.ts
- The operations, and the files beside it.
- packages/core/src/layout.ts
- Lines and pages.
- packages/core/src/docx/
- Reading and writing DOCX.
- packages/core/src/stories.tsfootnotes.tstextboxes.tssections.ts
- Headers, footers, footnotes, text boxes and sections.
- packages/core/src/revisions.tsversions.ts
- Tracked changes; comparing and restoring versions.
- packages/core/src/pdf.ts
- PDF, with font subsetting.
@rebodocs/editor
- packages/editor/src/editor.ts
- The Editor: its API and its state.
- packages/editor/src/painter.tspointer.tskeys.ts
- The pages and the caret drawn; the mouse and the fingers; the keys and the clipboard.
- packages/editor/src/
- One file per interface piece.
@rebodocs/collab
- packages/collab/src/changes.ts
- The change format and its transform.
- packages/collab/src/room.tsserver.ts
- A room, and the HTTP around it.
- packages/collab/src/sync.tssession.ts
- An editor kept in step with its room, and its connection.