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