Skip to main content

@lexical/selection

Variables​

$addNodeStyle​

const $addNodeStyle: (_node) => void

Defined in: packages/lexical-selection/src/lexical-node.ts:250

Parameters​

_node​

TextNode

Returns​

void

Deprecated​

node styles are parsed on demand and not cached eternally


getStyleObjectFromCSS​

const getStyleObjectFromCSS: (css) => Record<string, string> = getStyleObjectFromCSS_

Defined in: packages/lexical-selection/src/index.ts:42

Parses inline CSS text into an object that is compatible with CSSStyleDeclaration.setProperty().

Property names are expected to be kebab-case, such as font-size, and values are expected to include explicit units where needed, such as 12px.

Parameters​

css​

string

Returns​

Record<string, string>

Deprecated​

moved to the lexical package


trimTextContentFromAnchor​

const trimTextContentFromAnchor: (editor, anchor, delCount) => void = $trimTextContentFromAnchor

Defined in: packages/lexical-selection/src/index.ts:44

Trims text from a node in order to shorten it, eg. to enforce a text's max length. If it deletes text that is an ancestor of the anchor then it will leave 2 indents, otherwise, if no text content exists, it deletes the TextNode. It will move the focus to either the end of any left over text or beginning of a new TextNode.

Parameters​

editor​

LexicalEditor

The lexical editor.

anchor​

Point

The anchor of the current selection, where the selection should be pointing.

delCount​

number

The amount of characters to delete. Useful as a dynamic variable eg. textContentSize - maxLength;

Returns​

void

Deprecated​

renamed to $trimTextContentFromAnchor by @lexical/eslint-plugin rules-of-lexical

Functions​

$copyBlockFormatIndent()​

$copyBlockFormatIndent(srcNode, destNode): void

Defined in: packages/lexical-selection/src/range-selection.ts:46

Parameters​

srcNode​

ElementNode

destNode​

ElementNode

Returns​

void


$ensureForwardRangeSelection()​

$ensureForwardRangeSelection(selection): void

Defined in: packages/lexical-selection/src/lexical-node.ts:434

Ensure that the given RangeSelection is not backwards. If it is backwards, then the anchor and focus points will be swapped in-place. Ensuring that the selection is a writable RangeSelection is the responsibility of the caller (e.g. in a read-only context you will want to clone $getSelection() before using this).

Parameters​

selection​

RangeSelection

a writable RangeSelection

Returns​

void


$forEachSelectedTextNode()​

$forEachSelectedTextNode(fn): void

Defined in: packages/lexical-selection/src/lexical-node.ts:359

Parameters​

fn​

(textNode) => void

Returns​

void


$getComputedStyleForElement()​

$getComputedStyleForElement(element): CSSStyleDeclaration | null

Defined in: packages/lexical-selection/src/utils.ts:208

Gets the computed DOM styles of the element.

Parameters​

element​

ElementNode

The node to check the styles for.

Returns​

CSSStyleDeclaration | null

the computed styles of the element or null if there is no DOM element or no default view for the document.


$getComputedStyleForParent()​

$getComputedStyleForParent(node): CSSStyleDeclaration | null

Defined in: packages/lexical-selection/src/utils.ts:229

Gets the computed DOM styles of the parent of the node.

Parameters​

node​

LexicalNode

The node to check its parent's styles for.

Returns​

CSSStyleDeclaration | null

the computed styles of the node, or null if the node has no parent, there is no DOM element, or there is no default view for the document.


$getSelectionStyleValueForProperty()​

$getSelectionStyleValueForProperty(selection, styleProperty, defaultValue?): string

Defined in: packages/lexical-selection/src/range-selection.ts:694

Returns the current value of a CSS property for TextNodes in the Selection, if set. If not set, it returns the defaultValue. If all TextNodes do not have the same value, it returns an empty string.

Parameters​

selection​

BaseSelection

The selection of TextNodes whose value to find.

styleProperty​

string

The CSS style property.

defaultValue?​

