Skip to content

Core concepts ​

The document is JSON ​

The editor's content is a DocJSON value: a tree of nodes (paragraphs, headings, list items…) with text and marks (bold, link…).

json
{
  "type": "doc",
  "schemaVersion": 1,
  "content": [
    {
      "type": "paragraph",
      "content": [{ "type": "text", "text": "Hello", "marks": [{ "type": "bold" }] }]
    }
  ]
}
  • Store this JSON. It is lossless and does not depend on the browser. HTML and Markdown are derived from it (see Saving and loading).
  • schemaVersion is the version of the format itself. When it changes, migrateDoc() (run automatically on load) brings old documents up to date.

Extensions define what a document may contain ​

An extension adds node or mark types to the schema, plus behaviour: keyboard shortcuts, input rules (Markdown-style shortcuts such as ## ), and ProseMirror plugins. Presets are just arrays of extensions:

ts
import { basic } from '@writeon/core/presets'

basic() // everything
basic({ strike: false }) // without strikethrough
basic({ heading: { levels: [2, 3] } }) // only H2 and H3
basic({ link: { protocols: ['https'] } }) // https links only

Content the schema does not allow is dropped when a document is loaded or pasted. Use the same extensions on the server (see Security).

To write your own, see Your own extension.

Commands change the document ​

Commands are factories: toggleBold() returns a command, which you pass to editor.run(). Commands live in @writeon/core/commands and in the packages that add features.

ts
import type { Editor } from '@writeon/core'
import {
  setHeading,
  setLink,
  toggleBold,
  toggleBulletList,
} from '@writeon/core/commands'

export function examples(editor: Editor) {
  // Commands are factories: call them, then run the result.
  editor.run(toggleBold())

  // can() asks without changing anything.
  // Use it to disable buttons.
  const canList = editor.can(toggleBulletList())

  // Several commands, one undo step.
  // Nothing is applied if one of them fails.
  editor.transaction((tx) => {
    tx.run(setHeading(2))
    tx.run(setLink({ href: 'https://example.com' }))
  })

  // What is active at the selection.
  const inHeading = editor.isActive('heading', { level: 2 })

  // A snapshot for your UI; subscribe() says when it changed.
  const off = editor.subscribe(() => {
    const { marks, canUndo, block } = editor.getSnapshot()
    console.log([...marks], canUndo, block.type)
  })

  return { canList, inHeading, off }
}

State for your UI ​

editor.getSnapshot() returns an immutable object with what UI usually needs: active marks, the current block, selection, canUndo, focused, editable, empty. editor.subscribe() calls you when it changes (batched per microtask).

In React, useEditorState(editor, selector) re-renders only when the selected value changes; in Vue, it returns a ref.

Lifecycle ​

  • createEditor({ element }) mounts at once; without element, call editor.mount(el) later. Document and history survive unmount() and mount().
  • editor.destroy() releases everything. The React and Vue adapters do this for you.
  • editor.pm gives direct access to ProseMirror. It is not a stable API: it may change in any minor release.