|
|
|
|
@ -5,7 +5,6 @@ import {
|
|
|
|
|
EMOJI_TYPE_CUSTOM,
|
|
|
|
|
} from './constants';
|
|
|
|
|
import {
|
|
|
|
|
loadCustomEmojiByShortcode,
|
|
|
|
|
loadEmojiByHexcode,
|
|
|
|
|
loadLegacyShortcodesByShortcode,
|
|
|
|
|
LocaleNotLoadedError,
|
|
|
|
|
@ -80,7 +79,7 @@ export function tokenizeText(text: string): TokenizedText {
|
|
|
|
|
export function stringToEmojiState(
|
|
|
|
|
code: string,
|
|
|
|
|
customEmoji: ExtraCustomEmojiMap = {},
|
|
|
|
|
): EmojiState | null {
|
|
|
|
|
): EmojiStateUnicode | Required<EmojiStateCustom> | null {
|
|
|
|
|
if (isUnicodeEmoji(code)) {
|
|
|
|
|
return {
|
|
|
|
|
type: EMOJI_TYPE_UNICODE,
|
|
|
|
|
@ -90,11 +89,13 @@ export function stringToEmojiState(
|
|
|
|
|
|
|
|
|
|
if (isCustomEmoji(code)) {
|
|
|
|
|
const shortCode = code.slice(1, -1);
|
|
|
|
|
return {
|
|
|
|
|
type: EMOJI_TYPE_CUSTOM,
|
|
|
|
|
code: shortCode,
|
|
|
|
|
data: customEmoji[shortCode],
|
|
|
|
|
};
|
|
|
|
|
if (customEmoji[shortCode]) {
|
|
|
|
|
return {
|
|
|
|
|
type: EMOJI_TYPE_CUSTOM,
|
|
|
|
|
code: shortCode,
|
|
|
|
|
data: customEmoji[shortCode],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
@ -115,33 +116,29 @@ export async function loadEmojiDataToState(
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Don't try to load data for custom emoji.
|
|
|
|
|
if (state.type === EMOJI_TYPE_CUSTOM) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// First, try to load the data from IndexedDB.
|
|
|
|
|
try {
|
|
|
|
|
const legacyCode = await loadLegacyShortcodesByShortcode(state.code);
|
|
|
|
|
// This is duplicative, but that's because TS can't distinguish the state type easily.
|
|
|
|
|
if (state.type === EMOJI_TYPE_UNICODE || legacyCode) {
|
|
|
|
|
const data = await loadEmojiByHexcode(
|
|
|
|
|
legacyCode?.hexcode ?? state.code,
|
|
|
|
|
locale,
|
|
|
|
|
);
|
|
|
|
|
if (data) {
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
type: EMOJI_TYPE_UNICODE,
|
|
|
|
|
data,
|
|
|
|
|
// TODO: Use CLDR shortcodes when the picker supports them.
|
|
|
|
|
shortcode: legacyCode?.shortcodes.at(0),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
const data = await loadCustomEmojiByShortcode(state.code);
|
|
|
|
|
if (data) {
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
data,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
const data = await loadEmojiByHexcode(
|
|
|
|
|
legacyCode?.hexcode ?? state.code,
|
|
|
|
|
locale,
|
|
|
|
|
);
|
|
|
|
|
if (data) {
|
|
|
|
|
return {
|
|
|
|
|
...state,
|
|
|
|
|
type: EMOJI_TYPE_UNICODE,
|
|
|
|
|
data,
|
|
|
|
|
// TODO: Use CLDR shortcodes when the picker supports them.
|
|
|
|
|
shortcode: legacyCode?.shortcodes.at(0),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If not found, assume it's not an emoji and return null.
|
|
|
|
|
log(
|
|
|
|
|
'Could not find emoji %s of type %s for locale %s',
|
|
|
|
|
|