Fixes @mention draft changes

This commit is contained in:
Josh Perez 2023-01-18 19:59:47 -05:00 committed by GitHub
parent fa938e8c7d
commit e3d9e6b906
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 9 deletions

View file

@ -369,7 +369,7 @@ export function CompositionArea({
const previousConversationId = usePrevious(conversationId, conversationId); const previousConversationId = usePrevious(conversationId, conversationId);
useEffect(() => { useEffect(() => {
if (!draftText) { if (!draftText) {
inputApiRef.current?.setText(''); inputApiRef.current?.setContents('');
return; return;
} }
@ -377,8 +377,8 @@ export function CompositionArea({
return; return;
} }
inputApiRef.current?.setText(draftText, true); inputApiRef.current?.setContents(draftText, draftBodyRanges, true);
}, [conversationId, draftText, previousConversationId]); }, [conversationId, draftBodyRanges, draftText, previousConversationId]);
const handleToggleLarge = useCallback(() => { const handleToggleLarge = useCallback(() => {
setLarge(l => !l); setLarge(l => !l);

View file

@ -7,7 +7,7 @@ import Delta from 'quill-delta';
import ReactQuill from 'react-quill'; import ReactQuill from 'react-quill';
import classNames from 'classnames'; import classNames from 'classnames';
import { Manager, Reference } from 'react-popper'; import { Manager, Reference } from 'react-popper';
import type { KeyboardStatic, RangeStatic } from 'quill'; import type { DeltaStatic, KeyboardStatic, RangeStatic } from 'quill';
import Quill from 'quill'; import Quill from 'quill';
import { MentionCompletion } from '../quill/mentions/completion'; import { MentionCompletion } from '../quill/mentions/completion';
@ -60,7 +60,11 @@ type HistoryStatic = {
export type InputApi = { export type InputApi = {
focus: () => void; focus: () => void;
insertEmoji: (e: EmojiPickDataType) => void; insertEmoji: (e: EmojiPickDataType) => void;
setText: (text: string, cursorToEnd?: boolean) => void; setContents: (
text: string,
draftBodyRanges?: DraftBodyRangesType,
cursorToEnd?: boolean
) => void;
reset: () => void; reset: () => void;
submit: () => void; submit: () => void;
}; };
@ -234,15 +238,25 @@ export function CompositionInput(props: Props): React.ReactElement {
historyModule.clear(); historyModule.clear();
}; };
const setText = (text: string, cursorToEnd?: boolean) => { const setContents = (
text: string,
bodyRanges?: DraftBodyRangesType,
cursorToEnd?: boolean
) => {
const quill = quillRef.current; const quill = quillRef.current;
if (quill === undefined) { if (quill === undefined) {
return; return;
} }
const delta = generateDelta(text || '', bodyRanges || []);
canSendRef.current = true; canSendRef.current = true;
quill.setText(text); // We need to cast here because we use @types/quill@1.3.10 which has types
// for quill-delta even though quill-delta is written in TS and has its own
// types. @types/quill@2.0.0 fixes the issue but react-quill has a peer-dep
// on the older quill types.
quill.setContents(delta as unknown as DeltaStatic);
if (cursorToEnd) { if (cursorToEnd) {
quill.setSelection(quill.getLength(), 0); quill.setSelection(quill.getLength(), 0);
} }
@ -276,7 +290,7 @@ export function CompositionInput(props: Props): React.ReactElement {
inputApi.current = { inputApi.current = {
focus, focus,
insertEmoji, insertEmoji,
setText, setContents,
reset, reset,
submit, submit,
}; };

View file

@ -112,7 +112,7 @@ export function CompositionTextArea({
// was modifying text in the middle of the editor // was modifying text in the middle of the editor
// a better solution would be to prevent the change to begin with, but // a better solution would be to prevent the change to begin with, but
// quill makes this VERY difficult // quill makes this VERY difficult
inputEl.setText(newValueSized, true); inputEl.setContents(newValueSized, bodyRanges, true);
} }
} }
setCharacterCount(newCharacterCount); setCharacterCount(newCharacterCount);