debian-mirror-gitlab/app/assets/javascripts/emoji/index.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

261 lines
7.1 KiB
JavaScript
Raw Normal View History

2021-03-11 19:13:27 +05:30
import { escape, minBy } from 'lodash';
2022-01-26 12:08:38 +05:30
import emojiRegexFactory from 'emoji-regex';
2017-09-10 17:25:29 +05:30
import emojiAliases from 'emojis/aliases.json';
2022-01-26 12:08:38 +05:30
import { setAttributes } from '~/lib/utils/dom_utils';
2022-07-23 23:45:48 +05:30
import { getEmojiScoreWithIntent } from '~/emoji/utils';
2020-07-28 23:09:34 +05:30
import AccessorUtilities from '../lib/utils/accessor';
2021-03-11 19:13:27 +05:30
import axios from '../lib/utils/axios_utils';
2022-01-26 12:08:38 +05:30
import { CACHE_KEY, CACHE_VERSION_KEY, CATEGORY_ICON_MAP, FREQUENTLY_USED_KEY } from './constants';
2020-07-28 23:09:34 +05:30
let emojiMap = null;
let validEmojiNames = null;
2021-03-11 19:13:27 +05:30
export const FALLBACK_EMOJI_KEY = 'grey_question';
2020-07-28 23:09:34 +05:30
2021-12-11 22:18:48 +05:30
// Keep the version in sync with `lib/gitlab/emoji.rb`
2022-01-26 12:08:38 +05:30
export const EMOJI_VERSION = '2';
2020-07-28 23:09:34 +05:30
2021-11-11 11:23:49 +05:30
const isLocalStorageAvailable = AccessorUtilities.canUseLocalStorage();
2020-07-28 23:09:34 +05:30
2021-01-03 14:25:43 +05:30
async function loadEmoji() {
if (
isLocalStorageAvailable &&
2022-01-26 12:08:38 +05:30
window.localStorage.getItem(CACHE_VERSION_KEY) === EMOJI_VERSION &&
window.localStorage.getItem(CACHE_KEY)
2021-01-03 14:25:43 +05:30
) {
2022-01-26 12:08:38 +05:30
const emojis = JSON.parse(window.localStorage.getItem(CACHE_KEY));
// Workaround because the pride flag is broken in EMOJI_VERSION = '1'
if (emojis.gay_pride_flag) {
emojis.gay_pride_flag.e = '🏳️‍🌈';
}
return emojis;
2021-01-03 14:25:43 +05:30
}
// 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`,
);
2022-01-26 12:08:38 +05:30
window.localStorage.setItem(CACHE_VERSION_KEY, EMOJI_VERSION);
window.localStorage.setItem(CACHE_KEY, JSON.stringify(data));
2021-01-03 14:25:43 +05:30
return data;
}
2021-03-11 19:13:27 +05:30
async function loadEmojiWithNames() {
2022-01-26 12:08:38 +05:30
const emojiRegex = emojiRegexFactory();
2021-01-03 14:25:43 +05:30
2022-01-26 12:08:38 +05:30
return Object.entries(await loadEmoji()).reduce((acc, [key, value]) => {
// Filter out entries which aren't emojis
if (value.e.match(emojiRegex)?.[0] === value.e) {
acc[key] = { ...value, name: key };
}
2021-03-11 19:13:27 +05:30
return acc;
}, {});
}
2021-01-03 14:25:43 +05:30
2021-03-11 19:13:27 +05:30
async function prepareEmojiMap() {
emojiMap = await loadEmojiWithNames();
validEmojiNames = [...Object.keys(emojiMap), ...Object.keys(emojiAliases)];
2021-01-03 14:25:43 +05:30
}
2020-07-28 23:09:34 +05:30
2021-01-03 14:25:43 +05:30
export function initEmojiMap() {
initEmojiMap.promise = initEmojiMap.promise || prepareEmojiMap();
return initEmojiMap.promise;
2020-07-28 23:09:34 +05:30
}
2017-09-10 17:25:29 +05:30
export function normalizeEmojiName(name) {
return Object.prototype.hasOwnProperty.call(emojiAliases, name) ? emojiAliases[name] : name;
}
2020-07-28 23:09:34 +05:30
export function getValidEmojiNames() {
return validEmojiNames;
}
2017-09-10 17:25:29 +05:30
export function isEmojiNameValid(name) {
2021-03-11 19:13:27 +05:30
if (!emojiMap) {
// eslint-disable-next-line @gitlab/require-i18n-strings
throw new Error('The emoji map is uninitialized or initialization has not completed');
}
return name in emojiMap || name in emojiAliases;
2017-09-10 17:25:29 +05:30
}
2021-01-03 14:25:43 +05:30
export function getAllEmoji() {
return emojiMap;
}
2021-03-11 19:13:27 +05:30
function getAliasesMatchingQuery(query) {
return Object.keys(emojiAliases)
.filter((alias) => alias.includes(query))
.reduce((map, alias) => {
const emojiName = emojiAliases[alias];
const score = alias.indexOf(query);
const prev = map.get(emojiName);
// overwrite if we beat the previous score or we're more alphabetical
const shouldSet =
!prev ||
prev.score > score ||
(prev.score === score && prev.alias.localeCompare(alias) > 0);
if (shouldSet) {
map.set(emojiName, { score, alias });
}
2021-01-03 14:25:43 +05:30
2021-03-11 19:13:27 +05:30
return map;
}, new Map());
}
function getUnicodeMatch(emoji, query) {
if (emoji.e === query) {
return { score: 0, field: 'e', fieldValue: emoji.name, emoji };
2021-01-03 14:25:43 +05:30
}
2021-03-11 19:13:27 +05:30
return null;
}
2021-01-03 14:25:43 +05:30
2021-03-11 19:13:27 +05:30
function getDescriptionMatch(emoji, query) {
if (emoji.d.includes(query)) {
return { score: emoji.d.indexOf(query), field: 'd', fieldValue: emoji.d, emoji };
2021-01-03 14:25:43 +05:30
}
2021-03-11 19:13:27 +05:30
return null;
2017-09-10 17:25:29 +05:30
}
2021-03-11 19:13:27 +05:30
function getAliasMatch(emoji, matchingAliases) {
if (matchingAliases.has(emoji.name)) {
const { score, alias } = matchingAliases.get(emoji.name);
2021-01-03 14:25:43 +05:30
2021-03-11 19:13:27 +05:30
return { score, field: 'alias', fieldValue: alias, emoji };
2021-01-03 14:25:43 +05:30
}
2021-03-11 19:13:27 +05:30
return null;
}
2021-01-03 14:25:43 +05:30
2021-03-11 19:13:27 +05:30
function getNameMatch(emoji, query) {
if (emoji.name.includes(query)) {
return {
score: emoji.name.indexOf(query),
field: 'name',
fieldValue: emoji.name,
emoji,
};
2021-01-03 14:25:43 +05:30
}
2021-03-11 19:13:27 +05:30
return null;
}
2021-01-03 14:25:43 +05:30
2022-07-23 23:45:48 +05:30
// Sort emoji by emoji score falling back to a string comparison
export function sortEmoji(a, b) {
return a.score - b.score || a.fieldValue.localeCompare(b.fieldValue);
}
2021-03-11 19:13:27 +05:30
export function searchEmoji(query) {
const lowercaseQuery = query ? `${query}`.toLowerCase() : '';
2021-01-03 14:25:43 +05:30
2021-03-11 19:13:27 +05:30
const matchingAliases = getAliasesMatchingQuery(lowercaseQuery);
2021-01-03 14:25:43 +05:30
2021-03-11 19:13:27 +05:30
return Object.values(emojiMap)
.map((emoji) => {
const matches = [
getUnicodeMatch(emoji, query),
getDescriptionMatch(emoji, lowercaseQuery),
getAliasMatch(emoji, matchingAliases),
getNameMatch(emoji, lowercaseQuery),
2022-07-23 23:45:48 +05:30
]
.filter(Boolean)
.map((x) => ({ ...x, score: getEmojiScoreWithIntent(x.emoji.name, x.score) }));
2021-01-03 14:25:43 +05:30
2021-03-11 19:13:27 +05:30
return minBy(matches, (x) => x.score);
})
2022-07-23 23:45:48 +05:30
.filter(Boolean)
.sort(sortEmoji);
2017-09-10 17:25:29 +05:30
}
2021-04-17 20:07:23 +05:30
export const CATEGORY_NAMES = Object.keys(CATEGORY_ICON_MAP);
2017-09-10 17:25:29 +05:30
let emojiCategoryMap;
export function getEmojiCategoryMap() {
if (!emojiCategoryMap) {
2021-04-17 20:07:23 +05:30
emojiCategoryMap = CATEGORY_NAMES.reduce((acc, category) => {
if (category === FREQUENTLY_USED_KEY) {
return acc;
}
return { ...acc, [category]: [] };
}, {});
2021-03-08 18:12:59 +05:30
Object.keys(emojiMap).forEach((name) => {
2017-09-10 17:25:29 +05:30
const emoji = emojiMap[name];
2020-07-28 23:09:34 +05:30
if (emojiCategoryMap[emoji.c]) {
emojiCategoryMap[emoji.c].push(name);
2017-09-10 17:25:29 +05:30
}
});
}
return emojiCategoryMap;
}
2021-03-11 19:13:27 +05:30
/**
* Retrieves an emoji by name
*
* @param {String} query The emoji name
* @param {Boolean} fallback If true, a fallback emoji will be returned if the
* named emoji does not exist.
* @returns {Object} The matching emoji.
*/
export function getEmojiInfo(query, fallback = true) {
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 ? `${query}`.toLowerCase() : '';
const name = normalizeEmojiName(lowercaseQuery);
if (name in emojiMap) {
return emojiMap[name];
}
return fallback ? emojiMap[FALLBACK_EMOJI_KEY] : null;
2017-09-10 17:25:29 +05:30
}
export function emojiFallbackImageSrc(inputName) {
2020-07-28 23:09:34 +05:30
const { name } = getEmojiInfo(inputName);
2021-03-08 18:12:59 +05:30
return `${gon.asset_host || ''}${
gon.relative_url_root || ''
}/-/emojis/${EMOJI_VERSION}/${name}.png`;
2017-09-10 17:25:29 +05:30
}
export function emojiImageTag(name, src) {
2022-01-12 12:59:36 +05:30
const img = document.createElement('img');
img.className = 'emoji';
2022-01-26 12:08:38 +05:30
setAttributes(img, {
title: `:${name}:`,
alt: `:${name}:`,
src,
2023-06-20 00:43:36 +05:30
width: '16',
height: '16',
2022-01-26 12:08:38 +05:30
align: 'absmiddle',
});
2022-01-12 12:59:36 +05:30
return img;
2017-09-10 17:25:29 +05:30
}
export function glEmojiTag(inputName, options) {
2020-07-28 23:09:34 +05:30
const opts = { sprite: false, ...options };
const name = normalizeEmojiName(inputName);
2017-09-10 17:25:29 +05:30
const fallbackSpriteClass = `emoji-${name}`;
2018-10-15 14:42:47 +05:30
const fallbackSpriteAttribute = opts.sprite
2021-03-11 19:13:27 +05:30
? `data-fallback-sprite-class="${escape(fallbackSpriteClass)}" `
2018-10-15 14:42:47 +05:30
: '';
2017-09-10 17:25:29 +05:30
2022-06-21 17:19:12 +05:30
const fallbackUrl = opts.url;
const fallbackSrcAttribute = fallbackUrl
? `data-fallback-src="${fallbackUrl}" data-unicode-version="custom"`
: '';
return `<gl-emoji ${fallbackSrcAttribute}${fallbackSpriteAttribute}data-name="${escape(
name,
)}"></gl-emoji>`;
2017-09-10 17:25:29 +05:30
}