2016-06-15 02:31:51 +08:00
|
|
|
import { emoji, aliases, translations } from 'pretty-text/emoji/data';
|
|
|
|
|
|
|
|
// bump up this number to expire all emojis
|
2016-07-26 06:38:13 +08:00
|
|
|
export const IMAGE_VERSION = "3";
|
2016-06-15 02:31:51 +08:00
|
|
|
|
|
|
|
const extendedEmoji = {};
|
|
|
|
|
|
|
|
export function registerEmoji(code, url) {
|
|
|
|
code = code.toLowerCase();
|
|
|
|
extendedEmoji[code] = url;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function emojiList() {
|
|
|
|
const result = emoji.slice(0);
|
|
|
|
_.each(extendedEmoji, (v,k) => result.push(k));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const emojiHash = {};
|
|
|
|
|
|
|
|
// add all default emojis
|
|
|
|
emoji.forEach(code => emojiHash[code] = true);
|
|
|
|
|
|
|
|
// and their aliases
|
|
|
|
const aliasHash = {};
|
|
|
|
Object.keys(aliases).forEach(name => {
|
|
|
|
aliases[name].forEach(alias => aliasHash[alias] = name);
|
|
|
|
});
|
|
|
|
|
|
|
|
export function performEmojiUnescape(string, opts) {
|
|
|
|
// this can be further improved by supporting matches of emoticons that don't begin with a colon
|
|
|
|
if (string.indexOf(":") >= 0) {
|
|
|
|
return string.replace(/\B:[^\s:]+:?\B/g, m => {
|
|
|
|
const isEmoticon = !!translations[m];
|
|
|
|
const emojiVal = isEmoticon ? translations[m] : m.slice(1, m.length - 1);
|
|
|
|
const hasEndingColon = m.lastIndexOf(":") === m.length - 1;
|
|
|
|
const url = buildEmojiUrl(emojiVal, opts);
|
2016-11-18 06:28:27 +08:00
|
|
|
const classes = isCustomEmoji(emojiVal, opts) ? "emoji emoji-custom" : "emoji";
|
2016-06-15 02:31:51 +08:00
|
|
|
|
|
|
|
return url && (isEmoticon || hasEndingColon) ?
|
2017-01-03 11:07:55 +08:00
|
|
|
`<img src='${url}' ${opts.skipTitle ? '' : `title='${emojiVal}'`} alt='${emojiVal}' class='${classes}'>` : m;
|
2016-06-15 02:31:51 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2016-11-18 06:28:27 +08:00
|
|
|
export function isCustomEmoji(code, opts) {
|
|
|
|
code = code.toLowerCase();
|
|
|
|
if (extendedEmoji.hasOwnProperty(code)) return true;
|
|
|
|
if (opts && opts.customEmoji && opts.customEmoji.hasOwnProperty(code)) return true;
|
|
|
|
return false;
|
2016-11-08 23:36:09 +08:00
|
|
|
}
|
|
|
|
|
2016-06-15 02:31:51 +08:00
|
|
|
export function buildEmojiUrl(code, opts) {
|
|
|
|
let url;
|
|
|
|
code = code.toLowerCase();
|
|
|
|
|
|
|
|
if (extendedEmoji.hasOwnProperty(code)) {
|
|
|
|
url = extendedEmoji[code];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts && opts.customEmoji && opts.customEmoji[code]) {
|
|
|
|
url = opts.customEmoji[code];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!url && emojiHash.hasOwnProperty(code)) {
|
|
|
|
url = opts.getURL(`/images/emoji/${opts.emojiSet}/${code}.png`);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (url) {
|
|
|
|
url = url + "?v=" + IMAGE_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function emojiExists(code) {
|
|
|
|
code = code.toLowerCase();
|
|
|
|
return !!(extendedEmoji.hasOwnProperty(code) || emojiHash.hasOwnProperty(code));
|
|
|
|
};
|
|
|
|
|
|
|
|
let toSearch;
|
|
|
|
export function emojiSearch(term, options) {
|
|
|
|
const maxResults = (options && options["maxResults"]) || -1;
|
|
|
|
if (maxResults === 0) { return []; }
|
|
|
|
|
|
|
|
toSearch = toSearch || _.union(_.keys(emojiHash), _.keys(extendedEmoji), _.keys(aliasHash)).sort();
|
|
|
|
|
|
|
|
const results = [];
|
|
|
|
|
|
|
|
function addResult(t) {
|
|
|
|
const val = aliasHash[t] || t;
|
|
|
|
if (results.indexOf(val) === -1) {
|
|
|
|
results.push(val);
|
|
|
|
}
|
|
|
|
return maxResults > 0 && results.length >= maxResults;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i=0; i<toSearch.length; i++) {
|
|
|
|
const item = toSearch[i];
|
|
|
|
if (item.indexOf(term) === 0 && addResult(item)) {
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i=0; i<toSearch.length; i++) {
|
|
|
|
const item = toSearch[i];
|
|
|
|
if (item.indexOf(term) > 0 && addResult(item)) {
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
};
|