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 space inconsistency inserting emoji #21749

Merged
6 changes: 3 additions & 3 deletions src/libs/EmojiUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,10 @@ const getEmojiCodeWithSkinColor = (item, preferredSkinToneIndex) => {
* 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
* @param {Number} preferredSkinTone
* @returns {Object}
*/
function replaceEmojis(text, isSmallScreenWidth = false, preferredSkinTone = CONST.EMOJI_DEFAULT_SKIN_TONE) {
function replaceEmojis(text, preferredSkinTone = CONST.EMOJI_DEFAULT_SKIN_TONE) {
let newText = text;
const emojis = [];
const emojiData = text.match(CONST.REGEX.EMOJI_NAME);
Expand All @@ -244,9 +243,10 @@ function replaceEmojis(text, isSmallScreenWidth = false, preferredSkinTone = CON

// 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 (isSmallScreenWidth && i === emojiData.length - 1 && text.endsWith(emojiData[i])) {
if (i === emojiData.length - 1) {
emojiReplacement += ' ';
}

newText = newText.replace(emojiData[i], emojiReplacement);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/pages/home/report/ReportActionCompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -662,11 +662,11 @@ class ReportActionCompose extends React.Component {
* @param {String} emoji
*/
addEmojiToTextBox(emoji) {
this.updateComment(ComposerUtils.insertText(this.comment, this.state.selection, emoji));
this.updateComment(ComposerUtils.insertText(this.comment, this.state.selection, `${emoji} `));
this.setState((prevState) => ({
selection: {
start: prevState.selection.start + emoji.length,
end: prevState.selection.start + emoji.length,
start: prevState.selection.start + emoji.length + CONST.SPACE_LENGTH,
end: prevState.selection.start + emoji.length + CONST.SPACE_LENGTH,
},
}));
}
Expand Down Expand Up @@ -721,7 +721,7 @@ class ReportActionCompose extends React.Component {
* @param {Boolean} shouldDebounceSaveComment
*/
updateComment(comment, shouldDebounceSaveComment) {
const {text: newComment = '', emojis = []} = EmojiUtils.replaceEmojis(comment, this.props.isSmallScreenWidth, this.props.preferredSkinTone);
const {text: newComment = '', emojis = []} = EmojiUtils.replaceEmojis(comment, this.props.preferredSkinTone);

if (!_.isEmpty(emojis)) {
User.updateFrequentlyUsedEmojis(EmojiUtils.getFrequentlyUsedEmojis(emojis));
Expand Down
10 changes: 5 additions & 5 deletions src/pages/home/report/ReportActionItemMessageEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ function ReportActionItemMessageEdit(props) {
*/
const updateDraft = useCallback(
(newDraftInput) => {
const {text: newDraft = '', emojis = []} = EmojiUtils.replaceEmojis(newDraftInput, isSmallScreenWidth, props.preferredSkinTone);
const {text: newDraft = '', emojis = []} = EmojiUtils.replaceEmojis(newDraftInput, props.preferredSkinTone);

if (!_.isEmpty(emojis)) {
User.updateFrequentlyUsedEmojis(EmojiUtils.getFrequentlyUsedEmojis(emojis));
Expand All @@ -171,7 +171,7 @@ function ReportActionItemMessageEdit(props) {
debouncedSaveDraft(props.action.message[0].html);
}
},
[props.action.message, debouncedSaveDraft, isSmallScreenWidth, props.preferredSkinTone],
[props.action.message, debouncedSaveDraft, props.preferredSkinTone],
);

/**
Expand Down Expand Up @@ -226,10 +226,10 @@ function ReportActionItemMessageEdit(props) {
*/
const addEmojiToTextBox = (emoji) => {
setSelection((prevSelection) => ({
start: prevSelection.start + emoji.length,
end: prevSelection.start + emoji.length,
start: prevSelection.start + emoji.length + CONST.SPACE_LENGTH,
end: prevSelection.start + emoji.length + CONST.SPACE_LENGTH,
Copy link
Contributor

Choose a reason for hiding this comment

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

can you please add some description why do you change that - for future and easy reading for any other devs

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should we add it here? there are multiple places where space_length is added.

Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe on the top of the current function (somewhere in description)

Copy link
Contributor

Choose a reason for hiding this comment

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

TBH I think we're good without the comment since we have other instances without comments and that hasn't been a problem yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

 * we will add CONST.SPACE_LENGTH which is equal to 1 to the selection state
 * this will make sure the cursor is on the right place after the emoji is inserted
 
 what do you think about this?

Copy link
Contributor

@narefyev91 narefyev91 Jun 28, 2023

Choose a reason for hiding this comment

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

@getusha everything is fine - as @luacmartins mentioned we can skip it - thanks for flexibility

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@narefyev91 ohh, i missed that thanks!

}));
updateDraft(ComposerUtils.insertText(draft, selection, emoji));
updateDraft(ComposerUtils.insertText(draft, selection, `${emoji} `));
};

/**
Expand Down
20 changes: 5 additions & 15 deletions tests/unit/EmojiTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,24 +101,14 @@ describe('EmojiTest', () => {
expect(EmojiUtils.containsOnlyEmojis('🅃🄴🅂🅃')).toBe(false);
});

it('replaces an emoji code with an emoji and a space on mobile', () => {
it('replaces an emoji code with an emoji and a space', () => {
const text = 'Hi :smile:';
expect(lodashGet(EmojiUtils.replaceEmojis(text, true), 'text')).toBe('Hi 😄 ');
expect(lodashGet(EmojiUtils.replaceEmojis(text), 'text')).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(lodashGet(EmojiUtils.replaceEmojis(text), '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(lodashGet(EmojiUtils.replaceEmojis(text, true), 'text')).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(lodashGet(EmojiUtils.replaceEmojis(text), 'text')).toBe('Hi 😄');
it('will add a space after the last emoji if there is text after it', () => {
const text = 'Hi :smile::wave:space after last emoji';
expect(lodashGet(EmojiUtils.replaceEmojis(text), 'text')).toBe('Hi 😄👋 space after last emoji');
});

it('suggests emojis when typing emojis prefix after colon', () => {
Expand Down