Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cursor position when adding emoji #12632

Merged
merged 2 commits into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/pages/home/report/ReportActionCompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,9 +383,19 @@ class ReportActionCompose extends React.Component {
*/
updateComment(comment, shouldDebounceSaveComment) {
const newComment = EmojiUtils.replaceEmojis(comment);
this.setState({
isCommentEmpty: !!newComment.match(/^(\s|`)*$/),
value: newComment,
this.setState((prevState) => {
const newState = {
isCommentEmpty: !!newComment.match(/^(\s|`)*$/),
value: newComment,
};
if (comment !== newComment) {
const remainder = prevState.value.slice(prevState.selection.end).length;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was hidden bug in this logic. #17275 (comment) for more details about the root cause.

newState.selection = {
start: newComment.length - remainder,
end: newComment.length - remainder,
};
}
return newState;
});

// Indicate that draft has been created.
Expand Down
12 changes: 11 additions & 1 deletion src/pages/home/report/ReportActionItemMessageEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,17 @@ class ReportActionItemMessageEdit extends React.Component {
*/
updateDraft(draft) {
const newDraft = EmojiUtils.replaceEmojis(draft);
this.setState({draft: newDraft});
this.setState((prevState) => {
const newState = {draft: newDraft};
if (draft !== newDraft) {
const remainder = prevState.draft.slice(prevState.selection.end).length;
newState.selection = {
start: newDraft.length - remainder,
end: newDraft.length - remainder,
};
}
return newState;
});

// This component is rendered only when draft is set to a non-empty string. In order to prevent component
// unmount when user deletes content of textarea, we set previous message instead of empty string.
Expand Down