Selectors API Overview
Selectors are pure functions that derive state from the editor snapshot. They provide a clean way to access editor state without directly parsing the snapshot structure.
import * as selectors from '@portabletext/editor/selectors'Selectors are commonly used for creating behaviors and toolbar components.
Condition Selectors
Section titled “Condition Selectors”Condition selectors return a boolean or truthy value based on the current editor state.
Active State Conditions
Section titled “Active State Conditions”isActiveAnnotation(name)- Returnstrueif the specified annotation is active in the selectionisActiveDecorator(name)- Returnstrueif the specified decorator is active in the selectionisActiveListItem(name)- Returnstrueif the specified list type is activeisActiveStyle(name)- Returnstrueif the specified style is active
Selection Conditions
Section titled “Selection Conditions”isSelectionCollapsed- Returnstrueif the selection is collapsed (cursor with no range)isSelectionExpanded- Returnstrueif the selection spans multiple characters
Position Conditions
Section titled “Position Conditions”isAtTheStartOfBlock- Returnstrueif the cursor is at the start of a blockisAtTheEndOfBlock- Returnstrueif the cursor is at the end of a blockisSelectingEntireBlocks- Returnstrueif entire blocks are selected
Range Conditions
Section titled “Range Conditions”isOverlappingSelection- Returnstrueif selections overlapisPointBeforeSelection- Returnstrueif a point is before the current selectionisPointAfterSelection- Returnstrueif a point is after the current selection
Block Selectors
Section titled “Block Selectors”Retrieve block-level elements from the editor.
Navigation
Section titled “Navigation”getFirstBlock- Get the first block in the editorgetLastBlock- Get the last block in the editorgetNextBlock- Get the block after the current focus blockgetPreviousBlock- Get the block before the current focus block
Focus & Anchor
Section titled “Focus & Anchor”getAnchorBlock- Get the block where the selection anchor is locatedgetFocusBlock- Get the block where the selection focus is located
Selection Range
Section titled “Selection Range”getSelectedBlocks- Get all blocks within the current selectiongetSelectionStartBlock- Get the block at the start of the selectiongetSelectionEndBlock- Get the block at the end of the selection
Text Block Selectors
Section titled “Text Block Selectors”Retrieve text blocks specifically (excludes block objects).
getAnchorTextBlock- Get the text block where the selection anchor is locatedgetFocusTextBlock- Get the text block where the selection focus is locatedgetSelectedTextBlocks- Get all text blocks within the current selectiongetFocusListBlock- Get the list block where focus is located (if in a list)
Span Selectors
Section titled “Span Selectors”Retrieve span elements (text segments within blocks).
getAnchorSpan- Get the span where the selection anchor is locatedgetFocusSpan- Get the span where the selection focus is locatedgetNextSpan- Get the span after the current focus spangetPreviousSpan- Get the span before the current focus spangetSelectedSpans- Get all spans within the current selection
Child Selectors
Section titled “Child Selectors”Retrieve child elements within blocks.
getAnchorChild- Get the child element where the selection anchor is locatedgetFocusChild- Get the child element where the selection focus is locatedgetSelectionStartChild- Get the child at the start of the selectiongetSelectionEndChild- Get the child at the end of the selection
Inline Object Selectors
Section titled “Inline Object Selectors”Retrieve inline objects (custom elements embedded in text).
getFocusInlineObject- Get the inline object at the current focus positiongetNextInlineObject- Get the next inline object after focusgetNextInlineObjects- Get all inline objects after focusgetPreviousInlineObject- Get the previous inline object before focusgetPreviousInlineObjects- Get all inline objects before focus
Block Object Selectors
Section titled “Block Object Selectors”Retrieve block objects (custom block-level elements).
getFocusBlockObject- Get the block object at the current focus position
Selection Selectors
Section titled “Selection Selectors”Get information about the current selection.
getSelection- Get the current selection (anchor and focus points)getSelectionText- Get the text content of the current selectiongetSelectedValue- Get the Portable Text value of the selected contentgetSelectionStartPoint- Get the starting point of the selectiongetSelectionEndPoint- Get the ending point of the selectiongetCaretWordSelection- Get the word selection at the caret position
Active State Selectors
Section titled “Active State Selectors”Get information about active formatting and marks.
getActiveAnnotations- Get all active annotations in the selectiongetActiveListItem- Get the active list item typegetActiveStyle- Get the active block stylegetMarkState- Get comprehensive mark state information for the selection
Text Position Selectors
Section titled “Text Position Selectors”Get text content relative to the cursor position.
getBlockTextBefore- Get text in the current block before the cursorgetBlockTextAfter- Get text in the current block after the cursorgetBlockOffsets- Get character offsets within the current block
Value Selectors
Section titled “Value Selectors”Access the editor value.
getValue- Get the complete Portable Text value from the editor