From 691174fa8f7c6ed89f0786e079ffa43f8416fb1a Mon Sep 17 00:00:00 2001 From: Joffrey JAFFEUX Date: Tue, 1 May 2018 15:43:49 +0200 Subject: [PATCH] FEATURE: adds emoji search aliases --- .../javascripts/pretty-text/emoji.js.es6.erb | 28 +++++++++++-------- .../pretty-text/emoji/data.js.es6.erb | 1 + app/models/emoji.rb | 6 +++- lib/emoji/db.json | 12 ++++++++ lib/tasks/emoji.rake | 14 +++++++++- 5 files changed, 47 insertions(+), 14 deletions(-) diff --git a/app/assets/javascripts/pretty-text/emoji.js.es6.erb b/app/assets/javascripts/pretty-text/emoji.js.es6.erb index 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 emojis export 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 beginning + for (let i=0; i 0) addResult(item); } - for (let i=0; i 0 && addResult(item)) { - return results; - } + if (maxResults === -1) { + return results; + } else { + return results.slice(0, maxResults); } - - return results; }; 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.erb index 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.rb index dd01b21ed84..0112fbe8e4b 100644 --- a/app/models/emoji.rb +++ b/app/models/emoji.rb @@ -25,6 +25,10 @@ class Emoji Discourse.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.custom Discourse.cache.fetch(cache_key("custom_emojis")) { load_custom } end @@ -59,7 +63,7 @@ class Emoji end def 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")) end end diff --git a/lib/emoji/db.json b/lib/emoji/db.json index 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 file diff --git a/lib/tasks/emoji.rake b/lib/tasks/emoji.rake index 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.png EMOJI_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))