From 448d320f4e81dce183071f3501cddbf8ca41ec22 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 23 Sep 2023 14:54:47 -0500 Subject: [PATCH] lexical: pass the actual EditorState as the ref, count with onChange callback --- src/features/compose/components/compose-form.tsx | 8 +++++--- src/features/compose/editor/index.tsx | 13 ++++++++----- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/src/features/compose/components/compose-form.tsx b/src/features/compose/components/compose-form.tsx index e0533642d..132f8c5c2 100644 --- a/src/features/compose/components/compose-form.tsx +++ b/src/features/compose/components/compose-form.tsx @@ -1,4 +1,5 @@ import clsx from 'clsx'; +import { type EditorState } from 'lexical'; import React, { useCallback, useEffect, useRef, useState } from 'react'; import { defineMessages, FormattedMessage, useIntl } from 'react-intl'; import { Link, useHistory } from 'react-router-dom'; @@ -101,13 +102,13 @@ const ComposeForm = ({ id, shouldCondense, autoFocus, clickab const anyMedia = compose.media_attachments.size > 0; const [composeFocused, setComposeFocused] = useState(false); + const [text, setText] = useState(''); const firstRender = useRef(true); const formRef = useRef(null); const spoilerTextRef = useRef(null); const autosuggestTextareaRef = useRef(null); - const editorStateRef = useRef(null); - const text = editorStateRef.current || ''; + const editorStateRef = useRef(null); const { isDraggedOver } = useDraggedFiles(formRef); @@ -145,7 +146,7 @@ const ComposeForm = ({ id, shouldCondense, autoFocus, clickab }; const handleSubmit = (e?: React.FormEvent) => { - dispatch(changeCompose(id, editorStateRef.current!)); + dispatch(changeCompose(id, text)); // Submit disabled: const fulltext = [spoilerText, countableText(text)].join(''); @@ -322,6 +323,7 @@ const ComposeForm = ({ id, shouldCondense, autoFocus, clickab autoFocus={shouldAutoFocus} hasPoll={hasPoll} handleSubmit={handleSubmit} + onChange={setText} onFocus={handleComposeFocus} onPaste={onPaste} /> diff --git a/src/features/compose/editor/index.tsx b/src/features/compose/editor/index.tsx index d24ca4ae4..a0cba4ac2 100644 --- a/src/features/compose/editor/index.tsx +++ b/src/features/compose/editor/index.tsx @@ -13,7 +13,7 @@ import { HistoryPlugin } from '@lexical/react/LexicalHistoryPlugin'; import { OnChangePlugin } from '@lexical/react/LexicalOnChangePlugin'; import { PlainTextPlugin } from '@lexical/react/LexicalPlainTextPlugin'; import clsx from 'clsx'; -import { $createParagraphNode, $createTextNode, $getRoot } from 'lexical'; +import { $createParagraphNode, $createTextNode, $getRoot, type EditorState } from 'lexical'; import React, { useMemo, useState } from 'react'; import { FormattedMessage } from 'react-intl'; @@ -40,9 +40,10 @@ interface IComposeEditor { eventDiscussion?: boolean hasPoll?: boolean autoFocus?: boolean - handleSubmit?: () => void + handleSubmit?(): void + onPaste?(files: FileList): void + onChange?(text: string): void onFocus?: React.FocusEventHandler - onPaste?: (files: FileList) => void placeholder?: JSX.Element | string } @@ -65,7 +66,7 @@ const theme: InitialConfigType['theme'] = { }, }; -const ComposeEditor = React.forwardRef(({ +const ComposeEditor = React.forwardRef(({ className, placeholderClassName, composeId, @@ -74,6 +75,7 @@ const ComposeEditor = React.forwardRef(({ hasPoll, autoFocus, handleSubmit, + onChange, onFocus, onPaste, placeholder, @@ -153,8 +155,9 @@ const ComposeEditor = React.forwardRef(({ ErrorBoundary={LexicalErrorBoundary} /> { + onChange?.(editor.getEditorState().read(() => $getRoot().getTextContent())); if (editorStateRef && typeof editorStateRef !== 'function') { - editorStateRef.current = editor.getEditorState().read(() => $getRoot().getTextContent()); + editorStateRef.current = editor.getEditorState(); } }} />