You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.2 KiB
96 lines
3.2 KiB
7 years ago
|
import { autoPlayGif } from 'flavours/glitch/util/initial_state';
|
||
7 years ago
|
import unicodeMapping from './emoji_unicode_mapping_light';
|
||
8 years ago
|
import Trie from 'substring-trie';
|
||
8 years ago
|
|
||
8 years ago
|
const trie = new Trie(Object.keys(unicodeMapping));
|
||
8 years ago
|
|
||
7 years ago
|
const assetHost = process.env.CDN_HOST || '';
|
||
|
|
||
7 years ago
|
const emojify = (str, customEmojis = {}) => {
|
||
7 years ago
|
const tagCharsWithoutEmojis = '<&';
|
||
|
const tagCharsWithEmojis = Object.keys(customEmojis).length ? '<&:' : '<&';
|
||
|
let rtn = '', tagChars = tagCharsWithEmojis, invisible = 0;
|
||
7 years ago
|
for (;;) {
|
||
|
let match, i = 0, tag;
|
||
7 years ago
|
while (i < str.length && (tag = tagChars.indexOf(str[i])) === -1 && (invisible || !(match = trie.search(str.slice(i))))) {
|
||
7 years ago
|
i += str.codePointAt(i) < 65536 ? 1 : 2;
|
||
|
}
|
||
7 years ago
|
let rend, replacement = '';
|
||
|
if (i === str.length) {
|
||
7 years ago
|
break;
|
||
7 years ago
|
} else if (str[i] === ':') {
|
||
7 years ago
|
if (!(() => {
|
||
|
rend = str.indexOf(':', i + 1) + 1;
|
||
|
if (!rend) return false; // no pair of ':'
|
||
7 years ago
|
const lt = str.indexOf('<', i + 1);
|
||
7 years ago
|
if (!(lt === -1 || lt >= rend)) return false; // tag appeared before closing ':'
|
||
|
const shortname = str.slice(i, rend);
|
||
|
// now got a replacee as ':shortname:'
|
||
|
// if you want additional emoji handler, add statements below which set replacement and return true.
|
||
7 years ago
|
if (shortname in customEmojis) {
|
||
7 years ago
|
const filename = autoPlayGif ? customEmojis[shortname].url : customEmojis[shortname].static_url;
|
||
7 years ago
|
replacement = `<img draggable="false" class="emojione" alt="${shortname}" title="${shortname}" src="${filename}" />`;
|
||
7 years ago
|
return true;
|
||
7 years ago
|
}
|
||
7 years ago
|
return false;
|
||
|
})()) rend = ++i;
|
||
|
} else if (tag >= 0) { // <, &
|
||
|
rend = str.indexOf('>;'[tag], i + 1) + 1;
|
||
7 years ago
|
if (!rend) {
|
||
|
break;
|
||
|
}
|
||
|
if (tag === 0) {
|
||
|
if (invisible) {
|
||
|
if (str[i + 1] === '/') { // closing tag
|
||
|
if (!--invisible) {
|
||
|
tagChars = tagCharsWithEmojis;
|
||
|
}
|
||
|
} else if (str[rend - 2] !== '/') { // opening tag
|
||
|
invisible++;
|
||
|
}
|
||
|
} else {
|
||
|
if (str.startsWith('<span class="invisible">', i)) {
|
||
|
// avoid emojifying on invisible text
|
||
|
invisible = 1;
|
||
|
tagChars = tagCharsWithoutEmojis;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
7 years ago
|
i = rend;
|
||
|
} else { // matched to unicode emoji
|
||
7 years ago
|
const { filename, shortCode } = unicodeMapping[match];
|
||
|
const title = shortCode ? `:${shortCode}:` : '';
|
||
|
replacement = `<img draggable="false" class="emojione" alt="${match}" title="${title}" src="${assetHost}/emoji/${filename}.svg" />`;
|
||
7 years ago
|
rend = i + match.length;
|
||
8 years ago
|
}
|
||
7 years ago
|
rtn += str.slice(0, i) + replacement;
|
||
|
str = str.slice(rend);
|
||
8 years ago
|
}
|
||
7 years ago
|
return rtn + str;
|
||
7 years ago
|
};
|
||
8 years ago
|
|
||
8 years ago
|
export default emojify;
|
||
7 years ago
|
|
||
7 years ago
|
export const buildCustomEmojis = (customEmojis) => {
|
||
7 years ago
|
const emojis = [];
|
||
|
|
||
|
customEmojis.forEach(emoji => {
|
||
|
const shortcode = emoji.get('shortcode');
|
||
7 years ago
|
const url = autoPlayGif ? emoji.get('url') : emoji.get('static_url');
|
||
7 years ago
|
const name = shortcode.replace(':', '');
|
||
|
|
||
|
emojis.push({
|
||
7 years ago
|
id: name,
|
||
7 years ago
|
name,
|
||
|
short_names: [name],
|
||
|
text: '',
|
||
|
emoticons: [],
|
||
|
keywords: [name],
|
||
|
imageUrl: url,
|
||
7 years ago
|
custom: true,
|
||
7 years ago
|
});
|
||
|
});
|
||
|
|
||
|
return emojis;
|
||
|
};
|