variable-node.tsx
Overview
variable-node.tsx defines a custom Lexical editor node called VariableNode that represents a variable element within a rich text editing environment. This node extends Lexical's DecoratorNode to provide a React-based rendering of variables with customized styling and behavior.
The main purpose of this file is to encapsulate the logic for creating, cloning, rendering, and identifying variable nodes within the editor content. It supports displaying variables with a label, applying specific styles, and handling special prefixes to indicate "begin" variables.
Classes and Functions
Class: VariableNode
A subclass of Lexical's DecoratorNode, representing a variable element that can be inserted and rendered inside the text editor content.
Properties
Property | Type | Description |
|---|---|---|
|
| The internal value/key of the variable. |
|
| The text label displayed for the variable. |
Static Methods
getType(): stringReturns the string 'variable' which is the type identifier for this custom node.
static getType(): stringclone(node: VariableNode): VariableNodeCreates a new instance of
VariableNodeby cloning an existing one, preserving its value, label, and key.static clone(node: VariableNode): VariableNode
Constructor
constructor(value: string, label: string, key?: NodeKey)
Parameters:
value: The internal value/key for the variable.label: The display label for the variable.key(optional): Lexical node key.
Initializes a new VariableNode instance with the provided value, label, and optionally a key.
Instance Methods
createDOM(): HTMLElementCreates the DOM element for this node. Returns a
<span>element with a CSS class of'mr-1'to provide margin-right spacing.createDOM(): HTMLElementupdateDOM(): falseIndicates that the DOM does not need to be updated when the node updates, returning
falseto prevent unnecessary re-rendering.updateDOM(): falsedecorate(): ReactNodeReturns a React component that visually represents the variable node. It displays the variable's label styled with a blue color. If the variable's
__valuestarts with a specific prefix (defined asBeginId + '@'), it prepends a localized "begin" text before the label.The rendered content is wrapped in a styled
<div>with background color and rounded corners.decorate(): ReactNodeUsage Example:
<VariableNode value="start@123" label="MyVar" />This would render a blue label "MyVar" with a "begin" indicator if the value starts with the prefix.
getTextContent(): stringReturns the textual representation of the variable node, formatting the value within curly braces.
getTextContent(): stringExample return value:
"{myVariable}"
Functions
$createVariableNode(value: string, label: string): VariableNodeFactory function to create a new
VariableNodeinstance.function $createVariableNode(value: string, label: string): VariableNodeParameters:
value: The internal value/key of the variable.label: The display label to show.
Returns:
A new
VariableNodeinstance.
Example Usage:
const node = $createVariableNode('user_name', 'User Name');$isVariableNode(node: LexicalNode | null | undefined): node is VariableNodeType guard function to check if a given Lexical node is a
VariableNode.function $isVariableNode(node: LexicalNode | null | undefined): node is VariableNodeParameters:
node: The Lexical node to check.
Returns:
trueif the node is an instance ofVariableNode, otherwisefalse.
Example Usage:
if ($isVariableNode(someNode)) { // someNode is now typed as VariableNode }
Implementation Details
The class extends
DecoratorNode<ReactNode>, which allows integrating React components as visual decorations inside Lexical editor content.The
decorate()method leverages React to render the variable label with conditional logic:If the variable's value starts with a special prefix (
BeginId + '@'), it prepends a localized "begin" indicator.The prefix and localization are imported from other modules:
BeginIdfrom a constants file, andi18nfor translation.
The node's DOM element is a simple
<span>with some margin to separate it visually from other content.The node disables DOM updates by always returning
falseinupdateDOM(), optimizing rendering since changes to the node will trigger React re-rendering.
Interaction with Other Parts of the System
Localization: Uses a localization module (
i18n) to translate the "begin" label shown in the decorated node.Constants: Imports
BeginIdfrom a flow constants module to determine if a variable is a "begin" variable based on its value prefix.Lexical Editor: This file extends Lexical's node system by defining a custom node to represent variables inside the editor content. It integrates with Lexical's node lifecycle and rendering pipeline.
React: The node's visual decoration is rendered via React components, allowing rich styling and conditional rendering within the editor UI.
Diagram: Component Structure of VariableNode
classDiagram
class VariableNode {
- __value: string
- __label: string
+ static getType(): string
+ static clone(node: VariableNode): VariableNode
+ constructor(value: string, label: string, key?: NodeKey)
+ createDOM(): HTMLElement
+ updateDOM(): false
+ decorate(): ReactNode
+ getTextContent(): string
}
VariableNode ..|> DecoratorNode
class DecoratorNode {
<<abstract>>
+ createDOM()
+ updateDOM()
+ decorate()
}
Summary
The variable-node.tsx file provides a custom Lexical editor node to represent variables with labels and special "begin" indicators. It uses React for rendering, supports cloning, typing, and integrates with localization and constants modules. This abstraction allows variables to be visually distinct and semantically meaningful within rich text editor content.