string = ''

The default value for the property, defaults to an empty string.

Returns​

string

The value of the property for the selected TextNodes.


$isAtEdgeOfElement()​

$isAtEdgeOfElement(point, element, direction): boolean

Defined in: packages/lexical-selection/src/range-selection.ts:74

Determine whether a point sits at the leading ('previous') or trailing ('next') edge of element's content — i.e. there is no content between the point and that edge of the element.

This is the caret-based generalization of $isAtNodeEnd. An empty element is considered to be at both of its edges. @lexical/utils re-exports this as the direction-specific $isAtStartOfNode / $isAtEndOfNode helpers.

Parameters​

point​

Point

The point to test.

element​

ElementNode

The ancestor element whose edge is tested.

direction​

CaretDirection

'previous' for the start of element, 'next' for the end.

Returns​

boolean


$isAtNodeEnd()​

$isAtNodeEnd(point): boolean

Defined in: packages/lexical-selection/src/lexical-node.ts:103

Determines if the current selection is at the end of the node.

Parameters​

point​

Point

The point of the selection to test.

Returns​

boolean

true if the provided point offset is in the last possible position, false otherwise.


$isParentElementRTL()​

$isParentElementRTL(selection): boolean

Defined in: packages/lexical-selection/src/range-selection.ts:619

Tests a parent element for right to left direction.

Parameters​

selection​

RangeSelection

The selection whose parent is to be tested.

Returns​

boolean

true if the selections' parent element has a direction of 'rtl' (right to left), false otherwise.


$isParentRTL()​

$isParentRTL(node): boolean

Defined in: packages/lexical-selection/src/utils.ts:245

Determines whether a node's parent is RTL.

Parameters​

node​

LexicalNode

The node to check whether it is RTL.

Returns​

boolean

whether the node is RTL.


$moveCaretSelection()​

$moveCaretSelection(selection, isHoldingShift, isBackward, granularity): void

Defined in: packages/lexical-selection/src/range-selection.ts:605

Moves the selection according to the arguments.

Parameters​

selection​

RangeSelection

The selected text or nodes.

isHoldingShift​

boolean

Is the shift key being held down during the operation.

isBackward​

boolean

Is the selection selected backwards (the focus comes before the anchor)?

granularity​

"character" | "word" | "lineboundary"

The distance to adjust the current selection.

Returns​

void


$moveCharacter()​

$moveCharacter(selection, isHoldingShift, isBackward): void

Defined in: packages/lexical-selection/src/range-selection.ts:630

Moves selection by character according to arguments.

Parameters​

selection​

RangeSelection

The selection of the characters to move.

isHoldingShift​

boolean

Is the shift key being held down during the operation.

isBackward​

boolean

Is the selection backward (the focus comes before the anchor)?

Returns​

void


$patchStyleText()​

$patchStyleText(selection, patch): void

Defined in: packages/lexical-selection/src/lexical-node.ts:311

Applies the provided styles to the TextNodes in the provided Selection. Will update partially selected TextNodes by splitting the TextNode and applying the styles to the appropriate one.

Parameters​

selection​

BaseSelection

The selected node(s) to update.

patch​

Record<string, string | null | ((currentStyleValue, target) => string)>

The patch to apply, which can include multiple styles. {CSSProperty: value} . Can also accept a function that returns the new property value.

Returns​

void


$setBlocksType()​

$setBlocksType<T>(selection, $createElement, $afterCreateElement?): void

Defined in: packages/lexical-selection/src/range-selection.ts:134

Converts all nodes in the selection that are of one block type to another.

Type Parameters​

T​

T extends ElementNode

Parameters​

selection​

BaseSelection | null

The selected blocks to be converted.

$createElement​

() => T

The function that creates the node. eg. $createParagraphNode.

$afterCreateElement?​

(prevNodeSrc, newNodeDest) => void

The function that updates the new node based on the previous one ($copyBlockFormatIndent by default)

Returns​

void


