# Commands

> All built-in commands for the Editor42 WYSIWYG editor — execute, query state, and query value for formatting, lists, tables, and more.

Source: https://editor42.com/api/commands  
Every page of editor42.com has a Markdown copy at its URL + `.md` (`index.md` after a URL that ends in `/`); all of them are in one file: https://editor42.com/llms-full.txt

Editor42 includes a command system for programmatically controlling the editor. Commands cover formatting, clipboard operations, content manipulation, undo/redo, and more.

There are three types of command calls:

- **`execCommand(name, showUI?, value?)`** — executes a command
- **`queryCommandState(name)`** — returns `true` if a toggle command is currently active
- **`queryCommandValue(name)`** — returns the current value for a command

```js
// Execute a command
editor42.activeEditor.execCommand('Bold');

// Execute a command with a value
editor42.activeEditor.execCommand('ForeColor', false, '#ff0000');

// Query whether bold is active at the caret position
const isBold = editor42.activeEditor.queryCommandState('Bold');

// Query the current font name at the caret position
const font = editor42.activeEditor.queryCommandValue('FontName');
```

## Exec commands

### Alignment

| Command | Description | Value |
|---|---|---|
| `JustifyLeft` | Aligns content to the left | — |
| `JustifyCenter` | Aligns content to the center | — |
| `JustifyRight` | Aligns content to the right | — |
| `JustifyFull` | Justifies content (full width) | — |
| `JustifyNone` | Removes alignment | — |

### Clipboard

| Command | Description | Value |
|---|---|---|
| `Cut` | Cuts selected content to clipboard | — |
| `Copy` | Copies selected content to clipboard | — |
| `Paste` | Pastes clipboard content | — |

### Content

| Command | Description | Value |
|---|---|---|
| `editor42Cleanup` | Cleans up the editor content (re-serializes) | — |
| `insertImage` | Inserts an image element | URL string |
| `insertHorizontalRule` | Inserts a horizontal rule (`<hr>`) | — |
| `insertText` | Inserts plain text at caret position | text string |
| `insertHTML` | Inserts HTML at caret position | HTML string |
| `editor42InsertContent` | Inserts content at caret position | HTML string |
| `editor42SetContent` | Sets the entire editor content | HTML string |
| `editor42ReplaceContent` | Replaces content (`{$selection}` placeholder for current selection text) | HTML string |
| `editor42NewDocument` | Resets editor to new document (empty content or configured `new_document_content`) | — |

### Formatting

| Command | Description | Value |
|---|---|---|
| `Bold` | Toggles bold formatting | — |
| `Italic` | Toggles italic formatting | — |
| `Underline` | Toggles underline formatting | — |
| `Strikethrough` | Toggles strikethrough formatting | — |
| `Superscript` | Toggles superscript formatting | — |
| `Subscript` | Toggles subscript formatting | — |
| `ForeColor` | Sets text foreground color | color string |
| `HiliteColor` | Sets text highlight/background color | color string |
| `BackColor` | Sets text background color (alias for `HiliteColor`) | color string |
| `FontName` | Sets font family | font name string |
| `FontSize` | Sets font size | font size string |
| `LineHeight` | Sets line height | line height string |
| `Lang` | Sets language | `{ code: string, customCode?: string }` |
| `RemoveFormat` | Removes formatting from selection | — |
| `editor42BlockQuote` | Toggles blockquote | — |
| `FormatBlock` | Sets block format | block tag name (e.g. `p`, `h1`) |
| `editor42ToggleFormat` | Toggles a named format | format name string |

### History

| Command | Description | Value |
|---|---|---|
| `editor42AddUndoLevel` | Adds an undo level | — |
| `editor42EndUndoLevel` | Same as `editor42AddUndoLevel` | — |
| `Undo` | Undoes the last action | — |
| `Redo` | Redoes the last undone action | — |

### Indentation

| Command | Description | Value |
|---|---|---|
| `Indent` | Indents content | — |
| `Outdent` | Outdents content | — |

