createWritekitState
testedBuild the initial writekit state: normalize the document against the schema and ensure it has at least one editable block to place the caret in.
Signature
export function createWritekitState(options: CreateWritekitStateOptions): WritekitState{ ... }Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
options | CreateWritekitStateOptions | — | — |
Returns
WritekitState| Property | Type | Default | Description |
|---|---|---|---|
doc | WritekitDocument | — | — |
selection | Selection | — | — |
schema | Schema | — | — |
registry | Registry | — | — |
storedMarks | Marks | null | — | Marks to apply to the next typed character (toggle-before-type). |
Related Types
CommandView
Minimal view surface a command may use to move real DOM focus across blocks.
The Vue WritekitContext is structurally compatible; pure logic/tests can pass
a stub. Keeps the command layer free of any Vue/DOM dependency.
interface CommandView| Property | Type | Default | Description |
|---|---|---|---|
focusBlock | (blockId: string, offset: number | 'start' | 'end') => void | — | — |
Dispatch
Applies a transaction, updating writekit state and notifying subscribers.
export type Dispatch = (tr: Transaction) => void;Command
A command in the ProseMirror style: returns true when applicable (and
dispatches when dispatch is provided), false otherwise so the keymap can
fall through to native behavior. Called without dispatch it is a dry run for
computing UI enabled/active state.
export type Command = (state: WritekitState, dispatch?: Dispatch, view?: CommandView) => boolean;CommandFactory
A parameterized command constructor.
export type CommandFactory<Args extends readonly unknown[] = readonly unknown[]> = (...args: Args) => Command;WritekitState
Immutable snapshot of everything the writekit renders and commands read.
interface WritekitState| Property | Type | Default | Description |
|---|---|---|---|
docreadonly | WritekitDocument | — | — |
selectionreadonly | Selection | — | — |
schemareadonly | Schema | — | — |
registryreadonly | Registry | — | — |
storedMarksreadonly | Marks | null | — | Marks to apply to the next typed character (toggle-before-type). |
Step
The atomic, invertible, serializable unit of change. Steps are the contract shared by the undo history (each carries its exact inverse) and the CRDT adapter (each maps to a CRDT operation). Keeping the set small (~12) means a new block type never needs a new step.
export type Step
= | { readonly type: 'insertInline'; readonly blockId: string; readonly offset: number; readonly content: Inline }
| { readonly type: 'deleteText'; readonly blockId: string; readonly from: number; readonly to: number }
| { readonly type: 'replaceInline'; readonly blockId: string; readonly from: number; readonly to: number; readonly content: Inline }
| { readonly type: 'addMark'; readonly blockId: string; readonly from: number; readonly to: number; readonly mark: Mark }
| { readonly type: 'removeMark'; readonly blockId: string; readonly from: number; readonly to: number; readonly mark: Mark }
| { readonly type: 'setAttrs'; readonly blockId: string; readonly attrs: Attrs }
| { readonly type: 'setType'; readonly blockId: string; readonly blockType: string; readonly attrs: Attrs }
| { readonly type: 'splitBlock'; readonly blockId: string; readonly offset: number; readonly newId: string; readonly newType?: string; readonly newAttrs?: Attrs }
| { readonly type: 'mergeBlock'; readonly blockId: string; readonly intoId: string }
| { readonly type: 'insertBlock'; readonly node: Node; readonly index: number }
| { readonly type: 'removeBlock'; readonly blockId: string }
| { readonly type: 'moveBlock'; readonly blockId: string; readonly toIndex: number }
| { readonly type: 'setDoc'; readonly doc: WritekitDocument };HistoryEntry
One undoable change: the steps it applied, their inverses, and the selection
before and after. Undo replays inverted (reversed); redo replays steps.
interface HistoryEntry| Property | Type | Default | Description |
|---|---|---|---|
stepsreadonly | readonly Step[] | — | — |
invertedreadonly | readonly Step[] | — | — |
selectionBeforereadonly | Selection | — | — |
selectionAfterreadonly | Selection | — | — |
History
Undo/redo stacks of inverse-step entries. Borrows the ergonomics of stdlib's command history (bounded size, redo cleared on a new edit) but stores data (inverse steps) rather than closures — which is what makes it serializable and collab-friendly.
interface History| Property | Type | Default | Description |
|---|---|---|---|
record | (entry: HistoryEntry) => void | — | Record a new edit, clearing the redo stack. |
undo | () => HistoryEntry | undefined | — | Pop the latest undo entry (and push it onto the redo stack). |
redo | () => HistoryEntry | undefined | — | Pop the latest redo entry (and push it back onto the undo stack). |
canUndo | () => boolean | — | — |
canRedo | () => boolean | — | — |
clear | () => void | — | — |
WritekitEvents
Writekit event map. A type (not interface) so it satisfies the
Record<string, ...> constraint of PubSub.
interface WritekitEvents| Property | Type | Default | Description |
|---|---|---|---|
transaction | (tr: Transaction, next: WritekitState, prev: WritekitState) => void | — | Fired for every applied transaction (local, undo/redo, or remote). |
docChange | (next: WritekitState, prev: WritekitState) => void | — | Fired when the document changed. |
selectionChange | (next: WritekitState, prev: WritekitState) => void | — | Fired when the selection changed. |
Writekit
The headless writekit controller: owns live state, the undo history, and a typed event bus. The Vue layer wraps it; the CRDT adapter subscribes to it.
interface Writekit| Property | Type | Default | Description |
|---|---|---|---|
statereadonly | WritekitState | — | — |
dispatch | Dispatch | — | — |
command | (cmd: Command) => boolean | — | Run a command against the current state, dispatching if it applies. |
undo | () => boolean | — | — |
redo | () => boolean | — | — |
canUndo | () => boolean | — | — |
canRedo | () => boolean | — | — |
on | <K extends keyof WritekitEvents>(event: K, listener: WritekitEvents[K]) => void | — | — |
off | <K extends keyof WritekitEvents>(event: K, listener: WritekitEvents[K]) => void | — | — |
destroy | () => void | — | — |