

Selection and history are part of the editor state and available as signals. Selection is expressed in model coordinates, so it survives re-rendering and can be set from code.
A selection has an anchor, where it started, and a focus, where it ends. Both are block id and offset pairs; the focus can come first for backward selections. A collapsed selection, with equal points, is the caret.
const [first] = editor.document().blocks;
const last = editor.document().blocks.at(-1)!;
// select everything
editor.setSelection({
anchor: { blockId: first.id, offset: 0 },
focus: { blockId: last.id, offset: getNgsHeadlessEditorBlockText(last).length }
});
surface.focus();
// caret after the third character of the first block
editor.setSelection({
anchor: { blockId: first.id, offset: 3 },
focus: { blockId: first.id, offset: 3 }
});selectionchange, focus, and before input.setSelection() clamps offsets to the block text and stores the selection in the model. Call the surface focus() to apply it to the DOM.mousedown so the DOM selection is not lost; ngsHeadlessEditorCommand does this for you.| Method | Description |
|---|---|
insertText(text, origin?) | Replaces the selection with text; line breaks create paragraphs. |
deleteBackward(), deleteForward() | Delete the selection, one character, or merge blocks at a boundary. |
deleteRange(selection, origin, group?) | Deletes an explicit range. |
splitBlock() | Splits the block at the caret. |
clear() | Replaces the content with an empty paragraph and clears history. |
Every change records a snapshot of the previous document and selection. Snapshots share unchanged blocks with the live document, so history is cheap even for long documents.
NGS_HEADLESS_EDITOR_HISTORY_LIMIT); the grouping delay is NGS_HEADLESS_EDITOR_HISTORY_GROUP_DELAY.editor.canUndo(); // signal
editor.undo();
editor.redo();
// Load content without clearing the undo stack, so the load can be undone.
editor.setDocument(serverDocument, false);
// Replace content and clear history.
editor.clear();revision increases with every document change. origin tells where the last change came from:
| Origin | Source |
|---|---|
external | setDocument(): content loaded by the application. |
keyboard | Typing, deleting, and Enter in the surface. |
paste | Plain-text paste. |
composition | IME composition, drag and drop, and other DOM read-backs. |
command | Formatting and block operations. |
history | Undo and redo. |
api | Your own calls that pass 'api', and clear(). |
Use it to avoid writing loaded documents straight back to where they came from.