Type Editor / @type-editor/state / selection/NodeSelection / NodeSelection
Class: NodeSelection
Defined in: state/src/selection/NodeSelection.ts:19
A node selection is a selection that points at a single node. All nodes marked selectable can be the target of a node selection. In such a selection, from and to point directly before and after the selected node, anchor equals from, and head equals to.
Node selections are typically invisible and are used to select block-level elements like images, tables, or other atomic nodes that cannot be part of a text selection.
Extends
Implements
PmSelection
Constructors
Constructor
new NodeSelection($pos): NodeSelection;Defined in: state/src/selection/NodeSelection.ts:51
Create a node selection. Does not verify the validity of its argument - it's the caller's responsibility to ensure that the position points to a selectable node.
Parameters
| Parameter | Type | Description |
|---|---|---|
$pos | ResolvedPos | Position immediately before the node to select. The node should be accessible via $pos.nodeAfter. |
Returns
NodeSelection
Throws
If there is no node after the given position
Overrides
Properties
| Property | Modifier | Type | Default value | Description | Inherited from | Defined in |
|---|---|---|---|---|---|---|
anchorPos | readonly | ResolvedPos | undefined | The resolved position of the selection's anchor (the immobile end). | Selection.anchorPos | state/src/selection/Selection.ts:92 |
headPos | readonly | ResolvedPos | undefined | The resolved position of the selection's head (the mobile end). | Selection.headPos | state/src/selection/Selection.ts:98 |
isVisible | protected | boolean | true | Controls whether the selection should be visible in the browser. Some selection types (like node selections) may be invisible. | Selection.isVisible | state/src/selection/Selection.ts:105 |
selectionRanges | readonly | readonly PmSelectionRange[] | undefined | The array of selection ranges covered by this selection. Most selections have a single range, but some may span multiple ranges. | Selection.selectionRanges | state/src/selection/Selection.ts:86 |
Accessors
$anchor
Get Signature
get $anchor(): ResolvedPos;Defined in: state/src/selection/Selection.ts:167
The resolved anchor position of the selection. The anchor is the immobile end of the selection - it stays in place when the user extends the selection by moving the head.
Returns
ResolvedPos
The resolved anchor position
Implementation of
PmSelection.$anchor;Inherited from
$cursor
Get Signature
get $cursor(): ResolvedPos;Defined in: state/src/selection/Selection.ts:138
The cursor position if this is an empty text selection, null otherwise
Returns
ResolvedPos
Implementation of
PmSelection.$cursor;Inherited from
$from
Get Signature
get $from(): ResolvedPos;Defined in: state/src/selection/Selection.ts:234
The resolved lower bound of the selection's main range. This provides access to the document structure at the start of the selection, allowing you to query the context and perform position-based operations.
Throws
If the selection has no ranges
Returns
ResolvedPos
The resolved starting position
Implementation of
PmSelection.$from;Inherited from
$head
Get Signature
get $head(): ResolvedPos;Defined in: state/src/selection/Selection.ts:178
The resolved head position of the selection. The head is the mobile end of the selection - it moves when the user extends or modifies the selection.
Returns
ResolvedPos
The resolved head position
Implementation of
PmSelection.$head;Inherited from
$to
Get Signature
get $to(): ResolvedPos;Defined in: state/src/selection/Selection.ts:249
The resolved upper bound of the selection's main range. This provides access to the document structure at the end of the selection, allowing you to query the context and perform position-based operations.
Throws
If the selection has no ranges
Returns
ResolvedPos
The resolved ending position
Implementation of
PmSelection.$to;Inherited from
anchor
Get Signature
get anchor(): number;Defined in: state/src/selection/Selection.ts:189
The selection's anchor, as an unresolved position. This is the integer position in the document where the anchor is located. The anchor is the immobile end of the selection.
Returns
number
The anchor position as a number
Implementation of
PmSelection.anchor;Inherited from
empty
Get Signature
get empty(): boolean;Defined in: state/src/selection/Selection.ts:263
Indicates whether the selection contains any content. A selection is empty when all its ranges have identical from and to positions, which typically represents a cursor position rather than a content selection.
Returns
boolean
True if the selection is empty (cursor), false if it spans content
Implementation of
PmSelection.empty;Inherited from
from
Get Signature
get from(): number;Defined in: state/src/selection/Selection.ts:211
The lower bound of the selection's main range. This is always the smaller position value, regardless of which end is the anchor or head. For a cursor selection, this equals to.
Returns
number
The starting position of the selection
Implementation of
PmSelection.from;Inherited from
head
Get Signature
get head(): number;Defined in: state/src/selection/Selection.ts:200
The selection's head, as an unresolved position. This is the integer position in the document where the head is located. The head is the mobile end of the selection that moves when extending it.
Returns
number
The head position as a number
Implementation of
PmSelection.head;Inherited from
node
Get Signature
get node(): Node_2;Defined in: state/src/selection/NodeSelection.ts:78
The selected node. Provides direct access to the node object that is currently selected.
Returns
Node_2
The selected node
Implementation of
PmSelection.node;Overrides
ranges
Get Signature
get ranges(): readonly SelectionRange[];Defined in: state/src/selection/Selection.ts:156
The ranges covered by the selection. For most selections, this will be a single range. Multiple ranges are used for selections that span non-contiguous parts of the document.
Returns
readonly SelectionRange[]
A readonly array of selection ranges
Implementation of
PmSelection.ranges;Inherited from
to
Get Signature
get to(): number;Defined in: state/src/selection/Selection.ts:222
The upper bound of the selection's main range. This is always the larger position value, regardless of which end is the anchor or head. For a cursor selection, this equals from.
Returns
number
The ending position of the selection
Implementation of
PmSelection.to;Inherited from
type
Get Signature
get type(): string;Defined in: state/src/selection/NodeSelection.ts:68
The type identifier for this selection.
Returns
string
Always returns SelectionType.NODE
Implementation of
PmSelection.type;Overrides
visible
Get Signature
get visible(): boolean;Defined in: state/src/selection/Selection.ts:274
Controls whether the selection should be visible to the user in the browser. Most selections are visible (highlighted), but some selection types like node selections may choose to be invisible.
Returns
boolean
True if the selection should be visible, false otherwise
Set Signature
set visible(isVisible): void;Defined in: state/src/selection/Selection.ts:283
Sets whether the selection should be visible to the user in the browser.
Parameters
| Parameter | Type | Description |
|---|---|---|
isVisible | boolean | True to make the selection visible, false to hide it |
Returns
void
Implementation of
PmSelection.visible;Inherited from
Methods
content()
content(): Slice;Defined in: state/src/selection/NodeSelection.ts:104
Get the content of this selection as a slice. For node selections, this returns the selected node wrapped in a slice with zero open depth on both sides.
Returns
Slice
A slice containing only the selected node
Implementation of
PmSelection.content;Overrides
eq()
eq(other): boolean;Defined in: state/src/selection/NodeSelection.ts:116
Test whether this selection is equal to another selection. Node selections are equal if they select the same position (and thus the same node).
Parameters
| Parameter | Type | Description |
|---|---|---|
other | PmSelection | The selection to compare with |
Returns
boolean
True if both are node selections at the same anchor position
Implementation of
PmSelection.eq;Overrides
getBookmark()
getBookmark(): NodeBookmark;Defined in: state/src/selection/NodeSelection.ts:136
Create a bookmark for this selection. The bookmark can be used to restore this selection after document changes, by mapping the anchor position through those changes.
Returns
A NodeBookmark instance that can recreate this selection
Implementation of
PmSelection.getBookmark;Overrides
isAllSelection()
isAllSelection(): boolean;Defined in: state/src/selection/Selection.ts:590
Check if this is an all selection.
Returns
boolean
True if this is an AllSelection instance
Implementation of
PmSelection.isAllSelection;Inherited from
isNodeSelection()
isNodeSelection(): boolean;Defined in: state/src/selection/Selection.ts:581
Check if this is a node selection.
Returns
boolean
True if this is a NodeSelection instance
Implementation of
PmSelection.isNodeSelection;Inherited from
isTextSelection()
isTextSelection(): boolean;Defined in: state/src/selection/Selection.ts:572
Check if this is a text selection.
Returns
boolean
True if this is a TextSelection instance
Implementation of
PmSelection.isTextSelection;Inherited from
map()
map(doc, mapping): Selection;Defined in: state/src/selection/Selection.ts:640
Map this selection through a mappable transformation. This updates the selection to reflect changes made to the document, adjusting positions and potentially changing the selection type if the mapped positions are no longer valid for the current type.
For example, if a text selection's positions are mapped to non-inline content, this will find a nearby valid selection instead.
Parameters
| Parameter | Type | Description |
|---|---|---|
doc | Node_2 | The new document after the transformation |
mapping | Mappable | The mappable transformation (e.g., from a transaction) |
Returns
A new selection mapped to the new document
Implementation of
PmSelection.map;Inherited from
replace()
replace(transaction, content?): void;Defined in: state/src/selection/Selection.ts:672
Replace the selection with a slice or, if no slice is given, delete the selection. Will append to the given transaction.
Parameters
| Parameter | Type | Default value |
|---|---|---|
transaction | PmTransaction | undefined |
content | Slice | Slice.empty |
Returns
void
Implementation of
PmSelection.replace;Inherited from
replaceWith()
replaceWith(transaction, node): void;Defined in: state/src/selection/Selection.ts:754
Replace the selection with the given node, appending the changes to the given transaction.
For multi-range selections, the first range is replaced with the node, and subsequent ranges are deleted. The selection is then positioned after the inserted node.
Parameters
| Parameter | Type | Description |
|---|---|---|
transaction | PmTransaction | The transaction to append the replacement to |
node | Node_2 | The node to insert in place of the selection |
Returns
void
Implementation of
PmSelection.replaceWith;Inherited from
toJSON()
toJSON(): SelectionJSON;Defined in: state/src/selection/NodeSelection.ts:125
Convert this selection to a JSON-serializable representation.
Returns
SelectionJSON
A JSON object containing the type and anchor position
Implementation of
PmSelection.toJSON;Overrides
atEnd()
static atEnd(doc): Selection;Defined in: state/src/selection/Selection.ts:335
Find the cursor or leaf node selection closest to the end of the given document. Will return an AllSelection if no valid position exists.
This is commonly used to position the cursor at the end of a document.
Parameters
| Parameter | Type | Description |
|---|---|---|
doc | Node_2 | The document node to find a selection in |
Returns
A selection at the end of the document
Inherited from
atStart()
static atStart(doc): Selection;Defined in: state/src/selection/Selection.ts:320
Find the cursor or leaf node selection closest to the start of the given document. Will return an AllSelection if no valid position exists.
This is commonly used to position the cursor at the beginning of a document.
Parameters
| Parameter | Type | Description |
|---|---|---|
doc | Node_2 | The document node to find a selection in |
Returns
A selection at the start of the document
Inherited from
between()
static between(
$anchor,
$head,
bias?): Selection;Defined in: state/src/selection/Selection.ts:412
Parameters
| Parameter | Type |
|---|---|
$anchor | ResolvedPos |
$head | ResolvedPos |
bias? | number |
Returns
Inherited from
create()
Create a node selection. Overloaded to accept either a resolved position or a document with an integer position.
Param
Either a resolved position or a document node
Param
Optional integer position (required if first arg is a Node)
Call Signature
static create(position): NodeSelection;Defined in: state/src/selection/NodeSelection.ts:146
Create a node selection from a resolved position.
Parameters
| Parameter | Type | Description |
|---|---|---|
position | ResolvedPos | The resolved position immediately before the node to select |
Returns
NodeSelection
A new EditorSelection wrapping a NodeSelection
Call Signature
static create(node, position): NodeSelection;Defined in: state/src/selection/NodeSelection.ts:155
Create a node selection from a document and position offset.
Parameters
| Parameter | Type | Description |
|---|---|---|
node | Node_2 | The document node containing the selection |
position | number | The integer position immediately before the node to select |
Returns
NodeSelection
A new EditorSelection wrapping a NodeSelection
findFrom()
static findFrom(
$pos,
dir,
textOnly?): Selection;Defined in: state/src/selection/Selection.ts:354
Find a valid cursor or leaf node selection starting at the given position and searching in the specified direction. When textOnly is true, only consider cursor selections (no node selections). Will return null when no valid selection position is found.
This method searches both within the current parent node and up the document tree to find a valid selection position.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
$pos | ResolvedPos | undefined | The resolved position to start searching from |
dir | number | undefined | The search direction: negative for backward, positive for forward |
textOnly | boolean | false | If true, only return text/cursor selections (default: false) |
Returns
A valid selection, or null if none found
Inherited from
fromJSON()
static fromJSON(doc, json): NodeSelection;Defined in: state/src/selection/NodeSelection.ts:90
Deserialize a node selection from its JSON representation.
Parameters
| Parameter | Type | Description |
|---|---|---|
doc | Node_2 | The document node containing the selection |
json | SelectionJSON | The JSON representation with an anchor position |
Returns
NodeSelection
A new NodeSelection instance
Throws
If the JSON does not contain a valid anchor position
Overrides
isNodeSelectable()
static isNodeSelectable(node): boolean;Defined in: state/src/selection/Selection.ts:384
Determines whether the given node may be selected as a node selection. A node is selectable if it's not a text node and its type specification has not explicitly set selectable to false.
Parameters
| Parameter | Type | Description |
|---|---|---|
node | Node_2 | The node to check for selectability |
Returns
boolean
True if the node can be selected as a node selection, false otherwise
Inherited from
jsonID()
static jsonID(jsonId, jsonDeserializerClass): void;Defined in: state/src/selection/Selection.ts:72
Parameters
| Parameter | Type |
|---|---|
jsonId | string |
jsonDeserializerClass | JSONToSelectionDeserializer |
Returns
void
Inherited from
near()
static near($pos, bias?): Selection;Defined in: state/src/selection/Selection.ts:303
Find a valid cursor or leaf node selection near the given position. Searches in the direction specified by bias first, then tries the opposite direction if nothing is found. Falls back to an all-selection if no valid position exists.
This is useful for finding a valid selection position after document changes that may have invalidated the previous selection.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
$pos | ResolvedPos | undefined | The resolved position to search from |
bias | number | 1 | The search direction bias: 1 for forward (default), -1 for backward |
Returns
A valid selection near the given position, or an all-selection
Inherited from
registerAllSelectionHandler()
protected static registerAllSelectionHandler(handler): void;Defined in: state/src/selection/Selection.ts:68
Parameters
| Parameter | Type |
|---|---|
handler | (...param) => PmSelection |
Returns
void
Inherited from
Selection.registerAllSelectionHandler
registerJsonDeserializerClass()
static registerJsonDeserializerClass(jsonId, jsonDeserializerClass): void;Defined in: state/src/selection/Selection.ts:76
Parameters
| Parameter | Type |
|---|---|
jsonId | string |
jsonDeserializerClass | JSONToSelectionDeserializer |
Returns
void
Inherited from
Selection.registerJsonDeserializerClass
registerNodeSelectionHandler()
protected static registerNodeSelectionHandler(handler): void;Defined in: state/src/selection/Selection.ts:63
Parameters
| Parameter | Type |
|---|---|
handler | (...param) => PmSelection |
Returns
void
Inherited from
Selection.registerNodeSelectionHandler
registerTextSelectionHandler()
protected static registerTextSelectionHandler(handler): void;Defined in: state/src/selection/Selection.ts:58
Parameters
| Parameter | Type |
|---|---|
handler | (...param) => PmSelection |
Returns
void
Inherited from
Selection.registerTextSelectionHandler
textSelectionBetween()
static textSelectionBetween(
$anchor,
$head,
bias?): Selection;Defined in: state/src/selection/Selection.ts:432
Return a text selection that spans the given positions or, if they aren't text positions, find a text selection near them. bias determines whether the method searches forward (default) or backwards (negative number) first. Will fall back to calling Selection.near when the document doesn't contain a valid text position.
This method ensures that both anchor and head are positioned in inline content, adjusting them if necessary while maintaining the intended selection direction.
Parameters
| Parameter | Type | Description |
|---|---|---|
$anchor | ResolvedPos | The desired anchor position (may be adjusted if not in inline content) |
$head | ResolvedPos | The desired head position (may be adjusted if not in inline content) |
bias? | number | Optional search direction bias: positive for forward, negative for backward |
Returns
A text selection between valid positions