From a7feb58b63cebff9983ed55ba78d00dcb631bcd7 Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Wed, 19 Jul 2017 16:54:58 +0100 Subject: [PATCH 1/2] Match emojis by long name For example, searching for `:tong` will now match `:stuck_out_tongue` because the query `:tong` has all non-word characters removed, becoming `tong` and is then matched against the (long) names of emojis such as `smiling face with open mouth and tightly-closed eyes`. --- src/autocomplete/EmojiProvider.js | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/autocomplete/EmojiProvider.js b/src/autocomplete/EmojiProvider.js index cadbe44bf6..b0b972282b 100644 --- a/src/autocomplete/EmojiProvider.js +++ b/src/autocomplete/EmojiProvider.js @@ -23,6 +23,8 @@ import FuzzyMatcher from './FuzzyMatcher'; import sdk from '../index'; import {PillCompletion} from './Components'; import type {SelectionRange, Completion} from './Autocompleter'; +import _uniq from 'lodash/uniq'; +import _sortBy from 'lodash/sortBy'; import EmojiData from '../stripped-emoji.json'; @@ -57,11 +59,13 @@ const EMOJI_SHORTNAMES = Object.keys(EmojiData).map((key) => EmojiData[key]).sor } return CATEGORY_ORDER.indexOf(a.category) - CATEGORY_ORDER.indexOf(b.category); }, -).map((a) => { +).map((a, index) => { return { name: a.name, shortname: a.shortname, aliases_ascii: a.aliases_ascii ? a.aliases_ascii.join(' ') : '', + // Include the so that we can preserve the original order + _orderBy: index, }; }); @@ -71,10 +75,15 @@ export default class EmojiProvider extends AutocompleteProvider { constructor() { super(EMOJI_REGEX); this.matcher = new FuzzyMatcher(EMOJI_SHORTNAMES, { - keys: ['aliases_ascii', 'shortname', 'name'], + keys: ['aliases_ascii', 'shortname'], // For matching against ascii equivalents shouldMatchWordsOnly: false, }); + this.nameMatcher = new FuzzyMatcher(EMOJI_SHORTNAMES, { + keys: ['name'], + // For removing punctuation + shouldMatchWordsOnly: true, + }); } async getCompletions(query: string, selection: SelectionRange) { @@ -91,8 +100,13 @@ export default class EmojiProvider extends AutocompleteProvider { matchedString = matchedString.slice(prefixMatch[0].length); range.start += prefixMatch[0].length; } + completions = this.matcher.match(matchedString); - completions = this.matcher.match(matchedString).map((result) => { + // Do second match with shouldMatchWordsOnly in order to match against 'name' + completions = completions.concat(this.nameMatcher.match(matchedString)); + // Reinstate original order + completions = _sortBy(_uniq(completions), '_orderBy'); + completions = completions.map((result) => { const {shortname} = result; const unicode = shortnameToUnicode(shortname); return { From b3df546cb932bfdbf70e8b098e68485a22ae6d1a Mon Sep 17 00:00:00 2001 From: Luke Barnard Date: Thu, 20 Jul 2017 10:51:15 +0100 Subject: [PATCH 2/2] Fix comment --- src/autocomplete/EmojiProvider.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/autocomplete/EmojiProvider.js b/src/autocomplete/EmojiProvider.js index b0b972282b..d58587d423 100644 --- a/src/autocomplete/EmojiProvider.js +++ b/src/autocomplete/EmojiProvider.js @@ -64,7 +64,7 @@ const EMOJI_SHORTNAMES = Object.keys(EmojiData).map((key) => EmojiData[key]).sor name: a.name, shortname: a.shortname, aliases_ascii: a.aliases_ascii ? a.aliases_ascii.join(' ') : '', - // Include the so that we can preserve the original order + // Include the index so that we can preserve the original order _orderBy: index, }; });