Merge pull request #1236 from matrix-org/luke/fix-rte-emoji-match-by-name

Match emojis by long name
pull/21833/head
Luke Barnard 2017-07-20 11:53:41 +01:00 committed by GitHub
commit 0185119146
1 changed files with 17 additions and 3 deletions

View File

@ -23,6 +23,8 @@ import FuzzyMatcher from './FuzzyMatcher';
import sdk from '../index'; import sdk from '../index';
import {PillCompletion} from './Components'; import {PillCompletion} from './Components';
import type {SelectionRange, Completion} from './Autocompleter'; import type {SelectionRange, Completion} from './Autocompleter';
import _uniq from 'lodash/uniq';
import _sortBy from 'lodash/sortBy';
import EmojiData from '../stripped-emoji.json'; 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); return CATEGORY_ORDER.indexOf(a.category) - CATEGORY_ORDER.indexOf(b.category);
}, },
).map((a) => { ).map((a, index) => {
return { return {
name: a.name, name: a.name,
shortname: a.shortname, shortname: a.shortname,
aliases_ascii: a.aliases_ascii ? a.aliases_ascii.join(' ') : '', aliases_ascii: a.aliases_ascii ? a.aliases_ascii.join(' ') : '',
// Include the index so that we can preserve the original order
_orderBy: index,
}; };
}); });
@ -71,10 +75,15 @@ export default class EmojiProvider extends AutocompleteProvider {
constructor() { constructor() {
super(EMOJI_REGEX); super(EMOJI_REGEX);
this.matcher = new FuzzyMatcher(EMOJI_SHORTNAMES, { this.matcher = new FuzzyMatcher(EMOJI_SHORTNAMES, {
keys: ['aliases_ascii', 'shortname', 'name'], keys: ['aliases_ascii', 'shortname'],
// For matching against ascii equivalents // For matching against ascii equivalents
shouldMatchWordsOnly: false, shouldMatchWordsOnly: false,
}); });
this.nameMatcher = new FuzzyMatcher(EMOJI_SHORTNAMES, {
keys: ['name'],
// For removing punctuation
shouldMatchWordsOnly: true,
});
} }
async getCompletions(query: string, selection: SelectionRange) { async getCompletions(query: string, selection: SelectionRange) {
@ -91,8 +100,13 @@ export default class EmojiProvider extends AutocompleteProvider {
matchedString = matchedString.slice(prefixMatch[0].length); matchedString = matchedString.slice(prefixMatch[0].length);
range.start += 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 {shortname} = result;
const unicode = shortnameToUnicode(shortname); const unicode = shortnameToUnicode(shortname);
return { return {