MM-54856: Place emoji at cursor position while editing message (#27418)

* MM-54856: fix issue with emoji position in edit post editor

* use useRef instead of useState

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Ashish Dhama
2024-07-13 01:29:22 +05:30
коммит произвёл GitHub
родитель a4f2ec744c
Коммит 9bb22a369a
2 изменённых файлов: 12 добавлений и 5 удалений

Просмотреть файл

@@ -100,6 +100,7 @@ const EditPost = ({editingPost, actions, canEditPost, config, channelId, draft,
draft.message || editingPost?.post?.message_source || editingPost?.post?.message || '', draft.message || editingPost?.post?.message_source || editingPost?.post?.message || '',
); );
const [selectionRange, setSelectionRange] = useState<State['selectionRange']>({start: editText.length, end: editText.length}); const [selectionRange, setSelectionRange] = useState<State['selectionRange']>({start: editText.length, end: editText.length});
const caretPosition = useRef<number>(editText.length);
const [postError, setPostError] = useState<React.ReactNode | null>(null); const [postError, setPostError] = useState<React.ReactNode | null>(null);
const [errorClass, setErrorClass] = useState<string>(''); const [errorClass, setErrorClass] = useState<string>('');
const [showEmojiPicker, setShowEmojiPicker] = useState<boolean>(false); const [showEmojiPicker, setShowEmojiPicker] = useState<boolean>(false);
@@ -160,7 +161,12 @@ const EditPost = ({editingPost, actions, canEditPost, config, channelId, draft,
}, [selectionRange]); }, [selectionRange]);
// just a helper so it's not always needed to update with setting both properties to the same value // just a helper so it's not always needed to update with setting both properties to the same value
const setCaretPosition = (position: number) => setSelectionRange({start: position, end: position}); const setSelectionRangeByCaretPosition = (position: number) => setSelectionRange({start: position, end: position});
const handleBlur = (e: React.FocusEvent<TextboxElement, Element>) => {
const target = e.target as HTMLTextAreaElement;
caretPosition.current = target.selectionEnd;
};
const handlePaste = useCallback((e: ClipboardEvent) => { const handlePaste = useCallback((e: ClipboardEvent) => {
const {clipboardData, target} = e; const {clipboardData, target} = e;
@@ -194,7 +200,7 @@ const EditPost = ({editingPost, actions, canEditPost, config, channelId, draft,
} }
setEditText(message); setEditText(message);
setCaretPosition(newCaretPosition); setSelectionRangeByCaretPosition(newCaretPosition);
}, [canEditPost, selectionRange, editText]); }, [canEditPost, selectionRange, editText]);
const isSaveDisabled = () => { const isSaveDisabled = () => {
@@ -411,7 +417,7 @@ const EditPost = ({editingPost, actions, canEditPost, config, channelId, draft,
if (editText.length > 0) { if (editText.length > 0) {
const {firstPiece, lastPiece} = splitMessageBasedOnCaretPosition( const {firstPiece, lastPiece} = splitMessageBasedOnCaretPosition(
selectionRange.start, caretPosition.current,
editText, editText,
); );
@@ -427,7 +433,7 @@ const EditPost = ({editingPost, actions, canEditPost, config, channelId, draft,
}; };
setEditText(newMessage); setEditText(newMessage);
setCaretPosition(newCaretPosition); setSelectionRangeByCaretPosition(newCaretPosition);
setShowEmojiPicker(false); setShowEmojiPicker(false);
textboxRef.current?.focus(); textboxRef.current?.focus();
}; };
@@ -510,6 +516,7 @@ const EditPost = ({editingPost, actions, canEditPost, config, channelId, draft,
onChange={handleChange} onChange={handleChange}
onKeyPress={handleEditKeyPress} onKeyPress={handleEditKeyPress}
onKeyDown={handleKeyDown} onKeyDown={handleKeyDown}
onBlur={handleBlur}
onHeightChange={handleHeightChange} onHeightChange={handleHeightChange}
handlePostError={handlePostError} handlePostError={handlePostError}
onPaste={handlePaste} onPaste={handlePaste}

Просмотреть файл

@@ -296,7 +296,7 @@ export default class SuggestionBox extends React.PureComponent {
this.setState({focused: false}); this.setState({focused: false});
if (this.props.onBlur) { if (this.props.onBlur) {
this.props.onBlur(); this.props.onBlur(e);
} }
}; };