2017-06-05 08:06:23 -04:00
|
|
|
import { emojis, aliases, translations, tonableEmojis } from 'pretty-text/emoji/data';
|
2016-06-14 14:31:51 -04:00
|
|
|
|
|
|
|
// bump up this number to expire all emojis
|
2017-07-11 11:51:53 -04:00
|
|
|
export const IMAGE_VERSION = "<%= Emoji::EMOJI_VERSION %>";
|
2016-06-14 14:31:51 -04:00
|
|
|
|
|
|
|
const extendedEmoji = {};
|
|
|
|
|
|
|
|
export function registerEmoji(code, url) {
|
|
|
|
code = code.toLowerCase();
|
|
|
|
extendedEmoji[code] = url;
|
|
|
|
}
|
|
|
|
|
2017-07-11 11:51:53 -04:00
|
|
|
export function extendedEmojiList() {
|
|
|
|
return extendedEmoji;
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const emojiHash = {};
|
|
|
|
|
|
|
|
// add all default emojis
|
2017-06-05 08:06:23 -04:00
|
|
|
emojis.forEach(code => emojiHash[code] = true);
|
2016-06-14 14:31:51 -04:00
|
|
|
|
|
|
|
// and their aliases
|
|
|
|
const aliasHash = {};
|
|
|
|
Object.keys(aliases).forEach(name => {
|
|
|
|
aliases[name].forEach(alias => aliasHash[alias] = name);
|
|
|
|
});
|
|
|
|
|
|
|
|
export function performEmojiUnescape(string, opts) {
|
2017-09-11 10:31:38 -04:00
|
|
|
if (!string) { return; }
|
|
|
|
|
2016-06-14 14:31:51 -04:00
|
|
|
// this can be further improved by supporting matches of emoticons that don't begin with a colon
|
|
|
|
if (string.indexOf(":") >= 0) {
|
2017-06-05 08:06:23 -04:00
|
|
|
return string.replace(/\B:[^\s:]+(?::t\d)?:?\B/g, m => {
|
2016-06-14 14:31:51 -04:00
|
|
|
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-17 17:28:27 -05:00
|
|
|
const classes = isCustomEmoji(emojiVal, opts) ? "emoji emoji-custom" : "emoji";
|
2016-06-14 14:31:51 -04:00
|
|
|
|
|
|
|
return url && (isEmoticon || hasEndingColon) ?
|
2017-01-02 22:07:55 -05:00
|
|
|
`<img src='${url}' ${opts.skipTitle ? '' : `title='${emojiVal}'`} alt='${emojiVal}' class='${classes}'>` : m;
|
2016-06-14 14:31:51 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2016-11-17 17:28:27 -05: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 10:36:09 -05:00
|
|
|
}
|
|
|
|
|
2016-06-14 14:31:51 -04:00
|
|
|
export function buildEmojiUrl(code, opts) {
|
|
|
|
let url;
|
2017-07-11 11:51:53 -04:00
|
|
|
code = String(code).toLowerCase();
|
2016-06-14 14:31:51 -04:00
|
|
|
if (extendedEmoji.hasOwnProperty(code)) {
|
|
|
|
url = extendedEmoji[code];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (opts && opts.customEmoji && opts.customEmoji[code]) {
|
|
|
|
url = opts.customEmoji[code];
|
|
|
|
}
|
|
|
|
|
2017-07-17 17:25:40 -04:00
|
|
|
const noToneMatch = code.match(/([^:]+):?/);
|
2017-06-05 08:06:23 -04:00
|
|
|
if (noToneMatch && !url && (emojiHash.hasOwnProperty(noToneMatch[1]) || aliasHash.hasOwnProperty(noToneMatch[1]))) {
|
|
|
|
url = opts.getURL(`/images/emoji/${opts.emojiSet}/${code.replace(/:t/, '/')}.png`);
|
2016-06-14 14:31:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (url) {
|
|
|
|
url = url + "?v=" + IMAGE_VERSION;
|
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function emojiExists(code) {
|
|
|
|
code = code.toLowerCase();
|
2017-06-05 08:06:23 -04:00
|
|
|
return !!(extendedEmoji.hasOwnProperty(code) || emojiHash.hasOwnProperty(code) || aliasHash.hasOwnProperty(code));
|
2016-06-14 14:31:51 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
};
|
2017-06-05 08:06:23 -04:00
|
|
|
|
|
|
|
export function isSkinTonableEmoji(term) {
|
2017-07-11 11:51:53 -04:00
|
|
|
const match = _.compact(term.split(":"))[0];
|
2017-06-05 08:06:23 -04:00
|
|
|
if (match) {
|
2017-07-11 11:51:53 -04:00
|
|
|
return tonableEmojis.indexOf(match) !== -1;
|
2017-06-05 08:06:23 -04:00
|
|
|
}
|
2017-07-11 11:51:53 -04:00
|
|
|
return false;
|
2017-06-05 08:06:23 -04:00
|
|
|
}
|