Skip to main content

Class: TextNode

lexical.TextNode

Hierarchy

Constructors

constructor

new TextNode(text, key?)

Parameters

NameType
textstring
key?string

Overrides

LexicalNode.constructor

Defined in

lexical/src/nodes/LexicalTextNode.ts:299

Properties

__text

__text: string

Defined in

lexical/src/nodes/LexicalTextNode.ts:281

Methods

canInsertTextAfter

canInsertTextAfter(): boolean

This method is meant to be overriden by TextNode subclasses to control the behavior of those nodes when a user event would cause text to be inserted after them in the editor. If true, Lexical will attempt to insert text into this node. If false, it will insert the text in a new sibling node.

Returns

boolean

true if text can be inserted after the node, false otherwise.

Defined in

lexical/src/nodes/LexicalTextNode.ts:880


canInsertTextBefore

canInsertTextBefore(): boolean

This method is meant to be overriden by TextNode subclasses to control the behavior of those nodes when a user event would cause text to be inserted before them in the editor. If true, Lexical will attempt to insert text into this node. If false, it will insert the text in a new sibling node.

Returns

boolean

true if text can be inserted before the node, false otherwise.

Defined in

lexical/src/nodes/LexicalTextNode.ts:869


createDOM

createDOM(config): HTMLElement

Called during the reconciliation process to determine which nodes to insert into the DOM for this Lexical Node.

This method must return exactly one HTMLElement. Nested elements are not supported.

Do not attempt to update the Lexical EditorState during this phase of the update lifecyle.

Parameters

NameTypeDescription
configEditorConfigallows access to things like the EditorTheme (to apply classes) during reconciliation.

Returns

HTMLElement

Overrides

LexicalNode.createDOM

Defined in

lexical/src/nodes/LexicalTextNode.ts:447


exportDOM

exportDOM(editor): DOMExportOutput

Controls how the this node is serialized to HTML. This is important for copy and paste between Lexical and non-Lexical editors, or Lexical editors with different namespaces, in which case the primary transfer format is HTML. It's also important if you're serializing to HTML for any other reason via $generateHtmlFromNodes. You could also use this method to build your own HTML renderer.

Parameters

NameType
editorLexicalEditor

Returns

DOMExportOutput

Overrides

LexicalNode.exportDOM

Defined in

lexical/src/nodes/LexicalTextNode.ts:598


exportJSON

exportJSON(): SerializedTextNode

Controls how the this node is serialized to JSON. This is important for copy and paste between Lexical editors sharing the same namespace. It's also important if you're serializing to JSON for persistent storage somewhere. See Serialization & Deserialization.

Returns

SerializedTextNode

Overrides

LexicalNode.exportJSON

Defined in

lexical/src/nodes/LexicalTextNode.ts:626


getDetail

getDetail(): number

Returns a 32-bit integer that represents the TextDetailTypes currently applied to the TextNode. You probably don't want to use this method directly - consider using TextNode.isDirectionless or TextNode.isUnmergeable instead.

Returns

number

a number representing the detail of the text node.

Defined in

lexical/src/nodes/LexicalTextNode.ts:326


getFormat

getFormat(): number

Returns a 32-bit integer that represents the TextFormatTypes currently applied to the TextNode. You probably don't want to use this method directly - consider using TextNode.hasFormat instead.

Returns

number

a number representing the format of the text node.

Defined in

lexical/src/nodes/LexicalTextNode.ts:314


getFormatFlags

getFormatFlags(type, alignWithFormat): number

Returns the format flags applied to the node as a 32-bit integer.

Parameters

NameType
typeTextFormatType
alignWithFormatnull | number

Returns

number

a number representing the TextFormatTypes applied to the node.

Defined in

lexical/src/nodes/LexicalTextNode.ts:439


getMode

getMode(): TextModeType

Returns the mode (TextModeType) of the TextNode, which may be "normal", "token", or "segmented"

Returns

TextModeType

TextModeType.

Defined in

lexical/src/nodes/LexicalTextNode.ts:336


getStyle

getStyle(): string

Returns the styles currently applied to the node. This is analogous to CSSText in the DOM.

Returns

string

CSSText-like string of styles applied to the underlying DOM node.

Defined in

lexical/src/nodes/LexicalTextNode.ts:346


getTextContent

getTextContent(): string

Returns the text content of the node as a string.

Returns

string

a string representing the text content of the node.

Overrides

LexicalNode.getTextContent

Defined in

lexical/src/nodes/LexicalTextNode.ts:429


hasFormat

hasFormat(type): boolean

Returns whether or not the node has the provided format applied. Use this with the human-readable TextFormatType string values to get the format of a TextNode.

Parameters

