Skip to main content

Module: @lexical/selection

Functions

$addNodeStyle

$addNodeStyle(node): void

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

Parameters

NameTypeDescription
nodeTextNodeThe TextNode to add styles to.

Returns

void

Defined in

lexical-selection/src/lexical-node.ts:284


$cloneWithProperties

$cloneWithProperties<T>(node): T

Returns a copy of a node, but generates a new key for the copy.

Type parameters

NameType
Textends LexicalNode

Parameters

NameTypeDescription
nodeTThe node to be cloned.

Returns

T

The clone of the node.

Defined in

lexical-selection/src/lexical-node.ts:63


$getSelectionStyleValueForProperty

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

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

NameTypeDefault valueDescription
selectionRangeSelectionundefinedThe selection of TextNodes whose value to find.
stylePropertystringundefinedThe CSS style property.
defaultValuestring''The default value for the property, defaults to an empty string.

Returns

string

The value of the property for the selected TextNodes.

Defined in

lexical-selection/src/range-selection.ts:519


$isAtNodeEnd

$isAtNodeEnd(point): boolean

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

Parameters

NameTypeDescription
pointPointThe point of the selection to test.

Returns

boolean

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

Defined in

lexical-selection/src/lexical-node.ts:140


$isParentElementRTL

$isParentElementRTL(selection): boolean

Tests a parent element for right to left direction.

Parameters

NameTypeDescription
selectionRangeSelectionThe 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.

Defined in

lexical-selection/src/range-selection.ts:425


$moveCaretSelection

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

Moves the selection according to the arguments.

Parameters

NameTypeDescription
selectionRangeSelectionThe selected text or nodes.
isHoldingShiftbooleanIs the shift key being held down during the operation.
isBackwardbooleanIs the selection selected backwards (the focus comes before the anchor)?
granularity"character" | "word" | "lineboundary"The distance to adjust the current selection.

Returns

void

Defined in

lexical-selection/src/range-selection.ts:411


$moveCharacter

$moveCharacter(selection, isHoldingShift, isBackward): void

Moves selection by character according to arguments.

Parameters

NameTypeDescription
selectionRangeSelectionThe selection of the characters to move.
isHoldingShiftbooleanIs the shift key being held down during the operation.
isBackwardbooleanIs the selection backward (the focus comes before the anchor)?

Returns

void

Defined in

lexical-selection/src/range-selection.ts:440


$patchStyleText

$patchStyleText(selection, patch): void

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

NameTypeDescription
selectionBaseSelectionThe selected node(s) to update.
patchRecord<string, null | string | (currentStyleValue: null | string) => 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

Defined in

lexical-selection/src/lexical-node.ts:325


$selectAll

$selectAll(selection): void

Expands the current Selection to cover all of the content in the editor.

Parameters

NameTypeDescription
selectionRangeSelectionThe current selection.

Returns

void

Defined in

lexical-selection/src/range-selection.ts:458


$setBlocksType

$setBlocksType(selection, createElement): void

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

Parameters

NameTypeDescription
selectionnull | BaseSelectionThe selected blocks to be converted.
createElement() => ElementNodeThe function that creates the node. eg. $createParagraphNode.

Returns

void

Defined in

lexical-selection/src/range-selection.ts:43


$shouldOverrideDefaultCharacterSelection

$shouldOverrideDefaultCharacterSelection(selection, isBackward): boolean

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

Parameters

NameTypeDescription
selectionRangeSelectionThe selection whose default character selection may need to be overridden.
isBackwardbooleanIs the selection backwards (the focus comes before the anchor)?

Returns

boolean

true if it should be overridden, false if not.

Defined in

lexical-selection/src/range-selection.ts:390


$sliceSelectedTextNodeContent

$sliceSelectedTextNodeContent(selection, textNode): LexicalNode

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

NameTypeDescription
selectionBaseSelectionThe selection containing the node whose TextNode is to be edited.
textNodeTextNodeThe TextNode to be edited.

Returns

LexicalNode

The updated TextNode.

Defined in

lexical-selection/src/lexical-node.ts:89


$wrapNodes

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

Parameters

NameTypeDefault valueDescription
selectionBaseSelectionundefinedThe selection of nodes to be wrapped.
createElement() => ElementNodeundefinedA function that creates the wrapping ElementNode. eg. $createParagraphNode.
wrappingElementnull | ElementNodenullAn element to append the wrapped selection and its children to.

Returns

void

Deprecated

Wraps all nodes in the selection into another node of the type returned by createElement.

Defined in

lexical-selection/src/range-selection.ts:115


createDOMRange

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

Creates a selection range for the DOM.

Parameters

NameTypeDescription
editorLexicalEditorThe lexical editor.
anchorNodeLexicalNodeThe anchor node of a selection.
_anchorOffsetnumberThe amount of space offset from the anchor to the focus.
focusNodeLexicalNodeThe current focus.
_focusOffsetnumberThe amount of space offset from the focus to the anchor.

Returns

Range | null

The range of selection for the DOM that was created.

Defined in

lexical-selection/src/utils.ts:47


createRectsFromDOMRange

createRectsFromDOMRange(editor, range): ClientRect[]

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

Parameters

NameTypeDescription
editorLexicalEditorThe lexical editor
rangeRangeA fragment of a document that can contain nodes and parts of text nodes.

Returns

ClientRect[]

The selectionRects as an array.

Defined in

lexical-selection/src/utils.ts:124


getStyleObjectFromCSS

getStyleObjectFromCSS(css): Record<string, string>

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

Parameters

NameTypeDescription
cssstringThe CSS property as a string.

Returns

Record<string, string>

The value of the given CSS property.

Defined in

lexical-selection/src/utils.ts:198


trimTextContentFromAnchor

trimTextContentFromAnchor(editor, anchor, delCount): void

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

NameTypeDescription
editorLexicalEditorThe lexical editor.
anchorPointThe anchor of the current selection, where the selection should be pointing.
delCountnumberThe amount of characters to delete. Useful as a dynamic variable eg. textContentSize - maxLength;

Returns

void

Defined in

lexical-selection/src/lexical-node.ts:161