The Command API provides a comprehensive interface for interacting with the editor. All commands are accessed through the editor.command property and follow a consistent naming convention:
Execute commands: Start with execute (e.g., executeBold())
Canvas Editor uses the Command pattern to encapsulate operations. The Command class delegates to CommandAdapt, which provides the actual implementation:
export class Command { constructor(adapt: CommandAdapt) { this.executeBold = adapt.bold.bind(adapt) this.executeItalic = adapt.italic.bind(adapt) // ... all other commands }}
This architecture protects internal editor context while exposing a clean, consistent API.
// Change font familyeditor.command.executeFont('Arial')// Change font sizeeditor.command.executeSize(18)// Increase sizeeditor.command.executeSizeAdd()// Decrease sizeeditor.command.executeSizeMinus()// Text coloreditor.command.executeColor('#ff0000')// Highlight/background coloreditor.command.executeHighlight('#ffff00')
import { RowFlex } from 'canvas-editor'// Alignmenteditor.command.executeRowFlex(RowFlex.LEFT)editor.command.executeRowFlex(RowFlex.CENTER)editor.command.executeRowFlex(RowFlex.RIGHT)editor.command.executeRowFlex(RowFlex.JUSTIFY)// Row margin/spacingeditor.command.executeRowMargin(20)
// Get paragraph at current selectionconst paragraph = editor.command.getRangeParagraph()// Get row at current selection const row = editor.command.getRangeRow()// Find keyword occurrencesconst ranges = editor.command.getKeywordRangeList('search term')// Get context for keywordconst keywordContext = editor.command.getKeywordContext('keyword')
// Get element by IDconst element = editor.command.getElementById({ id: 'element-123' })// Get element by concept IDconst element = editor.command.getElementById({ conceptId: 'table-1' })
// Searchconst matches = editor.command.executeSearch('search term')// Navigate to next matcheditor.command.executeSearchNavigateNext()// Navigate to previous matcheditor.command.executeSearchNavigatePre()// Get search navigation infoconst info = editor.command.getSearchNavigateInfo()// Returns: { index: number, count: number }// Replaceeditor.command.executeReplace('replacement text')
// Force re-rendereditor.command.executeForceUpdate()// Printeditor.command.executePrint()// Get word countconst wordCount = editor.command.getWordCount()// Get cursor positionconst position = editor.command.getCursorPosition()// Get container elementconst container = editor.command.getContainer()// Get optionsconst options = editor.command.getOptions()// Update optionseditor.command.executeUpdateOptions({ defaultSize: 18, defaultColor: '#333'})
Use TypeScript autocomplete on editor.command. to see all available commands. The Command class exports all methods with full type information.
Are commands undoable?
Most editing commands (formatting, insertion, deletion) are automatically added to the undo stack. Some commands like executeForceUpdate() or queries don’t affect history.