NameTypeDescription
typeTextFormatTypethe TextFormatType to check for.

Returns

boolean

true if the node has the provided format, false otherwise.

Defined in

lexical/src/nodes/LexicalTextNode.ts:409


isComposing

isComposing(): boolean

Returns

boolean

true if Lexical detects that an IME or other 3rd-party script is attempting to mutate the TextNode, false otherwise.

Defined in

lexical/src/nodes/LexicalTextNode.ts:367


isDirectionless

isDirectionless(): boolean

Returns whether or not the node is "directionless". Directionless nodes don't respect changes between RTL and LTR modes.

Returns

boolean

true if the node is directionless, false otherwise.

Defined in

lexical/src/nodes/LexicalTextNode.ts:386


isSegmented

isSegmented(): boolean

Returns whether or not the node is in "segemented" mode. TextNodes in segemented mode can be navigated through character-by-character with a RangeSelection, but are deleted in space-delimited "segments".

Returns

boolean

true if the node is in segmented mode, false otherwise.

Defined in

lexical/src/nodes/LexicalTextNode.ts:377


isSimpleText

isSimpleText(): boolean

Returns whether or not the node is simple text. Simple text is defined as a TextNode that has the string type "text" (i.e., not a subclass) and has no mode applied to it (i.e., not segmented or token).

Returns

boolean

true if the node is simple text, false otherwise.

Defined in

lexical/src/nodes/LexicalTextNode.ts:420


isTextEntity

isTextEntity(): boolean

This method is meant to be overriden by TextNode subclasses to control the behavior of those nodes when used with the registerLexicalTextEntity function. If you're using registerLexicalTextEntity, the node class that you create and replace matched text with should return true from this method.

Returns

boolean

true if the node is to be treated as a "text entity", false otherwise.

Defined in

lexical/src/nodes/LexicalTextNode.ts:1075


isToken

isToken(): boolean

Returns whether or not the node is in "token" mode. TextNodes in token mode can be navigated through character-by-character with a RangeSelection, but are deleted as a single entity (not invdividually by character).

Returns

boolean

true if the node is in token mode, false otherwise.

Defined in

lexical/src/nodes/LexicalTextNode.ts:357


isUnmergeable

isUnmergeable(): boolean

Returns whether or not the node is unmergeable. In some scenarios, Lexical tries to merge adjacent TextNodes into a single TextNode. If a TextNode is unmergeable, this won't happen.

Returns

boolean

true if the node is unmergeable, false otherwise.

Defined in

lexical/src/nodes/LexicalTextNode.ts:396


mergeWithSibling

mergeWithSibling(target): TextNode

Merges the target TextNode into this TextNode, removing the target node.

Parameters

NameTypeDescription
targetTextNodethe TextNode to merge into this one.

Returns

TextNode

this TextNode.

Defined in

lexical/src/nodes/LexicalTextNode.ts:1018


select

select(_anchorOffset?, _focusOffset?): RangeSelection

Sets the current Lexical selection to be a RangeSelection with anchor and focus on this TextNode at the provided offsets.

Parameters

NameTypeDescription
_anchorOffset?numberthe offset at which the Selection anchor will be placed.
_focusOffset?numberthe offset at which the Selection focus will be placed.

Returns

RangeSelection

the new RangeSelection.

Defined in

lexical/src/nodes/LexicalTextNode.ts:776


selectionTransform

selectionTransform(prevSelection, nextSelection): void

Parameters

NameType
prevSelectionnull | RangeSelection | NodeSelection | GridSelection
nextSelectionRangeSelection

Returns

void

Defined in

lexical/src/nodes/LexicalTextNode.ts:639


setDetail

setDetail(detail): TextNode

Sets the node detail to the provided TextDetailType or 32-bit integer. Note that the TextDetailType version of the argument can only specify one detail value and doing so will remove all other detail values that may be applied to the node. For toggling behavior, consider using toggleDirectionless or TextNode.togglerUnmergeable

Parameters

NameTypeDescription
detailnumber | TextDetailTypeTextDetailType or 32-bit integer representing the node detail.

Returns

TextNode

this TextNode. // TODO 0.12 This should just be a string.

Defined in

lexical/src/nodes/LexicalTextNode.ts:674


setFormat

setFormat(format): TextNode

Sets the node format to the provided TextFormatType or 32-bit integer. Note that the TextFormatType version of the argument can only specify one format and doing so will remove all other formats that may be applied to the node. For toggling behavior, consider using toggleFormat

Parameters

NameTypeDescription
formatnumber | TextFormatTypeTextFormatType or 32-bit integer representing the node format.

Returns

TextNode

this TextNode. // TODO 0.12 This should just be a string.

Defined in

lexical/src/nodes/LexicalTextNode.ts:656


