Docs
Slate Utils

Slate Utils

API reference for @udecode/slate-utils.

@udecode/slate-utils contains utility functions for Slate.

Queries

findDescendant

Iterates through all nodes in the editor and returns the first match. If no match is found, returns undefined.

Parameters

Collapse all

    The editor to search for the descendant node.

    The options to find the node. It can contain match condition, starting location, whether to traverse in reverse, and whether to include voids.

Returns

    Returns the first node entry matching the condition or undefined if no match is found.

getBlockAbove

Returns the block above a specified location

  • Default: Selection.

Parameters

Collapse all

    The editor to search for the block.

    The options to find the block above a location.

Returns

    Returns the block above the specified location.

getChildren

Returns the children node entries of a node entry.

Parameters

Collapse all

    The node entry to get its children.

Returns

    Returns an array of the children node entries.

getEdgeBlocksAbove

Returns the edge blocks above a specified location.

  • Default: Selection.

Parameters

Collapse all

    The editor to search for the edge blocks.

    The options to find the edge blocks above a location.

Returns

    Returns an array containing the start block and end block above the specified location, or null if not found.

getLastChild

Returns the last child of a node or null if no children.

Parameters

Collapse all

    The node entry to get its last child.

Returns

    Returns the last child of a node or null if no children.

getLastNodeByLevel

Retrieves the last node at a specified level in the editor.

Parameters

Collapse all

    The editor to search for the last node.

    The level at which to find the last node.

Returns

    Returns the last node entry at the specified level or undefined if no node is found.

getMark

Retrieves the value of the selection mark by key in the editor.

Parameters

Collapse all

    The editor where the selection mark is located.

    The key of the selection mark to retrieve.

Returns

    Returns the value of the selection mark or undefined if the editor or the mark is not found.

getNextNodeStartPoint

Retrieves the start point of the next node at a specified path in the editor.

Parameters

Collapse all

    The editor to search for the next node.

    The path at which to find the next node.

Returns

    Returns the start point of the next node or undefined if the next node is not found.

getNextSiblingNodes

Retrieves the sibling nodes following a specified path in the ancestor node.

Parameters

Collapse all

    The ancestor node of the sibling nodes.

    The path of the reference node.

Returns

    Returns an array of sibling nodes after the path or an empty array if no sibling nodes are found.

getOperations

Retrieves the operations of the editor.

Parameters

Collapse all

    The editor to get its operations.

Returns

    Returns the operations of the editor as TOperation array.

getPointBeforeLocation

Returns the point before a location, with additional options to customize the behavior. If no options are provided or neither match nor matchString are defined, it will default to using getPointBefore.

Parameters

Collapse all

    The editor where to find the point before a location.

    The location where to start.

    The options to define how the search is performed.

Returns

    Returns the point before a location. If not found, returns undefined.

getPointFromLocation

Returns the point from a location (Default: selection). If the location is a range, it gets the anchor point. If the location is a path, it gets the point at this path with offset 0.

Parameters

Collapse all

    The editor where to find the point.

Returns

    Returns the point from the location.

getPointNextToVoid

If the start point is inside an inline void, returns the point before or after it.

Parameters

Collapse all

    The editor where to find the point next to the void.

Returns

    Returns the point next to the void.

getPreviousBlockById

Finds the block before a block by ID. If not found, it finds the first block by ID and returns [null, its previous path].

Parameters

Collapse all

    The editor where to find the previous block.

    The ID of the block.

    The options for the node query.

Returns

    Returns the node entry of the previous block if found, otherwise undefined.

getPreviousNodeEndPoint

Gets the end point of the previous node.

Parameters

Collapse all

    The editor where to find the previous node's end point.

    The path where to start the search.

Returns

    Returns the end point of the previous node if found, otherwise undefined.

getPreviousPath

Generates the previous path based on the given path.

Parameters

Collapse all

    The current path.

Returns

    The previous path or undefined if there is no previous path.

getPreviousSiblingNode

Gets the previous sibling node from the given path.

Parameters

Collapse all

    The editor instance.

    The current path.

Returns

    An array containing the previous sibling node and its path, or undefined if there is no previous sibling.

getRangeBefore

Gets the range from the point before a given location to the end point of the location.

Parameters

Collapse all

    The editor instance.

    The location to consider.

    Options for getting the point before the location.

Returns

    The range from the point before the location to the end point of the location, or undefined if no such point exists.

getRangeFromBlockStart

Gets the range from the start of the block above a location to the location.

Parameters

Collapse all

    The editor instance.

    Options for getting the block above the location.

Returns

    The range from the start of the block above the location to the location, or undefined if no such block exists.

getSelectionText

Gets the selected text from the editor.

Parameters

Collapse all

    The editor instance.

Returns

    The selected text, or an empty string if no text is selected.

isAncestorEmpty

Checks whether an ancestor node is empty (has empty text and no inline children).

Parameters

Collapse all

    The editor instance.

    The ancestor node to check.

Returns

    True if the ancestor node is empty, false otherwise.

isBlockAboveEmpty

Checks whether the block above the selection is empty.

Parameters

Collapse all

    The editor instance.

Returns

    True if the block above the selection is empty, false otherwise.

isBlockTextEmptyAfterSelection

