2021-09-16 23:25:11 +00:00
|
|
|
// Copyright 2019-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2020-09-01 00:09:28 +00:00
|
|
|
// Camelcase disabled due to emoji-datasource using snake_case
|
|
|
|
/* eslint-disable camelcase */
|
2019-05-24 23:58:27 +00:00
|
|
|
import untypedData from 'emoji-datasource';
|
2019-07-25 16:28:44 +00:00
|
|
|
import emojiRegex from 'emoji-regex';
|
2019-05-24 23:58:27 +00:00
|
|
|
import {
|
|
|
|
compact,
|
|
|
|
flatMap,
|
2019-12-17 20:25:57 +00:00
|
|
|
get,
|
2019-05-24 23:58:27 +00:00
|
|
|
groupBy,
|
|
|
|
isNumber,
|
|
|
|
keyBy,
|
|
|
|
map,
|
|
|
|
mapValues,
|
|
|
|
sortBy,
|
2019-06-27 20:35:21 +00:00
|
|
|
take,
|
2019-05-24 23:58:27 +00:00
|
|
|
} from 'lodash';
|
|
|
|
import Fuse from 'fuse.js';
|
2019-06-27 22:33:15 +00:00
|
|
|
import PQueue from 'p-queue';
|
2019-07-25 16:28:44 +00:00
|
|
|
import is from '@sindresorhus/is';
|
2020-11-04 19:56:49 +00:00
|
|
|
import { getOwn } from '../../util/getOwn';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../../logging/log';
|
2019-05-24 23:58:27 +00:00
|
|
|
|
|
|
|
export const skinTones = ['1F3FB', '1F3FC', '1F3FD', '1F3FE', '1F3FF'];
|
|
|
|
|
|
|
|
export type SkinToneKey = '1F3FB' | '1F3FC' | '1F3FD' | '1F3FE' | '1F3FF';
|
2021-10-06 17:37:53 +00:00
|
|
|
export type SizeClassType =
|
|
|
|
| ''
|
|
|
|
| 'small'
|
|
|
|
| 'medium'
|
|
|
|
| 'large'
|
|
|
|
| 'extra-large'
|
|
|
|
| 'max';
|
2019-05-24 23:58:27 +00:00
|
|
|
|
2019-06-17 18:46:42 +00:00
|
|
|
export type EmojiSkinVariation = {
|
|
|
|
unified: string;
|
|
|
|
non_qualified: null;
|
|
|
|
image: string;
|
|
|
|
sheet_x: number;
|
|
|
|
sheet_y: number;
|
|
|
|
added_in: string;
|
|
|
|
has_img_apple: boolean;
|
|
|
|
has_img_google: boolean;
|
|
|
|
has_img_twitter: boolean;
|
|
|
|
has_img_emojione: boolean;
|
|
|
|
has_img_facebook: boolean;
|
|
|
|
has_img_messenger: boolean;
|
|
|
|
};
|
|
|
|
|
2019-05-24 23:58:27 +00:00
|
|
|
export type EmojiData = {
|
|
|
|
name: string;
|
|
|
|
unified: string;
|
|
|
|
non_qualified: string | null;
|
|
|
|
docomo: string | null;
|
|
|
|
au: string | null;
|
|
|
|
softbank: string | null;
|
|
|
|
google: string | null;
|
|
|
|
image: string;
|
|
|
|
sheet_x: number;
|
|
|
|
sheet_y: number;
|
|
|
|
short_name: string;
|
|
|
|
short_names: Array<string>;
|
|
|
|
text: string | null;
|
|
|
|
texts: Array<string> | null;
|
|
|
|
category: string;
|
|
|
|
sort_order: number;
|
|
|
|
added_in: string;
|
|
|
|
has_img_apple: boolean;
|
|
|
|
has_img_google: boolean;
|
|
|
|
has_img_twitter: boolean;
|
|
|
|
has_img_facebook: boolean;
|
|
|
|
skin_variations?: {
|
2019-06-17 18:46:42 +00:00
|
|
|
[key: string]: EmojiSkinVariation;
|
2019-05-24 23:58:27 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2020-01-23 21:17:06 +00:00
|
|
|
const data = (untypedData as Array<EmojiData>)
|
|
|
|
.filter(emoji => emoji.has_img_apple)
|
|
|
|
.map(emoji =>
|
|
|
|
// Why this weird map?
|
|
|
|
// the emoji dataset has two separate categories for Emotions and People
|
|
|
|
// yet in our UI we display these as a single merged category. In order
|
|
|
|
// for the emojis to be sorted properly we're manually incrementing the
|
|
|
|
// sort_order for the People & Body emojis so that they fall below the
|
|
|
|
// Smiley & Emotions category.
|
|
|
|
emoji.category === 'People & Body'
|
|
|
|
? { ...emoji, sort_order: emoji.sort_order + 1000 }
|
|
|
|
: emoji
|
|
|
|
);
|
2019-06-17 18:46:42 +00:00
|
|
|
|
2019-12-17 20:25:57 +00:00
|
|
|
const ROOT_PATH = get(
|
|
|
|
typeof window !== 'undefined' ? window : null,
|
|
|
|
'ROOT_PATH',
|
|
|
|
''
|
|
|
|
);
|
|
|
|
|
2019-06-17 18:46:42 +00:00
|
|
|
const makeImagePath = (src: string) => {
|
2019-12-17 20:25:57 +00:00
|
|
|
return `${ROOT_PATH}node_modules/emoji-datasource-apple/img/apple/64/${src}`;
|
2019-06-17 18:46:42 +00:00
|
|
|
};
|
|
|
|
|
2021-11-23 22:01:03 +00:00
|
|
|
const imageQueue = new PQueue({
|
|
|
|
concurrency: 10,
|
|
|
|
timeout: 1000 * 60 * 2,
|
|
|
|
throwOnTimeout: true,
|
|
|
|
});
|
2019-06-27 22:33:15 +00:00
|
|
|
const images = new Set();
|
2019-06-17 18:46:42 +00:00
|
|
|
|
2020-09-01 00:09:28 +00:00
|
|
|
export const preloadImages = async (): Promise<void> => {
|
2019-06-21 00:48:54 +00:00
|
|
|
// Preload images
|
2019-06-27 22:45:28 +00:00
|
|
|
const preload = async (src: string) =>
|
2019-06-27 22:33:15 +00:00
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
const img = new Image();
|
|
|
|
img.onload = resolve;
|
|
|
|
img.onerror = reject;
|
|
|
|
img.src = src;
|
|
|
|
images.add(img);
|
|
|
|
setTimeout(reject, 5000);
|
|
|
|
});
|
|
|
|
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info('Preloading emoji images');
|
2019-06-27 22:33:15 +00:00
|
|
|
const start = Date.now();
|
2019-06-17 18:46:42 +00:00
|
|
|
|
2019-06-21 00:48:54 +00:00
|
|
|
data.forEach(emoji => {
|
2019-06-27 22:33:15 +00:00
|
|
|
imageQueue.add(() => preload(makeImagePath(emoji.image)));
|
2019-06-17 18:46:42 +00:00
|
|
|
|
2019-06-21 00:48:54 +00:00
|
|
|
if (emoji.skin_variations) {
|
|
|
|
Object.values(emoji.skin_variations).forEach(variation => {
|
2019-06-27 22:33:15 +00:00
|
|
|
imageQueue.add(() => preload(makeImagePath(variation.image)));
|
2019-06-21 00:48:54 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2019-06-27 22:33:15 +00:00
|
|
|
|
|
|
|
await imageQueue.onEmpty();
|
|
|
|
|
|
|
|
const end = Date.now();
|
2021-09-17 18:27:53 +00:00
|
|
|
log.info(`Done preloading emoji images in ${end - start}ms`);
|
2019-06-21 00:48:54 +00:00
|
|
|
};
|
2019-05-24 23:58:27 +00:00
|
|
|
|
2019-07-25 16:28:44 +00:00
|
|
|
const dataByShortName = keyBy(data, 'short_name');
|
|
|
|
const imageByEmoji: { [key: string]: string } = {};
|
2020-01-17 22:23:19 +00:00
|
|
|
const dataByEmoji: { [key: string]: EmojiData } = {};
|
2019-05-24 23:58:27 +00:00
|
|
|
|
|
|
|
export const dataByCategory = mapValues(
|
|
|
|
groupBy(data, ({ category }) => {
|
|
|
|
if (category === 'Activities') {
|
|
|
|
return 'activity';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (category === 'Animals & Nature') {
|
|
|
|
return 'animal';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (category === 'Flags') {
|
|
|
|
return 'flag';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (category === 'Food & Drink') {
|
|
|
|
return 'food';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (category === 'Objects') {
|
|
|
|
return 'object';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (category === 'Travel & Places') {
|
|
|
|
return 'travel';
|
|
|
|
}
|
|
|
|
|
2020-01-23 21:17:06 +00:00
|
|
|
if (category === 'Smileys & Emotion') {
|
|
|
|
return 'emoji';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (category === 'People & Body') {
|
2019-05-24 23:58:27 +00:00
|
|
|
return 'emoji';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (category === 'Symbols') {
|
|
|
|
return 'symbol';
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'misc';
|
|
|
|
}),
|
|
|
|
arr => sortBy(arr, 'sort_order')
|
|
|
|
);
|
|
|
|
|
2019-06-17 18:46:42 +00:00
|
|
|
export function getEmojiData(
|
2019-05-24 23:58:27 +00:00
|
|
|
shortName: keyof typeof dataByShortName,
|
|
|
|
skinTone?: SkinToneKey | number
|
2019-06-17 18:46:42 +00:00
|
|
|
): EmojiData | EmojiSkinVariation {
|
2019-05-24 23:58:27 +00:00
|
|
|
const base = dataByShortName[shortName];
|
|
|
|
|
|
|
|
if (skinTone && base.skin_variations) {
|
|
|
|
const variation = isNumber(skinTone) ? skinTones[skinTone - 1] : skinTone;
|
|
|
|
|
2020-01-23 21:17:06 +00:00
|
|
|
if (base.skin_variations[variation]) {
|
|
|
|
return base.skin_variations[variation];
|
|
|
|
}
|
|
|
|
|
|
|
|
// For emojis that have two people in them which can have diff skin tones
|
|
|
|
// the Map is of SkinTone-SkinTone. If we don't find the correct skin tone
|
|
|
|
// in the list of variations then we assume it is one of those double skin
|
|
|
|
// emojis and we default to both people having same skin.
|
|
|
|
return base.skin_variations[`${variation}-${variation}`];
|
2019-05-24 23:58:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-17 18:46:42 +00:00
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getImagePath(
|
|
|
|
shortName: keyof typeof dataByShortName,
|
|
|
|
skinTone?: SkinToneKey | number
|
|
|
|
): string {
|
2020-01-23 21:17:06 +00:00
|
|
|
const emojiData = getEmojiData(shortName, skinTone);
|
2019-06-17 18:46:42 +00:00
|
|
|
|
2020-01-23 21:17:06 +00:00
|
|
|
return makeImagePath(emojiData.image);
|
2019-05-24 23:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const fuse = new Fuse(data, {
|
|
|
|
shouldSort: true,
|
2019-06-25 22:40:04 +00:00
|
|
|
threshold: 0.2,
|
2019-06-06 04:06:45 +00:00
|
|
|
maxPatternLength: 32,
|
2019-05-24 23:58:27 +00:00
|
|
|
minMatchCharLength: 1,
|
2019-06-25 22:40:04 +00:00
|
|
|
tokenize: true,
|
|
|
|
tokenSeparator: /[-_\s]+/,
|
2020-12-14 16:15:55 +00:00
|
|
|
keys: ['short_name', 'name'],
|
2019-05-24 23:58:27 +00:00
|
|
|
});
|
|
|
|
|
2020-09-01 00:09:28 +00:00
|
|
|
export function search(query: string, count = 0): Array<EmojiData> {
|
2020-12-14 16:15:55 +00:00
|
|
|
const results = fuse.search(query.substr(0, 32));
|
2019-06-27 20:35:21 +00:00
|
|
|
|
|
|
|
if (count) {
|
|
|
|
return take(results, count);
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
2019-05-24 23:58:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const shortNames = new Set([
|
|
|
|
...map(data, 'short_name'),
|
|
|
|
...compact<string>(flatMap(data, 'short_names')),
|
|
|
|
]);
|
|
|
|
|
2020-09-01 00:09:28 +00:00
|
|
|
export function isShortName(name: string): boolean {
|
2019-05-24 23:58:27 +00:00
|
|
|
return shortNames.has(name);
|
|
|
|
}
|
|
|
|
|
2020-09-01 00:09:28 +00:00
|
|
|
export function unifiedToEmoji(unified: string): string {
|
2019-05-24 23:58:27 +00:00
|
|
|
return unified
|
|
|
|
.split('-')
|
|
|
|
.map(c => String.fromCodePoint(parseInt(c, 16)))
|
|
|
|
.join('');
|
|
|
|
}
|
|
|
|
|
2020-10-21 16:53:32 +00:00
|
|
|
export function convertShortNameToData(
|
2019-07-25 16:28:44 +00:00
|
|
|
shortName: string,
|
|
|
|
skinTone: number | SkinToneKey = 0
|
2020-10-21 16:53:32 +00:00
|
|
|
): EmojiData | undefined {
|
2019-05-24 23:58:27 +00:00
|
|
|
const base = dataByShortName[shortName];
|
|
|
|
|
|
|
|
if (!base) {
|
2020-10-21 16:53:32 +00:00
|
|
|
return undefined;
|
2019-05-24 23:58:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 16:28:44 +00:00
|
|
|
const toneKey = is.number(skinTone) ? skinTones[skinTone - 1] : skinTone;
|
|
|
|
|
|
|
|
if (skinTone && base.skin_variations) {
|
2019-05-24 23:58:27 +00:00
|
|
|
const variation = base.skin_variations[toneKey];
|
|
|
|
if (variation) {
|
2020-10-21 16:53:32 +00:00
|
|
|
return {
|
|
|
|
...base,
|
|
|
|
...variation,
|
|
|
|
};
|
2019-05-24 23:58:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 16:53:32 +00:00
|
|
|
return base;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function convertShortName(
|
|
|
|
shortName: string,
|
|
|
|
skinTone: number | SkinToneKey = 0
|
|
|
|
): string {
|
|
|
|
const emojiData = convertShortNameToData(shortName, skinTone);
|
|
|
|
|
|
|
|
if (!emojiData) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
return unifiedToEmoji(emojiData.unified);
|
2019-05-24 23:58:27 +00:00
|
|
|
}
|
|
|
|
|
2019-07-25 16:28:44 +00:00
|
|
|
export function emojiToImage(emoji: string): string | undefined {
|
2020-11-04 19:56:49 +00:00
|
|
|
return getOwn(imageByEmoji, emoji);
|
2019-07-25 16:28:44 +00:00
|
|
|
}
|
|
|
|
|
2020-10-02 20:05:09 +00:00
|
|
|
export function emojiToData(emoji: string): EmojiData | undefined {
|
2020-11-04 19:56:49 +00:00
|
|
|
return getOwn(dataByEmoji, emoji);
|
2020-10-02 20:05:09 +00:00
|
|
|
}
|
|
|
|
|
2021-08-06 00:17:05 +00:00
|
|
|
export function getEmojiCount(str: string): number {
|
|
|
|
const regex = emojiRegex();
|
|
|
|
|
2019-07-25 16:28:44 +00:00
|
|
|
let match = regex.exec(str);
|
|
|
|
let count = 0;
|
|
|
|
|
|
|
|
if (!regex.global) {
|
|
|
|
return match ? 1 : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (match) {
|
|
|
|
count += 1;
|
|
|
|
match = regex.exec(str);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSizeClass(str: string): SizeClassType {
|
|
|
|
// Do we have non-emoji characters?
|
|
|
|
if (str.replace(emojiRegex(), '').trim().length > 0) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2021-08-06 00:17:05 +00:00
|
|
|
const emojiCount = getEmojiCount(str);
|
2019-07-25 16:28:44 +00:00
|
|
|
|
2021-10-06 17:37:53 +00:00
|
|
|
if (emojiCount === 1) {
|
|
|
|
return 'max';
|
2020-09-01 00:09:28 +00:00
|
|
|
}
|
2021-10-06 17:37:53 +00:00
|
|
|
if (emojiCount === 2) {
|
|
|
|
return 'extra-large';
|
|
|
|
}
|
|
|
|
if (emojiCount === 3) {
|
|
|
|
return 'large';
|
2020-09-01 00:09:28 +00:00
|
|
|
}
|
2021-10-06 17:37:53 +00:00
|
|
|
if (emojiCount === 4) {
|
2019-07-25 16:28:44 +00:00
|
|
|
return 'medium';
|
2020-09-01 00:09:28 +00:00
|
|
|
}
|
2021-10-06 17:37:53 +00:00
|
|
|
if (emojiCount === 5) {
|
|
|
|
return 'small';
|
2019-07-25 16:28:44 +00:00
|
|
|
}
|
2021-10-06 17:37:53 +00:00
|
|
|
return '';
|
2019-07-25 16:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
data.forEach(emoji => {
|
|
|
|
const { short_name, short_names, skin_variations, image } = emoji;
|
|
|
|
|
|
|
|
if (short_names) {
|
|
|
|
short_names.forEach(name => {
|
|
|
|
dataByShortName[name] = emoji;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
imageByEmoji[convertShortName(short_name)] = makeImagePath(image);
|
2020-01-17 22:23:19 +00:00
|
|
|
dataByEmoji[convertShortName(short_name)] = emoji;
|
2019-07-25 16:28:44 +00:00
|
|
|
|
|
|
|
if (skin_variations) {
|
|
|
|
Object.entries(skin_variations).forEach(([tone, variation]) => {
|
2021-11-11 22:43:05 +00:00
|
|
|
imageByEmoji[convertShortName(short_name, tone as SkinToneKey)] =
|
|
|
|
makeImagePath(variation.image);
|
2020-01-17 22:23:19 +00:00
|
|
|
dataByEmoji[convertShortName(short_name, tone as SkinToneKey)] = emoji;
|
2019-07-25 16:28:44 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|