setMode

setMode(type): TextNode

Sets the mode of the node.

Parameters

NameType
typeTextModeType

Returns

TextNode

this TextNode.

Defined in

lexical/src/nodes/LexicalTextNode.ts:742


setStyle

setStyle(style): TextNode

Sets the node style to the provided CSSText-like string. Set this property as you would an HTMLElement style attribute to apply inline styles to the underlying DOM Element.

Parameters

NameTypeDescription
stylestringCSSText to be applied to the underlying HTMLElement.

Returns

TextNode

this TextNode.

Defined in

lexical/src/nodes/LexicalTextNode.ts:689


setTextContent

setTextContent(text): TextNode

Sets the text content of the node.

Parameters

NameTypeDescription
textstringthe string to set as the text value of the node.

Returns

TextNode

this TextNode.

Defined in

lexical/src/nodes/LexicalTextNode.ts:759


spliceText

spliceText(offset, delCount, newText, moveSelection?): TextNode

Inserts the provided text into this TextNode at the provided offset, deleting the number of characters specified. Can optionally calculate a new selection after the operation is complete.

Parameters

NameTypeDescription
offsetnumberthe offset at which the splice operation should begin.
delCountnumberthe number of characters to delete, starting from the offset.
newTextstringthe text to insert into the TextNode at the offset.
moveSelection?booleanoptional, whether or not to move selection to the end of the inserted substring.

Returns

TextNode

this TextNode.

Defined in

lexical/src/nodes/LexicalTextNode.ts:828


splitText

splitText(...splitOffsets): TextNode[]

Splits this TextNode at the provided character offsets, forming new TextNodes from the substrings formed by the split, and inserting those new TextNodes into the editor, replacing the one that was split.

Parameters

NameTypeDescription
...splitOffsetsnumber[]rest param of the text content character offsets at which this node should be split.

Returns

TextNode[]

an Array containing the newly-created TextNodes.

Defined in

lexical/src/nodes/LexicalTextNode.ts:892


toggleDirectionless

toggleDirectionless(): TextNode

Toggles the directionless detail value of the node. Prefer using this method over setDetail.

Returns

TextNode

this TextNode.

Defined in

lexical/src/nodes/LexicalTextNode.ts:720


toggleFormat

toggleFormat(type): TextNode

Applies the provided format to this TextNode if it's not present. Removes it if it's present. The subscript and superscript formats are mutually exclusive. Prefer using this method to turn specific formats on and off.

Parameters

NameTypeDescription
typeTextFormatTypeTextFormatType to toggle.

Returns

TextNode

this TextNode.

Defined in

lexical/src/nodes/LexicalTextNode.ts:704


toggleUnmergeable

toggleUnmergeable(): TextNode

Toggles the unmergeable detail value of the node. Prefer using this method over setDetail.

Returns

TextNode

this TextNode.

Defined in

lexical/src/nodes/LexicalTextNode.ts:731


updateDOM

updateDOM(prevNode, dom, config): boolean

Called when a node changes and should update the DOM in whatever way is necessary to make it align with any changes that might have happened during the update.

Returning "true" here will cause lexical to unmount and recreate the DOM node (by calling createDOM). You would need to do this if the element tag changes, for instance.

Parameters

NameType
prevNodeTextNode
domHTMLElement
configEditorConfig

Returns

boolean

Overrides

LexicalNode.updateDOM

Defined in

lexical/src/nodes/LexicalTextNode.ts:470


clone

Static clone(node): TextNode

Clones this node, creating a new node with a different key and adding it to the EditorState (but not attaching it anywhere!). All nodes must implement this method.

Parameters

NameType
nodeTextNode

Returns

TextNode

Overrides

LexicalNode.clone

Defined in

lexical/src/nodes/LexicalTextNode.ts:295


getType

Static getType(): string

Returns the string type of this node. Every node must implement this and it MUST BE UNIQUE amongst nodes registered on the editor.

Returns

string

Overrides

LexicalNode.getType

Defined in

lexical/src/nodes/LexicalTextNode.ts:291


importDOM

Static importDOM(): null | DOMConversionMap

Returns

null | DOMConversionMap

Defined in

lexical/src/nodes/LexicalTextNode.ts:537


importJSON

Static importJSON(serializedNode): TextNode

Controls how the this node is deserialized from JSON. This is usually boilerplate, but provides an abstraction between the node implementation and serialized interface that can be important if you ever make breaking changes to a node schema (by adding or removing properties). See Serialization & Deserialization.

Parameters

NameType
serializedNodeSerializedTextNode

Returns

TextNode

Overrides

LexicalNode.importJSON

Defined in

lexical/src/nodes/LexicalTextNode.ts:586