From bcd08633b45dfcf8b748bc4ae6f5f8de3d886ffd Mon Sep 17 00:00:00 2001 From: Gabe Kangas Date: Thu, 29 Jun 2023 20:34:06 -0700 Subject: [PATCH] fix(chat): simplify emoji + char count handling. Closes #3120 --- .../chat/ChatTextField/ChatTextField.tsx | 62 +++++++------------ 1 file changed, 23 insertions(+), 39 deletions(-) diff --git a/web/components/chat/ChatTextField/ChatTextField.tsx b/web/components/chat/ChatTextField/ChatTextField.tsx index 3fafb1970..e728098fa 100644 --- a/web/components/chat/ChatTextField/ChatTextField.tsx +++ b/web/components/chat/ChatTextField/ChatTextField.tsx @@ -67,7 +67,6 @@ function setCaretPosition(editableDiv, position) { } export const ChatTextField: FC = ({ defaultText, enabled, focusInput }) => { - const [showEmojis, setShowEmojis] = useState(false); const [characterCount, setCharacterCount] = useState(defaultText?.length); const websocketService = useRecoilValue(websocketServiceAtom); const text = useRef(defaultText || ''); @@ -97,33 +96,21 @@ export const ChatTextField: FC = ({ defaultText, enabled, fo forceUpdate(); }; - const insertTextAtCursor = (textToInsert: string) => { - let cursorLocation; - if (savedCursorLocation > 0) { - cursorLocation = savedCursorLocation; - } else { - cursorLocation = getCaretPosition(document.getElementById('chat-input')); - } - - const output = [ - text.current.slice(0, cursorLocation), - textToInsert, - text.current.slice(cursorLocation), - ].join(''); - + const insertTextAtEnd = (textToInsert: string) => { + const output = text.current + textToInsert; text.current = output; forceUpdate(); }; // Native emoji const onEmojiSelect = (emoji: string) => { - insertTextAtCursor(emoji); + insertTextAtEnd(emoji); }; // Custom emoji images const onCustomEmojiSelect = (name: string, emoji: string) => { const html = `${name}`; - insertTextAtCursor(html); + insertTextAtEnd(html); }; const onKeyDown = (e: React.KeyboardEvent) => { @@ -136,13 +123,11 @@ export const ChatTextField: FC = ({ defaultText, enabled, fo // Always allow backspace. if (e.key === 'Backspace') { - setCharacterCount(charCount - 1); return; } // Always allow delete. if (e.key === 'Delete') { - setCharacterCount(charCount - 1); return; } @@ -154,6 +139,7 @@ export const ChatTextField: FC = ({ defaultText, enabled, fo // Limit the number of characters. if (charCount + 1 > characterLimit) { e.preventDefault(); + return; } // Send the message when hitting enter. @@ -182,6 +168,10 @@ export const ChatTextField: FC = ({ defaultText, enabled, fo }, }); text.current = sanitized; + + const charCountString = sanitized.replace(/<\/?[^>]+(>|$)/g, ''); + setCharacterCount(charCountString.length); + setSavedCursorLocation( getCaretPosition(document.getElementById('chat-input-content-editable')), ); @@ -242,19 +232,6 @@ export const ChatTextField: FC = ({ defaultText, enabled, fo characterCount >= characterLimit && styles.maxCharacters, )} > - - } - trigger="click" - placement="topRight" - onOpenChange={open => setShowEmojis(open)} - open={showEmojis} - /> = ({ defaultText, enabled, fo /> {enabled && (
- + +