variable-on-change-plugin.tsx


Overview

The variable-on-change-plugin.tsx file defines a React plugin component for the Lexical text editor framework. Its primary purpose is to listen for manual changes in the editor state and invoke a callback function when such changes occur. Specifically, it filters out programmatic updates to prevent unnecessary or incorrect event triggers. This plugin enables integration with external state management or side effects based on user-driven content modifications within the Lexical editor.


Detailed Explanation

Interfaces

VariableOnChangePluginProps

Defines the expected props for the VariableOnChangePlugin component.

Property

Type

Description

onChange

(editorState: EditorState, editor?: LexicalEditor, tags?: Set<string>) => void

Callback function triggered when a manual content change occurs in the editor. It receives the current editor state and optionally the editor instance and a set of tags describing the update.


Function Components

VariableOnChangePlugin

function VariableOnChangePlugin({
  onChange,
}: VariableOnChangePluginProps): JSX.Element
Description

A React functional component that registers an update listener on a Lexical editor instance using the useLexicalComposerContext hook. It listens to editor state updates and calls the onChange callback only when the changes are manual (not programmatic) and when there are actual dirty elements (changed parts of the editor state).

Parameters

Name

Type

Description

onChange

(editorState: EditorState, editor?: LexicalEditor, tags?: Set<string>) => void

Callback to execute on user-driven editor changes.

Returns
Usage Example
import { VariableOnChangePlugin } from './variable-on-change-plugin';

function MyEditorWrapper() {
  const handleEditorChange = (editorState, editor, tags) => {
    console.log('Editor content changed manually.', editorState);
    // Additional logic such as updating state or syncing with backend
  };

  return (
    <LexicalComposer initialConfig={...}>
      {/* other editor plugins */}
      <VariableOnChangePlugin onChange={handleEditorChange} />
    </LexicalComposer>
  );
}
Implementation Details

This ensures that the onChange callback reflects user-initiated changes and prevents triggering on internal or programmatic editor updates, preserving content integrity and avoiding redundant processing.


Constants and Imports


Interaction with Other System Components


Visual Diagram

componentDiagram
    component VariableOnChangePlugin {
        +onChange(editorState, editor?, tags?)
    }
    component LexicalEditor
    component LexicalComposerContext
    component ProgrammaticTag

    VariableOnChangePlugin --> LexicalComposerContext : uses
    VariableOnChangePlugin --> LexicalEditor : registerUpdateListener()
    VariableOnChangePlugin ..> ProgrammaticTag : checks tag presence

Summary


End of Documentation