lexical: add RefPlugin to set the ref

environments/review-lexical-ujdd17/deployments/4018
Alex Gleason 1 year ago
parent d92c150d9d
commit a67b5f8a25
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7

@ -24,6 +24,7 @@ import { useNodes } from './nodes';
import AutosuggestPlugin from './plugins/autosuggest-plugin';
import FocusPlugin from './plugins/focus-plugin';
import MentionPlugin from './plugins/mention-plugin';
import RefPlugin from './plugins/ref-plugin';
import StatePlugin from './plugins/state-plugin';
const LINK_MATCHERS = [
@ -80,7 +81,7 @@ const ComposeEditor = React.forwardRef<LexicalEditor, IComposeEditor>(({
onFocus,
onPaste,
placeholder,
}, editorStateRef) => {
}, ref) => {
const dispatch = useAppDispatch();
const nodes = useNodes();
@ -157,9 +158,6 @@ const ComposeEditor = React.forwardRef<LexicalEditor, IComposeEditor>(({
/>
<OnChangePlugin onChange={(_, editor) => {
onChange?.(editor.getEditorState().read(() => $getRoot().getTextContent()));
if (editorStateRef && typeof editorStateRef !== 'function') {
editorStateRef.current = editor;
}
}}
/>
<HistoryPlugin />
@ -170,6 +168,7 @@ const ComposeEditor = React.forwardRef<LexicalEditor, IComposeEditor>(({
<StatePlugin composeId={composeId} handleSubmit={handleSubmit} />
<FocusPlugin autoFocus={autoFocus} />
<ClearEditorPlugin />
<RefPlugin ref={ref} />
</div>
</LexicalComposer>
);

@ -0,0 +1,18 @@
import { useLexicalComposerContext } from '@lexical/react/LexicalComposerContext';
import { LexicalEditor } from 'lexical';
import React, { useEffect } from 'react';
/** Set the ref to the current Lexical editor instance. */
const RefPlugin = React.forwardRef<LexicalEditor>((_props, ref) => {
const [editor] = useLexicalComposerContext();
useEffect(() => {
if (ref && typeof ref !== 'function') {
ref.current = editor;
}
}, [editor]);
return null;
});
export default RefPlugin;
Loading…
Cancel
Save