2021-02-06 04:09:11 -05:00
|
|
|
import {
|
|
|
|
initEmojiMap,
|
|
|
|
getEmojiInfo,
|
|
|
|
emojiFallbackImageSrc,
|
|
|
|
emojiImageTag,
|
|
|
|
FALLBACK_EMOJI_KEY,
|
|
|
|
} from '../emoji';
|
2021-02-14 13:09:20 -05:00
|
|
|
import isEmojiUnicodeSupported from '../emoji/support';
|
2017-02-27 23:44:34 -05:00
|
|
|
|
2019-02-05 03:33:14 -05:00
|
|
|
class GlEmoji extends HTMLElement {
|
2020-10-13 14:08:58 -04:00
|
|
|
connectedCallback() {
|
2020-07-08 02:09:13 -04:00
|
|
|
this.initialize();
|
|
|
|
}
|
|
|
|
initialize() {
|
|
|
|
let emojiUnicode = this.textContent.trim();
|
|
|
|
const { fallbackSpriteClass, fallbackSrc } = this.dataset;
|
|
|
|
let { name, unicodeVersion } = this.dataset;
|
|
|
|
|
|
|
|
return initEmojiMap().then(() => {
|
|
|
|
if (!unicodeVersion) {
|
|
|
|
const emojiInfo = getEmojiInfo(name);
|
|
|
|
|
|
|
|
if (emojiInfo) {
|
|
|
|
if (name !== emojiInfo.name) {
|
2021-02-06 04:09:11 -05:00
|
|
|
if (emojiInfo.name === FALLBACK_EMOJI_KEY && this.innerHTML) {
|
2021-02-05 10:09:28 -05:00
|
|
|
return; // When fallback emoji is used, but there is a <img> provided, use the <img> instead
|
|
|
|
}
|
|
|
|
|
2020-07-08 02:09:13 -04:00
|
|
|
({ name } = emojiInfo);
|
|
|
|
this.dataset.name = emojiInfo.name;
|
|
|
|
}
|
|
|
|
unicodeVersion = emojiInfo.u;
|
|
|
|
this.dataset.unicodeVersion = unicodeVersion;
|
|
|
|
|
|
|
|
emojiUnicode = emojiInfo.e;
|
2021-12-15 19:15:50 -05:00
|
|
|
this.textContent = emojiInfo.e;
|
2020-07-08 02:09:13 -04:00
|
|
|
|
|
|
|
this.title = emojiInfo.d;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const isEmojiUnicode =
|
|
|
|
this.childNodes &&
|
2020-12-23 16:10:24 -05:00
|
|
|
Array.prototype.every.call(this.childNodes, (childNode) => childNode.nodeType === 3);
|
2020-07-08 02:09:13 -04:00
|
|
|
|
|
|
|
if (
|
|
|
|
emojiUnicode &&
|
|
|
|
isEmojiUnicode &&
|
|
|
|
!isEmojiUnicodeSupported(emojiUnicode, unicodeVersion)
|
|
|
|
) {
|
|
|
|
const hasImageFallback = fallbackSrc && fallbackSrc.length > 0;
|
|
|
|
const hasCssSpriteFallback = fallbackSpriteClass && fallbackSpriteClass.length > 0;
|
|
|
|
|
|
|
|
// CSS sprite fallback takes precedence over image fallback
|
|
|
|
if (hasCssSpriteFallback) {
|
|
|
|
if (!gon.emoji_sprites_css_added && gon.emoji_sprites_css_path) {
|
|
|
|
const emojiSpriteLinkTag = document.createElement('link');
|
|
|
|
emojiSpriteLinkTag.setAttribute('rel', 'stylesheet');
|
|
|
|
emojiSpriteLinkTag.setAttribute('href', gon.emoji_sprites_css_path);
|
|
|
|
document.head.appendChild(emojiSpriteLinkTag);
|
|
|
|
gon.emoji_sprites_css_added = true;
|
|
|
|
}
|
|
|
|
// IE 11 doesn't like adding multiple at once :(
|
|
|
|
this.classList.add('emoji-icon');
|
|
|
|
this.classList.add(fallbackSpriteClass);
|
|
|
|
} else if (hasImageFallback) {
|
2022-01-11 19:16:35 -05:00
|
|
|
this.innerHTML = '';
|
|
|
|
this.appendChild(emojiImageTag(name, fallbackSrc));
|
2020-07-08 02:09:13 -04:00
|
|
|
} else {
|
|
|
|
const src = emojiFallbackImageSrc(name);
|
2022-01-11 19:16:35 -05:00
|
|
|
this.innerHTML = '';
|
|
|
|
this.appendChild(emojiImageTag(name, src));
|
2019-03-07 04:33:03 -05:00
|
|
|
}
|
2019-03-14 05:18:18 -04:00
|
|
|
}
|
2020-07-08 02:09:13 -04:00
|
|
|
});
|
2019-02-05 03:33:14 -05:00
|
|
|
}
|
|
|
|
}
|
2017-02-27 23:44:34 -05:00
|
|
|
|
2019-02-05 03:33:14 -05:00
|
|
|
export default function installGlEmojiElement() {
|
|
|
|
if (!customElements.get('gl-emoji')) {
|
|
|
|
customElements.define('gl-emoji', GlEmoji);
|
|
|
|
}
|
2017-02-27 23:44:34 -05:00
|
|
|
}
|