Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

emoji_picker: Allow filtering emojis by unicode characters in emoji picker.

Lani5a9cba1bba9e
diff --git a/frontend_tests/node_tests/emoji_picker.js b/frontend_tests/node_tests/emoji_picker.js
index cb32b881262..68a1a65fa1a 100644
--- a/frontend_tests/node_tests/emoji_picker.js
+++ b/frontend_tests/node_tests/emoji_picker.js
@@ -60,3 +60,33 @@ run_test("initialize", () => {
total_emoji_in_categories - popular_emoji_count + zulip_emoji_count,
);
});
+
+run_test("is_emoji_present_in_text", () => {
+ const thermometer_emoji = {
+ name: "thermometer",
+ emoji_code: "1f321",
+ reaction_type: "unicode_emoji",
+ };
+ const headphones_emoji = {
+ name: "headphones",
+ emoji_code: "1f3a7",
+ reaction_type: "unicode_emoji",
+ };
+ assert.equal(emoji_picker.is_emoji_present_in_text("🌑", thermometer_emoji), true);
+ assert.equal(
+ emoji_picker.is_emoji_present_in_text("no emojis at all", thermometer_emoji),
+ false,
+ );
+ assert.equal(emoji_picker.is_emoji_present_in_text("😎", thermometer_emoji), false);
+ assert.equal(emoji_picker.is_emoji_present_in_text("😎🌑🎧", thermometer_emoji), true);
+ assert.equal(emoji_picker.is_emoji_present_in_text("😎🎧", thermometer_emoji), false);
+ assert.equal(emoji_picker.is_emoji_present_in_text("😎🌑🎧", headphones_emoji), true);
+ assert.equal(
+ emoji_picker.is_emoji_present_in_text("emojis with text 😎🌑🎧", thermometer_emoji),
+ true,
+ );
+ assert.equal(
+ emoji_picker.is_emoji_present_in_text("emojis with text no space😎🌑🎧", headphones_emoji),
+ true,
+ );
+});
diff --git a/frontend_tests/node_tests/typeahead.js b/frontend_tests/node_tests/typeahead.js
index 2d02ad73765..35a912e992c 100644
--- a/frontend_tests/node_tests/typeahead.js
+++ b/frontend_tests/node_tests/typeahead.js
@@ -25,6 +25,7 @@ const emojis = [
...unicode_emojis.map(([emoji_code, emoji_name]) => ({
emoji_name,
emoji_code,
+ reaction_type: "unicode_emoji",
})),
];
@@ -56,6 +57,10 @@ run_test("matches starting at non-first word, too", () => {
assert_emoji_matches("traffic_", ["horizontal_traffic_light", "traffic_light"]);
});
+run_test("matches literal unicode emoji", () => {
+ assert_emoji_matches("🐼", ["panda_face"]);
+});
+
run_test("get_emoji_matcher: spaces equivalent to underscores", () => {
function assert_equivalent(query) {
assert.deepEqual(emoji_matches(query), emoji_matches(query.replace(" ", "_")));
diff --git a/static/js/emoji_picker.js b/static/js/emoji_picker.js
index 9200651c248..fa8b4430f92 100644
--- a/static/js/emoji_picker.js
+++ b/static/js/emoji_picker.js
@@ -219,6 +219,19 @@ function get_rendered_emoji(section, index) {
return undefined;
}
+export function is_emoji_present_in_text(text, emoji_dict) {
+ // fetching emoji details to ensure emoji_code and reaction_type are present
+ const emoji_info = emoji.get_emoji_details_by_name(emoji_dict.name);
+ if (typeahead.is_unicode_emoji(emoji_info)) {
+ // convert emoji_dict to an actual emoji character
+ const parsed_emoji_code = typeahead.parse_unicode_emoji_code(emoji_info.emoji_code);
+
+ return text.includes(parsed_emoji_code);
+ }
+
+ return false;
+}
+
function filter_emojis() {
const $elt = $(".emoji-popover-filter").expectOne();
const query = $elt.val().trim().toLowerCase();
@@ -234,7 +247,6 @@ function filter_emojis() {
continue;
}
const emojis = category.emojis;
-
for (const emoji_dict of emojis) {
for (const alias of emoji_dict.aliases) {
const match = search_terms.every((search_term) => alias.includes(search_term));
@@ -243,6 +255,12 @@ function filter_emojis() {
break; // We only need the first matching alias per emoji.
}
}
+
+ // using query instead of search_terms because it's possible multiple emojis were input
+ // without being separated by spaces
+ if (is_emoji_present_in_text(query, emoji_dict)) {
+ search_results.push({...emoji_dict, emoji_name: emoji_dict.name});
+ }
}
}
diff --git a/static/shared/js/typeahead.js b/static/shared/js/typeahead.js
index 6315eedfe60..4de7b44a873 100644
--- a/static/shared/js/typeahead.js
+++ b/static/shared/js/typeahead.js
@@ -81,13 +81,26 @@ export function clean_query_lowercase(query) {
return query;
}
+export const is_unicode_emoji = (emoji) =>
+ emoji.reaction_type === "unicode_emoji" && emoji.emoji_code;
+
+export const parse_unicode_emoji_code = (code) =>
+ code
+ .split("-")
+ .map((hex) => String.fromCodePoint(Number.parseInt(hex, 16)))
+ .join("");
+
export function get_emoji_matcher(query) {
// replace spaces with underscores for emoji matching
query = query.replace(/ /g, "_");
query = clean_query_lowercase(query);
return function (emoji) {
- return query_matches_source_attrs(query, emoji, ["emoji_name"], "_");
+ const matches_emoji_literal =
+ is_unicode_emoji(emoji) && parse_unicode_emoji_code(emoji.emoji_code) === query;
+ return (
+ matches_emoji_literal || query_matches_source_attrs(query, emoji, ["emoji_name"], "_")
+ );
};
}