Position and Selection work together to represent cursor location and text ranges.
Position
APosition represents a specific point in the document, defined by a path and an offset.
Structure
Creating Positions
Position Properties
Copying Positions
Serialization
Selection
ASelection represents a range in the document, from a start position to an end position.
Structure
Selection Direction
Selections are directional:- Forward: End position is after start position
- Backward: End position is before start position
- Collapsed: Start and end positions are identical (cursor)
Creating Selections
From Start and End Positions
Single Node Selection
Collapsed Selection (Cursor)
Invalid Selection
Selection Properties
Normalized Selection
Normalized selections always go forward (start before end):Selection Indices
Get offsets in the normalized order:Reversing Selection
Collapsing Selection
Convert a range selection to a cursor:Shifting Selection
Move both positions by an offset:Copying Selections
Working with Selection in EditorState
Getting Current Selection
Setting Selection
Selection Update Reasons
Listening to Selection Changes
Getting Nodes in Selection
Get All Nodes in Selection
Get Selected Nodes
- Partial text selections (slices deltas)
- Nested node structures
- Parent-child relationships
Selection Rectangles
Get visual bounds of the selection:Selection Types
EditorState:
Practical Examples
Select Entire Node
Move Cursor to End of Node
Select Multiple Nodes
Check if Position is in Selection
Extend Selection
Select Word at Cursor
Remote Selection
For collaborative editing, track remote users’ selections:Selection Extra Info
Attach metadata to selections:Best Practices
Normalize Selections
Use
normalized when you need consistent start/end order.Check for Null
Always check if
editorState.selection is null before using.Use Reasons
Provide
SelectionUpdateReason for better debugging and event handling.Validate Positions
Ensure positions are within valid node bounds.
See Also
- EditorState - Managing selection through EditorState
- Transactions - How transactions update selection
- Document Structure - Understanding paths and nodes