Skip to content

Commit

Permalink
Fixes @mention draft changes
Browse files Browse the repository at this point in the history
Co-authored-by: Josh Perez <60019601+josh-signal@users.noreply.github.com>
  • Loading branch information
automated-signal and josh-signal committed Jan 19, 2023
1 parent 0252b0d commit 4f1e129
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
6 changes: 3 additions & 3 deletions ts/components/CompositionArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -369,16 +369,16 @@ export function CompositionArea({
const previousConversationId = usePrevious(conversationId, conversationId);
useEffect(() => {
if (!draftText) {
inputApiRef.current?.setText('');
inputApiRef.current?.setContents('');
return;
}

if (conversationId === previousConversationId) {
return;
}

inputApiRef.current?.setText(draftText, true);
}, [conversationId, draftText, previousConversationId]);
inputApiRef.current?.setContents(draftText, draftBodyRanges, true);
}, [conversationId, draftBodyRanges, draftText, previousConversationId]);

const handleToggleLarge = useCallback(() => {
setLarge(l => !l);
Expand Down
24 changes: 19 additions & 5 deletions ts/components/CompositionInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Delta from 'quill-delta';
import ReactQuill from 'react-quill';
import classNames from 'classnames';
import { Manager, Reference } from 'react-popper';
import type { KeyboardStatic, RangeStatic } from 'quill';
import type { DeltaStatic, KeyboardStatic, RangeStatic } from 'quill';
import Quill from 'quill';

import { MentionCompletion } from '../quill/mentions/completion';
Expand Down Expand Up @@ -60,7 +60,11 @@ type HistoryStatic = {
export type InputApi = {
focus: () => void;
insertEmoji: (e: EmojiPickDataType) => void;
setText: (text: string, cursorToEnd?: boolean) => void;
setContents: (
text: string,
draftBodyRanges?: DraftBodyRangesType,
cursorToEnd?: boolean
) => void;
reset: () => void;
submit: () => void;
};
Expand Down Expand Up @@ -234,15 +238,25 @@ export function CompositionInput(props: Props): React.ReactElement {
historyModule.clear();
};

const setText = (text: string, cursorToEnd?: boolean) => {
const setContents = (
text: string,
bodyRanges?: DraftBodyRangesType,
cursorToEnd?: boolean
) => {
const quill = quillRef.current;

if (quill === undefined) {
return;
}

const delta = generateDelta(text || '', bodyRanges || []);

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) {
quill.setSelection(quill.getLength(), 0);
}
Expand Down Expand Up @@ -276,7 +290,7 @@ export function CompositionInput(props: Props): React.ReactElement {
inputApi.current = {
focus,
insertEmoji,
setText,
setContents,
reset,
submit,
};
Expand Down
2 changes: 1 addition & 1 deletion ts/components/CompositionTextArea.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export function CompositionTextArea({
// was modifying text in the middle of the editor
// a better solution would be to prevent the change to begin with, but
// quill makes this VERY difficult
inputEl.setText(newValueSized, true);
inputEl.setContents(newValueSized, bodyRanges, true);
}
}
setCharacterCount(newCharacterCount);
Expand Down

0 comments on commit 4f1e129

Please sign in to comment.