Editor: state, commands and lifetime
Task-oriented arguments, results, state shape and lifecycle effects.
Use the complete product to see these methods together. This page explains invocation semantics; Core API retains full source signatures.
Creation and state
new Editor(options) establishes schema, commands and create-time extensions. Common options are extensions, content, editable, element and mode. Do not pass browser DOM during SSR. Install an adapter before explicit mounting. Incompatible schema changes need a new session and migration rather than assuming setOptions({ extensions }) hot-recompiles the model.
const editor = new Editor({ extensions, content, editable: true })
const view = editor.mount({ element, mode: 'canvas', id: 'main' })
const pm = editor.state.pm
const { from, to } = pm.selection
const document = editor.state.doc
External editor.state is a QuireKit wrapper with native PM state at .pm. A raw command's contextual state is the shared-transaction PM facade, where state.selection is direct. Keep those contexts distinct.
See options and events for all creation, replacement and event fields, and selections for caret, range and node targets.
@quirekit/coreMITSerializable editor state owned by the core package.
export interface QuireEditorState {
readonly pm: PMEditorState;
readonly doc: PMNode;
readonly schema: Schema;
readonly extensions: readonly QuireAnyExtension[];
readonly storage: Readonly<Record<string, unknown>>;
}Reading and writing
| Method | Input → result | Effects and use |
|---|---|---|
getJSON() | none → canonical JSON | Serialize explicitly for persistence/download/conversion, not each input projection |
getHTML() | none → HTML | Uses installed serializers, not paginated Canvas pixels |
getText(options?) | text serialization options → string | Plain text with optional block separators and node serializers |
getMarkdown() | none → string | Uses installed Markdown conversion rules |
commands.setContent(content, options) | compatible content/options → boolean | Ordinary content replacement; does not replace all top-node attributes |
commands.replaceDocument(content, options) | complete document → boolean | Restore files including root attributes, reset selection to start, commit with import origin/non-history |
commands.insertContent(content) | text/HTML/JSON → boolean | Insert at current selection through command/history semantics |
isActive(name, attrs?) | Node/Mark name and conditions → boolean | Read formatting, not executability or persistence |
setEditable(value, emitUpdate?) | boolean → void | Update edit policy; may emit update by default; host permissions still need projection |
setOptions(options) | partial options → void | Update options, without assuming schema recompilation or content migration |
Choose invalid-content policy explicitly and validate independently before restoring. getHTML() requires a DOM document and cannot simply be assumed available on a server. Command handling, actual document change and persistence acknowledgement are separate results.
Three command modes
editor.commands.toggleBold()
editor.chain().focus().toggleBold().run()
editor.can().toggleBold()
editor.can().chain().toggleBold().run()
Custom raw commands use the supplied tr and dispatch guard. Dry runs must not perform networking, dialogs or real-state writes. A false result must not be displayed as success.
can() is not a permission check, and setEditable(false) does not block every programmatic command. A false run() does not automatically revert other steps in the chain; see commands and transactions.
@quirekit/coreMITTiptap-compatible props supplied to one raw command callback.
export type CommandProps = {
readonly editor: QuireEditor;
readonly tr: Transaction;
readonly commands: SingleCommands;
readonly can: () => CanCommands;
readonly chain: () => ChainedCommands;
readonly state: PMEditorState;
readonly view: QuireMountedEditorView;
readonly dispatch: ((transaction?: Transaction) => void) | undefined;
};Mounting and exit
| Method | Result | Lifetime |
|---|---|---|
mount({ element, mode, id?, editable? }) | mounted view | First is primary; later calls add views, read-only by default |
mount(element) | mounted view | Compatibility shorthand selects html, not native dom; tutorials use explicit options |
changeMode(mode, options?) | replacement primary view | Preserve Core content/history; register the mode first |
unmount() | void | Release every view while retaining Core state |
destroy() | void | Final destruction; no later commands or mounting |
@quirekit/coreMITOptions used to mount an editor view surface.
export interface QuireEditorMountOptions {
readonly element: HTMLElement;
readonly mode: QuireViewMode;
readonly id?: string;
readonly editable?: boolean | undefined;
readonly ownerDocument?: Document | undefined;
}@quirekit/coreMITOptions used to switch the currently mounted primary view mode.
export interface QuireEditorChangeModeOptions {
readonly id?: string;
readonly ownerDocument?: Document | undefined;
}Events
on('transaction', listener) observes accepted state. Use update for content, selectionUpdate for selection, and focus events only when consumed. Release host subscriptions using the same function reference with off. The reference makes one bounded control projection from transaction events and publishes saving state separately.
@quirekit/coreMITPayload emitted after a transaction is applied.
export interface QuireEditorTransactionEvent extends QuireEditorEventPayload {
readonly transaction: Transaction;
readonly appendedTransactions: Transaction[];
}See the complete session for dirty state and concurrent saving.