2019-03-15 12:44:49 +08:00
|
|
|
import {
|
|
|
|
emojis,
|
|
|
|
aliases,
|
|
|
|
searchAliases,
|
|
|
|
translations,
|
2019-03-21 16:11:33 +08:00
|
|
|
tonableEmojis,
|
|
|
|
replacements
|
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 = {};
|
|
|
|
|
|
|
|
export function registerEmoji(code, url) {
|
|
|
|
code = code.toLowerCase();
|
|
|
|
extendedEmoji[code] = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
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("|");
|
|
|
|
}
|
|
|
|
|
|
|
|
const unicodeRegexp = new RegExp(
|
|
|
|
buildReplacementsList(replacements) + "|\\B:[^\\s:]+(?::t\\d)?:?\\B",
|
2019-03-29 05:31:27 +08:00
|
|
|
"g"
|
|
|
|
);
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
export function performEmojiUnescape(string, opts) {
|
2019-03-15 12:44:49 +08:00
|
|
|
if (!string) {
|
|
|
|
return;
|
|
|
|
}
|
2019-03-13 20:02:56 +08:00
|
|
|
|
2019-03-29 05:31:27 +08:00
|
|
|
return string.replace(unicodeRegexp, m => {
|
2019-05-21 22:56:51 +08:00
|
|
|
const isEmoticon = opts.enableEmojiShortcuts && !!translations[m];
|
2019-03-21 16:11:33 +08:00
|
|
|
const isUnicodeEmoticon = !!replacements[m];
|
|
|
|
let emojiVal;
|
|
|
|
if (isEmoticon) {
|
|
|
|
emojiVal = translations[m];
|
|
|
|
} 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);
|
|
|
|
const classes = isCustomEmoji(emojiVal, opts)
|
|
|
|
? "emoji emoji-custom"
|
|
|
|
: "emoji";
|
|
|
|
|
|
|
|
return url && (isEmoticon || hasEndingColon || isUnicodeEmoticon)
|
|
|
|
? `<img src='${url}' ${
|
|
|
|
opts.skipTitle ? "" : `title='${emojiVal}'`
|
|
|
|
} alt='${emojiVal}' class='${classes}'>`
|
|
|
|
: m;
|
|
|
|
});
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2019-05-21 22:56:51 +08:00
|
|
|
export function performEmojiEscape(string, opts) {
|
2019-03-29 05:31:27 +08:00
|
|
|
return string.replace(unicodeRegexp, m => {
|
2019-03-21 16:11:33 +08:00
|
|
|
if (!!translations[m]) {
|
2019-05-21 22:56:51 +08:00
|
|
|
return opts.emojiShortcuts ? `:${translations[m]}:` : m;
|
2019-03-21 16:11:33 +08:00
|
|
|
} else if (!!replacements[m]) {
|
2019-05-21 22:56:51 +08:00
|
|
|
return `:${replacements[m]}:`;
|
2019-03-21 16:11:33 +08:00
|
|
|
} else {
|
|
|
|
return m;
|
|
|
|
}
|
|
|
|
});
|
2019-03-13 20:02:56 +08:00
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isCustomEmoji(code, opts) {
|
|
|
|
code = code.toLowerCase();
|
|
|
|
if (extendedEmoji.hasOwnProperty(code)) return true;
|
2019-03-15 12:44:49 +08:00
|
|
|
if (opts && opts.customEmoji && opts.customEmoji.hasOwnProperty(code))
|
|
|
|
return true;
|
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)) {
|
|
|
|
url = extendedEmoji[code];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts && opts.customEmoji && opts.customEmoji[code]) {
|
|
|
|
url = opts.customEmoji[code];
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
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 ||
|
|
|
|
_.union(_.keys(emojiHash), _.keys(extendedEmoji), _.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) {
|
|
|
|
results.push(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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];
|
|
|
|
if (item.indexOf(term) === 0) addResult(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
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];
|
|
|
|
if (item.indexOf(term) > 0) addResult(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
const match = _.compact(term.split(":"))[0];
|
|
|
|
if (match) {
|
|
|
|
return tonableEmojis.indexOf(match) !== -1;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|