Editor API
API reference for the Editor API.
The Editor API provides a set of helper functions for querying and manipulating the editor state.
Common Options
At
A location reference in the editor. Can be either a Location or a Node.
type At = TLocation | TNode
When a Node is passed, its path will be found using editor.api.findPath()
. This allows you to reference a location by either:
Example:
// Using a location
editor.api.nodes({ at: [0, 0] }) // Path location
editor.api.nodes({ at: { path: [0], offset: 0 } }) // Point location
editor.api.nodes({ at: { anchor: point1, focus: point2 } }) // Range location
// Using a node reference
const node = editor.children[0]
editor.api.nodes({ at: node }) // Will find node's path internally
Match
A predicate for matching nodes. The predicate can be either:
- A function that takes a
node
and itspath
and returns aboolean
- An object where each key-value pair must match the node's properties
- Values can be single values or arrays of values to match against
Example:
// Function predicate
editor.api.nodes({
match: (node) => node.type === 'p'
})
// Object predicate
editor.api.nodes({
match: { type: 'p' }
})
// Object predicate with multiple possible values
editor.api.nodes({
match: { type: ['p', 'h1'] }
})
QueryMode
Mode for querying nodes in a hierarchy.
'all'
(default): Return all matching nodes'highest'
: In a hierarchy of nodes, only return the highest-level matching nodes'lowest'
: In a hierarchy of nodes, only return the lowest-level matching nodes
Example:
// Given this structure:
// - blockquote (matches)
// - paragraph (matches)
// - text
// mode: 'all' returns both blockquote and paragraph
editor.api.nodes({ match: { type: ['blockquote', 'paragraph'] }, mode: 'all' })
// mode: 'highest' returns only blockquote
editor.api.nodes({ match: { type: ['blockquote', 'paragraph'] }, mode: 'highest' })
// mode: 'lowest' returns only paragraph
editor.api.nodes({ match: { type: ['blockquote', 'paragraph'] }, mode: 'lowest' })
QueryOptions
Common options for querying nodes in the editor.
- When true, matches only empty nodes
- When false, matches only non-empty nodes
- When true, matches all nodes with an id
- When string, matches nodes with that specific id
- Function:
(node, path) => boolean
- Object: Key-value pairs that should match the node
Where to start querying from. Defaults to current editor selection.
Match block nodes. When true, only matches block elements.
Match empty/non-empty nodes.
Match the node by id.
Custom function or object to match nodes.
Match text nodes. When true, matches only text nodes.
editor.api
above
Get the matching ancestor above a location in the document.
block
Get the block at a location or find the first block that matches options.
Blocks are typically top-level nodes, so this is a common way to retrieve the ancestor block.
editor.api.block() // Get block above selection
editor.api.block({ above: true }) // Get block above selection
editor.api.block({ at: [0, 0] }) // Get block at [0, 0]
editor.api.block({ at: [0, 0], above: true }) // Get block at [0]
editor.api.block({ highest: true }) // Get highest block at selection
Common query options for matching blocks.
The location to query at. Defaults to current selection.
Whether to ignore non-selectable nodes during traversal.
Whether to traverse in reverse order.
Whether to ensure the operation works universally across all nodes.
If true, get the block above the location. Ignored if at
is not a block path.
If true, get the highest block at the location (root-level block).
Query mode for matching blocks.
Whether to include void nodes in the search.
blocks
Returns all matching blocks.
Common query options for matching blocks.
The location to query at. Defaults to current selection.
Whether to ignore non-selectable nodes during traversal.
Whether to traverse in reverse order.
Whether to ensure the operation works universally across all nodes.
Query mode for matching blocks.
Whether to include void nodes in the search.
edgeBlocks
Returns the edge blocks above a location (default: selection).
Useful for retrieving the start and end block of a range.
Common query options for matching blocks.
The location to get edge blocks from. Defaults to current selection.
Whether to ignore non-selectable nodes during traversal.
Whether to traverse in reverse order.
Whether to ensure the operation works universally across all nodes.
Query mode for matching blocks.
Whether to include void nodes in the search.
first
Get the first node at a location.
fragment
Get the fragment at a location or selection.
getFragment
Returns the fragment at the current selection. Used when cutting or copying, as an example, to get the fragment at the current selection.
hasBlocks
Check if a node has block children.
hasInlines
Check if a node has inline and text children.
hasMark
Check if mark is active at selection.
hasPath
Check if a path exists in the editor.
hasTexts
Check if a node has text children.
isAt
Check if a location (point/range) is at a specific position.
// For ranges:
editor.api.isAt({ text: true }) // Check if range is in a single text node
editor.api.isAt({ block: true }) // Check if range is in a single block
editor.api.isAt({ blocks: true }) // Check if range is across multiple blocks
editor.api.isAt({ start: true }) // Check if range starts at block start
editor.api.isAt({ end: true }) // Check if range ends at block end
// For points:
editor.api.isAt({ word: true }) // Check relative to word boundaries
editor.api.isAt({ start: true }) // Check if at start
editor.api.isAt({ end: true }) // Check if at end
isCollapsed
Check if the selection is collapsed (start and end points are the same).
isEdge
Check if a point is an edge of a location.
isEditorEnd
Check if selection is at editor end.
isEmpty
Check if an element is empty, accounting for void nodes.
editor.api.isEmpty() // Check if editor is empty
editor.api.isEmpty(at) // Check if nodes at location are empty
editor.api.isEmpty(at, { after: true }) // Check if text after location is empty
editor.api.isEmpty(at, { block: true }) // Check if block above location is empty
isEnd
Check if a point is the end point of a location.
isExpanded
Check if the selection is expanded (start and end points are different).
isNormalizing
Check if the editor is currently normalizing after each operation.
isStart
Check if a point is the start point of a location.
isSelected
Check if a path is selected by the current selection.
leaf
Get the leaf text node at a location.
levels
Iterate through all levels at a location. This includes all ancestors up to the root editor node.
last
Get the last node at a location.
mark
Returns the selection mark value by key.
marks
Get the marks that would be added to text at the current selection.
next
Get the matching node in the branch of the document after a location.
'after'
: Start from point after current location'child'
: Start from the first child of current path
Common query options for matching nodes.
The location to start searching from. Defaults to current selection.
Query mode for matching nodes.
Whether to include void nodes in the search.
node
Get the node at a location or find the first node that matches options.
nodes
Iterate through all nodes in the editor that match the given options.
'all'
: Return all matching nodes'highest'
: Return highest-level matching nodes'lowest'
: Return lowest-level matching nodes
Common query options for matching nodes.
Where to start iterating. Defaults to editor selection.
Whether to ignore non-selectable nodes during traversal.
Whether to traverse in reverse order.
Whether to ensure the operation works universally across all nodes.
Whether to include void nodes in the search.
parent
Get the parent node of a location.
previous
Get the matching node in the branch of the document before a location.
'before'
: Start from point before current location'parent'
: Start from parent of current location
Common query options for matching nodes.
The location to start searching from. Defaults to current selection.
Query mode for matching nodes.
Whether to include void nodes in the search.
Whether to get the previous sibling node instead of any previous node.
prop
Get a property value from a list of nodes. Returns undefined
if the property value is not consistent across all nodes.
'all'
: Get property from all nodes'block'
: Get property from the first block node'text'
: Get property from the first text node
The list of nodes to get the property value from.
The property key to get from the nodes.
Default value to return if property is not found.
Custom function to extract property value from a node.
string
Get the text string content of a location.
void
Match a void node in the current branch of the editor.
Location
findPath
Find the path of a Slate node in the editor.
Common query options for finding nodes.
Whether to ignore non-selectable nodes during traversal.
Whether to traverse in reverse order.
Whether to ensure the operation works universally across all nodes.
Query mode for finding nodes.
Whether to include void nodes in the search.
path
Get the path of a location.
point
Get the start
or end
(default is start
) point of a location.
positions
Iterate through all possible point positions in the document.
'offset'
: Moves to the next offset Point'character'
: Moves to the next character'word'
: Moves to the position after the next word'line'
| 'block': Moves between block boundaries
Where to start iterating. Defaults to editor selection.
When true returns positions in reverse order.
Whether to include positions inside void nodes.
Whether to skip positions in non-selectable nodes.
nodesRange
Returns the range spanning the given node entries.
range
Create a range between two locations.
start
Get the start point of a location.
unhangRange
Convert a range into a non-hanging one.
A "hanging" range is one created by the browser's "triple-click" selection behavior. When triple-clicking a block, the browser selects from the start of that block to the start of the next block. The range thus "hangs over" into the next block. If unhangRange
is given such a range, it moves the end backwards until it's in a non-empty text node that precedes the hanging block.
Note that unhangRange
is designed for the specific purpose of fixing triple-clicked blocks, and therefore currently has a number of caveats:
- It does not modify the start of the range; only the end. For example, it does not "unhang" a selection that starts at the end of a previous block.
- It only does anything if the start block is fully selected. For example, it does not handle ranges created by double-clicking the end of a paragraph (which browsers treat by selecting from the end of that paragraph to the start of the next).
Element
elementReadOnly
Check if an element is read-only.
isBlock
Check if a value is a block Element
object.
isInline
Check if a value is an inline Element
object.
isSelectable
Check if a value is a selectable Element
object.
isVoid
Check if an element is void.
markableVoid
Check if an element is a markable void element.
Ref
pathRef
Create a mutable ref for a Path
.
pathRefs
Get the set of currently tracked path refs of the editor.
pointRef
Create a mutable ref for a Point
.
pointRefs
Get the set of currently tracked point refs of the editor.
rangeRef
Create a mutable ref for a Range
.
'forward'
: Resolve both points forward'backward'
: Resolve both points backward'outward'
: Resolve start backward and end forward'inward'
: Resolve start forward and end backwardnull
: Do not resolve to any position
The direction to resolve the ref when ambiguous:
rangeRefs
Get the set of currently tracked range refs of the editor.
DOM
findDocumentOrShadowRoot
Find the document or shadow root from the editor.
findEventRange
Get the target range from a DOM event.
findKey
Find a key for a Slate node. Returns an instance of Key
which looks like { id: string }
.
getWindow
Get the window object from the editor.
hasDOMNode
Check if a DOM node is within the editor.
hasEditableTarget
Check if a DOM target is editable.
hasRange
Check if the editor has a range.
hasSelectableTarget
Check if a DOM target is selectable.
hasTarget
Check if a DOM target exists.
isComposing
Check if the user is currently composing inside the editor.
isFocused
Check if the editor is focused.
isReadOnly
Check if the editor is in read-only mode.
toDOMNode
Find the native DOM element from a Slate node.
toDOMPoint
Find a native DOM selection point from a Slate point.
toDOMRange
Find a native DOM range from a Slate range.
toSlateNode
Find a Slate node from a native DOM element.
toSlatePoint
Find a Slate point from a DOM selection point.
toSlateRange
Find a Slate range from a DOM range.
Callback
onChange
Called when there is a change in the editor.
Core
getDirtyPaths
Get the paths that need to be normalized after an operation.
setNormalizing
Manually control the editor's normalizing state.
shouldMergeNodesRemovePrevNode
Determines whether to remove the previous node when merging nodes.
shouldNormalize
Controls whether the editor should normalize after an operation. Override this method to prevent normalizing in certain situations.
History
isMerging
Get the merge flag's current value.
isSaving
Get the saving flag's current value.
isSplittingOnce
Get the splitting flag's current value.
Utils
create.block
Default block factory for creating new block elements.
create.value
Default value factory for creating new editor values.