mirror of
https://github.com/mastodon/mastodon.git
synced 2025-10-05 16:42:47 +00:00
This commit is contained in:
parent
1d9a5e4abd
commit
df52e59a5c
|
@ -41,22 +41,8 @@ export function emojifyElement<Element extends HTMLElement>(
|
||||||
appState: EmojiAppState,
|
appState: EmojiAppState,
|
||||||
extraEmojis: ExtraCustomEmojiMap = {},
|
extraEmojis: ExtraCustomEmojiMap = {},
|
||||||
): Element | null {
|
): Element | null {
|
||||||
const finish = timingMeasurementHelper('emojifyElement');
|
|
||||||
// Check the cache and return it if we get a hit.
|
|
||||||
const cacheKey = createTextCacheKey(element, appState, extraEmojis);
|
|
||||||
const cached = textCache.get(cacheKey);
|
|
||||||
if (cached !== undefined) {
|
|
||||||
log('Cache hit on %s', element.outerHTML);
|
|
||||||
if (cached === null) {
|
|
||||||
// return null;
|
|
||||||
}
|
|
||||||
// element.innerHTML = cached;
|
|
||||||
// return element;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Exit if there are no emoji in the string.
|
// Exit if there are no emoji in the string.
|
||||||
if (!stringHasAnyEmoji(element.innerHTML)) {
|
if (!stringHasAnyEmoji(element.innerHTML)) {
|
||||||
textCache.set(cacheKey, null);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,8 +82,6 @@ export function emojifyElement<Element extends HTMLElement>(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
textCache.set(cacheKey, element.innerHTML);
|
|
||||||
finish();
|
|
||||||
return element;
|
return element;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,46 +90,20 @@ export function emojifyText(
|
||||||
appState: EmojiAppState,
|
appState: EmojiAppState,
|
||||||
extraEmojis: ExtraCustomEmojiMap = {},
|
extraEmojis: ExtraCustomEmojiMap = {},
|
||||||
): string | null {
|
): string | null {
|
||||||
const cacheKey = createTextCacheKey(text, appState, extraEmojis);
|
|
||||||
const cached = textCache.get(cacheKey);
|
|
||||||
if (cached !== undefined) {
|
|
||||||
log('Cache hit on %s', text);
|
|
||||||
return cached ?? text;
|
|
||||||
}
|
|
||||||
if (!stringHasAnyEmoji(text)) {
|
if (!stringHasAnyEmoji(text)) {
|
||||||
textCache.set(cacheKey, null);
|
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
const tokens = tokenizeText(text, appState.mode);
|
const tokens = tokenizeText(text, appState.mode);
|
||||||
const eleArray = tokensToElementArray(tokens, appState, extraEmojis);
|
const eleArray = tokensToElementArray(tokens, appState, extraEmojis);
|
||||||
if (!eleArray) {
|
if (!eleArray) {
|
||||||
textCache.set(cacheKey, null);
|
|
||||||
return text;
|
return text;
|
||||||
}
|
}
|
||||||
const rendered = renderedToHTML(eleArray, document.createElement('div'));
|
const rendered = renderedToHTML(eleArray, document.createElement('div'));
|
||||||
textCache.set(cacheKey, rendered.innerHTML);
|
|
||||||
return rendered.innerHTML;
|
return rendered.innerHTML;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Private functions
|
// Private functions
|
||||||
|
|
||||||
// This is the text cache. It contains full HTML strings or null to indicate there is no emoji here.
|
|
||||||
const textCache = createLimitedCache<string | null>({
|
|
||||||
log: log.extend('text'),
|
|
||||||
});
|
|
||||||
|
|
||||||
function createTextCacheKey(
|
|
||||||
input: HTMLElement | string,
|
|
||||||
appState: EmojiAppState,
|
|
||||||
extraEmojis: ExtraCustomEmojiMap,
|
|
||||||
) {
|
|
||||||
return JSON.stringify([
|
|
||||||
input instanceof HTMLElement ? input.outerHTML : input,
|
|
||||||
appState,
|
|
||||||
extraEmojis,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
// These are the unicode/custom emoji data caches.
|
// These are the unicode/custom emoji data caches.
|
||||||
const unicodeEmojiCache = createLimitedCache<
|
const unicodeEmojiCache = createLimitedCache<
|
||||||
Required<EmojiStateUnicode> | EmojiStateMissing
|
Required<EmojiStateUnicode> | EmojiStateMissing
|
||||||
|
@ -230,7 +188,10 @@ export function tokenizeText(text: string, mode: EmojiMode): TokenizedText {
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
||||||
|
|
||||||
function shouldRenderUnicodeImage(code: string, mode: EmojiMode): boolean {
|
export function shouldRenderUnicodeImage(
|
||||||
|
code: string,
|
||||||
|
mode: EmojiMode,
|
||||||
|
): boolean {
|
||||||
// If the mode is native or native with flags for non-flag emoji
|
// If the mode is native or native with flags for non-flag emoji
|
||||||
// we can just append the text node directly.
|
// we can just append the text node directly.
|
||||||
if (mode === EMOJI_MODE_NATIVE) {
|
if (mode === EMOJI_MODE_NATIVE) {
|
||||||
|
@ -424,7 +385,6 @@ function timingMeasurementHelper(name: string) {
|
||||||
|
|
||||||
// Testing helpers
|
// Testing helpers
|
||||||
export const testCacheClear = () => {
|
export const testCacheClear = () => {
|
||||||
textCache.clear();
|
|
||||||
unicodeEmojiCache.clear();
|
unicodeEmojiCache.clear();
|
||||||
customEmojiCache.clear();
|
customEmojiCache.clear();
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue
Block a user