lexical: add RefPlugin to set the ref
This commit is contained in:
parent
d92c150d9d
commit
a67b5f8a25
|
@ -24,6 +24,7 @@ import { useNodes } from './nodes';
|
||||||
import AutosuggestPlugin from './plugins/autosuggest-plugin';
|
import AutosuggestPlugin from './plugins/autosuggest-plugin';
|
||||||
import FocusPlugin from './plugins/focus-plugin';
|
import FocusPlugin from './plugins/focus-plugin';
|
||||||
import MentionPlugin from './plugins/mention-plugin';
|
import MentionPlugin from './plugins/mention-plugin';
|
||||||
|
import RefPlugin from './plugins/ref-plugin';
|
||||||
import StatePlugin from './plugins/state-plugin';
|
import StatePlugin from './plugins/state-plugin';
|
||||||
|
|
||||||
const LINK_MATCHERS = [
|
const LINK_MATCHERS = [
|
||||||
|
@ -80,7 +81,7 @@ const ComposeEditor = React.forwardRef<LexicalEditor, IComposeEditor>(({
|
||||||
onFocus,
|
onFocus,
|
||||||
onPaste,
|
onPaste,
|
||||||
placeholder,
|
placeholder,
|
||||||
}, editorStateRef) => {
|
}, ref) => {
|
||||||
const dispatch = useAppDispatch();
|
const dispatch = useAppDispatch();
|
||||||
const nodes = useNodes();
|
const nodes = useNodes();
|
||||||
|
|
||||||
|
@ -157,9 +158,6 @@ const ComposeEditor = React.forwardRef<LexicalEditor, IComposeEditor>(({
|
||||||
/>
|
/>
|
||||||
<OnChangePlugin onChange={(_, editor) => {
|
<OnChangePlugin onChange={(_, editor) => {
|
||||||
onChange?.(editor.getEditorState().read(() => $getRoot().getTextContent()));
|
onChange?.(editor.getEditorState().read(() => $getRoot().getTextContent()));
|
||||||
if (editorStateRef && typeof editorStateRef !== 'function') {
|
|
||||||
editorStateRef.current = editor;
|
|
||||||
}
|
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<HistoryPlugin />
|
<HistoryPlugin />
|
||||||
|
@ -170,6 +168,7 @@ const ComposeEditor = React.forwardRef<LexicalEditor, IComposeEditor>(({
|
||||||
<StatePlugin composeId={composeId} handleSubmit={handleSubmit} />
|
<StatePlugin composeId={composeId} handleSubmit={handleSubmit} />
|
||||||
<FocusPlugin autoFocus={autoFocus} />
|
<FocusPlugin autoFocus={autoFocus} />
|
||||||
<ClearEditorPlugin />
|
<ClearEditorPlugin />
|
||||||
|
<RefPlugin ref={ref} />
|
||||||
</div>
|
</div>
|
||||||
</LexicalComposer>
|
</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…
Reference in New Issue