Browse Source

Detect supported emoji version

master
Eduard Kuzmenko 2 years ago
parent
commit
895e96f6ec
  1. 6
      src/config/emoji.ts
  2. 38
      src/environment/emojiVersionsSupport.ts
  3. 6
      src/lib/richTextProcessor/wrapRichText.ts

6
src/config/emoji.ts

File diff suppressed because one or more lines are too long

38
src/environment/emojiVersionsSupport.ts

@ -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;

6
src/lib/richTextProcessor/wrapRichText.ts

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

Loading…
Cancel
Save