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 |
|---|---|---|
|
| 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 |
|---|---|---|
|
| Callback to execute on user-driven editor changes. |
Returns
null— This component does not render any UI directly; it acts purely as a plugin for side effects.
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
Context Hook: Uses
useLexicalComposerContextto obtain the currentLexicalEditorinstance.Effect Hook: Registers an update listener on the editor when the component mounts, and cleans it up on unmount.
Update Listener Logic:
Receives an object containing
editorState,tags, anddirtyElementson every update.Checks if the update was programmatic by inspecting the presence of a
ProgrammaticTagintags.Only calls
onChangeif there are dirty elements (changes to the editor state) and the update was not programmatic.
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
ProgrammaticTag: A constant imported from./constantused to mark updates as programmatic (not user-driven). This tag is used to filter updates within the listener.Lexical imports:
useLexicalComposerContext,EditorState,LexicalEditorfrom the Lexical framework.React imports:
useEffectfor lifecycle management.
Interaction with Other System Components
Lexical Editor: This plugin attaches itself to the Lexical editor instance provided by
LexicalComposerContext.Other Plugins: It can coexist with other Lexical plugins, as it only listens to editor changes.
External State or Systems: Through the
onChangecallback, this plugin allows external components or state managers to react to manual content changes, enabling synchronization or side effects (such as saving content, analytics, or UI updates).
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
Purpose: React plugin to detect manual changes in Lexical editor.
Key feature: Filters out programmatic updates to avoid incorrect
onChangetriggers.Usage: Pass a callback to
onChangeto react to user edits.Design: Lightweight, non-UI component relying on Lexical context and update listeners.
Benefit: Simplifies handling of editor content changes with clear distinction between manual and programmatic updates.
End of Documentation