2017-06-01 14:18:06 +00:00
|
|
|
/*
|
2017-06-01 16:29:40 +00:00
|
|
|
Copyright 2016 Aviral Dasgupta
|
2017-06-01 14:18:06 +00:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2018-06-19 11:06:13 +00:00
|
|
|
Copyright 2017, 2018 New Vector Ltd
|
2019-12-18 15:40:19 +00:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2017-06-01 14:18:06 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-07-03 16:45:13 +00:00
|
|
|
import React from 'react';
|
2017-05-25 10:39:08 +00:00
|
|
|
import { _t } from '../languageHandler';
|
2016-06-16 23:28:09 +00:00
|
|
|
import AutocompleteProvider from './AutocompleteProvider';
|
2018-08-13 18:15:42 +00:00
|
|
|
import QueryMatcher from './QueryMatcher';
|
2016-08-17 11:57:19 +00:00
|
|
|
import {PillCompletion} from './Components';
|
2018-06-19 11:06:13 +00:00
|
|
|
import type {Completion, SelectionRange} from './Autocompleter';
|
2017-07-19 15:54:58 +00:00
|
|
|
import _uniq from 'lodash/uniq';
|
|
|
|
import _sortBy from 'lodash/sortBy';
|
2017-10-29 07:43:52 +00:00
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2019-05-19 15:11:12 +00:00
|
|
|
import { shortcodeToUnicode } from '../HtmlUtils';
|
2016-06-16 23:28:09 +00:00
|
|
|
|
2019-05-19 14:23:43 +00:00
|
|
|
import EMOTICON_REGEX from 'emojibase-regex/emoticon';
|
2019-12-18 15:40:19 +00:00
|
|
|
import EMOJIBASE from 'emojibase-data/en/compact.json';
|
2017-06-27 19:13:48 +00:00
|
|
|
|
|
|
|
const LIMIT = 20;
|
2019-05-19 19:48:18 +00:00
|
|
|
|
|
|
|
// Match for ascii-style ";-)" emoticons or ":wink:" shortcodes provided by emojibase
|
|
|
|
const EMOJI_REGEX = new RegExp('(' + EMOTICON_REGEX.source + '|:[+-\\w]*:?)$', 'g');
|
2017-07-07 18:02:51 +00:00
|
|
|
|
2019-05-19 20:19:20 +00:00
|
|
|
// XXX: it's very unclear why we bother with this generated emojidata file.
|
|
|
|
// all it means is that we end up bloating the bundle with precomputed stuff
|
|
|
|
// which would be trivial to calculate and cache on demand.
|
2019-12-18 15:40:19 +00:00
|
|
|
const EMOJI_SHORTNAMES = EMOJIBASE.sort((a, b) => {
|
|
|
|
if (a.group === b.group) {
|
|
|
|
return a.order - b.order;
|
|
|
|
}
|
|
|
|
return a.group - b.group;
|
|
|
|
}).map((emoji, index) => {
|
2017-02-10 18:05:13 +00:00
|
|
|
return {
|
2019-12-18 15:40:19 +00:00
|
|
|
emoji,
|
|
|
|
shortname: `:${emoji.shortcodes[0]}:`,
|
2017-07-20 09:51:15 +00:00
|
|
|
// Include the index so that we can preserve the original order
|
2017-07-19 15:54:58 +00:00
|
|
|
_orderBy: index,
|
2017-02-10 18:05:13 +00:00
|
|
|
};
|
|
|
|
});
|
2016-06-16 23:28:09 +00:00
|
|
|
|
2017-08-01 16:36:41 +00:00
|
|
|
function score(query, space) {
|
|
|
|
const index = space.indexOf(query);
|
|
|
|
if (index === -1) {
|
|
|
|
return Infinity;
|
|
|
|
} else {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-16 23:28:09 +00:00
|
|
|
export default class EmojiProvider extends AutocompleteProvider {
|
|
|
|
constructor() {
|
2016-06-21 10:16:20 +00:00
|
|
|
super(EMOJI_REGEX);
|
2018-08-13 18:15:42 +00:00
|
|
|
this.matcher = new QueryMatcher(EMOJI_SHORTNAMES, {
|
2019-12-18 15:40:19 +00:00
|
|
|
keys: ['emoji.emoticon', 'shortname'],
|
|
|
|
funcs: [
|
|
|
|
(o) => o.emoji.shortcodes.length > 1 ? o.emoji.shortcodes.slice(1).map(s => `:${s}:`).join(" ") : "", // aliases
|
|
|
|
],
|
2017-06-29 10:29:55 +00:00
|
|
|
// For matching against ascii equivalents
|
|
|
|
shouldMatchWordsOnly: false,
|
2017-02-10 18:05:13 +00:00
|
|
|
});
|
2018-08-13 18:15:42 +00:00
|
|
|
this.nameMatcher = new QueryMatcher(EMOJI_SHORTNAMES, {
|
2019-12-18 15:40:19 +00:00
|
|
|
keys: ['emoji.annotation'],
|
2017-07-19 15:54:58 +00:00
|
|
|
// For removing punctuation
|
|
|
|
shouldMatchWordsOnly: true,
|
|
|
|
});
|
2016-06-16 23:28:09 +00:00
|
|
|
}
|
|
|
|
|
2018-06-19 11:06:13 +00:00
|
|
|
async getCompletions(query: string, selection: SelectionRange, force?: boolean): Array<Completion> {
|
2019-01-25 03:57:40 +00:00
|
|
|
if (!SettingsStore.getValue("MessageComposerInput.suggestEmoji")) {
|
2017-09-15 03:28:12 +00:00
|
|
|
return []; // don't give any suggestions if the user doesn't want them
|
|
|
|
}
|
|
|
|
|
2016-06-16 23:28:09 +00:00
|
|
|
let completions = [];
|
2017-07-07 18:02:51 +00:00
|
|
|
const {command, range} = this.getCurrentCommand(query, selection);
|
2016-07-03 16:45:13 +00:00
|
|
|
if (command) {
|
2019-05-19 20:00:14 +00:00
|
|
|
const matchedString = command[0];
|
2017-07-19 15:54:58 +00:00
|
|
|
completions = this.matcher.match(matchedString);
|
2017-07-07 18:02:51 +00:00
|
|
|
|
2017-07-19 15:54:58 +00:00
|
|
|
// Do second match with shouldMatchWordsOnly in order to match against 'name'
|
|
|
|
completions = completions.concat(this.nameMatcher.match(matchedString));
|
2017-08-01 16:36:41 +00:00
|
|
|
|
|
|
|
const sorters = [];
|
2019-05-19 19:48:18 +00:00
|
|
|
// make sure that emoticons come first
|
2019-12-18 15:40:19 +00:00
|
|
|
sorters.push((c) => score(matchedString, c.emoji.emoticon || ""));
|
2019-05-19 19:48:18 +00:00
|
|
|
|
|
|
|
// then sort by score (Infinity if matchedString not in shortname)
|
2017-08-02 09:09:00 +00:00
|
|
|
sorters.push((c) => score(matchedString, c.shortname));
|
|
|
|
// If the matchedString is not empty, sort by length of shortname. Example:
|
|
|
|
// matchedString = ":bookmark"
|
2017-08-01 16:36:41 +00:00
|
|
|
// completions = [":bookmark:", ":bookmark_tabs:", ...]
|
2017-08-02 09:09:00 +00:00
|
|
|
if (matchedString.length > 1) {
|
2017-08-01 16:36:41 +00:00
|
|
|
sorters.push((c) => c.shortname.length);
|
|
|
|
}
|
|
|
|
// Finally, sort by original ordering
|
|
|
|
sorters.push((c) => c._orderBy);
|
|
|
|
completions = _sortBy(_uniq(completions), sorters);
|
|
|
|
|
2019-12-18 15:40:19 +00:00
|
|
|
completions = completions.map(({shortname}) => {
|
2019-05-19 14:23:43 +00:00
|
|
|
const unicode = shortcodeToUnicode(shortname);
|
2016-06-16 23:28:09 +00:00
|
|
|
return {
|
2016-08-17 11:57:19 +00:00
|
|
|
completion: unicode,
|
2016-06-16 23:28:09 +00:00
|
|
|
component: (
|
2019-09-30 13:04:39 +00:00
|
|
|
<PillCompletion title={shortname} aria-label={unicode} initialComponent={
|
|
|
|
<span style={{maxWidth: '1em'}}>{ unicode }</span>
|
|
|
|
} />
|
2016-07-03 16:45:13 +00:00
|
|
|
),
|
|
|
|
range,
|
2016-06-16 23:28:09 +00:00
|
|
|
};
|
2017-06-27 19:13:48 +00:00
|
|
|
}).slice(0, LIMIT);
|
2016-06-16 23:28:09 +00:00
|
|
|
}
|
2016-09-13 10:11:52 +00:00
|
|
|
return completions;
|
2016-06-16 23:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getName() {
|
2017-05-23 14:16:31 +00:00
|
|
|
return '😃 ' + _t('Emoji');
|
2016-06-16 23:28:09 +00:00
|
|
|
}
|
2016-06-20 08:22:55 +00:00
|
|
|
|
2016-08-17 11:57:19 +00:00
|
|
|
renderCompletions(completions: [React.Component]): ?React.Component {
|
2019-09-30 13:04:39 +00:00
|
|
|
return (
|
|
|
|
<div className="mx_Autocomplete_Completion_container_pill" role="listbox" aria-label={_t("Emoji Autocomplete")}>
|
|
|
|
{ completions }
|
|
|
|
</div>
|
|
|
|
);
|
2016-08-17 11:57:19 +00:00
|
|
|
}
|
2016-06-16 23:28:09 +00:00
|
|
|
}
|