2017-08-17 22:00:37 +05:30
|
|
|
import installCustomElements from 'document-register-element';
|
2017-09-10 17:25:29 +05:30
|
|
|
import isEmojiUnicodeSupported from '../emoji/support';
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
installCustomElements(window);
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
export default function installGlEmojiElement() {
|
2017-08-17 22:00:37 +05:30
|
|
|
const GlEmojiElementProto = Object.create(HTMLElement.prototype);
|
|
|
|
GlEmojiElementProto.createdCallback = function createdCallback() {
|
|
|
|
const emojiUnicode = this.textContent.trim();
|
2018-10-15 14:42:47 +05:30
|
|
|
const { name, unicodeVersion, fallbackSrc, fallbackSpriteClass } = this.dataset;
|
2017-08-17 22:00:37 +05:30
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
const isEmojiUnicode =
|
|
|
|
this.childNodes &&
|
|
|
|
Array.prototype.every.call(this.childNodes, childNode => childNode.nodeType === 3);
|
2017-08-17 22:00:37 +05:30
|
|
|
const hasImageFallback = fallbackSrc && fallbackSrc.length > 0;
|
|
|
|
const hasCssSpriteFalback = fallbackSpriteClass && fallbackSpriteClass.length > 0;
|
|
|
|
|
2018-10-15 14:42:47 +05:30
|
|
|
if (emojiUnicode && isEmojiUnicode && !isEmojiUnicodeSupported(emojiUnicode, unicodeVersion)) {
|
2017-08-17 22:00:37 +05:30
|
|
|
// CSS sprite fallback takes precedence over image fallback
|
|
|
|
if (hasCssSpriteFalback) {
|
2018-10-15 14:42:47 +05:30
|
|
|
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;
|
|
|
|
}
|
2017-08-17 22:00:37 +05:30
|
|
|
// IE 11 doesn't like adding multiple at once :(
|
|
|
|
this.classList.add('emoji-icon');
|
|
|
|
this.classList.add(fallbackSpriteClass);
|
|
|
|
} else {
|
2017-09-10 17:25:29 +05:30
|
|
|
import(/* webpackChunkName: 'emoji' */ '../emoji')
|
|
|
|
.then(({ emojiImageTag, emojiFallbackImageSrc }) => {
|
|
|
|
if (hasImageFallback) {
|
|
|
|
this.innerHTML = emojiImageTag(name, fallbackSrc);
|
|
|
|
} else {
|
|
|
|
const src = emojiFallbackImageSrc(name);
|
|
|
|
this.innerHTML = emojiImageTag(name, src);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
// do nothing
|
|
|
|
});
|
2017-08-17 22:00:37 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
document.registerElement('gl-emoji', {
|
|
|
|
prototype: GlEmojiElementProto,
|
|
|
|
});
|
|
|
|
}
|