# Headless Editor

> The ProseMirror engine behind RichTextEditor and BlockEditor - loaded on demand, usable without React, with typed commands, HTML in and out, and the extensions API for nodes, marks, plugins, keys, input rules, commands and toolbar items.

Canonical: https://salmonui.dev/docs/editors/headless-editor

`RichTextEditor` and `BlockEditor` share one engine module: the
ProseMirror schema of the shared HTML vocabulary, typed commands,
transactions, history, key bindings, input rules, the sanitizer and the
serializers. The engine ships in
`@salmonui/rich-text` and loads on demand, so it never reaches the server
bundle or the first paint.

### Live demo: Rich text extensions

Live demo: https://salmonui.dev/demos/editors/rich-text-extensions

One engine, two editors - a custom kbd mark and a shared review-stamp command added through the extensions API to RichTextEditor and BlockEditor, and the same engine run headless without React.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { BlockEditor, loadRichTextEngine, RICH_TEXT_DEFAULT_TOOLBAR, RichTextEditor, type Block, type RichTextExtension } from '@salmonui/rich-text';
import '@salmonui/rich-text/styles.css';
import { Button } from '@salmonui/buttons/button';

/** A new mark: <kbd> for key names, with a command, a key binding, a toolbar item and its HTML allow-list entry. */
const kbd: RichTextExtension = {
  name: 'kbd',
  marks: { kbd: { parseDOM: [{ tag: 'kbd' }], toDOM: () => ['kbd', 0] } },
  commands: {
    toggleKbd: () => (state, dispatch) => {
      const type = state.schema.marks['kbd'];
      const { from, to, empty } = state.selection;
      if (!type || empty) return false;
      if (dispatch) dispatch(state.doc.rangeHasMark(from, to, type) ? state.tr.removeMark(from, to, type) : state.tr.addMark(from, to, type.create()));
      return true;
    },
  },
  keyBindings: { 'Mod-Shift-k': 'toggleKbd' },
  toolbarItems: [{ id: 'kbd', label: 'Keyboard key', command: 'toggleKbd' }],
  html: { tags: ['kbd'] },
};

/** A command-only extension, shared by both editors: insert a review stamp. */
const stamp: RichTextExtension = {
  name: 'stamp',
  commands: {
    insertStamp: () => (state, dispatch) => {
      if (dispatch) dispatch(state.tr.insertText('[reviewed] ').scrollIntoView());
      return true;
    },
  },
  keyBindings: { 'Mod-Shift-y': 'insertStamp' },
  toolbarItems: [{ id: 'stamp', label: 'Review stamp', command: 'insertStamp' }],
};

const RTE_EXTENSIONS = [kbd, stamp];
const BLOCK_EXTENSIONS = [stamp];
const RTE_TOOLBAR = [...RICH_TEXT_DEFAULT_TOOLBAR.slice(0, 12), '|', 'kbd', 'stamp'];

const initialHtml = '<p>Press <kbd>Ctrl</kbd> + <kbd>S</kbd> to save. Select a word and press Ctrl+Shift+K (Cmd+Shift+K) to mark it as a key.</p>';
const initialBlocks: Block[] = [{ id: 'b1', type: 'paragraph', content: [{ type: 'text', text: 'The same stamp command runs here: Ctrl+Shift+Y (Cmd+Shift+Y), or select text and use the toolbar.' }] }];

export default function RichTextExtensionsDemo() {
  const [html, setHtml] = useState(initialHtml);
  const [headless, setHeadless] = useState<string | null>(null);
  return (
    <div style={{ display: 'grid', gridTemplateColumns: 'minmax(0, 1fr)', gap: 16 }}>
      <section aria-labelledby="ext-rte" style={{ display: 'grid', gridTemplateColumns: 'minmax(0, 1fr)', gap: 6 }}>
        <strong id="ext-rte">RichTextEditor with the kbd and stamp extensions</strong>
        <RichTextEditor aria-labelledby="ext-rte" defaultValue={initialHtml} onChange={setHtml} extensions={RTE_EXTENSIONS} toolbar={RTE_TOOLBAR} style={{ minBlockSize: 160 }} />
        <details>
          <summary>HTML value (the kbd elements survive the sanitizer through the extension&apos;s html entry)</summary>
          <pre style={{ whiteSpace: 'pre-wrap', fontSize: 12 }}>{html}</pre>
        </details>
      </section>
      <section aria-labelledby="ext-block" style={{ display: 'grid', gridTemplateColumns: 'minmax(0, 1fr)', gap: 6 }}>
        <strong id="ext-block">BlockEditor with the stamp extension</strong>
        <BlockEditor
          aria-labelledby="ext-block"
          defaultValue={initialBlocks}
          extensions={BLOCK_EXTENSIONS}
          inlineToolbar={['bold', 'italic', 'code', 'link', { id: 'stamp', label: 'Review stamp', onSelect: ({ actions }) => actions.runCommand('insertStamp') }]}
          // A frame, so the editable area reads as one box like the RichTextEditor above.
          style={{ minBlockSize: 120, paddingBlock: 8, paddingInlineEnd: 12, border: '1px solid var(--salmon-border-default)', borderRadius: 'var(--salmon-radius-lg)' }}
        />
      </section>
      <section aria-labelledby="ext-headless" style={{ display: 'grid', gap: 6, justifyItems: 'start' }}>
        <strong id="ext-headless">The engine without React</strong>
        <Button variant="outline"
          type="button"
          onClick={async () => {
            const { createEngine } = await loadRichTextEngine();
            const engine = await createEngine({ html: '<p>Escape</p>', extensions: RTE_EXTENSIONS });
            engine.commands.selectAll();
            engine.run('toggleKbd');
            engine.commands.bold();
            setHeadless(`${engine.getHtml()}  (text: "${engine.getText()}")`);
            engine.destroy();
          }}
        >
          createEngine, select all, toggleKbd, bold, getHtml()
        </Button>
        {headless === null ? null : <code>{headless}</code>}
      </section>
    </div>
  );
}
```

## Using the engine directly

```ts
import { loadRichTextEngine } from '@salmonui/rich-text';

