lexical: set the actual editor as the ref, clear the form on submit

environments/review-lexical-ujdd17/deployments/4016
Alex Gleason 2023-09-23 15:01:16 -05:00
rodzic 448d320f4e
commit d92c150d9d
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 7211D1F99744FBB7
2 zmienionych plików z 9 dodań i 6 usunięć

Wyświetl plik

@ -1,5 +1,5 @@
import clsx from 'clsx'; import clsx from 'clsx';
import { type EditorState } from 'lexical'; import { CLEAR_EDITOR_COMMAND, type LexicalEditor } from 'lexical';
import React, { useCallback, useEffect, useRef, useState } from 'react'; import React, { useCallback, useEffect, useRef, useState } from 'react';
import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl';
import { Link, useHistory } from 'react-router-dom'; import { Link, useHistory } from 'react-router-dom';
@ -108,7 +108,7 @@ const ComposeForm = <ID extends string>({ id, shouldCondense, autoFocus, clickab
const formRef = useRef<HTMLDivElement>(null); const formRef = useRef<HTMLDivElement>(null);
const spoilerTextRef = useRef<AutosuggestInput>(null); const spoilerTextRef = useRef<AutosuggestInput>(null);
const autosuggestTextareaRef = useRef<AutosuggestTextarea>(null); const autosuggestTextareaRef = useRef<AutosuggestTextarea>(null);
const editorStateRef = useRef<EditorState>(null); const editorRef = useRef<LexicalEditor>(null);
const { isDraggedOver } = useDraggedFiles(formRef); const { isDraggedOver } = useDraggedFiles(formRef);
@ -160,6 +160,7 @@ const ComposeForm = <ID extends string>({ id, shouldCondense, autoFocus, clickab
} }
dispatch(submitCompose(id, history)); dispatch(submitCompose(id, history));
editorRef.current?.dispatchCommand(CLEAR_EDITOR_COMMAND, undefined);
}; };
const onSuggestionsClearRequested = () => { const onSuggestionsClearRequested = () => {
@ -315,7 +316,7 @@ const ComposeForm = <ID extends string>({ id, shouldCondense, autoFocus, clickab
<Bundle fetchComponent={ComposeEditor}> <Bundle fetchComponent={ComposeEditor}>
{(Component: any) => ( {(Component: any) => (
<Component <Component
ref={editorStateRef} ref={editorRef}
className='mt-2' className='mt-2'
composeId={id} composeId={id}
condensed={condensed} condensed={condensed}

Wyświetl plik

@ -5,6 +5,7 @@
*/ */
import { AutoLinkPlugin, createLinkMatcherWithRegExp } from '@lexical/react/LexicalAutoLinkPlugin'; import { AutoLinkPlugin, createLinkMatcherWithRegExp } from '@lexical/react/LexicalAutoLinkPlugin';
import { ClearEditorPlugin } from '@lexical/react/LexicalClearEditorPlugin';
import { LexicalComposer, InitialConfigType } from '@lexical/react/LexicalComposer'; import { LexicalComposer, InitialConfigType } from '@lexical/react/LexicalComposer';
import { ContentEditable } from '@lexical/react/LexicalContentEditable'; import { ContentEditable } from '@lexical/react/LexicalContentEditable';
import LexicalErrorBoundary from '@lexical/react/LexicalErrorBoundary'; import LexicalErrorBoundary from '@lexical/react/LexicalErrorBoundary';
@ -13,7 +14,7 @@ import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin';
import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin'; import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin';
import { PlainTextPlugin } from '@lexical/react/LexicalPlainTextPlugin'; import { PlainTextPlugin } from '@lexical/react/LexicalPlainTextPlugin';
import clsx from 'clsx'; import clsx from 'clsx';
import { $createParagraphNode, $createTextNode, $getRoot, type EditorState } from 'lexical'; import { $createParagraphNode, $createTextNode, $getRoot, type LexicalEditor } from 'lexical';
import React, { useMemo, useState } from 'react'; import React, { useMemo, useState } from 'react';
import { FormattedMessage } from 'react-intl'; import { FormattedMessage } from 'react-intl';
@ -66,7 +67,7 @@ const theme: InitialConfigType['theme'] = {
}, },
}; };
const ComposeEditor = React.forwardRef<EditorState, IComposeEditor>(({ const ComposeEditor = React.forwardRef<LexicalEditor, IComposeEditor>(({
className, className,
placeholderClassName, placeholderClassName,
composeId, composeId,
@ -157,7 +158,7 @@ const ComposeEditor = React.forwardRef<EditorState, IComposeEditor>(({
<OnChangePlugin onChange={(_, editor) => { <OnChangePlugin onChange={(_, editor) => {
onChange?.(editor.getEditorState().read(() => $getRoot().getTextContent())); onChange?.(editor.getEditorState().read(() => $getRoot().getTextContent()));
if (editorStateRef && typeof editorStateRef !== 'function') { if (editorStateRef && typeof editorStateRef !== 'function') {
editorStateRef.current = editor.getEditorState(); editorStateRef.current = editor;
} }
}} }}
/> />
@ -168,6 +169,7 @@ const ComposeEditor = React.forwardRef<EditorState, IComposeEditor>(({
<AutoLinkPlugin matchers={LINK_MATCHERS} /> <AutoLinkPlugin matchers={LINK_MATCHERS} />
<StatePlugin composeId={composeId} handleSubmit={handleSubmit} /> <StatePlugin composeId={composeId} handleSubmit={handleSubmit} />
<FocusPlugin autoFocus={autoFocus} /> <FocusPlugin autoFocus={autoFocus} />
<ClearEditorPlugin />
</div> </div>
</LexicalComposer> </LexicalComposer>
); );