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

73 lines
2.4 KiB
JavaScript
Raw Normal View History

2019-03-02 22:35:43 +05:30
import 'document-register-element';
2017-09-10 17:25:29 +05:30
import isEmojiUnicodeSupported from '../emoji/support';
2020-07-28 23:09:34 +05:30
import { initEmojiMap, getEmojiInfo, emojiFallbackImageSrc, emojiImageTag } from '../emoji';
2017-08-17 22:00:37 +05:30
2019-03-02 22:35:43 +05:30
class GlEmoji extends HTMLElement {
2021-01-03 14:25:43 +05:30
connectedCallback() {
2020-07-28 23:09:34 +05:30
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) {
({ name } = emojiInfo);
this.dataset.name = emojiInfo.name;
}
unicodeVersion = emojiInfo.u;
this.dataset.unicodeVersion = unicodeVersion;
emojiUnicode = emojiInfo.e;
this.innerHTML = emojiInfo.e;
this.title = emojiInfo.d;
}
}
const isEmojiUnicode =
this.childNodes &&
Array.prototype.every.call(this.childNodes, childNode => childNode.nodeType === 3);
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) {
this.innerHTML = emojiImageTag(name, fallbackSrc);
} else {
const src = emojiFallbackImageSrc(name);
this.innerHTML = emojiImageTag(name, src);
2018-10-15 14:42:47 +05:30
}
2017-08-17 22:00:37 +05:30
}
2020-07-28 23:09:34 +05:30
});
2019-03-02 22:35:43 +05:30
}
}
2017-08-17 22:00:37 +05:30
2019-03-02 22:35:43 +05:30
export default function installGlEmojiElement() {
if (!customElements.get('gl-emoji')) {
customElements.define('gl-emoji', GlEmoji);
}
2017-08-17 22:00:37 +05:30
}