debian-mirror-gitlab/app/assets/javascripts/behaviors/gl_emoji.js

55 lines
1.7 KiB
JavaScript
Raw Normal View History

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();
const {
name,
unicodeVersion,
fallbackSrc,
fallbackSpriteClass,
} = this.dataset;
const isEmojiUnicode = this.childNodes && Array.prototype.every.call(
this.childNodes,
childNode => childNode.nodeType === 3,
);
const hasImageFallback = fallbackSrc && fallbackSrc.length > 0;
const hasCssSpriteFalback = fallbackSpriteClass && fallbackSpriteClass.length > 0;
if (
2017-09-10 17:25:29 +05:30
emojiUnicode &&
2017-08-17 22:00:37 +05:30
isEmojiUnicode &&
2017-09-10 17:25:29 +05:30
!isEmojiUnicodeSupported(emojiUnicode, unicodeVersion)
2017-08-17 22:00:37 +05:30
) {
// CSS sprite fallback takes precedence over image fallback
if (hasCssSpriteFalback) {
// 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,
});
}