Skip to content

Getting started ​

Rector is a browser-native block editor. It stores content as a versioned JSON document, provides block and inline editing, and renders the same document without mounting an editor. The package is ESM-only and does not require a UI framework.

This page builds the smallest complete integration. The following chapters explain every option and extension boundary in detail.

Requirements ​

  • current stable Chrome or Chromium for the release-tested editing runtime;
  • Node.js ^20.19.0 || >=22.12.0 for installation and build tooling;
  • an application that can import CSS and ESM packages.

Rector depends on native contenteditable, Selection/Range, clipboard, focus, keyboard, and composition behavior. The automated release gate currently verifies those semantics in Chrome/Chromium. Firefox and WebKit/Safari may work, but they are not part of the supported browser contract until equivalent native engine gates are added.

Install ​

Install Rector 2 in your application.

bash
npm install @shelamkoff/rector@2

npm installs the runtime dependencies for you. To build a local package from a Git checkout, run npm ci --ignore-scripts followed by npm pack at the repository root.

Add a holder ​

Rector owns the contents of the holder for the lifetime of the editor instance. Pass the element itself, not a selector string.

html
<div id="editor"></div>
css
#editor {
  max-width: 760px;
  margin-inline: auto;
}

Create the editor ​

Register the block types you want to edit. The editor uses the dark Graphite palette by default. The injectStyles: true mode loads the editor, plugin, and dependency styles automatically.

js
import { createEditor } from '@shelamkoff/rector'
import { createParagraphPlugin } from '@shelamkoff/rector/plugins/paragraph'
import { createHeadingPlugin } from '@shelamkoff/rector/plugins/heading'

const holder = document.querySelector('#editor')

const editor = createEditor({
  holder,
  plugins: [createParagraphPlugin(), createHeadingPlugin()],
  data: {
    version: '2.0.0',
    blocks: [
      {
        id: 'intro',
        type: 'heading',
        dataVersion: 2,
        data: { text: 'A structured document', level: 2 },
      },
      {
        id: 'body',
        type: 'paragraph',
        dataVersion: 2,
        data: { text: 'Start editing this text.' },
      },
    ],
  },
  onChange(document) {
    console.log(document)
  },
})

For Vite, Nuxt, or another CSS-aware bundler, set injectStyles: false and import the base plus selected plugin CSS subpaths, or the all-in-one @shelamkoff/rector/styles.css. The styling guide lists both forms.

createEditor() returns the mounted editor synchronously. You can already read its document with save(). The ready microtask sets editor.isReady to true and emits editor:ready. Subscribe immediately after creation if the rest of the application needs to wait for that event.

js
await new Promise(resolve => {
  const unsubscribe = editor.on('editor:ready', () => {
    unsubscribe()
    resolve()
  })
})

Use the onReady configuration callback when the integration already passes its initialization observer in the options.

Save content ​

Use save() in application code. It synchronously returns a detached document object owned by the caller. A block serialization or strict-validation failure is thrown immediately.

js
const documentData = editor.save()
const json = JSON.stringify(documentData)

Rector does not send content to a server and does not choose a storage format beyond the documented JSON contract. Persist documentData using your own storage layer.

Load another document ​

render() replaces the current editor document after validating and normalizing the supplied envelope.

js
editor.render({
  version: '2.0.0',
  blocks: [
    { id: 'loaded', type: 'paragraph', dataVersion: 2, data: { text: 'Loaded content' } },
  ],
})

Every serialized block must include its current dataVersion. Invalid known block data and unsupported document or schema versions are rejected before replacing the existing document. Valid records of unregistered types remain inert and can round-trip without executing extension code. See Document format and Configuration.

Clean up ​

Call destroy() before removing the holder or the view that contains it. Rector then removes its listeners, observers, popups, plugin instances, and injected plugin styles owned by this editor.

js
editor.destroy()

Calling destroy() again is safe. After destruction, isReady remains readable and returns false; every other property or operation throws so that use-after-destroy errors are visible.

Where to continue ​

  1. Read Architecture to understand the document, editor, plugin, and renderer boundaries.
  2. Choose options in Configuration.
  3. Read Inline tools and inline plugins before extending text behavior.
  4. Learn Commands and history before writing interactive plugins.
  5. Use Editor API for application integration.
  6. Open the block plugin catalog when selecting document features.
  7. Add a live outline with the Table of contents plugin.

Rector is released under the MIT License.