### Links

| Command | Description | Value |
|---|---|---|
| `unlink` | Removes link from selection | — |
| `editor42InsertLink` | Inserts or updates a link | URL string or `{ href: string, ... }` |
| `createLink` | Same as `editor42InsertLink` | URL string or `{ href: string, ... }` |

### Lists

| Command | Description | Value |
|---|---|---|
| `InsertUnorderedList` | Toggles unordered list | — |
| `InsertOrderedList` | Toggles ordered list | — |

### Block insertion

| Command | Description | Value |
|---|---|---|
| `InsertNewBlockBefore` | Inserts a new block before current block | — |
| `InsertNewBlockAfter` | Inserts a new block after current block | — |

### Newline

| Command | Description | Value |
|---|---|---|
| `insertParagraph` | Inserts a new paragraph (Enter key behavior) | — |
| `editor42InsertNewLine` | Inserts a new line | — |
| `InsertLineBreak` | Inserts a line break (`<br>`) | — |

### Selection

| Command | Description | Value |
|---|---|---|
| `editor42SelectNodeDepth` | Selects parent node at specified depth | depth number |
| `editor42SelectNode` | Selects a specific DOM node | DOM node |
| `selectAll` | Selects all content in the editor | — |

### General

| Command | Description | Value |
|---|---|---|
| `editor42RemoveNode` | Removes a node from the DOM (preserving children) | optional DOM node |
| `editor42Print` | Opens browser print dialog | — |
| `editor42Focus` | Focuses the editor | optional boolean (`true` = skip DOM focus) |
| `editor42ToggleVisualAid` | Toggles visual aids (table borders, anchors, etc.) | — |

## Query state commands

These commands can be used with `queryCommandState()` to check whether a format or state is currently active. Returns `true` when active.

| Command | Returns true when |
|---|---|
| `JustifyLeft` | Content is left-aligned |
| `JustifyCenter` | Content is center-aligned |
| `JustifyRight` | Content is right-aligned |
| `JustifyFull` | Content is justified |
| `Bold` | Bold format is active |
| `Italic` | Italic format is active |
| `Underline` | Underline format is active |
| `Strikethrough` | Strikethrough format is active |
| `Superscript` | Superscript format is active |
| `Subscript` | Subscript format is active |
| `editor42BlockQuote` | Blockquote format is active |
| `Outdent` | Outdent is possible |

## Query value commands

These commands can be used with `queryCommandValue()` to retrieve the current value of a format at the caret position.

| Command | Returns |
|---|---|
| `FontName` | Current font name |
| `FontSize` | Current font size |
| `LineHeight` | Current line height |

## Usage examples

### Toggling formatting

```js
// Toggle bold on the current selection
editor42.activeEditor.execCommand('Bold');

// Toggle italic
editor42.activeEditor.execCommand('Italic');
```

### Setting text color

```js
editor42.activeEditor.execCommand('ForeColor', false, '#e03e2d');
editor42.activeEditor.execCommand('HiliteColor', false, '#fbeeb8');
```

### Inserting content

```js
// Insert HTML at the caret
editor42.activeEditor.execCommand('editor42InsertContent', false, '<strong>Hello</strong>');

// Replace the entire editor content
editor42.activeEditor.execCommand('editor42SetContent', false, '<p>New content</p>');
```

### Working with links

```js
// Insert a link
editor42.activeEditor.execCommand('editor42InsertLink', false, 'https://example.com');

// Insert a link with attributes
editor42.activeEditor.execCommand('editor42InsertLink', false, {
  href: 'https://example.com',
  target: '_blank',
  title: 'Example'
});

// Remove a link
editor42.activeEditor.execCommand('unlink');
```

### Checking format state

```js
if (editor42.activeEditor.queryCommandState('Bold')) {
  console.log('Bold is active');
}

const currentFont = editor42.activeEditor.queryCommandValue('FontName');
console.log('Current font:', currentFont);
```
