From 7942b2878089a9cb75d52bdf1bc070a5efb22c93 Mon Sep 17 00:00:00 2001 From: Sam Saffron Date: Mon, 1 Jun 2015 12:33:42 +1000 Subject: [PATCH] FIX: Emoji aliases not autocompleted --- .../discourse/lib/emoji/emoji.js.erb | 65 ++++++++++--------- test/javascripts/lib/emoji-test.js.es6 | 10 +++ 2 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 test/javascripts/lib/emoji-test.js.es6 diff --git a/app/assets/javascripts/discourse/lib/emoji/emoji.js.erb b/app/assets/javascripts/discourse/lib/emoji/emoji.js.erb index 8a1206d88..aa577a67d 100644 --- a/app/assets/javascripts/discourse/lib/emoji/emoji.js.erb +++ b/app/assets/javascripts/discourse/lib/emoji/emoji.js.erb @@ -18,38 +18,6 @@ Discourse.Emoji.list = function(){ return list; }; -var toSearch; - -Discourse.Emoji.search = function(term, options) { - var maxResults = (options && options["maxResults"]) || -1; - if (maxResults === 0) { return []; } - - toSearch = toSearch || Discourse.Emoji.list(); - - var i, results = []; - - var done = function() { - return maxResults > 0 && results.length >= maxResults; - } - - for (i=0; i < toSearch.length; i++) { - if (toSearch[i].indexOf(term) === 0) { - results.push(toSearch[i]); - if(done()) { break; } - } - } - - if(!done()){ - for (i=0; i < toSearch.length; i++) { - if (toSearch[i].indexOf(term) > 0) { - results.push(toSearch[i]); - if(done()) { break; } - } - } - } - - return results; -} var emojiHash = {}; // add all default emojis @@ -192,4 +160,37 @@ Discourse.Dialect.registerInline(':', function(text, match, prev) { } }); + +var toSearch; +Discourse.Emoji.search = function(term, options) { + var maxResults = (options && options["maxResults"]) || -1; + if (maxResults === 0) { return []; } + + toSearch = toSearch || _.keys(emojiHash); + + var i, results = []; + + var done = function() { + return maxResults > 0 && results.length >= maxResults; + } + + for (i=0; i < toSearch.length; i++) { + if (toSearch[i].indexOf(term) === 0) { + results.push(toSearch[i]); + if(done()) { break; } + } + } + + if(!done()){ + for (i=0; i < toSearch.length; i++) { + if (toSearch[i].indexOf(term) > 0) { + results.push(toSearch[i]); + if(done()) { break; } + } + } + } + + return results; +} + Discourse.Markdown.whiteListTag('img', 'class', 'emoji'); diff --git a/test/javascripts/lib/emoji-test.js.es6 b/test/javascripts/lib/emoji-test.js.es6 new file mode 100644 index 000000000..e96941fc0 --- /dev/null +++ b/test/javascripts/lib/emoji-test.js.es6 @@ -0,0 +1,10 @@ + +module('emoji'); + +test("Emoji.search", function(){ + + // able to find an alias + equal(Discourse.Emoji.search("coll").length, 1); + +}); +