Skip to content

Fix space logic after the emoji #13875

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

Merged
merged 11 commits into from
Dec 30, 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
8 changes: 5 additions & 3 deletions src/libs/EmojiUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,13 @@ function addToFrequentlyUsedEmojis(frequentlyUsedEmojis, newEmoji) {
}

/**
* Replace any emoji name in a text with the emoji icon
* Replace any emoji name in a text with the emoji icon.
* If we're on mobile, we also add a space after the emoji granted there's no text after it.
* @param {String} text
* @param {Boolean} isSmallScreenWidth
* @returns {String}
*/
function replaceEmojis(text) {
function replaceEmojis(text, isSmallScreenWidth = false) {
let newText = text;
const emojiData = text.match(CONST.REGEX.EMOJI_NAME);
if (!emojiData || emojiData.length === 0) {
Expand All @@ -201,7 +203,7 @@ function replaceEmojis(text) {

// If this is the last emoji in the message and it's the end of the message so far,
// add a space after it so the user can keep typing easily.
if (i === emojiData.length - 1 && text.endsWith(emojiData[i])) {
if (isSmallScreenWidth && i === emojiData.length - 1 && text.endsWith(emojiData[i])) {
emojiReplacement += ' ';
}
newText = newText.replace(emojiData[i], emojiReplacement);
Expand Down
9 changes: 4 additions & 5 deletions src/pages/home/report/ReportActionCompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,12 @@ class ReportActionCompose extends React.Component {
* @param {String} emoji
*/
addEmojiToTextBox(emoji) {
const emojiWithSpace = `${emoji} `;
const newComment = this.comment.slice(0, this.state.selection.start)
+ emojiWithSpace + this.comment.slice(this.state.selection.end, this.comment.length);
+ emoji + this.comment.slice(this.state.selection.end, this.comment.length);
this.setState(prevState => ({
selection: {
start: prevState.selection.start + emojiWithSpace.length,
end: prevState.selection.start + emojiWithSpace.length,
start: prevState.selection.start + emoji.length,
end: prevState.selection.start + emoji.length,
},
}));
this.updateComment(newComment);
Expand Down Expand Up @@ -391,7 +390,7 @@ class ReportActionCompose extends React.Component {
* @param {Boolean} shouldDebounceSaveComment
*/
updateComment(comment, shouldDebounceSaveComment) {
const newComment = EmojiUtils.replaceEmojis(comment);
const newComment = EmojiUtils.replaceEmojis(comment, this.props.isSmallScreenWidth);
this.setState((prevState) => {
const newState = {
isCommentEmpty: !!newComment.match(/^(\s|`)*$/),
Expand Down
9 changes: 4 additions & 5 deletions src/pages/home/report/ReportActionItemMessageEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ class ReportActionItemMessageEdit extends React.Component {
* @param {String} draft
*/
updateDraft(draft) {
const newDraft = EmojiUtils.replaceEmojis(draft);
const newDraft = EmojiUtils.replaceEmojis(draft, this.props.isSmallScreenWidth);
this.setState((prevState) => {
const newState = {draft: newDraft};
if (draft !== newDraft) {
Expand Down Expand Up @@ -183,13 +183,12 @@ class ReportActionItemMessageEdit extends React.Component {
* @param {String} emoji
*/
addEmojiToTextBox(emoji) {
const emojiWithSpace = `${emoji} `;
const newComment = this.state.draft.slice(0, this.state.selection.start)
+ emojiWithSpace + this.state.draft.slice(this.state.selection.end, this.state.draft.length);
+ emoji + this.state.draft.slice(this.state.selection.end, this.state.draft.length);
this.setState(prevState => ({
selection: {
start: prevState.selection.start + emojiWithSpace.length,
end: prevState.selection.start + emojiWithSpace.length,
start: prevState.selection.start + emoji.length,
end: prevState.selection.start + emoji.length,
},
}));
this.updateDraft(newComment);
Expand Down
14 changes: 12 additions & 2 deletions tests/unit/EmojiTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,26 @@ describe('EmojiTest', () => {
expect(EmojiUtils.containsOnlyEmojis('😄 👋')).toBe(true);
});

it('replaces an emoji code with an emoji and a space', () => {
it('replaces an emoji code with an emoji and a space on mobile', () => {
const text = 'Hi :smile:';
expect(EmojiUtils.replaceEmojis(text)).toBe('Hi 😄 ');
expect(EmojiUtils.replaceEmojis(text, true)).toBe('Hi 😄 ');
});

it('will not add a space after the last emoji if there is text after it', () => {
const text = 'Hi :smile::wave:no space after last emoji';
expect(EmojiUtils.replaceEmojis(text)).toBe('Hi 😄👋no space after last emoji');
});

it('will not add a space after the last emoji when there is text after it on mobile', () => {
const text = 'Hi :smile::wave:no space after last emoji';
expect(EmojiUtils.replaceEmojis(text, true)).toBe('Hi 😄👋no space after last emoji');
});

it('will not add a space after the last emoji if we\'re not on mobile', () => {
const text = 'Hi :smile:';
expect(EmojiUtils.replaceEmojis(text)).toBe('Hi 😄');
});

it('suggests emojis when typing emojis prefix after colon', () => {
const text = 'Hi :coffin';
expect(EmojiUtils.suggestEmojis(text)).toEqual([{code: '⚰️', name: 'coffin'}]);
Expand Down