2020-03-04 04:08:20 -05:00
|
|
|
import { uniq } from 'lodash';
|
2019-03-14 05:18:18 -04:00
|
|
|
import emojiMap from 'emojis/digests.json';
|
2017-06-23 15:08:06 -04:00
|
|
|
import emojiAliases from 'emojis/aliases.json';
|
|
|
|
|
2019-03-14 05:18:18 -04:00
|
|
|
export const validEmojiNames = [...Object.keys(emojiMap), ...Object.keys(emojiAliases)];
|
2017-06-24 02:41:49 -04:00
|
|
|
|
2017-06-27 01:54:34 -04:00
|
|
|
export function normalizeEmojiName(name) {
|
2017-06-24 02:41:49 -04:00
|
|
|
return Object.prototype.hasOwnProperty.call(emojiAliases, name) ? emojiAliases[name] : name;
|
|
|
|
}
|
|
|
|
|
2017-06-27 01:54:34 -04:00
|
|
|
export function isEmojiNameValid(name) {
|
2017-06-24 02:41:49 -04:00
|
|
|
return validEmojiNames.indexOf(name) >= 0;
|
|
|
|
}
|
|
|
|
|
2017-06-27 01:54:34 -04:00
|
|
|
export function filterEmojiNames(filter) {
|
2017-06-24 02:44:57 -04:00
|
|
|
const match = filter.toLowerCase();
|
|
|
|
return validEmojiNames.filter(name => name.indexOf(match) >= 0);
|
|
|
|
}
|
|
|
|
|
2017-06-27 01:54:34 -04:00
|
|
|
export function filterEmojiNamesByAlias(filter) {
|
2020-03-04 04:08:20 -05:00
|
|
|
return uniq(filterEmojiNames(filter).map(name => normalizeEmojiName(name)));
|
2017-06-24 02:44:57 -04:00
|
|
|
}
|
|
|
|
|
2017-06-27 04:48:56 -04:00
|
|
|
let emojiCategoryMap;
|
|
|
|
export function getEmojiCategoryMap() {
|
|
|
|
if (!emojiCategoryMap) {
|
|
|
|
emojiCategoryMap = {
|
2017-06-24 03:25:04 -04:00
|
|
|
activity: [],
|
|
|
|
people: [],
|
|
|
|
nature: [],
|
|
|
|
food: [],
|
|
|
|
travel: [],
|
|
|
|
objects: [],
|
|
|
|
symbols: [],
|
|
|
|
flags: [],
|
|
|
|
};
|
2018-05-02 14:23:17 -04:00
|
|
|
Object.keys(emojiMap).forEach(name => {
|
2017-06-24 03:25:04 -04:00
|
|
|
const emoji = emojiMap[name];
|
2019-03-14 05:18:18 -04:00
|
|
|
if (emojiCategoryMap[emoji.category]) {
|
|
|
|
emojiCategoryMap[emoji.category].push(name);
|
2017-06-24 03:25:04 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2017-06-27 04:48:56 -04:00
|
|
|
return emojiCategoryMap;
|
2017-06-24 03:25:04 -04:00
|
|
|
}
|
|
|
|
|
2017-06-27 01:54:34 -04:00
|
|
|
export function getEmojiInfo(query) {
|
|
|
|
let name = normalizeEmojiName(query);
|
|
|
|
let emojiInfo = emojiMap[name];
|
|
|
|
|
|
|
|
// Fallback to question mark for unknown emojis
|
|
|
|
if (!emojiInfo) {
|
|
|
|
name = 'grey_question';
|
|
|
|
emojiInfo = emojiMap[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
return { ...emojiInfo, name };
|
|
|
|
}
|
|
|
|
|
|
|
|
export function emojiFallbackImageSrc(inputName) {
|
2019-03-14 05:18:18 -04:00
|
|
|
const { name, digest } = getEmojiInfo(inputName);
|
|
|
|
return `${gon.asset_host || ''}${gon.relative_url_root || ''}/assets/emoji/${name}-${digest}.png`;
|
2017-06-27 01:54:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function emojiImageTag(name, src) {
|
|
|
|
return `<img class="emoji" title=":${name}:" alt=":${name}:" src="${src}" width="20" height="20" align="absmiddle" />`;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function glEmojiTag(inputName, options) {
|
|
|
|
const opts = { sprite: false, forceFallback: false, ...options };
|
2019-03-14 05:18:18 -04:00
|
|
|
const { name, ...emojiInfo } = getEmojiInfo(inputName);
|
2017-06-27 01:54:34 -04:00
|
|
|
|
2019-03-14 05:18:18 -04:00
|
|
|
const fallbackImageSrc = emojiFallbackImageSrc(name);
|
2017-06-27 01:54:34 -04:00
|
|
|
const fallbackSpriteClass = `emoji-${name}`;
|
|
|
|
|
|
|
|
const classList = [];
|
|
|
|
if (opts.forceFallback && opts.sprite) {
|
|
|
|
classList.push('emoji-icon');
|
|
|
|
classList.push(fallbackSpriteClass);
|
|
|
|
}
|
|
|
|
const classAttribute = classList.length > 0 ? `class="${classList.join(' ')}"` : '';
|
2018-05-02 14:23:17 -04:00
|
|
|
const fallbackSpriteAttribute = opts.sprite
|
|
|
|
? `data-fallback-sprite-class="${fallbackSpriteClass}"`
|
|
|
|
: '';
|
2019-03-14 05:18:18 -04:00
|
|
|
let contents = emojiInfo.moji;
|
|
|
|
if (opts.forceFallback && !opts.sprite) {
|
|
|
|
contents = emojiImageTag(name, fallbackImageSrc);
|
|
|
|
}
|
2017-06-27 01:54:34 -04:00
|
|
|
|
|
|
|
return `
|
|
|
|
<gl-emoji
|
|
|
|
${classAttribute}
|
|
|
|
data-name="${name}"
|
2019-03-14 05:18:18 -04:00
|
|
|
data-fallback-src="${fallbackImageSrc}"
|
2017-06-27 01:54:34 -04:00
|
|
|
${fallbackSpriteAttribute}
|
2019-03-14 05:18:18 -04:00
|
|
|
data-unicode-version="${emojiInfo.unicodeVersion}"
|
|
|
|
title="${emojiInfo.description}"
|
2017-06-27 01:54:34 -04:00
|
|
|
>
|
2019-03-14 05:18:18 -04:00
|
|
|
${contents}
|
2017-06-27 01:54:34 -04:00
|
|
|
</gl-emoji>
|
|
|
|
`;
|
|
|
|
}
|