Checks whether the text in the block after the selection is empty.

Parameters

Collapse all

    The editor instance.

Returns

    True if the text in the block after the selection is empty, false otherwise.

isDocumentEnd

Checks whether the selection is at the end of the document.

Parameters

Collapse all

    The editor instance.

Returns

    True if the selection is at the end of the document, false otherwise.

isFirstChild

Checks whether a node is the first child of its parent.

Parameters

Collapse all

    The path of the node to check.

Returns

    True if the node is the first child of its parent, false otherwise.

isMarkActive

Checks whether a mark is active in the selection.

Parameters

Collapse all

    The editor instance.

    The mark key.

Returns

    True if the mark is active in the selection.

isPointAtWordEnd

Checks whether a point is at the end of a word.

Parameters

Collapse all

    The editor instance.

    The point to check.

Returns

    True if the point is at the end of a word, false otherwise.

isRangeAcrossBlocks

Determines whether the range (Default: selection) is across blocks.

Parameters

Collapse all

    The editor instance.

    The range to check. If not provided, the selection range is used.

Returns

    True if the range is across blocks, false if it's within a single block, or undefined if the block was not found.

isRangeInSameBlock

Determines whether the range is within the same block.

Parameters

Collapse all

    The editor instance.

    The range to check. If not provided, the selection range is used.

Returns

    True if the range is within the same block, false otherwise.

isRangeInSingleText

Checks whether a range is within a single text path.

Parameters

Collapse all

    The range to check.

Returns

    True if the range is within a single text path, false otherwise.

isSelectionAtBlockEnd

Checks whether the selection focus is at the end of its parent block.

Parameters

Collapse all

    The editor instance.

    The options object.

Returns

    True if the selection focus is at the end of its parent block, false otherwise.

isSelectionAtBlockStart

Checks whether the selection anchor or focus is at the start of its parent block.

Parameters

Collapse all

    The editor instance.

    The options object.

Returns

    True if the selection anchor or focus is at the start of its parent block, false otherwise.

isSelectionExpanded

Checks whether the selection is expanded.

Parameters

Collapse all

    The editor instance.

Returns

    True if the selection is expanded, false otherwise.

isTextByPath

Checks whether the node at a given path is a text node.

Parameters

Collapse all

    The editor instance.

    The path to the node to check.

Returns

    True if the node is a text node, false otherwise.

isWordAfterTrigger

Checks whether the word at a given point is after a trigger (punctuation character).

Parameters

Collapse all

    The editor instance.

    The point to check.

    The trigger character to check for.

Returns

    An object containing the range from the point before the start of the previous word to the given point, and a match if the text at the range starts with the trigger and ends with word characters.

queryEditor

Queries the editor state.

Parameters

Collapse all

    The editor instance.

    The options object, which can include a filter function, selectionAtBlockStart, selectionAtBlockEnd, allow, exclude, and at properties.

Returns

    True if the editor state matches the query, false otherwise.

Transforms

insertElements

Inserts nodes at a location in the document.

Parameters

Collapse all

    The editor instance.

    The nodes to insert.

    The options object.

insertEmptyElement

Inserts an empty element at a location in the document.

Parameters

Collapse all

    The editor instance.

    The type of the element to insert.

    The options object.

moveChildren

Moves the children of a node to a path.

Parameters

Collapse all

    The editor instance.

    The options object, which includes at, to, match, and fromStartIndex properties.

Returns

    The number of children moved.

removeMark

Removes a mark and triggers onChange if the selection is collapsed.

Parameters

Collapse all

    The editor instance.

    The options object, which includes key, at, and shouldChange properties.

removeNodeChildren

Removes all children of a node.

Parameters

Collapse all

    The editor instance.

    The path to the node to remove children from.

    The options object.

removeSelectionMark

Removes all marks from the selection.

Parameters

Collapse all

    The editor instance.

replaceNodeChildren

Replaces the children of a node: removes then inserts them.

Parameters

Collapse all

    The editor instance.

    The options object, which includes at, nodes, insertOptions, and removeOptions properties.

selectEndOfBlockAboveSelection

Selects the end point of the block above the selection.

Parameters

Collapse all

    The editor instance.

setMarks

Sets marks to selected text.

Parameters

Collapse all

    The editor instance.

    The marks to set.

    Marks to clear.

toggleMark

Adds or removes marks in the selection.

Parameters

Collapse all

    The editor instance.

    The options object, which includes key and clear properties.

toggleWrapNodes

Unwraps a node if the node type is in selection, otherwise wraps it.

Parameters

Collapse all

    The editor instance.

    The type of the node.

wrapNodeChildren

Wraps the children of a node into a single element.

Parameters

Collapse all

    The editor instance.

    The new parent element.

    The options object, which includes at property.

Utils

createDocumentNode

Creates a new document node.

Parameters

Collapse all

    The type of the node.

    • Default: 'p'

    The text for the node.

    • Default: an empty string.

    The remaining nodes.

    • Default: an empty array.

Returns

    An array of TDescendant nodes, starting with the newly created node.

createNode

Creates a new node.

Parameters

Collapse all

    The type of the node.

    • Default: 'p'

    The text for the node.

    • Default: an empty string.

Returns

    A new TElement node.