variable-picker-plugin.tsx


Overview

variable-picker-plugin.tsx is a React component file that implements a custom variable picker menu plugin for a Lexical-based rich text editor. This plugin enables users to insert structured "variables" into the editor content via a typeahead menu triggered by the / character. The variables are organized into categories and can be filtered dynamically as users type. Selected variables are inserted as specialized nodes in the Lexical editor, supporting both textual and complex variable representations.

The plugin supports:


Detailed Explanation of Contents

Imports and Dependencies


Classes

VariableInnerOption (extends MenuOption)

Represents an individual selectable variable option in the menu.


VariableOption (extends MenuOption)

Represents a category grouping multiple VariableInnerOption items.


Functional Components

VariablePickerMenuItem

Renders a single category item in the menu, including its nested variable options.


Main Exported Component: VariablePickerMenuPlugin

A React component implementing the variable picker typeahead menu plugin.


Important Implementation Details / Algorithms


Interaction with Other Parts of the System


Usage Example

import VariablePickerMenuPlugin from './variable-picker-plugin';

function EditorWithVariablePicker() {
  const initialValue = "Hello {userName}, welcome to {platform}!";
  const extraOptions = [
    {
      label: "Custom Variables",
      title: "customVars",
      options: [
        { label: "User Age", value: "userAge" },
        { label: "Signup Date", value: "signupDate" },
      ],
    },
  ];

  return (
    <LexicalComposer initialConfig={/*...*/}>
      {/* Other plugins */}
      <VariablePickerMenuPlugin value={initialValue} extraOptions={extraOptions} />
      {/* Editor UI */}
    </LexicalComposer>
  );
}

Mermaid Diagram: Component Structure and Interactions

classDiagram
    class VariablePickerMenuPlugin {
        - editor: LexicalEditor
        - queryString: string | null
        - options: VariableOption[]
        + buildNextOptions(): VariableOption[]
        + findItemByValue(value: string): VariableInnerOption | undefined
        + onSelectOption(selectedOption: VariableInnerOption, nodeToRemove: TextNode | null, closeMenu: () => void): void
        + parseTextToVariableNodes(text: string): void
        + render(): JSX.Element
    }

    class VariableOption {
        +label: ReactElement | string
        +title: string
        +options: VariableInnerOption[]
        +constructor(label, title, options)
    }

    class VariableInnerOption {
        +label: string
        +value: string
        +parentLabel: string | JSX.Element
        +icon?: ReactNode
        +constructor(label, value, parentLabel, icon?)
    }

    class VariablePickerMenuItem {
        +index: number
        +option: VariableOption
        +selectOptionAndCleanUp(option: VariableOption | VariableInnerOption): void
        +render(): JSX.Element
    }

    VariablePickerMenuPlugin "1" --> "*" VariableOption : builds options
    VariableOption "1" --> "*" VariableInnerOption : contains
    VariablePickerMenuPlugin "1" --> "1" VariablePickerMenuItem : renders

Summary

variable-picker-plugin.tsx is a specialized React Lexical plugin that provides an intuitive, searchable variable insertion UI triggered by the / character within a rich text editor. It supports nested categories of variables, dynamic filtering, and seamless integration with the Lexical editor state and custom variable nodes. The file is modular, leveraging classes for menu options, React functional components for UI, and Lexical editor APIs for content manipulation.

This plugin is essential for applications that require users to insert dynamic placeholders or variables into text content, such as chatbots, template editors, or advanced form builders.