Skip to main content

@lexical/selection

Variables

trimTextContentFromAnchor()

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

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

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

$addNodeStyle()

$addNodeStyle(node): void

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

Gets the TextNode's style object and adds the styles to the CSS.

Parameters

node

TextNode

The TextNode to add styles to.

Returns

void


$copyBlockFormatIndent()

$copyBlockFormatIndent(srcNode, destNode): void

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

Parameters

srcNode

ElementNode

destNode

ElementNode

Returns

void


$ensureForwardRangeSelection()

$ensureForwardRangeSelection(selection): void

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

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:329

Parameters

fn

(textNode) => void

Returns

void


$getComputedStyleForElement()

$getComputedStyleForElement(element): null | CSSStyleDeclaration

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

Gets the computed DOM styles of the element.

Parameters

element

ElementNode

Returns

null | CSSStyleDeclaration

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


$getComputedStyleForParent()

$getComputedStyleForParent(node): null | CSSStyleDeclaration

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

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

Parameters

node

LexicalNode

The node to check its parent's styles for.

Returns

null | CSSStyleDeclaration

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


$getSelectionStyleValueForProperty()

$getSelectionStyleValueForProperty(selection, styleProperty, defaultValue): string

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

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

The selection of TextNodes whose value to find.

RangeSelection | TableSelection

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.


$isAtNodeEnd()

$isAtNodeEnd(point): boolean

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

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:517

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:270

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:503

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

The distance to adjust the current selection.

"character" | "word" | "lineboundary"

Returns

void


$moveCharacter()

$moveCharacter(selection, isHoldingShift, isBackward): void

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

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:305

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:67

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

Type Parameters

T

T extends ElementNode

Parameters

selection

The selected blocks to be converted.

null | BaseSelection

$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:462

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(selection, textNode): LexicalNode

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

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

Parameters

selection

BaseSelection

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

textNode

TextNode

The TextNode to be edited.

Returns

LexicalNode

The updated TextNode.


$trimTextContentFromAnchor()

$trimTextContentFromAnchor(editor, anchor, delCount): void

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

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:162

Parameters

selection

BaseSelection

The selection of nodes to be wrapped.

createElement

() => ElementNode

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

wrappingElement

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

null | ElementNode

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): null | Range

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

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

null | Range

The range of selection for the DOM that was created.


createRectsFromDOMRange()

createRectsFromDOMRange(editor, range): ClientRect[]

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

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

Parameters

editor

LexicalEditor

The lexical editor

range

Range

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

Returns

ClientRect[]

The selectionRects as an array.


getCSSFromStyleObject()

getCSSFromStyleObject(styles): string

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

Gets the CSS styles from the style object.

Parameters

styles

Record<string, string>

The style object containing the styles to get.

Returns

string

A string containing the CSS styles and their values.


getStyleObjectFromCSS()

getStyleObjectFromCSS(css): Record<string, string>

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

Given a CSS string, returns an object from the style cache.

Parameters

css

string

The CSS property as a string.

Returns

Record<string, string>

The value of the given CSS property.

References

$cloneWithProperties

Re-exports $cloneWithProperties


$selectAll

Re-exports $selectAll