const { createEngine } = await loadRichTextEngine();
const engine = await createEngine({ html: '<p>Hello</p>' });

engine.commands.selectAll();
engine.commands.bold();
engine.getHtml();           // '<p><strong>Hello</strong></p>'
engine.getText();           // 'Hello'
engine.setHtml('<h2>New</h2>');

const view = engine.mount(element);   // an EditorView on an existing element
const off = engine.subscribe((tr) => render(engine.state));
// ...
off();
engine.destroy();
```

The engine runs headless (in Node or a worker, commands apply to the state
directly) and mounts on an existing element, typically the server-rendered
content element. `createEngineSync` skips async extension plugins.

| Member | Does |
| --- | --- |
| `state`, `view`, `schema` | The ProseMirror state, the view once mounted, the schema |
| `commands` | Typed commands (`bold()`, `heading(2)`, `setLink({ href })`, `insertTable({ rows, cols })`, `undo()`, ...); each returns whether it applied |
| `run(id, ...args)` | A command, an alias (`heading2`, `alignCenter`), an extension command or a UI action by id |
| `getHtml({ xhtml? })`, `getText()`, `setHtml(html)` | HTML in and out, always sanitized |
| `getSelection()`, `setSelection(anchor, head?)` | Selection in document positions |
| `subscribe(listener)` | Called after every transaction |
| `insertFiles(files)` | Images, audio and video through the upload handler |
| `mount(element)`, `focus()`, `destroy()` | View lifecycle |

`RichTextEditor` exposes its engine as `actionsRef.current.engine`.

## Extensions

Both editors take `extensions`: plain objects that add to the
schema and the editing behavior. Keep the array stable.

```ts
import type { RichTextExtension } from '@salmonui/rich-text';
import { Plugin } from 'prosemirror-state';
import { toggleMark } from 'prosemirror-commands';

const highlight: RichTextExtension = {
  name: 'highlight',
  marks: {
    highlight: { parseDOM: [{ tag: 'mark' }], toDOM: () => ['mark', 0] },
  },
  commands: {
    toggleHighlight: () => (state, dispatch) => toggleMark(state.schema.marks.highlight!)(state, dispatch),
  },
  keyBindings: { 'Mod-Shift-h': 'toggleHighlight' },
  toolbarItems: [{ id: 'highlight', label: 'Highlight', command: 'toggleHighlight' }],
  plugins: ({ schema, getState, getView }) => [new Plugin({ /* ... */ })],
};

<RichTextEditor extensions={[highlight]} toolbar={[...RICH_TEXT_DEFAULT_TOOLBAR, '|', 'highlight']} />
```

| Field | Type | Notes |
| --- | --- | --- |
| `name` | `string` | Identifies the extension |
| `nodes`, `marks` | `Record<string, NodeSpec / MarkSpec>` | Added to the schema |
| `plugins` | `(context) => Plugin[] \| Promise<Plugin[]>` | ProseMirror plugins; the factory gets `schema`, `getState()` and `getView()`, and may be async (the editor waits) |
| `keyBindings` | `Record<string, string \| Command>` | Key to command id or ProseMirror command, before the built-in keys |
| `inputRules` | `(schema) => InputRule[]` | Typing shortcuts |
| `commands` | `Record<string, (...args) => Command>` | Run with `actionsRef.current.run(id, ...args)` or from a key or toolbar item |
| `toolbarItems` | `{ id, label, command, icon? }[]` | Items the `toolbar` prop can name |
| `html` | `{ tags?, attributes? }` | Elements and attributes the extension writes, added to the sanitizer allow-list for value in, paste and HTML out (never active content, `on*` handlers or `style`) |
| `replacesHistory`, `history` | `boolean`, `{ undo, redo }` | Replace the engine's undo history (the Yjs undo manager of `/collab` does) |

Content an extension adds still passes the sanitizer: the shared vocabulary
plus the extension's `html` entry survive `getHtml()` and paste. Script,
style, iframe, object, embed and form elements, `on*` handlers, `style` and
URL attributes other than `href`, `cite`, `src` and `poster` can never be
allowed, and those four are still checked by the URL rules. `BlockEditor`
stores marks in its block JSON, so an extension mark there needs a matching
block mark; command, key binding, plugin and toolbar extensions work in both
editors.

The example imports `prosemirror-state` and `prosemirror-commands`, which
`@salmonui/rich-text` already depends on; add them to your own
`dependencies` when you import them directly. Commands can also be written
against the `state` and `dispatch` they receive without any import, as in
the demo above.

React is the only framework binding: the editors are the binding, and the
engine is plain TypeScript for anything else.

## API reference

[`@salmonui/rich-text`](https://salmonui.dev/docs/reference/rich-text) lists
`RichTextExtension`, `RichTextEngine`, `loadRichTextEngine` and the
command types.
