Detect supported emoji version

This commit is contained in:
Eduard Kuzmenko 2022-06-27 23:14:52 +02:00
parent bad2574647
commit 895e96f6ec
3 changed files with 46 additions and 4 deletions

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,38 @@
import IS_EMOJI_SUPPORTED from "./emojiSupport";
export type EMOJI_VERSION = '' | '14';
const EMOJI_VERSIONS_SUPPORTED: {
[v in EMOJI_VERSION]: boolean
} = {} as any;
// Thanks to WebZ for the detect
function isEmojiSupported(emoji: string) {
const ALLOWABLE_CALCULATION_ERROR_SIZE = 5;
const inlineEl = document.createElement('span');
inlineEl.classList.add('emoji');
document.body.appendChild(inlineEl);
inlineEl.innerText = emoji; // Emoji from 14.0 version
const newEmojiWidth = inlineEl.offsetWidth;
inlineEl.innerText = '❤️'; // Emoji from 1.0 version
const legacyEmojiWidth = inlineEl.offsetWidth;
document.body.removeChild(inlineEl);
return Math.abs(newEmojiWidth - legacyEmojiWidth) < ALLOWABLE_CALCULATION_ERROR_SIZE;
}
if(IS_EMOJI_SUPPORTED) {
EMOJI_VERSIONS_SUPPORTED[''] = true;
const a: [keyof typeof EMOJI_VERSIONS_SUPPORTED, string][] = [
['14', '🫱🏻']
];
a.forEach(([version, emoji]) => {
EMOJI_VERSIONS_SUPPORTED[version] = isEmojiSupported(emoji);
});
}
export default EMOJI_VERSIONS_SUPPORTED;

View File

@ -4,6 +4,7 @@
* https://github.com/morethanwords/tweb/blob/master/LICENSE
*/
import type { EMOJI_VERSION } from "../../environment/emojiVersionsSupport";
import { SITE_HASHTAGS } from ".";
import { EmojiVersions } from "../../config/emoji";
import IS_EMOJI_SUPPORTED from "../../environment/emojiSupport";
@ -16,6 +17,7 @@ import encodeSpoiler from "./encodeSpoiler";
import parseEntities from "./parseEntities";
import setBlankToAnchor from "./setBlankToAnchor";
import wrapUrl from "./wrapUrl";
import EMOJI_VERSIONS_SUPPORTED from "../../environment/emojiVersionsSupport";
/**
* * Expecting correctly sorted nested entities (RichTextProcessor.sortEntities)
@ -219,8 +221,8 @@ export default function wrapRichText(text: string, options: Partial<{
if(isSupported) {
for(const version in EmojiVersions) {
if(version) {
const emojiData = EmojiVersions[version];
if(emojiData.hasOwnProperty(entity.unicode)) {
const emojiData = EmojiVersions[version as EMOJI_VERSION];
if(emojiData.hasOwnProperty(entity.unicode) && !EMOJI_VERSIONS_SUPPORTED[version as EMOJI_VERSION]) {
isSupported = false;
break;
}