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

Extract emojis from paste text #23153

Merged
merged 43 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
d8594f5
chore: extract emojis from paste text
jeet-dhandha Jul 19, 2023
29f0a45
chore: lint issue fix
jeet-dhandha Jul 19, 2023
0d5523b
Merge branch 'main' into fix-emoji-paste-issue
jeet-dhandha Jul 19, 2023
164b2b3
Merge remote-tracking branch 'upstream/main'
jeet-dhandha Jul 20, 2023
61444f8
chore: change jsdoc and varible names
jeet-dhandha Jul 20, 2023
27f37b8
chore: change condition for early return
jeet-dhandha Jul 20, 2023
2758401
chore: remove extra regex and use the available one EMOJIS
jeet-dhandha Jul 20, 2023
8cccc89
chore: variable name spell fix
jeet-dhandha Jul 21, 2023
e8b2632
fix: zwj sequences issue
jeet-dhandha Jul 25, 2023
4a533bd
Removed extra emojis
jeet-dhandha Jul 25, 2023
8b10df2
fix: lint issue
jeet-dhandha Jul 26, 2023
2729ae4
Merge remote-tracking branch 'upstream/main'
jeet-dhandha Jul 27, 2023
c1d189c
Merge branch 'main' into fix-emoji-paste-issue
jeet-dhandha Jul 27, 2023
af59f1b
fix: emoji not adding due to null sequence
jeet-dhandha Jul 27, 2023
a8b6aaa
fix: used all the emoji constants
jeet-dhandha Jul 28, 2023
eb1ec24
Update CONST.js
jeet-dhandha Jul 29, 2023
dec57bc
Merge remote-tracking branch 'upstream/main'
jeet-dhandha Jul 29, 2023
d863fef
chore: remove extra regex for emoji and use updated one
jeet-dhandha Jul 31, 2023
5b98212
chore: remove extra str and change continue code
jeet-dhandha Jul 31, 2023
c81da8b
Merge remote-tracking branch 'upstream/main'
jeet-dhandha Jul 31, 2023
0ad3371
Merge remote-tracking branch 'upstream/main'
jeet-dhandha Aug 4, 2023
d0acea1
Merge remote-tracking branch 'upstream/main'
jeet-dhandha Aug 4, 2023
9f170bd
Merge branch 'main' into fix-emoji-paste-issue
jeet-dhandha Aug 4, 2023
91d7a3e
Merge remote-tracking branch 'upstream/main'
jeet-dhandha Aug 7, 2023
4fdd603
Merge remote-tracking branch 'upstream/main'
jeet-dhandha Aug 7, 2023
e76bbd0
Merge remote-tracking branch 'upstream/main'
jeet-dhandha Aug 8, 2023
ecf4f05
Merge remote-tracking branch 'upstream/main'
jeet-dhandha Aug 10, 2023
db51c50
Merge remote-tracking branch 'upstream/main'
jeet-dhandha Aug 10, 2023
5ce9cf2
fix: handle skin tones for emojis
jeet-dhandha Aug 11, 2023
2c59c73
Merge remote-tracking branch 'upstream/main'
jeet-dhandha Aug 12, 2023
c72a561
Merge branch 'main' into fix-emoji-paste-issue
jeet-dhandha Aug 12, 2023
420c187
chore: replace emojiCodeTable with emojiCodeTableWithSkinTones
jeet-dhandha Aug 14, 2023
c0b42b1
fix: logic for removing duplicates
jeet-dhandha Aug 14, 2023
99ce37c
Merge remote-tracking branch 'upstream/main'
jeet-dhandha Aug 19, 2023
e8f02ee
Merge branch 'main' into fix-emoji-paste-issue
jeet-dhandha Aug 19, 2023
425cd7f
Merge remote-tracking branch 'upstream/main'
jeet-dhandha Aug 19, 2023
66b0b9e
Merge branch 'main' into fix-emoji-paste-issue
jeet-dhandha Aug 19, 2023
b7ef46d
updated comments
jeet-dhandha Aug 22, 2023
8f9fba2
Merge remote-tracking branch 'upstream/main'
jeet-dhandha Aug 22, 2023
3ee107f
Merge branch 'main' into fix-emoji-paste-issue
jeet-dhandha Aug 22, 2023
fb9ced7
modify code to remove extra set
jeet-dhandha Aug 22, 2023
3ea6611
Added comments and updated variable name
jeet-dhandha Aug 22, 2023
f4e916a
updated comment
jeet-dhandha Aug 22, 2023
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
9 changes: 7 additions & 2 deletions assets/emojis/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ const emojiNameTable = _.reduce(
{},
);

