Skip to main content

Module: @lexical/selection

References

$cloneWithProperties

Re-exports $cloneWithProperties


$selectAll

Re-exports $selectAll

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

packages/lexical-selection/src/lexical-node.ts:236


$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
selectionRangeSelection | TableSelectionundefinedThe 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

packages/lexical-selection/src/range-selection.ts:485


$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

packages/lexical-selection/src/lexical-node.ts:92


$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

packages/lexical-selection/src/range-selection.ts:426


$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

packages/lexical-selection/src/range-selection.ts:412


$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

packages/lexical-selection/src/range-selection.ts:441


$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, target: RangeSelection | TextNode) => 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

packages/lexical-selection/src/lexical-node.ts:279


$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

packages/lexical-selection/src/range-selection.ts:44


$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

packages/lexical-selection/src/range-selection.ts:391


$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

packages/lexical-selection/src/lexical-node.ts:41


$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

packages/lexical-selection/src/lexical-node.ts:113


$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

packages/lexical-selection/src/range-selection.ts:116


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

packages/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

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


getCSSFromStyleObject

getCSSFromStyleObject(styles): string

Gets the CSS styles from the style object.

Parameters

NameTypeDescription
stylesRecord<string, string>The style object containing the styles to get.

Returns

string

A string containing the CSS styles and their values.

Defined in

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


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

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


trimTextContentFromAnchor

trimTextContentFromAnchor(editor, anchor, delCount): void

Parameters

NameType
editorLexicalEditor
anchorPoint
delCountnumber

Returns

void

Deprecated

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

Defined in

packages/lexical-selection/src/index.ts:45