2017-06-01 17:18:06 +03:00
|
|
|
/*
|
2017-06-01 19:29:40 +03:00
|
|
|
Copyright 2016 Aviral Dasgupta
|
2017-06-01 17:18:06 +03:00
|
|
|
Copyright 2017 Vector Creations Ltd
|
2018-06-19 14:06:13 +03:00
|
|
|
Copyright 2017, 2018 New Vector Ltd
|
2019-12-18 18:40:19 +03:00
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
2017-06-01 17:18:06 +03: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.
|
|
|
|
*/
|
|
|
|
|
2020-04-21 12:01:05 +03:00
|
|
|
import React from 'react';
|
2017-05-25 13:39:08 +03:00
|
|
|
import { _t } from '../languageHandler';
|
2016-06-17 02:28:09 +03:00
|
|
|
import AutocompleteProvider from './AutocompleteProvider';
|
2018-08-13 21:15:42 +03:00
|
|
|
import QueryMatcher from './QueryMatcher';
|
2016-08-17 14:57:19 +03:00
|
|
|
import {PillCompletion} from './Components';
|
2020-04-20 21:00:54 +03:00
|
|
|
import {ICompletion, ISelectionRange} from './Autocompleter';
|
2020-08-28 20:53:43 +03:00
|
|
|
import {uniq, sortBy} from 'lodash';
|
2017-10-29 10:43:52 +03:00
|
|
|
import SettingsStore from "../settings/SettingsStore";
|
2019-05-19 18:11:12 +03:00
|
|
|
import { shortcodeToUnicode } from '../HtmlUtils';
|
2020-04-20 21:17:58 +03:00
|
|
|
import { EMOJI, IEmoji } from '../emoji';
|
2016-06-17 02:28:09 +03:00
|
|
|
|
2019-05-19 17:23:43 +03:00
|
|
|
import EMOTICON_REGEX from 'emojibase-regex/emoticon';
|
2017-06-27 22:13:48 +03:00
|
|
|
|
|
|
|
const LIMIT = 20;
|
2019-05-19 22:48:18 +03:00
|
|
|
|
|
|
|
// Match for ascii-style ";-)" emoticons or ":wink:" shortcodes provided by emojibase
|
2020-07-21 19:53:16 +03:00
|
|
|
// anchored to only match from the start of parts otherwise it'll show emoji suggestions whilst typing matrix IDs
|
|
|
|
const EMOJI_REGEX = new RegExp('(' + EMOTICON_REGEX.source + '|(?:^|\\s):[+-\\w]*:?)$', 'g');
|
2017-07-07 21:02:51 +03:00
|
|
|
|
2020-04-20 21:00:54 +03:00
|
|
|
interface IEmojiShort {
|
|
|
|
emoji: IEmoji;
|
|
|
|
shortname: string;
|
|
|
|
_orderBy: number;
|
|
|
|
}
|
|
|
|
|
2020-04-20 21:17:58 +03:00
|
|
|
const EMOJI_SHORTNAMES: IEmojiShort[] = EMOJI.sort((a, b) => {
|
2019-12-18 18:40:19 +03:00
|
|
|
if (a.group === b.group) {
|
|
|
|
return a.order - b.order;
|
|
|
|
}
|
|
|
|
return a.group - b.group;
|
2020-04-20 21:00:54 +03:00
|
|
|
}).map((emoji, index) => ({
|
|
|
|
emoji,
|
|
|
|
shortname: `:${emoji.shortcodes[0]}:`,
|
|
|
|
// Include the index so that we can preserve the original order
|
|
|
|
_orderBy: index,
|
|
|
|
}));
|
2016-06-17 02:28:09 +03:00
|
|
|
|
2017-08-01 19:36:41 +03:00
|
|
|
function score(query, space) {
|
|
|
|
const index = space.indexOf(query);
|
|
|
|
if (index === -1) {
|
|
|
|
return Infinity;
|
|
|
|
} else {
|
|
|
|
return index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-17 02:28:09 +03:00
|
|
|
export default class EmojiProvider extends AutocompleteProvider {
|
2020-04-20 21:00:54 +03:00
|
|
|
matcher: QueryMatcher<IEmojiShort>;
|
|
|
|
nameMatcher: QueryMatcher<IEmojiShort>;
|
|
|
|
|
2016-06-17 02:28:09 +03:00
|
|
|
constructor() {
|
2016-06-21 13:16:20 +03:00
|
|
|
super(EMOJI_REGEX);
|
2020-05-25 18:47:57 +03:00
|
|
|
this.matcher = new QueryMatcher<IEmojiShort>(EMOJI_SHORTNAMES, {
|
2019-12-18 18:40:19 +03: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 13:29:55 +03:00
|
|
|
// For matching against ascii equivalents
|
|
|
|
shouldMatchWordsOnly: false,
|
2017-02-10 21:05:13 +03:00
|
|
|
});
|
2018-08-13 21:15:42 +03:00
|
|
|
this.nameMatcher = new QueryMatcher(EMOJI_SHORTNAMES, {
|
2019-12-18 18:40:19 +03:00
|
|
|
keys: ['emoji.annotation'],
|
2017-07-19 18:54:58 +03:00
|
|
|
// For removing punctuation
|
|
|
|
shouldMatchWordsOnly: true,
|
|
|
|
});
|
2016-06-17 02:28:09 +03:00
|
|
|
}
|
|
|
|
|
2021-05-12 14:18:56 +03:00
|
|
|
async getCompletions(
|
|
|
|
query: string,
|
|
|
|
selection: ISelectionRange,
|
|
|
|
force?: boolean,
|
|
|
|
limit = -1,
|
|
|
|
): Promise<ICompletion[]> {
|
2019-01-25 06:57:40 +03:00
|
|
|
if (!SettingsStore.getValue("MessageComposerInput.suggestEmoji")) {
|
2017-09-15 06:28:12 +03:00
|
|
|
return []; // don't give any suggestions if the user doesn't want them
|
|
|
|
}
|
|
|
|
|
2016-06-17 02:28:09 +03:00
|
|
|
let completions = [];
|
2017-07-07 21:02:51 +03:00
|
|
|
const {command, range} = this.getCurrentCommand(query, selection);
|
2016-07-03 19:45:13 +03:00
|
|
|
if (command) {
|
2019-05-19 23:00:14 +03:00
|
|
|
const matchedString = command[0];
|
2021-05-12 14:18:56 +03:00
|
|
|
completions = this.matcher.match(matchedString, limit);
|
2017-07-07 21:02:51 +03:00
|
|
|
|
2017-07-19 18:54:58 +03:00
|
|
|
// Do second match with shouldMatchWordsOnly in order to match against 'name'
|
|
|
|
completions = completions.concat(this.nameMatcher.match(matchedString));
|
2017-08-01 19:36:41 +03:00
|
|
|
|
|
|
|
const sorters = [];
|
2019-05-19 22:48:18 +03:00
|
|
|
// make sure that emoticons come first
|
2019-12-18 18:40:19 +03:00
|
|
|
sorters.push((c) => score(matchedString, c.emoji.emoticon || ""));
|
2019-05-19 22:48:18 +03:00
|
|
|
|
|
|
|
// then sort by score (Infinity if matchedString not in shortname)
|
2017-08-02 12:09:00 +03:00
|
|
|
sorters.push((c) => score(matchedString, c.shortname));
|
2020-04-11 20:09:28 +03:00
|
|
|
// then sort by max score of all shortcodes, trim off the `:`
|
2020-04-14 15:07:44 +03:00
|
|
|
sorters.push((c) => Math.min(...c.emoji.shortcodes.map(s => score(matchedString.substring(1), s))));
|
2017-08-02 12:09:00 +03:00
|
|
|
// If the matchedString is not empty, sort by length of shortname. Example:
|
|
|
|
// matchedString = ":bookmark"
|
2017-08-01 19:36:41 +03:00
|
|
|
// completions = [":bookmark:", ":bookmark_tabs:", ...]
|
2017-08-02 12:09:00 +03:00
|
|
|
if (matchedString.length > 1) {
|
2017-08-01 19:36:41 +03:00
|
|
|
sorters.push((c) => c.shortname.length);
|
|
|
|
}
|
|
|
|
// Finally, sort by original ordering
|
|
|
|
sorters.push((c) => c._orderBy);
|
2020-08-28 20:53:43 +03:00
|
|
|
completions = sortBy(uniq(completions), sorters);
|
2017-08-01 19:36:41 +03:00
|
|
|
|
2019-12-18 18:40:19 +03:00
|
|
|
completions = completions.map(({shortname}) => {
|
2019-05-19 17:23:43 +03:00
|
|
|
const unicode = shortcodeToUnicode(shortname);
|
2016-06-17 02:28:09 +03:00
|
|
|
return {
|
2016-08-17 14:57:19 +03:00
|
|
|
completion: unicode,
|
2016-06-17 02:28:09 +03:00
|
|
|
component: (
|
2020-05-30 15:30:59 +03:00
|
|
|
<PillCompletion title={shortname} aria-label={unicode}>
|
2020-06-05 00:01:16 +03:00
|
|
|
<span>{ unicode }</span>
|
2020-05-30 15:30:59 +03:00
|
|
|
</PillCompletion>
|
2016-07-03 19:45:13 +03:00
|
|
|
),
|
|
|
|
range,
|
2016-06-17 02:28:09 +03:00
|
|
|
};
|
2017-06-27 22:13:48 +03:00
|
|
|
}).slice(0, LIMIT);
|
2016-06-17 02:28:09 +03:00
|
|
|
}
|
2016-09-13 13:11:52 +03:00
|
|
|
return completions;
|
2016-06-17 02:28:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
getName() {
|
2017-05-23 17:16:31 +03:00
|
|
|
return '😃 ' + _t('Emoji');
|
2016-06-17 02:28:09 +03:00
|
|
|
}
|
2016-06-20 11:22:55 +03:00
|
|
|
|
2020-04-20 21:00:54 +03:00
|
|
|
renderCompletions(completions: React.ReactNode[]): React.ReactNode {
|
2019-09-30 16:04:39 +03:00
|
|
|
return (
|
2020-08-29 03:11:08 +03:00
|
|
|
<div
|
|
|
|
className="mx_Autocomplete_Completion_container_pill"
|
|
|
|
role="listbox"
|
|
|
|
aria-label={_t("Emoji Autocomplete")}
|
|
|
|
>
|
2019-09-30 16:04:39 +03:00
|
|
|
{ completions }
|
|
|
|
</div>
|
|
|
|
);
|
2016-08-17 14:57:19 +03:00
|
|
|
}
|
2016-06-17 02:28:09 +03:00
|
|
|
}
|