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

Emoji Suggestions Adjustments + Remove Duplicates #26507

Merged
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
38 changes: 26 additions & 12 deletions src/libs/EmojiTrie.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,40 @@ function createTrie(lang = CONST.LOCALES.DEFAULT) {
return;
}

const name = isDefaultLocale ? item.name : _.get(langEmojis, [item.code, 'name']);
const names = isDefaultLocale ? [name] : [...new Set([name, item.name])];
_.forEach(names, (nm) => {
const node = trie.search(nm);
if (!node) {
trie.add(nm, {code: item.code, types: item.types, name: nm, suggestions: []});
} else {
trie.update(nm, {code: item.code, types: item.types, name: nm, suggestions: node.metaData.suggestions});
}
});
const englishName = item.name;
const localeName = _.get(langEmojis, [item.code, 'name'], englishName);

const node = trie.search(localeName);
if (!node) {
trie.add(localeName, {code: item.code, types: item.types, name: localeName, suggestions: []});
} else {
trie.update(localeName, {code: item.code, types: item.types, name: localeName, suggestions: node.metaData.suggestions});
}

// Add keywords for both the locale language and English to enable users to search using either language.
const keywords = _.get(langEmojis, [item.code, 'keywords'], []).concat(isDefaultLocale ? [] : _.get(localeEmojis, [CONST.LOCALES.DEFAULT, item.code, 'keywords'], []));
for (let j = 0; j < keywords.length; j++) {
const keywordNode = trie.search(keywords[j]);
if (!keywordNode) {
trie.add(keywords[j], {suggestions: [{code: item.code, types: item.types, name}]});
trie.add(keywords[j], {suggestions: [{code: item.code, types: item.types, name: localeName}]});
} else {
trie.update(keywords[j], {
...keywordNode.metaData,
suggestions: [...keywordNode.metaData.suggestions, {code: item.code, types: item.types, name}],
suggestions: [...keywordNode.metaData.suggestions, {code: item.code, types: item.types, name: localeName}],
});
}
}

// If current language isn't the default, prepend the English name of the emoji in the suggestions as well.
Thanos30 marked this conversation as resolved.
Show resolved Hide resolved
// We do this because when the user types the english name of the emoji, we want to show the emoji in the suggestions before all the others.
if (!isDefaultLocale) {
const englishNode = trie.search(englishName);
if (!englishNode) {
trie.add(englishName, {suggestions: [{code: item.code, types: item.types, name: localeName}]});
} else {
trie.update(englishName, {
...englishNode.metaData,
suggestions: [{code: item.code, types: item.types, name: localeName}, ...englishNode.metaData.suggestions],
});
}
}
Expand Down
11 changes: 10 additions & 1 deletion src/libs/EmojiUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,16 @@ function replaceEmojis(text, preferredSkinTone = CONST.EMOJI_DEFAULT_SKIN_TONE,
}
for (let i = 0; i < emojiData.length; i++) {
const name = emojiData[i].slice(1, -1);
const checkEmoji = trie.search(name);
let checkEmoji = trie.search(name);
// If the user has selected a language other than English, and the emoji doesn't exist in that language,
// we will check if the emoji exists in English.
if (lang !== CONST.LOCALES.DEFAULT && (!checkEmoji || !checkEmoji.metaData.code)) {
const englishTrie = emojisTrie[CONST.LOCALES.DEFAULT];
if (englishTrie) {
const englishEmoji = englishTrie.search(name);
checkEmoji = englishEmoji;
}
}
if (checkEmoji && checkEmoji.metaData.code) {
let emojiReplacement = getEmojiCodeWithSkinColor(checkEmoji.metaData, preferredSkinTone);
emojis.push({
Expand Down
Loading