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