lexical: pass the actual EditorState as the ref, count with onChange callback
This commit is contained in:
parent
89b1574c77
commit
448d320f4e
2 changed files with 13 additions and 8 deletions
|
@ -1,4 +1,5 @@
|
||||||
import clsx from 'clsx';
|
import clsx from 'clsx';
|
||||||
|
import { type EditorState } 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';
|
||||||
|
@ -101,13 +102,13 @@ const ComposeForm = <ID extends string>({ id, shouldCondense, autoFocus, clickab
|
||||||
const anyMedia = compose.media_attachments.size > 0;
|
const anyMedia = compose.media_attachments.size > 0;
|
||||||
|
|
||||||
const [composeFocused, setComposeFocused] = useState(false);
|
const [composeFocused, setComposeFocused] = useState(false);
|
||||||
|
const [text, setText] = useState('');
|
||||||
|
|
||||||
const firstRender = useRef(true);
|
const firstRender = useRef(true);
|
||||||
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<string>(null);
|
const editorStateRef = useRef<EditorState>(null);
|
||||||
const text = editorStateRef.current || '';
|
|
||||||
|
|
||||||
const { isDraggedOver } = useDraggedFiles(formRef);
|
const { isDraggedOver } = useDraggedFiles(formRef);
|
||||||
|
|
||||||
|
@ -145,7 +146,7 @@ const ComposeForm = <ID extends string>({ id, shouldCondense, autoFocus, clickab
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleSubmit = (e?: React.FormEvent<Element>) => {
|
const handleSubmit = (e?: React.FormEvent<Element>) => {
|
||||||
dispatch(changeCompose(id, editorStateRef.current!));
|
dispatch(changeCompose(id, text));
|
||||||
|
|
||||||
// Submit disabled:
|
// Submit disabled:
|
||||||
const fulltext = [spoilerText, countableText(text)].join('');
|
const fulltext = [spoilerText, countableText(text)].join('');
|
||||||
|
@ -322,6 +323,7 @@ const ComposeForm = <ID extends string>({ id, shouldCondense, autoFocus, clickab
|
||||||
autoFocus={shouldAutoFocus}
|
autoFocus={shouldAutoFocus}
|
||||||
hasPoll={hasPoll}
|
hasPoll={hasPoll}
|
||||||
handleSubmit={handleSubmit}
|
handleSubmit={handleSubmit}
|
||||||
|
onChange={setText}
|
||||||
onFocus={handleComposeFocus}
|
onFocus={handleComposeFocus}
|
||||||
onPaste={onPaste}
|
onPaste={onPaste}
|
||||||
/>
|
/>
|
||||||
|
|
|
@ -13,7 +13,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 } from 'lexical';
|
import { $createParagraphNode, $createTextNode, $getRoot, type EditorState } from 'lexical';
|
||||||
import React, { useMemo, useState } from 'react';
|
import React, { useMemo, useState } from 'react';
|
||||||
import { FormattedMessage } from 'react-intl';
|
import { FormattedMessage } from 'react-intl';
|
||||||
|
|
||||||
|
@ -40,9 +40,10 @@ interface IComposeEditor {
|
||||||
eventDiscussion?: boolean
|
eventDiscussion?: boolean
|
||||||
hasPoll?: boolean
|
hasPoll?: boolean
|
||||||
autoFocus?: boolean
|
autoFocus?: boolean
|
||||||
handleSubmit?: () => void
|
handleSubmit?(): void
|
||||||
|
onPaste?(files: FileList): void
|
||||||
|
onChange?(text: string): void
|
||||||
onFocus?: React.FocusEventHandler<HTMLDivElement>
|
onFocus?: React.FocusEventHandler<HTMLDivElement>
|
||||||
onPaste?: (files: FileList) => void
|
|
||||||
placeholder?: JSX.Element | string
|
placeholder?: JSX.Element | string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +66,7 @@ const theme: InitialConfigType['theme'] = {
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const ComposeEditor = React.forwardRef<string, IComposeEditor>(({
|
const ComposeEditor = React.forwardRef<EditorState, IComposeEditor>(({
|
||||||
className,
|
className,
|
||||||
placeholderClassName,
|
placeholderClassName,
|
||||||
composeId,
|
composeId,
|
||||||
|
@ -74,6 +75,7 @@ const ComposeEditor = React.forwardRef<string, IComposeEditor>(({
|
||||||
hasPoll,
|
hasPoll,
|
||||||
autoFocus,
|
autoFocus,
|
||||||
handleSubmit,
|
handleSubmit,
|
||||||
|
onChange,
|
||||||
onFocus,
|
onFocus,
|
||||||
onPaste,
|
onPaste,
|
||||||
placeholder,
|
placeholder,
|
||||||
|
@ -153,8 +155,9 @@ const ComposeEditor = React.forwardRef<string, IComposeEditor>(({
|
||||||
ErrorBoundary={LexicalErrorBoundary}
|
ErrorBoundary={LexicalErrorBoundary}
|
||||||
/>
|
/>
|
||||||
<OnChangePlugin onChange={(_, editor) => {
|
<OnChangePlugin onChange={(_, editor) => {
|
||||||
|
onChange?.(editor.getEditorState().read(() => $getRoot().getTextContent()));
|
||||||
if (editorStateRef && typeof editorStateRef !== 'function') {
|
if (editorStateRef && typeof editorStateRef !== 'function') {
|
||||||
editorStateRef.current = editor.getEditorState().read(() => $getRoot().getTextContent());
|
editorStateRef.current = editor.getEditorState();
|
||||||
}
|
}
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
|
|
Loading…
Reference in a new issue