Using Commands
All commands are available througheditor.commands:
/home/daytona/workspace/source/packages/core/src/Editor.ts:232
Command Types
Tiptap provides three ways to execute commands:Single Commands
Execute one command immediately.
Chained Commands
Execute multiple commands in sequence.
Can Commands
Check if a command can be executed.
Single Commands
Single commands execute immediately and return a boolean indicating success:/home/daytona/workspace/source/packages/core/src/CommandManager.ts:28
Chained Commands
Chained commands batch multiple operations into a single transaction:true if all commands succeeded, false otherwise:
/home/daytona/workspace/source/packages/core/src/CommandManager.ts:59
Can Commands
Useeditor.can() to check if a command can be executed without actually executing it:
can() commands:
/home/daytona/workspace/source/packages/core/src/CommandManager.ts:95
Command Categories
Content Commands
(content: string | JSONContent, options?) => boolean
Replace the entire document.Source:
/home/daytona/workspace/source/packages/core/src/commands/setContent.ts:35(content: string | JSONContent, options?) => boolean
Insert content at the current cursor position.
(position: number | Range, content: string | JSONContent) => boolean
Insert content at a specific position.
(emitUpdate?: boolean) => boolean
Clear the entire document.
Selection Commands
(position?: 'start' | 'end' | number | boolean) => boolean
Focus the editor.
() => boolean
Remove focus from the editor.
(position: number | Range) => boolean
Set the text selection.
() => boolean
Select the entire document.
Node Commands
(typeOrName: string | NodeType, attributes?) => boolean
Replace the current node with a different type.
(typeOrName: string | NodeType, toggleTypeOrName: string | NodeType, attributes?) => boolean
Toggle between two node types.
(typeOrName: string | NodeType) => boolean
Delete a specific node.
(typeOrName: string | NodeType | MarkType, attributes: Record<string, any>) => boolean
Update attributes of the current node or mark.
Mark Commands
(typeOrName: string | MarkType, attributes?) => boolean
Apply a mark to the current selection.
(typeOrName: string | MarkType, attributes?, options?) => boolean
Toggle a mark on the current selection.Source:
/home/daytona/workspace/source/packages/core/src/commands/toggleMark.ts:39(typeOrName: string | MarkType, options?) => boolean
Remove a mark from the current selection.
() => boolean
Remove all marks from the current selection.
List Commands
(listTypeOrName: string | NodeType, itemTypeOrName: string | NodeType) => boolean
Toggle a list.
(typeOrName: string | NodeType, attributes?) => boolean
Wrap the current selection in a list.
(typeOrName: string | NodeType) => boolean
Lift a list item out of its parent list.
(typeOrName: string | NodeType) => boolean
Sink a list item into the previous list item.
Creating Custom Commands
You can create custom commands in your extensions:Command Props
Commands receive props that give you access to the editor state:/home/daytona/workspace/source/packages/core/src/CommandManager.ts:112
Real Example: setParagraph
Here’s the actual implementation of thesetParagraph command from the Paragraph extension:
/home/daytona/workspace/source/packages/extension-paragraph/src/paragraph.ts:109
Real Example: toggleBold
Here’s the implementation from the Bold extension:/home/daytona/workspace/source/packages/extension-bold/src/bold.tsx:106
Advanced Command Usage
Conditional Execution
Accessing State in Commands
Checking Command Success
Common Command Patterns
Toggle Formatting
Set Heading Level
Insert Link
Remove Link
Toggle List
TypeScript Types
Best Practices
Always Use Chains for Multiple Commands
Batch multiple commands into a single transaction for better performance and UX.
Check Before Executing
Use
can() to check if a command can run before executing it.Focus Before Editing
Always focus the editor before running content commands.
Return True on Success
Custom commands should return
true if successful, false otherwise.Related
Editor
Learn about the Editor class
Extensions
Learn how to create commands in extensions
Nodes & Marks
Learn about content structure
Schema
Learn about the ProseMirror schema