"use client"; import { useLexicalComposerContext } from "@lexical/react/LexicalComposerContext"; import { $getSelection, $isRangeSelection } from "lexical"; import { useEffect } from "react"; import { $isVariableNode } from "../nodes/variable-node"; export function VariableSelectionPlugin({ onSelectVariable, }: { onSelectVariable?: (variableName: string | null) => void; }) { const [editor] = useLexicalComposerContext(); useEffect(() => { if (!onSelectVariable) return; return editor.registerUpdateListener(({ editorState }) => { editorState.read(() => { const selection = $getSelection(); if ($isRangeSelection(selection)) { const nodes = selection.getNodes(); const variableNode = nodes.find($isVariableNode); onSelectVariable(variableNode ? variableNode.__variableName : null); } else { onSelectVariable(null); } }); }); }, [editor, onSelectVariable]); return null; }