Skip to main content

Class: LexicalNode

lexical.LexicalNode

Hierarchy

Indexable

[x: string]: any

Constructors

constructor

new LexicalNode(key?)

Parameters

NameType
key?string

Defined in

lexical/src/LexicalNode.ts:202

Methods

createDOM

createDOM(_config, _editor): 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.
_editorLexicalEditorallows access to the editor for context during reconciliation.

Returns

HTMLElement

Defined in

lexical/src/LexicalNode.ts:743


createParentElementNode

createParentElementNode(): ElementNode

The creation logic for any required parent. Should be implemented if isParentRequired returns true.

Returns

ElementNode

Defined in

lexical/src/LexicalNode.ts:1016


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

Defined in

lexical/src/LexicalNode.ts:773


exportJSON

exportJSON(): SerializedLexicalNode

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

SerializedLexicalNode

Defined in

lexical/src/LexicalNode.ts:785


getCommonAncestor

getCommonAncestor<T>(node): null | T

Returns the closest common ancestor of this node and the provided one or null if one cannot be found.

Type parameters

NameType
Textends ElementNode<T> = ElementNode

Parameters

NameTypeDescription
nodeLexicalNodethe other node to find the common ancestor of.

Returns

null | T

Defined in

lexical/src/LexicalNode.ts:467


getIndexWithinParent

getIndexWithinParent(): number

Returns the zero-based index of this node within the parent.

Returns

number

Defined in

lexical/src/LexicalNode.ts:299


getKey

getKey(): string

Returns this nodes key.

Returns

string

Defined in

lexical/src/LexicalNode.ts:291


getLatest

getLatest(): LexicalNode

Returns the latest version of the node from the active EditorState. This is used to avoid getting values from stale node references.

Returns

LexicalNode

Defined in

lexical/src/LexicalNode.ts:648


getNextSibling

getNextSibling<T>(): null | T

Returns the "next" siblings - that is, the node that comes after this one in the same parent

Type parameters

NameType
Textends LexicalNode<T>

Returns

null | T

Defined in

lexical/src/LexicalNode.ts:440


getNextSiblings

getNextSiblings<T>(): T[]

Returns all "next" siblings - that is, the nodes that come between this one and the last child of it's parent, inclusive.

Type parameters

NameType
Textends LexicalNode<T>

Returns

T[]

Defined in

lexical/src/LexicalNode.ts:451


getNodesBetween

getNodesBetween(targetNode): LexicalNode[]

Returns a list of nodes that are between this node and the target node in the EditorState.

Parameters

NameTypeDescription
targetNodeLexicalNodethe node that marks the other end of the range of nodes to be returned.

Returns

LexicalNode[]

Defined in

lexical/src/LexicalNode.ts:572


getParent

getParent<T>(): null | T

Returns the parent of this node, or null if none is found.

Type parameters

NameType
Textends ElementNode<T>

Returns

null | T

Defined in

lexical/src/LexicalNode.ts:319


getParentKeys

getParentKeys(): string[]

Returns a list of the keys of every ancestor of this node, all the way up to the RootNode.

Returns

string[]

Defined in

lexical/src/LexicalNode.ts:392


getParentOrThrow

getParentOrThrow<T>(): T

Returns the parent of this node, or throws if none is found.

Type parameters

NameType
Textends ElementNode<T>

Returns

T

Defined in

lexical/src/LexicalNode.ts:330


getParents

getParents(): ElementNode[]

Returns a list of the every ancestor of this node, all the way up to the RootNode.

Returns

ElementNode[]

Defined in

lexical/src/LexicalNode.ts:377


getPreviousSibling

getPreviousSibling<T>(): null | T

Returns the "previous" siblings - that is, the node that comes before this one in the same parent.

Type parameters

NameType
Textends LexicalNode<T>

Returns

null | T

Defined in

lexical/src/LexicalNode.ts:407


getPreviousSiblings

getPreviousSiblings<T>(): T[]

Returns the "previous" siblings - that is, the nodes that come between this one and the first child of it's parent, inclusive.

Type parameters

NameType
Textends LexicalNode<T>

Returns

T[]

Defined in

lexical/src/LexicalNode.ts:418


getTextContent

getTextContent(): string

Returns the text content of the node. Override this for custom nodes that should have a representation in plain text format (for copy + paste, for example)

Returns

string

Defined in

lexical/src/LexicalNode.ts:717


getTextContentSize

getTextContentSize(): number

Returns the length of the string produced by calling getTextContent on this node.

Returns

number

Defined in

lexical/src/LexicalNode.ts:725


getTopLevelElement

getTopLevelElement(): null | LexicalNode | ElementNode

Returns the highest (in the EditorState tree) non-root ancestor of this node, or null if none is found. See $isRootOrShadowRoot for more information on which Elements comprise "roots".

Returns

null | LexicalNode | ElementNode

Defined in

lexical/src/LexicalNode.ts:343


getTopLevelElementOrThrow

getTopLevelElementOrThrow(): LexicalNode | ElementNode

Returns the highest (in the EditorState tree) non-root ancestor of this node, or throws if none is found. See $isRootOrShadowRoot for more information on which Elements comprise "roots".

Returns

LexicalNode | ElementNode

Defined in

lexical/src/LexicalNode.ts:360


getType

getType(): string

Returns the string type of this node.

Returns

string

Defined in

lexical/src/LexicalNode.ts:226


getWritable

getWritable(): LexicalNode

Returns a mutable version of the node. Will throw an error if called outside of a Lexical Editor update callback.

Returns

LexicalNode

Defined in

lexical/src/LexicalNode.ts:664


insertAfter

insertAfter(nodeToInsert, restoreSelection?): LexicalNode

