Commands – inksane
inksane
Introduction

Commands

Run one command at a time, or chain several into a single editor update.

Commands change editor content or editor state. Core document commands are available on every editor instance. Feature commands, such as toggleBold, are available only when their extensions are registered, individually or through BaseKit.

Every command returns a boolean. true means the command ran, false means it could not be applied, for example because the content already had the requested formatting. Commands that fail leave the document untouched.

To ask whether a command would run without running it, use editor.canRun. See Editor state.

Run a single command

editor.commands exposes every registered command, bound to the editor.

const inserted = editor.commands.insertContent({
  content: "Hello, inksane!",
  from: 0,
});

if (!inserted) {
  console.error("Could not insert content");
}

Commands that work on the current selection, such as toggleBold, can be called without arguments:

editor.commands.toggleBold();
editor.commands.focus();

Formatting commands accept a pos option to act on a specific position or range instead of the selection. A position is a number, a range is { from, to }, and both are 0-based document offsets.

editor.commands.setBold({ pos: { from: 0, to: 5 } });
editor.commands.toggleItalic({ pos: 12 });

Chain commands

Use editor.chain() to collect commands and run them together with .run().

const applied = editor.chain().setContent("# Draft").toggleBold().focus().run();

Nothing changes until .run() is called. At that point inksane evaluates the chain against the current state and dispatches its changes as one update:

  • Each command receives the state produced by the commands before it, so positions and selections stay correct as the chain progresses.
  • .run() returns true only when every command returned true.
  • A command that returns false does not stop the chain. The remaining commands still run.
  • A chain can only be run once. Create a new one to apply more commands.

Build the chain before running it when the commands are not known upfront. The editor state is read at .run(), not when editor.chain() is called.

const chain = editor.chain().setContent("# Draft").focus();

await saveDraft();

const applied = chain.run();

Command reference

pos is optional everywhere it appears. Without it, a formatting command uses the current selection.

Core

CommandDescription
clearContent()Clears the document.
setContent(content)Replaces the whole document.
insertContent({ content, from, to? })Inserts or replaces content at a position or range.
deleteContent(from, to)Removes a document range.
cutContent(fromRange, toRange)Moves content from one range to another.
focus()Focuses the editor and scrolls it into view.

Bold

CommandDescription
setBold({ content?, pos? })Wraps the range in **. With content, inserts **content**. At a bare cursor, inserts ****.
removeBold({ pos? })Removes the ** around the range.
toggleBold({ pos? })setBold or removeBold, depending on the range.

Italic

CommandDescription
setItalic({ content?, pos? })Wraps the range in *. With content, inserts *content*. At a bare cursor, inserts **.
removeItalic({ pos? })Removes the * around the range.
toggleItalic({ pos? })setItalic or removeItalic.

Strikethrough

CommandDescription
setStrikethrough({ pos? })Wraps the range in ~~, or inserts ~~~~ at a bare cursor.
removeStrikethrough({ pos? })Removes the ~~ around the range.
toggleStrikethrough({ pos? })setStrikethrough or removeStrikethrough.

Heading

CommandDescription
insertHeading({ level, pos? })Sets the line to a heading. level is 1 to 6.
toggleHeading({ level, pos? })Sets the level, or converts the heading back to a paragraph.
removeHeading({ pos? })Removes the heading marker from the line.

List

CommandDescription
toggleBulletList({ pos? })Adds or removes a - marker on the line.
toggleOrderedList({ pos? })Adds or removes a 1. marker on the line.

Task list

CommandDescription
insertTaskList({ checked?, pos? })Converts the line or selection into a task item.
removeTaskList({ pos? })Removes the task list syntax.
toggleTaskList({ pos? })insertTaskList or removeTaskList.

Blockquote

CommandDescription
insertBlockquote({ pos? })Prefixes the line with > .
removeBlockquote({ pos? })Removes the > prefix.
toggleBlockquote({ pos? })insertBlockquote or removeBlockquote.
CommandDescription
setLink({ url, content?, pos? })Wraps the range in a link. With content, uses it as the link text.
removeLink({ pos? })Replaces the link with its text.
toggleLink({ url, pos? })setLink or removeLink.

Image

CommandDescription
insertImage({ src, alt, pos? })Inserts ![alt](src). Returns false for a URL the editor rejects.

Code

CommandDescription
setCode({ content?, pos? })Wraps the range in backticks, or inserts `content` when given.
removeCode({ pos? })Removes the backticks.
toggleCode({ pos? })setCode or removeCode.
insertCodeBlock({ lang?, pos? })Wraps the range in a fenced code block, with an optional language.
removeCodeBlock({ pos? })Removes the fence and keeps the content.
toggleCodeBlock({ lang?, pos? })insertCodeBlock or removeCodeBlock.

Horizontal rule

CommandDescription
insertHorizontalRule({ pos? })Inserts --- on its own line, padded with blank lines when needed.

Extending the type

Commands are typed through module augmentation, so editor.commands and editor.chain() know about the commands an extension adds. The custom extensions page shows the full pattern.