2020-09-23 02:09:45 -04:00
|
|
|
import fuzzaldrinPlus from 'fuzzaldrin-plus';
|
2017-06-23 15:08:06 -04:00
|
|
|
import emojiAliases from 'emojis/aliases.json';
|
2020-07-08 02:09:13 -04:00
|
|
|
import axios from '../lib/utils/axios_utils';
|
|
|
|
import AccessorUtilities from '../lib/utils/accessor';
|
|
|
|
|
|
|
|
let emojiMap = null;
|
|
|
|
let validEmojiNames = null;
|
|
|
|
|
|
|
|
export const EMOJI_VERSION = '1';
|
|
|
|
|
|
|
|
const isLocalStorageAvailable = AccessorUtilities.isLocalStorageAccessSafe();
|
|
|
|
|
2020-09-28 14:09:40 -04:00
|
|
|
async function loadEmoji() {
|
|
|
|
if (
|
|
|
|
isLocalStorageAvailable &&
|
|
|
|
window.localStorage.getItem('gl-emoji-map-version') === EMOJI_VERSION &&
|
|
|
|
window.localStorage.getItem('gl-emoji-map')
|
|
|
|
) {
|
|
|
|
return JSON.parse(window.localStorage.getItem('gl-emoji-map'));
|
|
|
|
}
|
|
|
|
|
|
|
|
// We load the JSON file direct from the server
|
|
|
|
// because it can't be loaded from a CDN due to
|
|
|
|
// cross domain problems with JSON
|
|
|
|
const { data } = await axios.get(
|
|
|
|
`${gon.relative_url_root || ''}/-/emojis/${EMOJI_VERSION}/emojis.json`,
|
|
|
|
);
|
|
|
|
window.localStorage.setItem('gl-emoji-map-version', EMOJI_VERSION);
|
|
|
|
window.localStorage.setItem('gl-emoji-map', JSON.stringify(data));
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function prepareEmojiMap() {
|
|
|
|
emojiMap = await loadEmoji();
|
|
|
|
|
|
|
|
validEmojiNames = [...Object.keys(emojiMap), ...Object.keys(emojiAliases)];
|
|
|
|
|
|
|
|
Object.keys(emojiMap).forEach(name => {
|
|
|
|
emojiMap[name].aliases = [];
|
|
|
|
emojiMap[name].name = name;
|
|
|
|
});
|
|
|
|
Object.entries(emojiAliases).forEach(([alias, name]) => {
|
|
|
|
// This check, `if (name in emojiMap)` is necessary during testing. In
|
|
|
|
// production, it shouldn't be necessary, because at no point should there
|
|
|
|
// be an entry in aliases.json with no corresponding entry in emojis.json.
|
|
|
|
// However, during testing, the endpoint for emojis.json is mocked with a
|
|
|
|
// small dataset, whereas aliases.json is always `import`ed directly.
|
|
|
|
if (name in emojiMap) emojiMap[name].aliases.push(alias);
|
|
|
|
});
|
|
|
|
}
|
2020-07-08 02:09:13 -04:00
|
|
|
|
2020-09-28 14:09:40 -04:00
|
|
|
export function initEmojiMap() {
|
|
|
|
initEmojiMap.promise = initEmojiMap.promise || prepareEmojiMap();
|
|
|
|
return initEmojiMap.promise;
|
2020-07-08 02:09:13 -04:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-08 02:09:13 -04:00
|
|
|
export function getValidEmojiNames() {
|
|
|
|
return validEmojiNames;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-10-14 11:08:42 -04:00
|
|
|
export function getAllEmoji() {
|
|
|
|
return emojiMap;
|
2020-10-13 05:08:27 -04:00
|
|
|
}
|
|
|
|
|
2020-09-23 02:09:45 -04:00
|
|
|
/**
|
2020-10-13 05:08:27 -04:00
|
|
|
* Retrieves an emoji by name or alias.
|
2020-09-23 02:09:45 -04:00
|
|
|
*
|
2020-10-13 05:08:27 -04:00
|
|
|
* Note: `initEmojiMap` must have been called and completed before this method
|
|
|
|
* can safely be called.
|
2020-09-23 02:09:45 -04:00
|
|
|
*
|
2020-10-13 05:08:27 -04:00
|
|
|
* @param {String} query The emoji name
|
|
|
|
* @param {Boolean} fallback If true, a fallback emoji will be returned if the
|
|
|
|
* named emoji does not exist. Defaults to false.
|
|
|
|
* @returns {Object} The matching emoji.
|
2020-09-23 02:09:45 -04:00
|
|
|
*/
|
2020-10-13 05:08:27 -04:00
|
|
|
export function getEmoji(query, fallback = false) {
|
|
|
|
if (!emojiMap) {
|
|
|
|
// eslint-disable-next-line @gitlab/require-i18n-strings
|
|
|
|
throw new Error('The emoji map is uninitialized or initialization has not completed');
|
|
|
|
}
|
|
|
|
|
|
|
|
const lowercaseQuery = query.toLowerCase();
|
|
|
|
const name = normalizeEmojiName(lowercaseQuery);
|
|
|
|
|
|
|
|
if (name in emojiMap) {
|
|
|
|
return emojiMap[name];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fallback) {
|
|
|
|
return emojiMap.grey_question;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
2017-06-24 02:44:57 -04:00
|
|
|
}
|
|
|
|
|
2020-10-13 05:08:27 -04:00
|
|
|
const searchMatchers = {
|
2020-10-14 11:08:42 -04:00
|
|
|
// Fuzzy matching compares using a fuzzy matching library
|
|
|
|
fuzzy: (value, query) => {
|
|
|
|
const score = fuzzaldrinPlus.score(value, query) > 0;
|
|
|
|
return { score, success: score > 0 };
|
|
|
|
},
|
|
|
|
// Contains matching compares by indexOf
|
|
|
|
contains: (value, query) => {
|
|
|
|
const index = value.indexOf(query.toLowerCase());
|
|
|
|
return { index, success: index >= 0 };
|
|
|
|
},
|
|
|
|
// Exact matching compares by equality
|
|
|
|
exact: (value, query) => {
|
|
|
|
return { success: value === query.toLowerCase() };
|
|
|
|
},
|
2020-10-13 05:08:27 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
const searchPredicates = {
|
2020-10-14 11:08:42 -04:00
|
|
|
// Search by name
|
|
|
|
name: (matcher, query) => emoji => {
|
|
|
|
const m = matcher(emoji.name, query);
|
|
|
|
return [{ ...m, emoji, field: emoji.name }];
|
|
|
|
},
|
|
|
|
// Search by alias
|
|
|
|
alias: (matcher, query) => emoji =>
|
|
|
|
emoji.aliases.map(alias => {
|
|
|
|
const m = matcher(alias, query);
|
|
|
|
return { ...m, emoji, field: alias };
|
|
|
|
}),
|
|
|
|
// Search by description
|
|
|
|
description: (matcher, query) => emoji => {
|
|
|
|
const m = matcher(emoji.d, query);
|
|
|
|
return [{ ...m, emoji, field: emoji.d }];
|
|
|
|
},
|
|
|
|
// Search by unicode value (always exact)
|
|
|
|
unicode: (matcher, query) => emoji => {
|
|
|
|
return [{ emoji, field: emoji.e, success: emoji.e === query }];
|
|
|
|
},
|
2020-10-13 05:08:27 -04:00
|
|
|
};
|
|
|
|
|
2020-09-28 14:09:40 -04:00
|
|
|
/**
|
2020-10-13 05:08:27 -04:00
|
|
|
* Searches emoji by name, aliases, description, and unicode value and returns
|
|
|
|
* an array of matches.
|
|
|
|
*
|
|
|
|
* Behavior is undefined if `opts.fields` is empty or if `opts.match` is fuzzy
|
|
|
|
* and the query is empty.
|
2020-09-28 14:09:40 -04:00
|
|
|
*
|
|
|
|
* Note: `initEmojiMap` must have been called and completed before this method
|
|
|
|
* can safely be called.
|
|
|
|
*
|
2020-10-13 05:08:27 -04:00
|
|
|
* @param {String} query Search query.
|
|
|
|
* @param {Object} opts Search options (optional).
|
|
|
|
* @param {String[]} opts.fields Fields to search. Choices are 'name', 'alias',
|
|
|
|
* 'description', and 'unicode' (value). Default is all (four) fields.
|
|
|
|
* @param {String} opts.match Search method to use. Choices are 'exact',
|
|
|
|
* 'contains', or 'fuzzy'. All methods are case-insensitive. Exact matching (the
|
|
|
|
* default) compares by equality. Contains matching compares by indexOf. Fuzzy
|
|
|
|
* matching compares using a fuzzy matching library.
|
|
|
|
* @param {Boolean} opts.fallback If true, a fallback emoji will be returned if
|
|
|
|
* the result set is empty. Defaults to false.
|
2020-10-14 11:08:42 -04:00
|
|
|
* @param {Boolean} opts.raw Returns the raw match data instead of just the
|
|
|
|
* matching emoji.
|
2020-10-13 05:08:27 -04:00
|
|
|
* @returns {Object[]} A list of emoji that match the query.
|
2020-09-28 14:09:40 -04:00
|
|
|
*/
|
2020-10-13 05:08:27 -04:00
|
|
|
export function searchEmoji(query, opts) {
|
|
|
|
if (!emojiMap) {
|
2020-09-28 14:09:40 -04:00
|
|
|
// eslint-disable-next-line @gitlab/require-i18n-strings
|
|
|
|
throw new Error('The emoji map is uninitialized or initialization has not completed');
|
2020-10-13 05:08:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const {
|
|
|
|
fields = ['name', 'alias', 'description', 'unicode'],
|
|
|
|
match = 'exact',
|
|
|
|
fallback = false,
|
2020-10-14 11:08:42 -04:00
|
|
|
raw = false,
|
2020-10-13 05:08:27 -04:00
|
|
|
} = opts || {};
|
2020-09-28 14:09:40 -04:00
|
|
|
|
2020-10-13 05:08:27 -04:00
|
|
|
// optimization for an exact match in name and alias
|
|
|
|
if (match === 'exact' && new Set([...fields, 'name', 'alias']).size === 2) {
|
|
|
|
const emoji = getEmoji(query, fallback);
|
|
|
|
return emoji ? [emoji] : [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const matcher = searchMatchers[match] || searchMatchers.exact;
|
|
|
|
const predicates = fields.map(f => searchPredicates[f](matcher, query));
|
|
|
|
|
2020-10-14 11:08:42 -04:00
|
|
|
const results = Object.values(emojiMap)
|
|
|
|
.flatMap(emoji => predicates.flatMap(predicate => predicate(emoji)))
|
|
|
|
.filter(r => r.success);
|
2020-10-13 05:08:27 -04:00
|
|
|
|
|
|
|
// Fallback to question mark for unknown emojis
|
|
|
|
if (fallback && results.length === 0) {
|
2020-10-14 11:08:42 -04:00
|
|
|
if (raw) {
|
|
|
|
return [{ emoji: emojiMap.grey_question }];
|
|
|
|
}
|
2020-10-13 05:08:27 -04:00
|
|
|
return [emojiMap.grey_question];
|
|
|
|
}
|
|
|
|
|
2020-10-14 11:08:42 -04:00
|
|
|
if (raw) {
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
return results.map(r => r.emoji);
|
2020-09-28 14:09:40 -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];
|
2020-07-08 02:09:13 -04:00
|
|
|
if (emojiCategoryMap[emoji.c]) {
|
|
|
|
emojiCategoryMap[emoji.c].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) {
|
2020-10-13 05:08:27 -04:00
|
|
|
return searchEmoji(query, {
|
|
|
|
fields: ['name', 'alias'],
|
|
|
|
fallback: true,
|
|
|
|
})[0];
|
2017-06-27 01:54:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function emojiFallbackImageSrc(inputName) {
|
2020-07-08 02:09:13 -04:00
|
|
|
const { name } = getEmojiInfo(inputName);
|
|
|
|
return `${gon.asset_host || ''}${gon.relative_url_root ||
|
|
|
|
''}/-/emojis/${EMOJI_VERSION}/${name}.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) {
|
2020-07-08 02:09:13 -04:00
|
|
|
const opts = { sprite: false, ...options };
|
|
|
|
const name = normalizeEmojiName(inputName);
|
2017-06-27 01:54:34 -04:00
|
|
|
const fallbackSpriteClass = `emoji-${name}`;
|
|
|
|
|
2018-05-02 14:23:17 -04:00
|
|
|
const fallbackSpriteAttribute = opts.sprite
|
|
|
|
? `data-fallback-sprite-class="${fallbackSpriteClass}"`
|
|
|
|
: '';
|
2017-06-27 01:54:34 -04:00
|
|
|
|
|
|
|
return `
|
|
|
|
<gl-emoji
|
|
|
|
${fallbackSpriteAttribute}
|
2020-07-08 02:09:13 -04:00
|
|
|
data-name="${name}"></gl-emoji>
|
2017-06-27 01:54:34 -04:00
|
|
|
`;
|
|
|
|
}
|