TextNode
classImport
import { TextNode } from '@modular/sdk/editor';Members81
- __text
string- importDOMoptional
() => DOMConversionMap<any> | null- $config()
() => import("../index.js").BaseStaticNodeConfig & { readonly text?: { readonly extends: typeof LexicalNode; readonly generated: import("../index.js").GeneratedJSONFactory; readonly importDOM: { readonly '#text': () => { conversion: typeof $convertTextDOMNode; priority: 0; }; readonly b: () => { conversion: typeof convertBringAttentionToElement; priority: 0; }; readonly code: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly em: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly i: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly mark: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly s: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly span: () => { conversion: typeof convertSpanElement; priority: 0; }; readonly strong: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly sub: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly sup: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly u: () => { conversion: typeof convertTextFormatElement; priority: 0; }; }; readonly json: import("../index.js").NodeSerializationSchema<TextNode, { readonly detail?: string | number | undefined; readonly format?: string | number | undefined; readonly mode?: "normal" | "token" | "segmented" | undefined; readonly style?: string | undefined; readonly text?: string | undefined; }>; } | undefined; } & import("../index.js").StaticNodeTypeAccessor<"text"> & import("../index.js").StaticNodeConfigAccessor<{ readonly extends: typeof LexicalNode; readonly generated: import("../index.js").GeneratedJSONFactory; readonly importDOM: { readonly '#text': () => { conversion: typeof $convertTextDOMNode; priority: 0; }; readonly b: () => { conversion: typeof convertBringAttentionToElement; priority: 0; }; readonly code: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly em: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly i: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly mark: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly s: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly span: () => { conversion: typeof convertSpanElement; priority: 0; }; readonly strong: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly sub: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly sup: () => { conversion: typeof convertTextFormatElement; priority: 0; }; readonly u: () => { conversion: typeof convertTextFormatElement; priority: 0; }; }; readonly json: import("../index.js").NodeSerializationSchema<TextNode, { readonly detail?: string | number | undefined; readonly format?: string | number | undefined; readonly mode?: "normal" | "token" | "segmented" | undefined; readonly style?: string | undefined; readonly text?: string | undefined; }>; }>Override this to implement the new static node configuration protocol, this method is called directly on the prototype and must not depend on anything initialized in the constructor. Generally it should be a trivial implementation.
- afterCloneFrom()
(prevNode: this) => voidPerform any state updates on the clone of prevNode that are not already handled by the constructor call in the static clone method. If you have state to update in your clone that is not handled directly by the constructor, it is advisable to override this method but it is required to include a call to `super.afterCloneFrom(prevNode)` in your implementation. This is only intended to be called by $cloneWithProperties function or via a super call.
- canHaveFormat()
() => boolean- canInsertTextAfter()
() => booleanThis method is meant to be overridden 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.
- canInsertTextBefore()
() => booleanThis method is meant to be overridden 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.
- clone()
(_data: unknown) => LexicalNodeClones 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.
- config()
<const Config extends StaticNodeConfigValue<this, string>>(type: symbol, config: Config) => AbstractStaticNodeConfigRecord<Config> | <Type extends string, const Config extends StaticNodeConfigValue<this, Type>>(type: Type, config: Config) => StaticNodeConfigRecord<Type, Config>This is a convenience method for $config that aids in type inference. See LexicalNode.$config for example usage. An abstract base class that has no concrete node `type` may pass a well-known symbol (by convention `Symbol.for(<NodeClassName>)`) instead of a string `type` to declare configuration shared with its subclasses.
- createDOM()
(config: EditorConfig, editor?: LexicalEditor) => HTMLElementCalled 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 lifecycle.
- createParentElementNode()
() => ElementNodeThe creation logic for any required parent. Should be implemented if isParentRequired returns true.
- exportDOM()
(editor: LexicalEditor) => DOMExportOutputControls 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 @lexical/html!$generateHtmlFromNodes. You could also use this method to build your own HTML renderer.
- exportJSON()
(compact?: false) => SerializedTextNode | (compact: boolean) => SerializedPartial<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](https://lexical.dev/docs/serialization/serialization#json). The base implementation writes every property the node's schema declares (its own and those it inherits), reading each through its getter — `get<Prop>` by default, or the name recorded with `withAccessors`. A getter that returns `undefined` omits its property. Override this only for output a schema can not describe, and call `super.exportJSON(compact)` when you do. **This may serialize the instance as-is, without resolving the latest version.** A property declared with withField is read straight off the node, which is the optimization the serialization walk is built on — every node the walk reaches comes from the EditorState's node map and is already current, so it resolves nothing per node. So on a reference that a `getWritable()` (any `set<Prop>`) has since superseded, this writes pre-mutation values. Which properties do is not something to rely on: a property whose accessor a subclass overrode still goes through that accessor and resolves the latest, so one node can write a current `text` beside a stale `style`. Call `node.getLatest().exportJSON()` whenever you hold such a reference rather than reasoning about which properties resolve. This is a breaking change. Every property previously went through an accessor, and every accessor resolves `getLatest()`, so a stale reference exported current values.
- getDetail()
() => numberReturns 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.
- getDOMSlot()
(element: HTMLElement) => DOMSlot<HTMLElement>Returns a DOMSlot pointing at the content-bearing element of this node's DOM. The default returns a slot wrapping the keyed DOM as-is. Override this when createDOM returns a wrapper around the content-bearing element (e.g. `<span><br/></span>` for a styled line break), so selection / reconciliation logic can target the inner element. ElementNode overrides this to return an ElementDOMSlot with children-management semantics (used by the reconciler to place managed children).
- getFormat()
() => numberReturns 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.
- getFormatFlags()
(type: TextFormatType, alignWithFormat: null | number) => numberReturns the format flags applied to the node as a 32-bit integer.
- getIndexWithinParent()
() => numberReturns the zero-based index of this node within the parent.
- getKey()
() => NodeKeyReturns this nodes key.
- getLatest()
() => thisReturns the latest version of the node from the active EditorState. This is used to avoid getting values from stale node references.
- getMode()
() => TextModeTypeReturns the mode (TextModeType) of the TextNode, which may be "normal", "token", or "segmented"
- getNextSibling()
() => LexicalNode | null | <T extends LexicalNode>() => T | nullReturns the node after this one in the same parent, or null if there is no such node.
- getNextSiblings()
() => LexicalNode[] | <T extends LexicalNode>() => T[]Returns all nodes after this one in the same parent, in document order.
- getNodesBetween()
(targetNode: LexicalNode) => LexicalNode[]Returns a list of nodes that are between this node and the target node in the EditorState.
- getParent()
() => ElementNode | null | <T extends ElementNode>() => T | nullReturns the parent of this node, or null if none is found.
- getParentKeys()
() => NodeKey[]Returns a list of the keys of every ancestor of this node, all the way up to the RootNode.
- getParentOrThrow()
() => ElementNode | <T extends ElementNode>() => TReturns the parent of this node, or throws if none is found.
- getParents()
() => ElementNode[]Returns a list of the every ancestor of this node, all the way up to the RootNode.
- getPreviousSibling()
() => LexicalNode | null | <T extends LexicalNode>() => T | nullReturns the node before this one in the same parent, or null if there is no such node.
- getPreviousSiblings()
() => LexicalNode[] | <T extends LexicalNode>() => T[]Returns all nodes before this one in the same parent, in document order.
- getStyle()
() => stringReturns the styles currently applied to the node. This is analogous to CSSText in the DOM.
- getTextContent()
() => stringReturns the text content of the node as a string.
- getTextContentSize()
() => numberReturns the length of the string produced by calling getTextContent on this node.
- getTopLevelElement()
() => ElementNode | nullReturns the highest (in the EditorState tree) non-root ancestor of this node, or null if none is found. See lexical !$isRootOrShadowRoot for more information on which Elements comprise "roots".
- getTopLevelElementOrThrow()
() => ElementNodeReturns the highest (in the EditorState tree) non-root ancestor of this node, or throws if none is found. See lexical !$isRootOrShadowRoot for more information on which Elements comprise "roots".
- getType()
() => stringReturns the string type of this node.
- getType()
() => stringReturns the string type of this node. Every node must implement this and it MUST BE UNIQUE amongst nodes registered on the editor.
- getWritable()
() => thisReturns a mutable version of the node using $cloneWithProperties if necessary. Will throw an error if called outside of a Lexical Editor LexicalEditor.update callback.
- hasFormat()
(type: TextFormatType) => booleanReturns 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.
- importJSON()
(_serializedNode: SerializedPartial<SerializedLexicalNode> & Record<string, unknown>) => LexicalNodeControls 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](https://lexical.dev/docs/serialization/serialization#json).
- insertAfter()
(nodeToInsert: LexicalNode, restoreSelection?: boolean) => LexicalNodeInserts a node after this LexicalNode (as the next sibling).
- insertBefore()
(nodeToInsert: LexicalNode, restoreSelection?: boolean) => LexicalNodeInserts a node before this LexicalNode (as the previous sibling).
- is()
(object: LexicalNode | null | undefined) => booleanReturns true if the provided node is the exact same one as this node, from Lexical's perspective. Always use this instead of referential equality.
- isAttached()
() => booleanReturns 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 ultimately be cleaned up by the Lexical GC.
- isBefore()
(targetNode: LexicalNode) => booleanReturns true if this node logically precedes the target node in the editor state, false otherwise (including if there is no common ancestor). Note that this notion of isBefore is based on post-order; a descendant node is always before its ancestors. See also $getCommonAncestor and $comparePointCaretNext for more flexible ways to determine the relative positions of nodes.
- isComposing()
() => boolean- isDirectionless()
() => booleanReturns whether or not the node is "directionless". Directionless nodes don't respect changes between RTL and LTR modes.
- isDirty()
() => booleanReturns true if this node has been marked dirty during this update cycle.
- isInline()
() => true- isParentOf()
(targetNode: LexicalNode) => booleanReturns true if this node is an ancestor of and distinct from the target node, false otherwise.
- isParentRequired()
() => booleanWhether 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.
- isSegmented()
() => booleanReturns whether or not the node is in "segmented" mode. TextNodes in segmented mode can be navigated through character-by-character with a RangeSelection, but are deleted in space-delimited "segments".
- isSelected()
(selection?: null | BaseSelection) => booleanReturns true if this node is contained within the provided Selection., false otherwise. Relies on the algorithms implemented in BaseSelection.getNodes to determine what's included.
- isSimpleText()
() => booleanReturns 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).
- isTextEntity()
() => booleanThis method is meant to be overridden 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.
- isToken()
() => booleanReturns 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 individually by character).
- isUnmergeable()
() => booleanReturns 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.
- markDirty()
() => voidMarks a node dirty, triggering transforms and forcing it to be reconciled during the update cycle.
- mergeWithSibling()
(target: TextNode) => TextNodeMerges the target TextNode into this TextNode, removing the target node.
- remove()
(preserveEmptyParent?: boolean) => voidRemoves this LexicalNode from the EditorState. If the node isn't re-inserted somewhere, the Lexical garbage collector will eventually clean it up.
- replace()
<N extends LexicalNode>(replaceWith: N, includeChildren?: boolean) => NReplaces this LexicalNode with the provided node, optionally transferring the children of the replaced node to the replacing node. Named slots are bound to their host node and are never transferred: this node keeps its slot map, so if it is reattached elsewhere (as `$wrapNodeInElement` does) its slots come with it, and if it stays detached the slot subtrees are garbage-collected along with it. To move a slot value onto another host, use `$setSlot` explicitly.
- resetOnCopyNodeFrom()
(originalNode: this) => voidReset state in this copy of originalNode, if necessary
- select()
(_anchorOffset?: number, _focusOffset?: number) => RangeSelectionSets the current Lexical selection to be a RangeSelection with anchor and focus on this TextNode at the provided offsets.
- selectEnd()
() => RangeSelection- selectionTransform()
(prevSelection: null | BaseSelection, nextSelection: RangeSelection) => void- selectNext()
(anchorOffset?: number, focusOffset?: number) => RangeSelectionMoves selection to the next sibling of this node, at the specified offsets.
- selectPrevious()
(anchorOffset?: number, focusOffset?: number) => RangeSelectionMoves selection to the previous sibling of this node, at the specified offsets.
- selectStart()
() => RangeSelection- setDetail()
(detail: TextDetailType | number) => thisSets 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 TextNode.toggleDirectionless or TextNode.toggleUnmergeable
- setFormat()
(format: TextFormatType | number) => thisSets 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 TextNode.toggleFormat
- setMode()
(type: TextModeType) => thisSets the mode of the node. Note: during IME composition, a segmented TextNode may be temporarily switched to normal mode to preserve the DOM element that the browser's composition tracker is bound to. Subclass transforms or method overrides that assume the node is always in segmented mode should account for this transient state.
- setStyle()
(style: string) => thisSets 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.
- setTextContent()
(text: string) => thisSets the text content of the node.
- spliceText()
(offset: number, delCount: number, newText: string, moveSelection?: boolean) => TextNodeInserts 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.
- splitText()
(...splitOffsets: number[]) => 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.
- toggleDirectionless()
() => thisToggles the directionless detail value of the node. Prefer using this method over setDetail.
- toggleFormat()
(type: TextFormatType) => thisApplies 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.
- toggleUnmergeable()
() => thisToggles the unmergeable detail value of the node. Prefer using this method over setDetail.
- transform()
() => ((node: LexicalNode) => void) | null- updateDOM()
(prevNode: this, dom: HTMLElement, config: EditorConfig) => booleanCalled 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.
- updateFromJSON()
(serializedNode: LexicalParseJSON<SerializedTextNode>) => thisUpdate this LexicalNode instance from serialized JSON. It's recommended to implement as much logic as possible in this method instead of the static importJSON method, so that the functionality can be inherited in subclasses. The LexicalUpdateJSON utility type should be used to ignore any type, version, or children properties in the JSON so that the extended JSON from subclasses are acceptable parameters for the super call. If overridden, this method must call super.
Notes
- noInheritDoc