Commit c9ba4f76 authored by Tim Zallmann's avatar Tim Zallmann

Moved Class to its own class

parent 17ad4f8f
import 'document-register-element'; import 'document-register-element';
import isEmojiUnicodeSupported from '../emoji/support'; import isEmojiUnicodeSupported from '../emoji/support';
export default function installGlEmojiElement() { class GlEmoji extends HTMLElement {
if (!customElements.get('gl-emoji')) { constructor() {
customElements.define( super();
'gl-emoji',
class extends HTMLElement {
constructor() {
super();
const emojiUnicode = this.textContent.trim(); const emojiUnicode = this.textContent.trim();
const { name, unicodeVersion, fallbackSrc, fallbackSpriteClass } = this.dataset; const { name, unicodeVersion, fallbackSrc, fallbackSpriteClass } = this.dataset;
const isEmojiUnicode = const isEmojiUnicode =
this.childNodes && this.childNodes &&
Array.prototype.every.call(this.childNodes, childNode => childNode.nodeType === 3); Array.prototype.every.call(this.childNodes, childNode => childNode.nodeType === 3);
const hasImageFallback = fallbackSrc && fallbackSrc.length > 0; const hasImageFallback = fallbackSrc && fallbackSrc.length > 0;
const hasCssSpriteFalback = fallbackSpriteClass && fallbackSpriteClass.length > 0; const hasCssSpriteFalback = fallbackSpriteClass && fallbackSpriteClass.length > 0;
if ( if (emojiUnicode && isEmojiUnicode && !isEmojiUnicodeSupported(emojiUnicode, unicodeVersion)) {
emojiUnicode && // CSS sprite fallback takes precedence over image fallback
isEmojiUnicode && if (hasCssSpriteFalback) {
!isEmojiUnicodeSupported(emojiUnicode, unicodeVersion) if (!gon.emoji_sprites_css_added && gon.emoji_sprites_css_path) {
) { const emojiSpriteLinkTag = document.createElement('link');
// CSS sprite fallback takes precedence over image fallback emojiSpriteLinkTag.setAttribute('rel', 'stylesheet');
if (hasCssSpriteFalback) { emojiSpriteLinkTag.setAttribute('href', gon.emoji_sprites_css_path);
if (!gon.emoji_sprites_css_added && gon.emoji_sprites_css_path) { document.head.appendChild(emojiSpriteLinkTag);
const emojiSpriteLinkTag = document.createElement('link'); gon.emoji_sprites_css_added = true;
emojiSpriteLinkTag.setAttribute('rel', 'stylesheet'); }
emojiSpriteLinkTag.setAttribute('href', gon.emoji_sprites_css_path); // IE 11 doesn't like adding multiple at once :(
document.head.appendChild(emojiSpriteLinkTag); this.classList.add('emoji-icon');
gon.emoji_sprites_css_added = true; this.classList.add(fallbackSpriteClass);
} } else {
// IE 11 doesn't like adding multiple at once :( import(/* webpackChunkName: 'emoji' */ '../emoji')
this.classList.add('emoji-icon'); .then(({ emojiImageTag, emojiFallbackImageSrc }) => {
this.classList.add(fallbackSpriteClass); if (hasImageFallback) {
this.innerHTML = emojiImageTag(name, fallbackSrc);
} else { } else {
import(/* webpackChunkName: 'emoji' */ '../emoji') const src = emojiFallbackImageSrc(name);
.then(({ emojiImageTag, emojiFallbackImageSrc }) => { this.innerHTML = emojiImageTag(name, src);
if (hasImageFallback) {
this.innerHTML = emojiImageTag(name, fallbackSrc);
} else {
const src = emojiFallbackImageSrc(name);
this.innerHTML = emojiImageTag(name, src);
}
})
.catch(() => {
// do nothing
});
} }
} })
} .catch(() => {
}, // do nothing
); });
}
}
}
}
export default function installGlEmojiElement() {
if (!customElements.get('gl-emoji')) {
customElements.define('gl-emoji', GlEmoji);
} }
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment