Overview
Node commands allow you to manipulate block-level elements (like paragraphs, headings, lists, etc.) in your ProseKit editor.
setBlockType()
Sets the selected text blocks to the given node type with the given attributes.
Function Signature
function setBlockType(options: SetBlockTypeOptions): Command
Parameters
options
SetBlockTypeOptions
required
options.type
NodeType | string
required
The node type to set. Can be a string name or a NodeType instance.
The attributes to set on the node
The start position in the document. Defaults to the start of the current selection.
The end position in the document. Defaults to the end of the current selection.
Return Value
A ProseMirror command that returns true if the block type was successfully set, false if the operation is not applicable.
Examples
Set Selection to Heading
import { setBlockType } from 'prosekit/core'
const command = setBlockType({
type: 'heading',
attrs: { level: 2 }
})
editor.exec(command)
Convert to Code Block
import { setBlockType } from 'prosekit/core'
const command = setBlockType({ type: 'codeBlock' })
editor.exec(command)
Set Block Type in Range
import { setBlockType } from 'prosekit/core'
// Convert all blocks from position 10 to 50 to paragraphs
const command = setBlockType({
type: 'paragraph',
from: 10,
to: 50
})
editor.exec(command)
toggleNode()
Toggles the selected text blocks between the given node type and the default block type.
Function Signature
function toggleNode(options: ToggleNodeOptions): Command
Parameters
options
ToggleNodeOptions
required
options.type
string | NodeType
required
The type of the node to toggle
The attributes of the node to toggle
Return Value
A ProseMirror command that returns true if the node was successfully toggled, false otherwise.
Examples
Toggle Heading
import { toggleNode } from 'prosekit/core'
// Toggle between heading and paragraph
const command = toggleNode({
type: 'heading',
attrs: { level: 1 }
})
editor.exec(command)
Toggle Code Block
import { toggleNode } from 'prosekit/core'
const command = toggleNode({ type: 'codeBlock' })
editor.exec(command)
insertNode()
Inserts a node at the current selection.
Function Signature
function insertNode(options: InsertNodeOptions): Command
Parameters
options
InsertNodeOptions
required
options.type
string | NodeType
required
The type of node to insert
The attributes for the node
Return Value
A ProseMirror command that returns true if the node was successfully inserted, false otherwise.
Example
import { insertNode } from 'prosekit/core'
// Insert a horizontal rule
const command = insertNode({ type: 'horizontalRule' })
editor.exec(command)
removeNode()
Removes a node from the document.
Function Signature
function removeNode(options: RemoveNodeOptions): Command
Parameters
options
RemoveNodeOptions
required
The position of the node to remove
Return Value
A ProseMirror command that returns true if the node was successfully removed, false otherwise.
Example
import { removeNode } from 'prosekit/core'
// Remove node at position 42
const command = removeNode({ pos: 42 })
editor.exec(command)
setNodeAttrs()
Sets attributes on a node.
Function Signature
function setNodeAttrs(options: SetNodeAttrsOptions): Command
Parameters
options
SetNodeAttrsOptions
required
Return Value
A ProseMirror command that returns true if the attributes were successfully set, false otherwise.
Example
import { setNodeAttrs } from 'prosekit/core'
// Change heading level
const command = setNodeAttrs({
pos: 10,
attrs: { level: 3 }
})
editor.exec(command)
setNodeAttrsBetween()
Sets attributes on all nodes of a specific type within a range.
Function Signature
function setNodeAttrsBetween(options: SetNodeAttrsBetweenOptions): Command
Parameters
options
SetNodeAttrsBetweenOptions
required
options.type
string | NodeType
required
The type of nodes to update
Return Value
A ProseMirror command that returns true if the attributes were successfully set, false otherwise.
Example
import { setNodeAttrsBetween } from 'prosekit/core'
// Set all headings in range to level 2
const command = setNodeAttrsBetween({
from: 10,
to: 100,
type: 'heading',
attrs: { level: 2 }
})
editor.exec(command)
wrap()
Wraps the current selection in a node.
Function Signature
function wrap(options: WrapOptions): Command
Parameters
options.type
string | NodeType
required
The type of node to wrap with
Attributes for the wrapping node
Return Value
A ProseMirror command that returns true if the selection was successfully wrapped, false otherwise.
Example
import { wrap } from 'prosekit/core'
// Wrap selection in a blockquote
const command = wrap({ type: 'blockquote' })
editor.exec(command)
toggleWrap()
Toggles wrapping the selection in a node.
Function Signature
function toggleWrap(options: ToggleWrapOptions): Command
Parameters
options
ToggleWrapOptions
required
options.type
string | NodeType
required
The type of node to toggle wrapping with
Attributes for the wrapping node
Return Value
A ProseMirror command that returns true if the operation succeeded, false otherwise.
Example
import { toggleWrap } from 'prosekit/core'
// Toggle blockquote wrapping
const command = toggleWrap({ type: 'blockquote' })
editor.exec(command)
unsetBlockType()
Converts the current block to the default block type.
Function Signature
function unsetBlockType(options: UnsetBlockTypeOptions): Command
Parameters
options
UnsetBlockTypeOptions
required
options.types
Array<string | NodeType>
required
The types to convert from
Return Value
A ProseMirror command that returns true if the block type was successfully reset, false otherwise.
Example
import { unsetBlockType } from 'prosekit/core'
// Convert headings and code blocks back to paragraphs
const command = unsetBlockType({
types: ['heading', 'codeBlock']
})
editor.exec(command)
insertDefaultBlock()
Inserts a default block node at the current selection.
Function Signature
function insertDefaultBlock(options?: InsertDefaultBlockOptions): Command
Parameters
options
InsertDefaultBlockOptions
Optional configuration (currently empty, reserved for future use)
Return Value
A ProseMirror command that returns true if the block was successfully inserted, false otherwise.
Example
import { insertDefaultBlock } from 'prosekit/core'
const command = insertDefaultBlock()
editor.exec(command)
Usage with Editor Commands
When you define nodes in your extension, ProseKit automatically generates command actions:
import { createEditor, union } from 'prosekit/core'
import { defineHeading, defineParagraph } from 'prosekit/extensions/node'
const editor = createEditor({
extension: union([
defineParagraph(),
defineHeading()
])
})
// Use generated command actions
editor.commands.setHeading?.({ level: 1 })
editor.commands.toggleHeading?.({ level: 2 })
// Check if command can execute
if (editor.commands.setHeading?.canExec({ level: 1 })) {
console.log('Can set heading')
}
Common Patterns
import { setBlockType } from 'prosekit/core'
function FormatMenu({ editor }) {
const formats = [
{ type: 'paragraph', label: 'Paragraph' },
{ type: 'heading', label: 'Heading 1', attrs: { level: 1 } },
{ type: 'heading', label: 'Heading 2', attrs: { level: 2 } },
{ type: 'codeBlock', label: 'Code Block' },
]
return (
<select onChange={(e) => {
const format = formats[e.target.selectedIndex]
const command = setBlockType({
type: format.type,
attrs: format.attrs
})
editor.exec(command)
}}>
{formats.map(f => <option key={f.label}>{f.label}</option>)}
</select>
)
}
Keyboard Shortcuts for Block Types
import { defineKeymap } from 'prosekit/core'
import { setBlockType } from 'prosekit/core'
const keymap = defineKeymap({
'Mod-Alt-1': () => setBlockType({ type: 'heading', attrs: { level: 1 } }),
'Mod-Alt-2': () => setBlockType({ type: 'heading', attrs: { level: 2 } }),
'Mod-Alt-0': () => setBlockType({ type: 'paragraph' }),
})
Conditional Node Operations
import { toggleWrap, setBlockType } from 'prosekit/core'
function handleQuoteToggle(editor) {
// First try to toggle blockquote wrap
const wrapCmd = toggleWrap({ type: 'blockquote' })
if (!editor.exec(wrapCmd)) {
// If that fails, try setting block type
const setCmd = setBlockType({ type: 'blockquote' })
editor.exec(setCmd)
}
}
See Also