Fix localized emoji auto-completions
This commit is contained in:
parent
dd3ab66593
commit
074fa8af4b
11 changed files with 66 additions and 49 deletions
|
@ -12,13 +12,21 @@ import type { LocaleEmojiListType } from '../../types/emoji';
|
|||
import { strictAssert } from '../../util/assert';
|
||||
import { useFunEmojiLocalization } from './FunEmojiLocalizationProvider';
|
||||
|
||||
export type FunEmojiLocalizerIndex = ReadonlyMap<EmojiParentKey, string>;
|
||||
export type FunEmojiLocalizer = (key: EmojiVariantKey) => string;
|
||||
export type FunEmojiLocalizerIndex = Readonly<{
|
||||
parentKeyToLocaleShortName: ReadonlyMap<EmojiParentKey, string>;
|
||||
localeShortNameToParentKey: ReadonlyMap<string, EmojiParentKey>;
|
||||
}>;
|
||||
|
||||
export type FunEmojiLocalizer = Readonly<{
|
||||
getLocaleShortName: (key: EmojiVariantKey) => string;
|
||||
getParentKeyForText: (text: string) => EmojiParentKey | null;
|
||||
}>;
|
||||
|
||||
export function createFunEmojiLocalizerIndex(
|
||||
localeEmojiList: LocaleEmojiListType
|
||||
): FunEmojiLocalizerIndex {
|
||||
const index = new Map<EmojiParentKey, string>();
|
||||
const parentKeyToLocaleShortName = new Map<EmojiParentKey, string>();
|
||||
const localeShortNameToParentKey = new Map<string, EmojiParentKey>();
|
||||
|
||||
for (const entry of localeEmojiList) {
|
||||
strictAssert(
|
||||
|
@ -29,26 +37,35 @@ export function createFunEmojiLocalizerIndex(
|
|||
const variantKey = getEmojiVariantKeyByValue(entry.emoji);
|
||||
const parentKey = getEmojiParentKeyByVariantKey(variantKey);
|
||||
const localizedShortName = entry.tags.at(0) ?? entry.shortName;
|
||||
index.set(parentKey, localizedShortName);
|
||||
parentKeyToLocaleShortName.set(parentKey, localizedShortName);
|
||||
localeShortNameToParentKey.set(localizedShortName, parentKey);
|
||||
}
|
||||
|
||||
return index;
|
||||
return { parentKeyToLocaleShortName, localeShortNameToParentKey };
|
||||
}
|
||||
|
||||
/** @internal exported for tests */
|
||||
export function _createFunEmojiLocalizer(
|
||||
emojiLocalizerIndex: FunEmojiLocalizerIndex
|
||||
): FunEmojiLocalizer {
|
||||
return variantKey => {
|
||||
function getLocaleShortName(variantKey: EmojiVariantKey) {
|
||||
const parentKey = getEmojiParentKeyByVariantKey(variantKey);
|
||||
const localeShortName = emojiLocalizerIndex.get(parentKey);
|
||||
const localeShortName =
|
||||
emojiLocalizerIndex.parentKeyToLocaleShortName.get(parentKey);
|
||||
if (localeShortName != null) {
|
||||
return localeShortName;
|
||||
}
|
||||
// Fallback to english short name
|
||||
const parent = getEmojiParentByKey(parentKey);
|
||||
return parent.englishShortNameDefault;
|
||||
};
|
||||
}
|
||||
|
||||
function getParentKeyForText(text: string): EmojiParentKey | null {
|
||||
const parentKey = emojiLocalizerIndex.localeShortNameToParentKey.get(text);
|
||||
return parentKey ?? null;
|
||||
}
|
||||
|
||||
return { getLocaleShortName, getParentKeyForText };
|
||||
}
|
||||
|
||||
export function useFunEmojiLocalizer(): FunEmojiLocalizer {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue