Skip to main content

Class: CodeNode

@lexical/code.CodeNode

Hierarchy

Constructors

constructor

new CodeNode(language?, key?): CodeNode

Parameters

NameType
language?null | string
key?string

Returns

CodeNode

Overrides

ElementNode.constructor

Defined in

packages/lexical-code/src/CodeNode.ts:85

Methods

afterCloneFrom

afterCloneFrom(prevNode): void

Perform 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.

Parameters

NameType
prevNodethis

Returns

void

Example

class ClassesTextNode extends TextNode {
// Not shown: static getType, static importJSON, exportJSON, createDOM, updateDOM
__classes = new Set<string>();
static clone(node: ClassesTextNode): ClassesTextNode {
// The inherited TextNode constructor is used here, so
// classes is not set by this method.
return new ClassesTextNode(node.__text, node.__key);
}
afterCloneFrom(node: this): void {
// This calls TextNode.afterCloneFrom and LexicalNode.afterCloneFrom
// for necessary state updates
super.afterCloneFrom(node);
this.__addClasses(node.__classes);
}
// This method is a private implementation detail, it is not
// suitable for the public API because it does not call getWritable
__addClasses(classNames: Iterable<string>): this {
for (const className of classNames) {
this.__classes.add(className);
}
return this;
}
addClass(...classNames: string[]): this {
return this.getWritable().__addClasses(classNames);
}
removeClass(...classNames: string[]): this {
const node = this.getWritable();
for (const className of classNames) {
this.__classes.delete(className);
}
return this;
}
getClasses(): Set<string> {
return this.getLatest().__classes;
}
}

Overrides

ElementNode.afterCloneFrom

Defined in

packages/lexical-code/src/CodeNode.ts:92


canIndent

canIndent(): false

Returns

false

Overrides

ElementNode.canIndent

Defined in

packages/lexical-code/src/CodeNode.ts:366


collapseAtStart

collapseAtStart(): boolean

Returns

boolean

Overrides

ElementNode.collapseAtStart

Defined in

packages/lexical-code/src/CodeNode.ts:370


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 lifecycle.

Parameters

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

Returns

HTMLElement

Overrides

ElementNode.createDOM

Defined in

packages/lexical-code/src/CodeNode.ts:100


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

ElementNode.exportDOM

Defined in

packages/lexical-code/src/CodeNode.ts:175


exportJSON

exportJSON(): SerializedCodeNode

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

SerializedCodeNode

Overrides

ElementNode.exportJSON

Defined in

packages/lexical-code/src/CodeNode.ts:278


getIsSyntaxHighlightSupported

getIsSyntaxHighlightSupported(): boolean

Returns

boolean

Defined in

packages/lexical-code/src/CodeNode.ts:394


getLanguage

getLanguage(): undefined | null | string

Returns

undefined | null | string

Defined in

packages/lexical-code/src/CodeNode.ts:384


getTheme

getTheme(): undefined | string

Returns

undefined | string

Defined in

packages/lexical-code/src/CodeNode.ts:404


insertNewAfter

insertNewAfter(selection, restoreSelection?): null | TabNode | ParagraphNode | CodeHighlightNode

Parameters

NameTypeDefault value
selectionRangeSelectionundefined
restoreSelectionbooleantrue

Returns

null | TabNode | ParagraphNode | CodeHighlightNode

Overrides

ElementNode.insertNewAfter

Defined in

packages/lexical-code/src/CodeNode.ts:287


setIsSyntaxHighlightSupported

setIsSyntaxHighlightSupported(isSupported): this

Parameters

NameType
isSupportedboolean

Returns

this

Defined in

packages/lexical-code/src/CodeNode.ts:388


setLanguage

setLanguage(language): this

Parameters

NameType
languageundefined | null | string

Returns

this

Defined in

packages/lexical-code/src/CodeNode.ts:378


setTheme

setTheme(theme): this

Parameters

NameType
themeundefined | null | string

Returns

this

Defined in

packages/lexical-code/src/CodeNode.ts:398


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
prevNodethis
domHTMLElement
configEditorConfig

Returns

boolean

Overrides

ElementNode.updateDOM

Defined in

packages/lexical-code/src/CodeNode.ts:123


updateFromJSON

updateFromJSON(serializedNode): this

Update 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.

Parameters

NameType
serializedNodeLexicalUpdateJSON<SerializedCodeNode>

Returns

this

Example

class MyTextNode extends TextNode {
// ...
static importJSON(serializedNode: SerializedMyTextNode): MyTextNode {
return $createMyTextNode()
.updateFromJSON(serializedNode);
}
updateFromJSON(
serializedNode: LexicalUpdateJSON<SerializedMyTextNode>,
): this {
return super.updateFromJSON(serializedNode)
.setMyProperty(serializedNode.myProperty);
}
}

Overrides

ElementNode.updateFromJSON

Defined in

packages/lexical-code/src/CodeNode.ts:271


clone

clone(node): CodeNode

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
nodeCodeNode

Returns

CodeNode

Overrides

ElementNode.clone

Defined in

packages/lexical-code/src/CodeNode.ts:81


getType

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

ElementNode.getType

Defined in

packages/lexical-code/src/CodeNode.ts:77


importDOM

importDOM(): null | DOMConversionMap

Returns

null | DOMConversionMap

Overrides

ElementNode.importDOM

Defined in

packages/lexical-code/src/CodeNode.ts:200


importJSON

importJSON(serializedNode): CodeNode

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
serializedNodeSerializedCodeNode

Returns

CodeNode

Overrides

ElementNode.importJSON

Defined in

packages/lexical-code/src/CodeNode.ts:267