2023-07-27 05:31:52 +00:00
|
|
|
|
/*
|
2024-02-13 15:59:27 +00:00
|
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 05:31:52 +00:00
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
*/
|
|
|
|
|
|
2022-01-18 14:06:16 +00:00
|
|
|
|
export const unicodeEmojiCategories = ['face', 'people', 'animals_and_nature', 'food_and_drink', 'activity', 'travel_and_places', 'objects', 'symbols', 'flags'] as const;
|
|
|
|
|
|
|
|
|
|
export type UnicodeEmojiDef = {
|
2019-09-21 12:31:38 +00:00
|
|
|
|
name: string;
|
|
|
|
|
char: string;
|
2022-01-18 14:06:16 +00:00
|
|
|
|
category: typeof unicodeEmojiCategories[number];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// initial converted from https://github.com/muan/emojilib/commit/242fe68be86ed6536843b83f7e32f376468b38fb
|
2024-09-09 11:57:36 +00:00
|
|
|
|
import _emojilist from './emojilist.json';
|
2022-07-13 12:17:19 +00:00
|
|
|
|
|
2023-05-30 02:18:40 +00:00
|
|
|
|
export const emojilist: UnicodeEmojiDef[] = _emojilist.map(x => ({
|
|
|
|
|
name: x[1] as string,
|
|
|
|
|
char: x[0] as string,
|
2024-09-09 11:57:36 +00:00
|
|
|
|
category: unicodeEmojiCategories[x[2] as number],
|
2023-05-30 02:18:40 +00:00
|
|
|
|
}));
|
2022-12-25 06:52:52 +00:00
|
|
|
|
|
2024-02-29 11:49:40 +00:00
|
|
|
|
const unicodeEmojisMap = new Map<string, UnicodeEmojiDef>(
|
2024-02-29 11:47:24 +00:00
|
|
|
|
emojilist.map(x => [x.char, x]),
|
2024-02-24 01:22:23 +00:00
|
|
|
|
);
|
|
|
|
|
|
2023-02-01 02:25:13 +00:00
|
|
|
|
const _indexByChar = new Map<string, number>();
|
|
|
|
|
const _charGroupByCategory = new Map<string, string[]>();
|
2023-05-30 02:18:40 +00:00
|
|
|
|
for (let i = 0; i < emojilist.length; i++) {
|
|
|
|
|
const emo = emojilist[i];
|
2023-02-01 02:25:13 +00:00
|
|
|
|
_indexByChar.set(emo.char, i);
|
|
|
|
|
|
|
|
|
|
if (_charGroupByCategory.has(emo.category)) {
|
|
|
|
|
_charGroupByCategory.get(emo.category)?.push(emo.char);
|
|
|
|
|
} else {
|
|
|
|
|
_charGroupByCategory.set(emo.category, [emo.char]);
|
|
|
|
|
}
|
2023-05-30 02:18:40 +00:00
|
|
|
|
}
|
2023-02-01 02:25:13 +00:00
|
|
|
|
|
|
|
|
|
export const emojiCharByCategory = _charGroupByCategory;
|
|
|
|
|
|
2024-03-02 04:28:10 +00:00
|
|
|
|
export function getUnicodeEmoji(char: string): UnicodeEmojiDef | string {
|
2024-02-29 11:47:24 +00:00
|
|
|
|
// Colorize it because emojilist.json assumes that
|
2024-03-02 04:28:10 +00:00
|
|
|
|
return unicodeEmojisMap.get(colorizeEmoji(char))
|
|
|
|
|
// カラースタイル絵文字がjsonに無い場合はテキストスタイル絵文字にフォールバックする
|
|
|
|
|
?? unicodeEmojisMap.get(char)
|
|
|
|
|
// それでも見つからない場合はそのまま返す(絵文字情報がjsonに無い場合、このフォールバックが無いとレンダリングに失敗する)
|
|
|
|
|
?? char;
|
2024-02-29 11:47:24 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-02 04:28:10 +00:00
|
|
|
|
export function getEmojiName(char: string): string {
|
2024-01-07 07:02:53 +00:00
|
|
|
|
// Colorize it because emojilist.json assumes that
|
2024-03-02 04:28:10 +00:00
|
|
|
|
const idx = _indexByChar.get(colorizeEmoji(char)) ?? _indexByChar.get(char);
|
|
|
|
|
if (idx === undefined) {
|
|
|
|
|
// 絵文字情報がjsonに無い場合は名前の取得が出来ないのでそのまま返すしか無い
|
|
|
|
|
return char;
|
2023-02-01 02:25:13 +00:00
|
|
|
|
} else {
|
|
|
|
|
return emojilist[idx].name;
|
|
|
|
|
}
|
2022-12-25 06:52:52 +00:00
|
|
|
|
}
|
2023-12-02 06:26:46 +00:00
|
|
|
|
|
2024-03-02 04:28:10 +00:00
|
|
|
|
/**
|
|
|
|
|
* テキストスタイル絵文字(U+260Eなどの1文字で表現される絵文字)をカラースタイル絵文字に変換します(VS16:U+FE0Fを付与)。
|
|
|
|
|
*/
|
2024-01-07 07:02:53 +00:00
|
|
|
|
export function colorizeEmoji(char: string) {
|
|
|
|
|
return char.length === 1 ? `${char}\uFE0F` : char;
|
|
|
|
|
}
|
|
|
|
|
|
2023-12-02 06:26:46 +00:00
|
|
|
|
export interface CustomEmojiFolderTree {
|
|
|
|
|
value: string;
|
|
|
|
|
category: string;
|
|
|
|
|
children: CustomEmojiFolderTree[];
|
|
|
|
|
}
|