$shouldOverrideDefaultCharacterSelection()​

$shouldOverrideDefaultCharacterSelection(selection, isBackward): boolean

Defined in: packages/lexical-selection/src/range-selection.ts:550

Determines if the default character selection should be overridden. Used with DecoratorNodes

Parameters​

selection​

RangeSelection

The selection whose default character selection may need to be overridden.

isBackward​

boolean

Is the selection backwards (the focus comes before the anchor)?

Returns​

boolean

true if it should be overridden, false if not.


$sliceSelectedTextNodeContent()​

$sliceSelectedTextNodeContent<T>(selection, textNode, mutates?): T

Defined in: packages/lexical-selection/src/lexical-node.ts:44

Generally used to append text content to HTML and JSON. Grabs the text content and "slices" it to be generated into the new TextNode.

Type Parameters​

T​

T extends TextNode

Parameters​

selection​

BaseSelection

The selection containing the node whose TextNode is to be edited.

textNode​

T

The TextNode to be edited.

mutates?​

"clone" | "self"

'clone' to return a clone before mutating, 'self' to update in-place

Returns​

T

The updated TextNode or clone.


$trimTextContentFromAnchor()​

$trimTextContentFromAnchor(editor, anchor, delCount): void

Defined in: packages/lexical-selection/src/lexical-node.ts:124

Trims text from a node in order to shorten it, eg. to enforce a text's max length. If it deletes text that is an ancestor of the anchor then it will leave 2 indents, otherwise, if no text content exists, it deletes the TextNode. It will move the focus to either the end of any left over text or beginning of a new TextNode.

Parameters​

editor​

LexicalEditor

The lexical editor.

anchor​

Point

The anchor of the current selection, where the selection should be pointing.

delCount​

number

The amount of characters to delete. Useful as a dynamic variable eg. textContentSize - maxLength;

Returns​

void


$wrapNodes()​

$wrapNodes(selection, createElement, wrappingElement?): void

Defined in: packages/lexical-selection/src/range-selection.ts:229

Parameters​

selection​

BaseSelection

The selection of nodes to be wrapped.

createElement​

() => ElementNode

A function that creates the wrapping ElementNode. eg. $createParagraphNode.

wrappingElement?​

ElementNode | null

An element to append the wrapped selection and its children to.

Returns​

void

Deprecated​

In favor of $setBlockTypes Wraps all nodes in the selection into another node of the type returned by createElement.


createDOMRange()​

createDOMRange(editor, anchorNode, _anchorOffset, focusNode, _focusOffset): Range | null

Defined in: packages/lexical-selection/src/utils.ts:53

Creates a selection range for the DOM.

Parameters​

editor​

LexicalEditor

The lexical editor.

anchorNode​

LexicalNode

The anchor node of a selection.

_anchorOffset​

number

The amount of space offset from the anchor to the focus.

focusNode​

LexicalNode

The current focus.

_focusOffset​

number

The amount of space offset from the focus to the anchor.

Returns​

Range | null

The range of selection for the DOM that was created.


createRectsFromDOMRange()​

createRectsFromDOMRange(editor, range): DOMRect[]

Defined in: packages/lexical-selection/src/utils.ts:132

Creates DOMRects, generally used to help the editor find a specific location on the screen.

Parameters​

editor​

Pick<LexicalEditor, "getRootElement">

The lexical editor

range​

Range

A fragment of a document that can contain nodes and parts of text nodes.

Returns​

DOMRect[]

The selectionRects as an array.


getCSSFromStyleObject()​

getCSSFromStyleObject(styles): string

Defined in: packages/lexical-selection/src/utils.ts:191

Serializes a style object into a CSS declaration string, the inverse of getStyleObjectFromCSS.

Parameters​

styles​

Record<string, string>

An object mapping CSS property names to their values.

Returns​

string

A CSS string of the form prop: value; for each entry, concatenated together.

References​

$cloneWithProperties​

Re-exports $cloneWithProperties


$selectAll​

Re-exports $selectAll