Skip to main content

Commands

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
// 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

CommandDescriptionValue
JustifyLeftAligns content to the left
JustifyCenterAligns content to the center
JustifyRightAligns content to the right
JustifyFullJustifies content (full width)
JustifyNoneRemoves alignment

Clipboard

CommandDescriptionValue
CutCuts selected content to clipboard
CopyCopies selected content to clipboard
PastePastes clipboard content

Content

CommandDescriptionValue
editor42CleanupCleans up the editor content (re-serializes)
insertImageInserts an image elementURL string
insertHorizontalRuleInserts a horizontal rule (<hr>)
insertTextInserts plain text at caret positiontext string
insertHTMLInserts HTML at caret positionHTML string
editor42InsertContentInserts content at caret positionHTML string
editor42SetContentSets the entire editor contentHTML string
editor42ReplaceContentReplaces content ({$selection} placeholder for current selection text)HTML string
editor42NewDocumentResets editor to new document (empty content or configured new_document_content)

Formatting

CommandDescriptionValue
BoldToggles bold formatting
ItalicToggles italic formatting
UnderlineToggles underline formatting
StrikethroughToggles strikethrough formatting
SuperscriptToggles superscript formatting
SubscriptToggles subscript formatting
ForeColorSets text foreground colorcolor string
HiliteColorSets text highlight/background colorcolor string
BackColorSets text background color (alias for HiliteColor)color string
FontNameSets font familyfont name string
FontSizeSets font sizefont size string
LineHeightSets line heightline height string
LangSets language{ code: string, customCode?: string }
RemoveFormatRemoves formatting from selection
editor42BlockQuoteToggles blockquote
FormatBlockSets block formatblock tag name (e.g. 'p', 'h1')
editor42ToggleFormatToggles a named formatformat name string

History

CommandDescriptionValue
editor42AddUndoLevelAdds an undo level
editor42EndUndoLevelSame as editor42AddUndoLevel
UndoUndoes the last action
RedoRedoes the last undone action

Indentation

CommandDescriptionValue
IndentIndents content
OutdentOutdents content
CommandDescriptionValue
unlinkRemoves link from selection
editor42InsertLinkInserts or updates a linkURL string or { href: string, ... }
createLinkSame as editor42InsertLinkURL string or { href: string, ... }

Lists

CommandDescriptionValue
InsertUnorderedListToggles unordered list
InsertOrderedListToggles ordered list

Block insertion

CommandDescriptionValue
InsertNewBlockBeforeInserts a new block before current block
InsertNewBlockAfterInserts a new block after current block

Newline

CommandDescriptionValue
insertParagraphInserts a new paragraph (Enter key behavior)
editor42InsertNewLineInserts a new line
InsertLineBreakInserts a line break (<br>)

Selection

CommandDescriptionValue
editor42SelectNodeDepthSelects parent node at specified depthdepth number
editor42SelectNodeSelects a specific DOM nodeDOM node
selectAllSelects all content in the editor

General

CommandDescriptionValue
editor42RemoveNodeRemoves a node from the DOM (preserving children)optional DOM node
editor42PrintOpens browser print dialog
editor42FocusFocuses the editoroptional boolean (true = skip DOM focus)
editor42ToggleVisualAidToggles 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.

CommandReturns true when
JustifyLeftContent is left-aligned
JustifyCenterContent is center-aligned
JustifyRightContent is right-aligned
JustifyFullContent is justified
BoldBold format is active
ItalicItalic format is active
UnderlineUnderline format is active
StrikethroughStrikethrough format is active
SuperscriptSuperscript format is active
SubscriptSubscript format is active
editor42BlockQuoteBlockquote format is active
OutdentOutdent is possible

Query value commands

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

CommandReturns
FontNameCurrent font name
FontSizeCurrent font size
LineHeightCurrent line height

Usage examples

Toggling formatting

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

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

Setting text color

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

Inserting content

// 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>');
// 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

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

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