variable-node.tsx


Overview

The variable-node.tsx file defines a custom Lexical editor node representing a "variable" element within rich text content. This node extends the DecoratorNode class from the Lexical framework to provide a specialized inline component that visually displays a variable label, optionally prefixed by a parent label and an icon. It facilitates the embedding of variable references in an editable text area, offering both a textual representation and a React-rendered visual decoration.

This file primarily supports the rendering and manipulation of variable nodes inside a Lexical editor instance, allowing for consistent display and interaction with variable placeholders in user-generated content or flow definitions.


Detailed Explanation

Imports


Class: VariableNode

VariableNode extends DecoratorNode<ReactNode>, meaning it leverages Lexical's decorator node functionality to embed React components inline within the editor content.

Properties

Property

Type

Description

__value

string

The internal value or identifier of the variable.

__label

string

The display label text for the variable.

key

NodeKey (optional)

Unique key identifier for the node instance.

__parentLabel

`string

ReactNode` (optional)

__icon

ReactNode (optional)

Optional icon shown alongside the parent label.

Static Methods

Constructor

constructor(
  value: string,
  label: string,
  key?: NodeKey,
  parent?: string | ReactNode,
  icon?: ReactNode,
)

Creates a new VariableNode.

Instance Methods


Functions

$createVariableNode

function $createVariableNode(
  value: string,
  label: string,
  parentLabel: string | ReactNode,
  icon?: ReactNode,
): VariableNode

Factory function to create a new VariableNode instance.

$isVariableNode

function $isVariableNode(
  node: LexicalNode | null | undefined,
): node is VariableNode

Type guard function to check if a given Lexical node is an instance of VariableNode.


Implementation Details


Interaction with Other Parts of the System


Usage Example

import { $createVariableNode, $isVariableNode } from './variable-node';

// Creating a variable node
const variableNode = $createVariableNode(
  'userName',
  'User Name',
  'User Info',
  <UserIcon />
);

// Checking node type
if ($isVariableNode(variableNode)) {
  console.log(variableNode.getTextContent()); // Outputs: {userName}
}

Visual Diagram: Component Interaction

classDiagram
    class VariableNode {
        -__value: string
        -__label: string
        -key?: NodeKey
        -__parentLabel?: string | ReactNode
        -__icon?: ReactNode
        +static getType(): string
        +static clone(node: VariableNode): VariableNode
        +constructor(value: string, label: string, key?: NodeKey, parent?: string | ReactNode, icon?: ReactNode)
        +createDOM(): HTMLElement
        +updateDOM(): false
        +decorate(): ReactNode
        +getTextContent(): string
    }

    VariableNode ..> DecoratorNode : extends
    VariableNode ..> ReactNode : uses in properties (__icon, __parentLabel)
    DecoratorNode ..|> LexicalNode

    class $createVariableNode {
        +function(value: string, label: string, parentLabel: string | ReactNode, icon?: ReactNode): VariableNode
    }

    class $isVariableNode {
        +function(node: LexicalNode | null | undefined): node is VariableNode
    }

    $createVariableNode --> VariableNode : creates
    $isVariableNode --> VariableNode : type guard

Summary

The variable-node.tsx file provides a specialized Lexical editor node class VariableNode and helper functions to create and identify such nodes. It supports rich visual decoration of variables with optional parent context and icons, enabling enhanced user interfaces for variable placeholders in flow or text editors. The design leverages React for rendering and Lexical for editor integration, forming a critical piece in the system's variable management and display functionality.