From d92c150d9d0ff69b5c3222afe019cfaa70aa2f5a Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 23 Sep 2023 15:01:16 -0500 Subject: [PATCH] lexical: set the actual editor as the ref, clear the form on submit --- src/features/compose/components/compose-form.tsx | 7 ++++--- src/features/compose/editor/index.tsx | 8 +++++--- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/features/compose/components/compose-form.tsx b/src/features/compose/components/compose-form.tsx index 132f8c5c2..db316b138 100644 --- a/src/features/compose/components/compose-form.tsx +++ b/src/features/compose/components/compose-form.tsx @@ -1,5 +1,5 @@ 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 { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import { Link, useHistory } from 'react-router-dom'; @@ -108,7 +108,7 @@ const ComposeForm = ({ id, shouldCondense, autoFocus, clickab const formRef = useRef(null); const spoilerTextRef = useRef(null); const autosuggestTextareaRef = useRef(null); - const editorStateRef = useRef(null); + const editorRef = useRef(null); const { isDraggedOver } = useDraggedFiles(formRef); @@ -160,6 +160,7 @@ const ComposeForm = ({ id, shouldCondense, autoFocus, clickab } dispatch(submitCompose(id, history)); + editorRef.current?.dispatchCommand(CLEAR_EDITOR_COMMAND, undefined); }; const onSuggestionsClearRequested = () => { @@ -315,7 +316,7 @@ const ComposeForm = ({ id, shouldCondense, autoFocus, clickab {(Component: any) => ( (({ +const ComposeEditor = React.forwardRef(({ className, placeholderClassName, composeId, @@ -157,7 +158,7 @@ const ComposeEditor = React.forwardRef(({ { onChange?.(editor.getEditorState().read(() => $getRoot().getTextContent())); if (editorStateRef && typeof editorStateRef !== 'function') { - editorStateRef.current = editor.getEditorState(); + editorStateRef.current = editor; } }} /> @@ -168,6 +169,7 @@ const ComposeEditor = React.forwardRef(({ + );