const emojiCodeTable = _.reduce(
const emojiCodeTableWithSkinTones = _.reduce(
Copy link
Member

Choose a reason for hiding this comment

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

Why don't we replace the existing emojiCodeTable with the new emojiCodeTableWithSkinTones instead of keeping two different maps?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool. Updating the code.

emojis,
(prev, cur) => {
const newValue = prev;
if (!cur.header) {
newValue[cur.code] = cur;
}
if (cur.types) {
cur.types.forEach((type) => {
newValue[type] = cur;
});
}
return newValue;
},
{},
Expand All @@ -32,5 +37,5 @@ const localeEmojis = {
es: esEmojis,
};

export {emojiNameTable, emojiCodeTable, localeEmojis};
export {emojiNameTable, emojiCodeTableWithSkinTones, localeEmojis};
export {skinTones, categoryFrequentlyUsed, default} from './common';
3 changes: 2 additions & 1 deletion src/CONST.js
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,8 @@ const CONST = {
ROOM_NAME: /^#[a-z0-9à-ÿ-]{1,80}$/,

// eslint-disable-next-line max-len, no-misleading-character-class
EMOJIS: /[\p{Extended_Pictographic}\u200d\u{1f1e6}-\u{1f1ff}\u{1f3fb}-\u{1f3ff}\u{e0020}-\u{e007f}\u20E3\uFE0F]|[#*0-9]\uFE0F?\u20E3/gu,
EMOJIS: /[\p{Extended_Pictographic}](\u200D[\p{Extended_Pictographic}]|[\u{1F3FB}-\u{1F3FF}]|[\u{E0020}-\u{E007F}]|\uFE0F|\u20E3)*|[\u{1F1E6}-\u{1F1FF}]{2}|[#*0-9]\uFE0F?\u20E3/gu,

TAX_ID: /^\d{9}$/,
NON_NUMERIC: /\D/g,

Expand Down
60 changes: 57 additions & 3 deletions src/libs/EmojiUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Onyx.connect({
key: ONYXKEYS.FREQUENTLY_USED_EMOJIS,
callback: (val) => {
frequentlyUsedEmojis = _.map(val, (item) => {
const emoji = Emojis.emojiCodeTable[item.code];
const emoji = Emojis.emojiCodeTableWithSkinTones[item.code];
if (emoji) {
return {...emoji, count: item.count, lastUpdatedAt: item.lastUpdatedAt};
}
Expand All @@ -33,7 +33,7 @@ const findEmojiByName = (name) => Emojis.emojiNameTable[name];
* @param {String} code
* @returns {Object}
*/
const findEmojiByCode = (code) => Emojis.emojiCodeTable[code];
const findEmojiByCode = (code) => Emojis.emojiCodeTableWithSkinTones[code];

/**
*
Expand Down Expand Up @@ -229,7 +229,7 @@ function getFrequentlyUsedEmojis(newEmoji) {
frequentEmojiList.splice(emojiIndex, 1);
}

const updatedEmoji = {...Emojis.emojiCodeTable[emoji.code], count: currentEmojiCount, lastUpdatedAt: currentTimestamp};
const updatedEmoji = {...Emojis.emojiCodeTableWithSkinTones[emoji.code], count: currentEmojiCount, lastUpdatedAt: currentTimestamp};

// We want to make sure the current emoji is added to the list
// Hence, we take one less than the current frequent used emojis
Expand Down Expand Up @@ -259,6 +259,43 @@ const getEmojiCodeWithSkinColor = (item, preferredSkinToneIndex) => {
return code;
};

/**
* Extracts emojis from a given text.
*
* @param {String} text - The text to extract emojis from.
* @returns {Object[]} An array of emoji codes.
*/
function extractEmojis(text) {
if (!text) {
return [];
}

// Parse Emojis including skin tones - Eg: ['👩🏻', '👩🏻', '👩🏼', '👩🏻', '👩🏼', '👩']
const parsedEmojis = text.match(CONST.REGEX.EMOJIS);

if (!parsedEmojis) {
return [];
}

const emojis = [];

// Text can contain similar emojis as well as their skin tone variants. Create a Set to remove duplicate emojis from the search.
const foundEmojiCodes = new Set();

for (let i = 0; i < parsedEmojis.length; i++) {
const character = parsedEmojis[i];
const emoji = Emojis.emojiCodeTableWithSkinTones[character];

// Add the parsed emoji to the final emojis if not already present.
if (emoji && !foundEmojiCodes.has(emoji.code)) {
foundEmojiCodes.add(emoji.code);
emojis.push(emoji);
}
}

return emojis;
}

/**
* 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.
Expand Down Expand Up @@ -304,6 +341,22 @@ function replaceEmojis(text, preferredSkinTone = CONST.EMOJI_DEFAULT_SKIN_TONE,
return {text: newText, emojis};
}

/**
* Find all emojis in a text and replace them with their code.
* @param {String} text
* @param {Number} preferredSkinTone
* @param {String} lang
* @returns {Object}
*/
function replaceAndExtractEmojis(text, preferredSkinTone = CONST.EMOJI_DEFAULT_SKIN_TONE, lang = CONST.LOCALES.DEFAULT) {
const {text: convertedText = '', emojis = []} = replaceEmojis(text, preferredSkinTone, lang);

return {
text: convertedText,
emojis: emojis.concat(extractEmojis(text)),
};
}

/**
* Suggest emojis when typing emojis prefix after colon
* @param {String} text
Expand Down Expand Up @@ -421,4 +474,5 @@ export {
getPreferredSkinToneIndex,
getPreferredEmojiCode,
getUniqueEmojiCodes,
replaceAndExtractEmojis,
};
2 changes: 1 addition & 1 deletion src/pages/home/report/ReportActionCompose.js
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ function ReportActionCompose({
*/
const updateComment = useCallback(
(commentValue, shouldDebounceSaveComment) => {
const {text: newComment = '', emojis = []} = EmojiUtils.replaceEmojis(commentValue, preferredSkinTone, preferredLocale);
const {text: newComment, emojis} = EmojiUtils.replaceAndExtractEmojis(commentValue, preferredSkinTone, preferredLocale);

if (!_.isEmpty(emojis)) {
insertedEmojisRef.current = [...insertedEmojisRef.current, ...emojis];
Expand Down
2 changes: 1 addition & 1 deletion src/pages/home/report/ReportActionItemMessageEdit.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ function ReportActionItemMessageEdit(props) {
*/
const updateDraft = useCallback(
(newDraftInput) => {
const {text: newDraft = '', emojis = []} = EmojiUtils.replaceEmojis(newDraftInput, props.preferredSkinTone, props.preferredLocale);
const {text: newDraft, emojis} = EmojiUtils.replaceAndExtractEmojis(newDraftInput, props.preferredSkinTone, props.preferredLocale);

jeet-dhandha marked this conversation as resolved.
Show resolved Hide resolved
if (!_.isEmpty(emojis)) {
insertedEmojis.current = [...insertedEmojis.current, ...emojis];
Expand Down
Loading