2019-12-18 18:40:19 +03:00
|
|
|
/*
|
|
|
|
Copyright 2019 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import EMOJIBASE from 'emojibase-data/en/compact.json';
|
2021-07-11 05:40:30 +03:00
|
|
|
import SHORTCODES from 'emojibase-data/en/shortcodes/iamcal.json';
|
2019-12-18 18:40:19 +03:00
|
|
|
|
2020-04-20 21:17:58 +03:00
|
|
|
export interface IEmoji {
|
|
|
|
annotation: string;
|
2021-07-11 05:40:30 +03:00
|
|
|
group?: number;
|
2020-04-20 21:17:58 +03:00
|
|
|
hexcode: string;
|
2021-07-11 05:40:30 +03:00
|
|
|
order?: number;
|
|
|
|
tags?: string[];
|
2020-04-20 21:17:58 +03:00
|
|
|
unicode: string;
|
|
|
|
emoticon?: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface IEmojiWithFilterString extends IEmoji {
|
|
|
|
filterString?: string;
|
|
|
|
}
|
|
|
|
|
2019-12-18 18:40:19 +03:00
|
|
|
// The unicode is stored without the variant selector
|
2020-04-20 21:17:58 +03:00
|
|
|
const UNICODE_TO_EMOJI = new Map<string, IEmojiWithFilterString>(); // not exported as gets for it are handled by getEmojiFromUnicode
|
|
|
|
export const EMOTICON_TO_EMOJI = new Map<string, IEmojiWithFilterString>();
|
2019-12-18 18:40:19 +03:00
|
|
|
|
2020-01-07 21:48:55 +03:00
|
|
|
export const getEmojiFromUnicode = unicode => UNICODE_TO_EMOJI.get(stripVariation(unicode));
|
2019-12-18 18:40:19 +03:00
|
|
|
|
2021-07-11 05:40:30 +03:00
|
|
|
const toArray = (shortcodes?: string | string[]): string[] =>
|
|
|
|
typeof shortcodes === "string" ? [shortcodes] : (shortcodes ?? []);
|
|
|
|
export const getShortcodes = (emoji: IEmoji): string[] =>
|
|
|
|
toArray(SHORTCODES[emoji.hexcode]);
|
|
|
|
|
2019-12-18 18:40:19 +03:00
|
|
|
const EMOJIBASE_GROUP_ID_TO_CATEGORY = [
|
|
|
|
"people", // smileys
|
|
|
|
"people", // actually people
|
|
|
|
"control", // modifiers and such, not displayed in picker
|
|
|
|
"nature",
|
|
|
|
"foods",
|
|
|
|
"places",
|
|
|
|
"activity",
|
|
|
|
"objects",
|
|
|
|
"symbols",
|
|
|
|
"flags",
|
|
|
|
];
|
|
|
|
|
|
|
|
export const DATA_BY_CATEGORY = {
|
|
|
|
"people": [],
|
|
|
|
"nature": [],
|
|
|
|
"foods": [],
|
|
|
|
"places": [],
|
|
|
|
"activity": [],
|
|
|
|
"objects": [],
|
|
|
|
"symbols": [],
|
|
|
|
"flags": [],
|
|
|
|
};
|
|
|
|
|
2020-06-06 01:34:04 +03:00
|
|
|
const ZERO_WIDTH_JOINER = "\u200D";
|
|
|
|
|
2019-12-18 18:40:19 +03:00
|
|
|
// Store various mappings from unicode/emoticon/shortcode to the Emoji objects
|
2020-04-20 21:17:58 +03:00
|
|
|
EMOJIBASE.forEach((emoji: IEmojiWithFilterString) => {
|
2021-07-11 05:40:30 +03:00
|
|
|
const shortcodes = getShortcodes(emoji);
|
2019-12-18 18:40:19 +03:00
|
|
|
const categoryId = EMOJIBASE_GROUP_ID_TO_CATEGORY[emoji.group];
|
|
|
|
if (DATA_BY_CATEGORY.hasOwnProperty(categoryId)) {
|
|
|
|
DATA_BY_CATEGORY[categoryId].push(emoji);
|
|
|
|
}
|
2021-07-11 05:40:30 +03:00
|
|
|
|
2019-12-18 18:40:19 +03:00
|
|
|
// This is used as the string to match the query against when filtering emojis
|
2021-07-11 05:40:30 +03:00
|
|
|
emoji.filterString = (`${emoji.annotation}\n${shortcodes.join('\n')}}\n${emoji.emoticon || ''}\n` +
|
2020-07-17 18:03:07 +03:00
|
|
|
`${emoji.unicode.split(ZERO_WIDTH_JOINER).join("\n")}`).toLowerCase();
|
2019-12-18 18:40:19 +03:00
|
|
|
|
|
|
|
// Add mapping from unicode to Emoji object
|
2020-01-07 21:48:55 +03:00
|
|
|
// The 'unicode' field that we use in emojibase has either
|
|
|
|
// VS15 or VS16 appended to any characters that can take
|
|
|
|
// variation selectors. Which one it appends depends
|
|
|
|
// on whether emojibase considers their type to be 'text' or
|
|
|
|
// 'emoji'. We therefore strip any variation chars from strings
|
|
|
|
// both when building the map and when looking up.
|
|
|
|
UNICODE_TO_EMOJI.set(stripVariation(emoji.unicode), emoji);
|
2019-12-18 18:40:19 +03:00
|
|
|
|
|
|
|
if (emoji.emoticon) {
|
|
|
|
// Add mapping from emoticon to Emoji object
|
|
|
|
EMOTICON_TO_EMOJI.set(emoji.emoticon, emoji);
|
|
|
|
}
|
|
|
|
});
|
2020-01-07 21:48:55 +03:00
|
|
|
|
|
|
|
/**
|
2020-01-27 01:15:44 +03:00
|
|
|
* Strips variation selectors from the end of given string
|
|
|
|
* NB. Skin tone modifiers are not variation selectors:
|
2020-01-07 21:48:55 +03:00
|
|
|
* this function does not touch them. (Should it?)
|
|
|
|
*
|
|
|
|
* @param {string} str string to strip
|
|
|
|
* @returns {string} stripped string
|
|
|
|
*/
|
|
|
|
function stripVariation(str) {
|
2020-01-27 01:15:44 +03:00
|
|
|
return str.replace(/[\uFE00-\uFE0F]$/, "");
|
2020-01-07 21:48:55 +03:00
|
|
|
}
|
2020-04-20 21:17:58 +03:00
|
|
|
|
|
|
|
export const EMOJI: IEmoji[] = EMOJIBASE;
|