SelectionState
SelectionState
is an Immutable
Record
that represents a selection range in the editor.
The most common use for the SelectionState
object is via EditorState.getSelection()
,
which provides the SelectionState
currently being rendered in the editor.
Keys and Offsets
A selection range has two points: an anchor and a focus. (Read more on MDN).
The native DOM approach represents each point as a Node/offset pair, where the offset
is a number corresponding either to a position within a Node's childNodes
or, if the
Node is a text node, a character offset within the text contents.
Since Draft maintains the contents of the editor using ContentBlock
objects,
we can use our own model to represent these points. Thus, selection points are
tracked as key/offset pairs, where the key
value is the key of the ContentBlock
where the point is positioned and the offset
value is the character offset
within the block.
Start/End vs. Anchor/Focus
The concept of anchor and focus is very useful when actually rendering a selection state in the browser, as it allows us to use forward and backward selection as needed. For editing operations, however, the direction of the selection doesn't matter. In this case, it is more appropriate to think in terms of start and end points.
The SelectionState
therefore exposes both anchor/focus values and
start/end values. When managing selection behavior, we recommend that
you work with anchor and focus values to maintain selection direction.
When managing content operations, however, we recommend that you use start
and end values.
For instance, when extracting a slice of text from a block based on a
SelectionState
, it is irrelevant whether the selection is backward:
Note that SelectionState
itself tracks only anchor and focus values.
Start and end values are derived.
Overview
Static Methods
Methods
- getStartKey()
- getStartOffset()
- getEndKey()
- getEndOffset()
- getAnchorKey()
- getAnchorOffset()
- getFocusKey()
- getFocusOffset()
- getIsBackward()
- getHasFocus()
- isCollapsed()
- hasEdgeWithin(blockKey, start, end)
- serialize()
Properties
Use Immutable Map API to set properties.
Example
const selectionState = SelectionState.createEmpty();const selectionStateWithNewFocusOffset = selection.set('focusOffset', 1);
Static Methods
createEmpty()
Create a SelectionState
object at the zero offset of the provided block key
and hasFocus
set to false.
Methods
getStartKey()
Returns the key of the block containing the start position of the selection range.
getStartOffset()
Returns the block-level character offset of the start position of the selection range.
getEndKey()
Returns the key of the block containing the end position of the selection range.
getEndOffset()
Returns the block-level character offset of the end position of the selection range.
getAnchorKey()
Returns the key of the block containing the anchor position of the selection range.
getAnchorOffset()
Returns the block-level character offset of the anchor position of the selection range.
getFocusKey()
Returns the key of the block containing the focus position of the selection range.
getFocusOffset()
Returns the block-level character offset of the focus position of the selection range.
getIsBackward()
Returns whether the focus position is before the anchor position in the document.
This must be derived from the key order of the active ContentState
, or if the selection
range is entirely within one block, a comparison of the anchor and focus offset values.
getHasFocus()
Returns whether the editor has focus.
isCollapsed()
Returns whether the selection range is collapsed, i.e. a caret. This is true when the anchor and focus keys are the same /and/ the anchor and focus offsets are the same.
hasEdgeWithin()
Returns whether the selection range has an edge that overlaps with the specified start/end range within a given block.
This is useful when setting DOM selection within a block after contents are rendered.
serialize()
Returns a serialized version of the SelectionState
. Useful for debugging.
Properties
Use Immutable Map API to set properties.
anchorKey
The block containing the anchor end of the selection range.
anchorOffset
The offset position of the anchor end of the selection range.
focusKey
The block containing the focus end of the selection range.
focusOffset
The offset position of the focus end of the selection range.
isBackward
If the anchor position is lower in the document than the focus position, the selection is backward. Note: The SelectionState
is an object with no knowledge of the ContentState
structure. Therefore, when updating SelectionState
values, you are responsible for updating isBackward
as well.
hasFocus
Whether the editor currently has focus.