Inserts a node after this LexicalNode (as the next sibling).

Parameters

NameTypeDefault valueDescription
nodeToInsertLexicalNodeundefinedThe node to insert after this one.
restoreSelectionbooleantrueWhether or not to attempt to resolve the selection to the appropriate place after the operation is complete.

Returns

LexicalNode

Defined in

lexical/src/LexicalNode.ts:901


insertBefore

insertBefore(nodeToInsert, restoreSelection?): LexicalNode

Inserts a node before this LexicalNode (as the previous sibling).

Parameters

NameTypeDefault valueDescription
nodeToInsertLexicalNodeundefinedThe node to insert after this one.
restoreSelectionbooleantrueWhether or not to attempt to resolve the selection to the appropriate place after the operation is complete.

Returns

LexicalNode

Defined in

lexical/src/LexicalNode.ts:968


is

is(object): boolean

Returns true if the provided node is the exact same one as this node, from Lexical's perspective. Always use this instead of referential equality.

Parameters

NameTypeDescription
objectundefined | null | LexicalNodethe node to perform the equality comparison on.

Returns

boolean

Defined in

lexical/src/LexicalNode.ts:499


isAttached

isAttached(): boolean

Returns true if there is a path between this node and the RootNode, false otherwise. This is a way of determining if the node is "attached" EditorState. Unattached nodes won't be reconciled and will ultimatelt be cleaned up by the Lexical GC.

Returns

boolean

Defined in

lexical/src/LexicalNode.ts:235


isBefore

isBefore(targetNode): boolean

Returns true if this node logical precedes the target node in the editor state.

Parameters

NameTypeDescription
targetNodeLexicalNodethe node we're testing to see if it's after this one.

Returns

boolean

Defined in

lexical/src/LexicalNode.ts:511


isDirty

isDirty(): boolean

Returns true if this node has been marked dirty during this update cycle.

Returns

boolean

Defined in

lexical/src/LexicalNode.ts:637


isParentOf

isParentOf(targetNode): boolean

Returns true if this node is the parent of the target node, false otherwise.

Parameters

NameTypeDescription
targetNodeLexicalNodethe would-be child node.

Returns

boolean

Defined in

lexical/src/LexicalNode.ts:550


isParentRequired

isParentRequired(): boolean

Whether or not this node has a required parent. Used during copy + paste operations to normalize nodes that would otherwise be orphaned. For example, ListItemNodes without a ListNode parent or TextNodes with a ParagraphNode parent.

Returns

boolean

Defined in

lexical/src/LexicalNode.ts:1008


isSelected

isSelected(selection?): boolean

Returns true if this node is contained within the provided Selection., false otherwise. Relies on the algorithms implemented in getNodes to determine what's included.

Parameters

NameTypeDescription
selection?null | RangeSelection | GridSelection | NodeSelectionThe selection that we want to determine if the node is in.

Returns

boolean

Defined in

lexical/src/LexicalNode.ts:259


markDirty

markDirty(): void

Marks a node dirty, triggering transforms and forcing it to be reconciled during the update cycle.

Returns

void

Defined in

lexical/src/LexicalNode.ts:1069


remove

remove(preserveEmptyParent?): void

Removes this LexicalNode from the EditorState. If the node isn't re-inserted somewhere, the Lexical garbage collector will eventually clean it up.

Parameters

NameTypeDescription
preserveEmptyParent?booleanIf falsy, the node's parent will be removed if it's empty after the removal operation. This is the default behavior, subject to other node heuristics such as canBeEmpty

Returns

void

Defined in

lexical/src/LexicalNode.ts:826


replace

replace<N>(replaceWith, includeChildren?): N

Replaces this LexicalNode with the provided node, optionally transferring the children of the replaced node to the replacing node.

Type parameters

NameType
Nextends LexicalNode<N>

Parameters

NameTypeDescription
replaceWithNThe node to replace this one with.
includeChildren?booleanWhether or not to transfer the children of this node to the replacing node.

Returns

N

Defined in

lexical/src/LexicalNode.ts:837


selectNext

selectNext(anchorOffset?, focusOffset?): RangeSelection

Moves selection to the next sibling of this node, at the specified offsets.

Parameters

NameTypeDescription
anchorOffset?numberThe anchor offset for selection.
focusOffset?numberThe focus offset for selection

Returns

RangeSelection

Defined in

lexical/src/LexicalNode.ts:1048


selectPrevious

selectPrevious(anchorOffset?, focusOffset?): RangeSelection

Moves selection to the previous sibling of this node, at the specified offsets.

Parameters

NameTypeDescription
anchorOffset?numberThe anchor offset for selection.
focusOffset?numberThe focus offset for selection

Returns

RangeSelection

Defined in

lexical/src/LexicalNode.ts:1026


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
_prevNodeunknown
_domHTMLElement
_configEditorConfig

Returns

boolean

Defined in

lexical/src/LexicalNode.ts:757


clone

Static clone(_data): LexicalNode

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
_dataunknown

Returns

LexicalNode

Defined in

lexical/src/LexicalNode.ts:194


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

Defined in

lexical/src/LexicalNode.ts:180


importJSON

Static importJSON(_serializedNode): LexicalNode

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
_serializedNodeSerializedLexicalNode

Returns

LexicalNode

Defined in

lexical/src/LexicalNode.ts:796


transform

Static transform(): null | (node: LexicalNode) => void

Registers the returned function as a transform on the node during Editor initialization. Most such use cases should be addressed via the registerNodeTransform API.

Experimental - use at your own risk.

Returns

null | (node: LexicalNode) => void

Defined in

lexical/src/LexicalNode.ts:812