Skip to main content
@reductoai/docx-editor/model is the layer under the editor chrome. It has no Svelte and no markup of its own. You get EditorModel: it opens a document, paints the pages, holds the caret and selection, takes keyboard and mouse input, and runs commands. You build every button and panel.

The smallest editor

style.css styles the .viewer, .pages and page classes. If you do not import it, copy the page and caret rules from @reductoai/docx-editor/styles.css. The base rules (reset, font, selection colour, scrollbars) apply only under the reductoai class. To use them in your chrome, add the class to its root. When you are done, call detach() and model.destroy().

What each part does

Bind your UI

EditorModel extends Observable:
Fields you will use: Commands:
await model.run(command) and helpers such as addBookmark(model, name) resolve when the edit is applied locally, after the edits queued before it. Host reads such as model.host.bookmarks() then see the edit in solo and live mode. Before a server exec read or download, also await editor.live.synced(): see When an edit is done. model.undo() takes back typing in Word’s steps. Text typed without a pause in the edits is one step, across its spaces and full stops. The step ends when the caret moves (even back to the same place), at Backspace or at a format change. Word’s AutoCorrect capital at the start of a sentence is a step of its own. formatCommands(model) returns every command as a list ({ id, group, label, run }), which is a good start for a command palette. Side models for the panels: model.review (comments and changes), model.find, model.nav (headings), model.spell (spelling), model.controls (content controls), model.ui (context menu, palette and dialogs). model.keys is the keymap: run(id), label(id), enabled(id) and register(command). The model gives the selection and the pages in client pixels, so your chrome does not have to read the layout: page from hitTest is the page number that the other page calls take. x and y are in hundredths of a millimetre from the page’s top-left corner. position is the text position under the point, and link is the link’s href.

Page overlays

model.overlays pins your own nodes to a page. The box is in points from the page’s top-left corner:
  • The nodes stay in place when the user scrolls, zooms or edits.
  • When a page goes away, its nodes leave the DOM. They come back when the page comes back.
  • model.overlays.list() returns every pinned node.
  • The layer is over the page and under the caret and selection. It takes no pointer events. A node that takes clicks sets pointer-events: auto and stops the event. If it does not, the press also places the caret.

Example: a selection bar and a citation preview

The editor below has its own selection bar with an Ask button and a citation card on hover. It uses only the model:
selectionRects and hover.rect are in client pixels, so the bar and the card use position: fixed. The bar moves only when the selection changes. To follow a scroll as well, subscribe to "page" or listen to the scroll of #pages. For a full skin (toolbar, menus, galleries, context menu) built this way, read Build your own skin. Editor actions and Fonts, colours and galleries list the helpers that a skin calls.

Your own worker

The default worker uses the portable fonts. To use other fonts or other layout options, write your own worker. It is three lines:
Then point the page at it:
Use the same fonts in the page and in the worker. The worker sends face bytes to the page with each layout, and the page skips the faces it already has. trackedChanges: "inline" with markupGutter: false is what the built-in editor uses. The model then switches the view for Display for Review and the review margin (model.setMarkup, open(..., { markup })), so keep these values in your own worker.