2019-03-15 00:44:49 -04:00
|
|
|
import {
|
|
|
|
aliases,
|
2020-12-01 13:31:26 -05:00
|
|
|
emojis,
|
2019-03-21 04:11:33 -04:00
|
|
|
replacements,
|
2019-03-15 00:44:49 -04:00
|
|
|
searchAliases,
|
2019-03-21 04:11:33 -04:00
|
|
|
tonableEmojis,
|
|
|
|
translations,
|
2019-03-15 00:44:49 -04:00
|
|
|
} from "pretty-text/emoji/data";
|
|
|
|
import { IMAGE_VERSION } from "pretty-text/emoji/version";
|
2019-03-13 08:02:56 -04:00
|
|
|
|
|
|
|
const extendedEmoji = {};
|
|
|
|
|
2020-03-30 14:16:10 -04:00
|
|
|
export function registerEmoji(code, url, group) {
|
2019-03-13 08:02:56 -04:00
|
|
|
code = code.toLowerCase();
|
2020-03-30 14:16:10 -04:00
|
|
|
extendedEmoji[code] = { url, group };
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function extendedEmojiList() {
|
|
|
|
return extendedEmoji;
|
|
|
|
}
|
|
|
|
|
|
|
|
const emojiHash = {};
|
|
|
|
|
2019-08-07 05:38:58 -04:00
|
|
|
export function buildReplacementsList(emojiReplacements) {
|
|
|
|
return Object.keys(emojiReplacements)
|
2019-03-28 17:31:27 -04:00
|
|
|
.sort()
|
|
|
|
.reverse()
|
2019-08-07 05:38:58 -04:00
|
|
|
.map((emoji) => {
|
|
|
|
return emoji
|
|
|
|
.split("")
|
|
|
|
.map((chr) => {
|
|
|
|
return "\\u" + chr.charCodeAt(0).toString(16).padStart(4, "0");
|
|
|
|
})
|
|
|
|
.join("");
|
|
|
|
})
|
|
|
|
.join("|");
|
|
|
|
}
|
|
|
|
|
2019-12-03 11:32:33 -05:00
|
|
|
let replacementListCache;
|
|
|
|
const unicodeRegexpCache = {};
|
|
|
|
|
|
|
|
function replacementList() {
|
|
|
|
if (replacementListCache === undefined) {
|
|
|
|
replacementListCache = buildReplacementsList(replacements);
|
|
|
|
}
|
|
|
|
|
|
|
|
return replacementListCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
function unicodeRegexp(inlineEmoji) {
|
|
|
|
if (unicodeRegexpCache[inlineEmoji] === undefined) {
|
|
|
|
const emojiExpression = inlineEmoji
|
|
|
|
? "|:[^\\s:]+(?::t\\d)?:?"
|
|
|
|
: "|\\B:[^\\s:]+(?::t\\d)?:?\\B";
|
|
|
|
|
|
|
|
unicodeRegexpCache[inlineEmoji] = new RegExp(
|
|
|
|
replacementList() + emojiExpression,
|
|
|
|
"g"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return unicodeRegexpCache[inlineEmoji];
|
|
|
|
}
|
2019-03-28 17:31:27 -04:00
|
|
|
|
2019-03-13 08:02:56 -04:00
|
|
|
// add all default emojis
|
2019-03-15 00:44:49 -04:00
|
|
|
emojis.forEach((code) => (emojiHash[code] = true));
|
2019-03-13 08:02:56 -04:00
|
|
|
|
|
|
|
// and their aliases
|
|
|
|
const aliasHash = {};
|
|
|
|
Object.keys(aliases).forEach((name) => {
|
2019-03-15 00:44:49 -04:00
|
|
|
aliases[name].forEach((alias) => (aliasHash[alias] = name));
|
2019-03-13 08:02:56 -04:00
|
|
|
});
|
|
|
|
|
2019-12-03 11:32:33 -05:00
|
|
|
function isReplacableInlineEmoji(string, index, inlineEmoji) {
|
2020-09-22 10:28:28 -04:00
|
|
|
if (inlineEmoji) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-12-03 11:32:33 -05:00
|
|
|
|
|
|
|
// index depends on regex; when `inlineEmoji` is false, the regex starts
|
|
|
|
// with a `\B` character, so there's no need to subtract from the index
|
2020-05-07 10:25:23 -04:00
|
|
|
const beforeEmoji = string.slice(0, index);
|
2019-12-03 11:32:33 -05:00
|
|
|
|
|
|
|
return (
|
|
|
|
beforeEmoji.length === 0 ||
|
|
|
|
/(?:\s|[>.,\/#!$%^&*;:{}=\-_`~()])$/.test(beforeEmoji) ||
|
|
|
|
new RegExp(`(?:${replacementList()})$`).test(beforeEmoji)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-03-13 08:02:56 -04:00
|
|
|
export function performEmojiUnescape(string, opts) {
|
2020-03-30 14:16:10 -04:00
|
|
|
if (!string) {
|
2019-03-15 00:44:49 -04:00
|
|
|
return;
|
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
|
2019-12-03 11:32:33 -05:00
|
|
|
const inlineEmoji = opts.inlineEmoji;
|
|
|
|
const regexp = unicodeRegexp(inlineEmoji);
|
2020-05-27 14:11:52 -04:00
|
|
|
const allTranslations = Object.assign(
|
|
|
|
{},
|
|
|
|
translations,
|
|
|
|
opts.customEmojiTranslation || {}
|
|
|
|
);
|
2019-12-03 11:32:33 -05:00
|
|
|
|
|
|
|
return string.replace(regexp, (m, index) => {
|
2020-05-27 14:11:52 -04:00
|
|
|
const isEmoticon = opts.enableEmojiShortcuts && !!allTranslations[m];
|
2019-03-21 04:11:33 -04:00
|
|
|
const isUnicodeEmoticon = !!replacements[m];
|
|
|
|
let emojiVal;
|
|
|
|
if (isEmoticon) {
|
2020-05-27 14:11:52 -04:00
|
|
|
emojiVal = allTranslations[m];
|
2019-03-21 04:11:33 -04:00
|
|
|
} else if (isUnicodeEmoticon) {
|
|
|
|
emojiVal = replacements[m];
|
|
|
|
} else {
|
|
|
|
emojiVal = m.slice(1, m.length - 1);
|
|
|
|
}
|
|
|
|
const hasEndingColon = m.lastIndexOf(":") === m.length - 1;
|
|
|
|
const url = buildEmojiUrl(emojiVal, opts);
|
2020-08-24 08:20:51 -04:00
|
|
|
let classes = isCustomEmoji(emojiVal, opts)
|
2019-03-21 04:11:33 -04:00
|
|
|
? "emoji emoji-custom"
|
|
|
|
: "emoji";
|
|
|
|
|
2020-08-24 08:20:51 -04:00
|
|
|
if (opts.class) {
|
|
|
|
classes = `${classes} ${opts.class}`;
|
|
|
|
}
|
|
|
|
|
2019-12-03 11:32:33 -05:00
|
|
|
const isReplacable =
|
|
|
|
(isEmoticon || hasEndingColon || isUnicodeEmoticon) &&
|
|
|
|
isReplacableInlineEmoji(string, index, inlineEmoji);
|
|
|
|
|
|
|
|
return url && isReplacable
|
2020-09-28 07:23:46 -04:00
|
|
|
? `<img width="20" height="20" src='${url}' ${
|
|
|
|
opts.skipTitle ? "" : `title='${emojiVal}'`
|
|
|
|
} ${
|
2020-08-24 08:20:51 -04:00
|
|
|
opts.lazy ? "loading='lazy' " : ""
|
|
|
|
}alt='${emojiVal}' class='${classes}'>`
|
2019-03-21 04:11:33 -04:00
|
|
|
: m;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-05-21 10:56:51 -04:00
|
|
|
export function performEmojiEscape(string, opts) {
|
2019-12-03 11:32:33 -05:00
|
|
|
const inlineEmoji = opts.inlineEmoji;
|
|
|
|
const regexp = unicodeRegexp(inlineEmoji);
|
2020-05-27 14:11:52 -04:00
|
|
|
const allTranslations = Object.assign(
|
|
|
|
{},
|
|
|
|
translations,
|
|
|
|
opts.customEmojiTranslation || {}
|
|
|
|
);
|
2019-12-03 11:32:33 -05:00
|
|
|
|
|
|
|
return string.replace(regexp, (m, index) => {
|
|
|
|
if (isReplacableInlineEmoji(string, index, inlineEmoji)) {
|
2020-05-27 14:11:52 -04:00
|
|
|
if (!!allTranslations[m]) {
|
|
|
|
return opts.emojiShortcuts ? `:${allTranslations[m]}:` : m;
|
2019-12-03 11:32:33 -05:00
|
|
|
} else if (!!replacements[m]) {
|
|
|
|
return `:${replacements[m]}:`;
|
|
|
|
}
|
2019-03-21 04:11:33 -04:00
|
|
|
}
|
2019-12-03 11:32:33 -05:00
|
|
|
|
|
|
|
return m;
|
2019-03-21 04:11:33 -04:00
|
|
|
});
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export function isCustomEmoji(code, opts) {
|
|
|
|
code = code.toLowerCase();
|
2020-09-22 10:28:28 -04:00
|
|
|
if (extendedEmoji.hasOwnProperty(code)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (opts && opts.customEmoji && opts.customEmoji.hasOwnProperty(code)) {
|
2019-03-15 00:44:49 -04:00
|
|
|
return true;
|
2020-09-22 10:28:28 -04:00
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function buildEmojiUrl(code, opts) {
|
|
|
|
let url;
|
|
|
|
code = String(code).toLowerCase();
|
|
|
|
if (extendedEmoji.hasOwnProperty(code)) {
|
2020-03-30 14:16:10 -04:00
|
|
|
url = extendedEmoji[code].url;
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (opts && opts.customEmoji && opts.customEmoji[code]) {
|
2020-03-30 14:16:10 -04:00
|
|
|
url = opts.customEmoji[code].url || opts.customEmoji[code];
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const noToneMatch = code.match(/([^:]+):?/);
|
2021-03-02 14:04:16 -05:00
|
|
|
|
|
|
|
let emojiBasePath = "/images/emoji";
|
|
|
|
if (opts.emojiCDNUrl) {
|
|
|
|
emojiBasePath = opts.emojiCDNUrl;
|
|
|
|
}
|
|
|
|
|
2019-03-15 00:44:49 -04:00
|
|
|
if (
|
|
|
|
noToneMatch &&
|
|
|
|
!url &&
|
|
|
|
(emojiHash.hasOwnProperty(noToneMatch[1]) ||
|
|
|
|
aliasHash.hasOwnProperty(noToneMatch[1]))
|
|
|
|
) {
|
|
|
|
url = opts.getURL(
|
2021-03-02 14:04:16 -05:00
|
|
|
`${emojiBasePath}/${opts.emojiSet}/${code.replace(/:t/, "/")}.png`
|
2019-03-15 00:44:49 -04:00
|
|
|
);
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (url) {
|
|
|
|
url = url + "?v=" + IMAGE_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function emojiExists(code) {
|
|
|
|
code = code.toLowerCase();
|
2019-03-15 00:44:49 -04:00
|
|
|
return !!(
|
|
|
|
extendedEmoji.hasOwnProperty(code) ||
|
|
|
|
emojiHash.hasOwnProperty(code) ||
|
|
|
|
aliasHash.hasOwnProperty(code)
|
|
|
|
);
|
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
|
|
|
|
let toSearch;
|
|
|
|
export function emojiSearch(term, options) {
|
|
|
|
const maxResults = (options && options["maxResults"]) || -1;
|
2021-02-01 11:36:35 -05:00
|
|
|
const diversity = options && options.diversity;
|
2019-03-15 00:44:49 -04:00
|
|
|
if (maxResults === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
|
2019-03-15 00:44:49 -04:00
|
|
|
toSearch =
|
|
|
|
toSearch ||
|
2020-09-01 14:45:44 -04:00
|
|
|
[
|
|
|
|
...Object.keys(emojiHash),
|
|
|
|
...Object.keys(extendedEmoji),
|
|
|
|
...Object.keys(aliasHash),
|
|
|
|
].sort();
|
2019-03-13 08:02:56 -04:00
|
|
|
|
|
|
|
const results = [];
|
|
|
|
|
|
|
|
function addResult(t) {
|
|
|
|
const val = aliasHash[t] || t;
|
|
|
|
if (results.indexOf(val) === -1) {
|
2021-02-01 11:36:35 -05:00
|
|
|
if (diversity && diversity > 1 && isSkinTonableEmoji(val)) {
|
|
|
|
results.push(`${val}:t${diversity}`);
|
|
|
|
} else {
|
|
|
|
results.push(val);
|
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if term matches from beginning
|
2019-03-15 00:44:49 -04:00
|
|
|
for (let i = 0; i < toSearch.length; i++) {
|
2019-03-13 08:02:56 -04:00
|
|
|
const item = toSearch[i];
|
2020-09-22 10:28:28 -04:00
|
|
|
if (item.indexOf(term) === 0) {
|
|
|
|
addResult(item);
|
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (searchAliases[term]) {
|
|
|
|
results.push.apply(results, searchAliases[term]);
|
|
|
|
}
|
|
|
|
|
2019-03-15 00:44:49 -04:00
|
|
|
for (let i = 0; i < toSearch.length; i++) {
|
2019-03-13 08:02:56 -04:00
|
|
|
const item = toSearch[i];
|
2020-09-22 10:28:28 -04:00
|
|
|
if (item.indexOf(term) > 0) {
|
|
|
|
addResult(item);
|
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (maxResults === -1) {
|
|
|
|
return results;
|
|
|
|
} else {
|
|
|
|
return results.slice(0, maxResults);
|
|
|
|
}
|
2019-03-15 00:44:49 -04:00
|
|
|
}
|
2019-03-13 08:02:56 -04:00
|
|
|
|
|
|
|
export function isSkinTonableEmoji(term) {
|
2020-09-01 16:47:22 -04:00
|
|
|
const match = term.split(":").filter(Boolean)[0];
|
2019-03-13 08:02:56 -04:00
|
|
|
if (match) {
|
|
|
|
return tonableEmojis.indexOf(match) !== -1;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|