A platform for community discussion. Free, open, simple.
FEATURE: adds emoji search aliases
Joffrey JAFFEUX691174fa8f7c
diff --git a/app/assets/javascripts/pretty-text/emoji.js.es6.erb b/app/assets/javascripts/pretty-text/emoji.js.es6.erbindex fc7f79c94b6..09ab4ce5ed6 100644--- a/app/assets/javascripts/pretty-text/emoji.js.es6.erb+++ b/app/assets/javascripts/pretty-text/emoji.js.es6.erb@@ -1,4 +1,4 @@-import { emojis, aliases, translations, tonableEmojis } from 'pretty-text/emoji/data';+import { emojis, aliases, searchAliases, translations, tonableEmojis } from 'pretty-text/emoji/data';// bump up this number to expire all emojisexport const IMAGE_VERSION = "<%= Emoji::EMOJI_VERSION %>";@@ -94,24 +94,28 @@ export function emojiSearch(term, options) {if (results.indexOf(val) === -1) {results.push(val);}- return maxResults > 0 && results.length >= maxResults;}+ // if term matches from beginningfor (let i=0; i<toSearch.length; i++) {const item = toSearch[i];- if (item.indexOf(term) === 0 && addResult(item)) {- return results;- }+ if (item.indexOf(term) === 0) addResult(item);+ }++ if (searchAliases[term]) {+ results.push.apply(results, searchAliases[term]);}for (let i=0; i<toSearch.length; i++) {const item = toSearch[i];- if (item.indexOf(term) > 0 && addResult(item)) {- return results;- }+ if (item.indexOf(term) > 0) addResult(item);}- return results;+ if (maxResults === -1) {+ return results;+ } else {+ return results.slice(0, maxResults);+ }};export function isSkinTonableEmoji(term) {diff --git a/app/assets/javascripts/pretty-text/emoji/data.js.es6.erb b/app/assets/javascripts/pretty-text/emoji/data.js.es6.erbindex 18648449574..765ecc3303e 100644--- a/app/assets/javascripts/pretty-text/emoji/data.js.es6.erb+++ b/app/assets/javascripts/pretty-text/emoji/data.js.es6.erb@@ -1,6 +1,7 @@export const emojis = <%= Emoji.standard.map(&:name).flatten.inspect %>;export const tonableEmojis = <%= Emoji.tonable_emojis.flatten.inspect %>;export const aliases = <%= Emoji.aliases.inspect.gsub("=>", ":") %>;+export const searchAliases = <%= Emoji.searchAliases.inspect.gsub("=>", ":") %>;export const translations = {':)' : 'slight_smile',':-)' : 'slight_smile',diff --git a/app/models/emoji.rb b/app/models/emoji.rbindex dd01b21ed84..0112fbe8e4b 100644--- a/app/models/emoji.rb+++ b/app/models/emoji.rb@@ -25,6 +25,10 @@ class EmojiDiscourse.cache.fetch(cache_key("aliases_emojis")) { db['aliases'] }end+ def self.searchAliases+ Discourse.cache.fetch(cache_key("search_aliases_emojis")) { db['searchAliases'] }+ end+def self.customDiscourse.cache.fetch(cache_key("custom_emojis")) { load_custom }end@@ -59,7 +63,7 @@ class Emojienddef self.clear_cache- %w{custom standard aliases all tonable}.each do |key|+ %w{custom standard aliases search_aliases all tonable}.each do |key|Discourse.cache.delete(cache_key("#{key}_emojis"))endenddiff --git a/lib/emoji/db.json b/lib/emoji/db.jsonindex 11d488e3a66..f23998d8f48 100644--- a/lib/emoji/db.json+++ b/lib/emoji/db.json@@ -6795,5 +6795,17 @@"face_vomiting": ["puke"]+ },+ "searchAliases": {+ "sad": [+ "frowning_face",+ "slightly_frowning_face",+ "sob",+ "crying_cat_face",+ "cry"+ ],+ "cry": [+ "sob"+ ]}}\ No newline at end of filediff --git a/lib/tasks/emoji.rake b/lib/tasks/emoji.rakeindex 2a1a7e1a8a3..2ebfb4964a3 100644--- a/lib/tasks/emoji.rake+++ b/lib/tasks/emoji.rake@@ -12,6 +12,17 @@ EMOJI_IMAGES_PATH ||= "public/images/emoji"EMOJI_ORDERING_URL ||= "http://www.unicode.org/emoji/charts/emoji-ordering.html"+# Format is search pattern => associated emojis+# eg: "cry" => [ "sob" ]+# for a "cry" query should return: cry and sob+SEARCH_ALIASES ||= {+ "sad" => [ "frowning_face", "slightly_frowning_face", "sob", "crying_cat_face", "cry" ],+ "cry" => [ "sob" ]+}++# emoji aliases are actually created as images+# eg: "right_anger_bubble" => [ "anger_right" ]+# your app will physically have right_anger_bubble.png and anger_right.pngEMOJI_ALIASES ||= {"right_anger_bubble" => [ "anger_right" ],"ballot_box" => [ "ballot_box_with_ballot" ],@@ -508,7 +519,8 @@ def write_db_json(emojis)db = {"emojis" => emojis_without_tones,"tonableEmojis" => emoji_with_tones,- "aliases" => EMOJI_ALIASES+ "aliases" => EMOJI_ALIASES,+ "searchAliases" => SEARCH_ALIASES}File.write(EMOJI_DB_PATH, JSON.pretty_generate(db))