Sengi-Windows-MacOS-Linux/src/app/tools/emoji-one.ts

220 lines
521 KiB
TypeScript
Raw Normal View History

2019-05-27 00:04:41 +02:00
//Migrated from
//https://github.com/joypixels/emojione/blob/master/lib/js/emojione.js
export class EmojiOne {
tmpShortNames = []
shortnames: string;
regShortNames;
constructor() {
for (let emoji in this.emojioneList) {
//if (!this.emojioneList.hasOwnProperty(emoji) || (emoji === '')) continue;
this.tmpShortNames.push(emoji.replace(/[+]/g, "\\$&"));
for (var i = 0; i < this.emojioneList[emoji].shortnames.length; i++) {
this.tmpShortNames.push(this.emojioneList[emoji].shortnames[i].replace(/[+]/g, "\\$&"));
}
}
this.shortnames = this.tmpShortNames.join('|');
this.regShortNames = new RegExp("<object[^>]*>.*?<\/object>|<span[^>]*>.*?<\/span>|<(?:object|embed|svg|img|div|span|p|a)[^>]*>|(" + this.shortnames + ")", "gi");
2019-05-27 02:32:10 +02:00
2019-05-27 00:04:41 +02:00
// this.imagePathPNG = 'https://cdn.jsdelivr.net/emojione/assets/' + this.emojiVersion + '/png/';
2019-05-27 02:32:10 +02:00
this.imagePathPNG = 'assets/emoji/72x72/';
2019-05-27 00:04:41 +02:00
this.defaultPathPNG = this.imagePathPNG;
this.regAscii = new RegExp("<object[^>]*>.*?<\/object>|<span[^>]*>.*?<\/span>|<(?:object|embed|svg|img|div|span|p|a)[^>]*>|((\\s|^)" + this.asciiRegexp + "(?=\\s|$|[!,.?]))", "gi");
this.regAsciiRisky = new RegExp("<object[^>]*>.*?<\/object>|<span[^>]*>.*?<\/span>|<(?:object|embed|svg|img|div|span|p|a)[^>]*>|(()" + this.asciiRegexp + "())", "gi");
}
2019-05-27 02:32:10 +02:00
toImage(str, className) {
2019-05-27 00:04:41 +02:00
str = this.toShort(str);
2019-05-27 02:32:10 +02:00
str = this.shortnameToImage(str, className);
2019-05-27 00:04:41 +02:00
str = this.wipeVS16(str);
return str;
};
toShort(str) {
var find = this.unicodeCharRegex();
str = this.replaceAll(str, find);
return str;
};
replaceAll(string, find) {
var escapedFind = this.escapeRegExp(find); //sorted largest output to smallest output
var search = new RegExp("<object[^>]*>.*?<\/object>|<span[^>]*>.*?<\/span>|<(?:object|embed|svg|img|div|span|p|a)[^>]*>|(" + escapedFind + ")", "gi");
// callback prevents replacing anything inside of these common html tags as well as between an <object></object> tag
var replace = (entire, m1) => {
return ((typeof m1 === 'undefined') || (m1 === '')) ? entire : this.shortnameConversionMap()[m1];
};
return string.replace(search, replace);
};
shortnameConversionMap() {
var map = [], emoji;
for (emoji in this.emojioneList) {
if (!this.emojioneList.hasOwnProperty(emoji) || (emoji === '')) continue;
map[this.convert(this.emojioneList[emoji].uc_output)] = emoji;
}
return map;
};
unicodeCharRegex() {
var map = [];
for (let emoji in this.emojioneList) {
if (!this.emojioneList.hasOwnProperty(emoji) || (emoji === '')) continue;
map.push(this.convert(this.emojioneList[emoji].uc_output));
}
return map.join('|');
};
spriteSize;
2019-05-27 02:32:10 +02:00
shortnameToImage(str, className) {
2019-05-27 00:04:41 +02:00
// replace regular shortnames first
var replaceWith, shortname, unicode, fname, alt, category, title, size, ePath;
var mappedUnicode = this.mapUnicodeToShort();
str = str.replace(this.regShortNames, (shortname) => {
if ((typeof shortname === 'undefined') || (shortname === '') || (this.shortnames.indexOf(shortname) === -1)) {
// if the shortname doesnt exist just return the entire match
return shortname;
}
else {
// map shortname to parent
if (!this.emojioneList[shortname]) {
for (var emoji in this.emojioneList) {
if (!this.emojioneList.hasOwnProperty(emoji) || (emoji === '')) continue;
if (this.emojioneList[emoji].shortnames.indexOf(shortname) === -1) continue;
shortname = emoji;
break;
}
}
unicode = this.emojioneList[shortname].uc_output;
fname = this.emojioneList[shortname].uc_base;
category = (fname.indexOf("-1f3f") >= 0) ? 'diversity' : this.emojioneList[shortname].category;
title = this.imageTitleTag ? 'title="' + shortname + '"' : '';
size = (this.spriteSize == '32' || this.spriteSize == '64') ? this.spriteSize : '32';
//if the emoji path has been set, we'll use the provided path, otherwise we'll use the default path
// // ePath = (this.defaultPathPNG !== this.imagePathPNG) ? this.imagePathPNG : this.defaultPathPNG + this.emojiSize + '/';
while(fname.charAt(0) == '0'){
fname = fname.substr(1);
}
// depending on the settings, we'll either add the native unicode as the alt tag, otherwise the shortname
alt = (this.unicodeAlt) ? this.convert(unicode.toUpperCase()) : shortname;
if (this.sprites) {
replaceWith = '<span class="emojione emojione-' + size + '-' + category + ' _' + fname + '" ' + title + '>' + alt + '</span>';
}
else {
2019-05-27 02:32:10 +02:00
replaceWith = '<img class="emojione ' + className + '" alt="' + alt + '" ' + title + ' src="' + this.defaultPathPNG + fname + this.fileExtension + '"/>';
2019-05-27 00:04:41 +02:00
2019-05-27 02:32:10 +02:00
// replaceWith = '<img class="emojione ' + className + '" alt="' + alt + '" ' + title + ' src="' + this.defaultPathPNG + fname +'.svg' + '"/>';
2019-05-27 00:04:41 +02:00
//replaceWith = '<img class="emojione" onError="this.src=this.alt;" alt="' + alt + '" ' + title + ' src="' + this.defaultPathPNG + fname + this.fileExtension + '"/>';
//replaceWith = `<object data="${this.defaultPathPNG + fname + this.fileExtension}" type="image/png"><img alt="${alt}" /></object>`
}
return replaceWith;
}
});
return str;
}
wipeVS16(str) {
var find = this.vs16Regex();
return this.replaceAllVS16(str, find);
};
vs16Regex() {
return this.convert('fe0f');
};
convert(unicode) {
if (unicode.indexOf("-") > -1) {
var parts = [];
var s = unicode.split('-');
for (var i = 0; i < s.length; i++) {
var part: any = parseInt(s[i], 16);
if (part >= 0x10000 && part <= 0x10FFFF) {
var hi = Math.floor((part - 0x10000) / 0x400) + 0xD800;
var lo = ((part - 0x10000) % 0x400) + 0xDC00;
part = (String.fromCharCode(hi) + String.fromCharCode(lo));
}
else {
part = String.fromCharCode(part);
}
parts.push(part);
}
return parts.join('');
}
else {
var s: any = parseInt(unicode, 16);
if (s >= 0x10000 && s <= 0x10FFFF) {
var hi = Math.floor((s - 0x10000) / 0x400) + 0xD800;
var lo = ((s - 0x10000) % 0x400) + 0xDC00;
return (String.fromCharCode(hi) + String.fromCharCode(lo));
}
else {
return String.fromCharCode(s);
}
}
};
replaceAllVS16(string, find) {
var escapedFind = this.escapeRegExp(find); //sorted largest output to smallest output
var search = new RegExp("<object[^>]*>.*?<\/object>|<span[^>]*>.*?<\/span>|<(?:object|embed|svg|img|div|span|p|a)[^>]*>|(" + escapedFind + ")", "gi");
// callback prevents replacing anything inside of these common html tags as well as between an <object></object> tag
var replace = (entire, m1) => {
return ((typeof m1 === 'undefined') || (m1 === '')) ? entire : '';
};
return string.replace(search, replace);
};
escapeRegExp(string) {
return string.replace(/[-[\]{}()*+?.,;:&\\^$#\s]/g, "\\$&");
};
memMapShortToUnicode: any = {};
mapUnicodeToShort() {
if (!this.memMapShortToUnicode) {
this.memMapShortToUnicode = {};
this.mapEmojioneList((unicode, shortname) => {
this.memMapShortToUnicode[unicode] = shortname;
});
}
return this.memMapShortToUnicode;
};
mapEmojioneList(addToMapStorage) {
for (var shortname in this.emojioneList) {
if (!this.emojioneList.hasOwnProperty(shortname)) { continue; }
var unicode = this.emojioneList[shortname].uc_base;
addToMapStorage(unicode, shortname);
}
};
asciiRegexp = '(\\*\\\\0\\/\\*|\\*\\\\O\\/\\*|\\-___\\-|\\:\'\\-\\)|\'\\:\\-\\)|\'\\:\\-D|\\>\\:\\-\\)|>\\:\\-\\)|\'\\:\\-\\(|\\>\\:\\-\\(|>\\:\\-\\(|\\:\'\\-\\(|O\\:\\-\\)|0\\:\\-3|0\\:\\-\\)|0;\\^\\)|O;\\-\\)|0;\\-\\)|O\\:\\-3|\\-__\\-|\\:\\-Þ|\\:\\-Þ|\\<\\/3|<\\/3|\\:\'\\)|\\:\\-D|\'\\:\\)|\'\\=\\)|\'\\:D|\'\\=D|\\>\\:\\)|>\\:\\)|\\>;\\)|>;\\)|\\>\\=\\)|>\\=\\)|;\\-\\)|\\*\\-\\)|;\\-\\]|;\\^\\)|\'\\:\\(|\'\\=\\(|\\:\\-\\*|\\:\\^\\*|\\>\\:P|>\\:P|X\\-P|\\>\\:\\[|>\\:\\[|\\:\\-\\(|\\:\\-\\[|\\>\\:\\(|>\\:\\(|\\:\'\\(|;\\-\\(|\\>\\.\\<|>\\.<|#\\-\\)|%\\-\\)|X\\-\\)|\\\\0\\/|\\\\O\\/|0\\:3|0\\:\\)|O\\:\\)|O\\=\\)|O\\:3|B\\-\\)|8\\-\\)|B\\-D|8\\-D|\\-_\\-|\\>\\:\\\\|>\\:\\\\|\\>\\:\\/|>\\:\\/|\\:\\-\\/|\\:\\-\\.|\\:\\-P|\\:Þ|\\:Þ|\\:\\-b|\\:\\-O|O_O|\\>\\:O|>\\:O|\\:\\-X|\\:\\-#|\\:\\-\\)|\\(y\\)|\\<3|<3|\\:D|\\=D|;\\)|\\*\\)|;\\]|;D|\\:\\*|\\=\\*|\\:\\(|\\:\\[|\\=\\(|\\:@|;\\(|D\\:|\\:\\$|\\=\\$|#\\)|%\\)|X\\)|B\\)|8\\)|\\:\\/|\\:\\\\|\\=\\/|\\=\\\\|\\:L|\\=L|\\:P|\\=P|\\:b|\\:O|\\:X|\\:#|\\=X|\\=#|\\:\\)|\\=\\]|\\=\\)|\\:\\])';
emojiVersion = '4.5'; // you can [optionally] modify this to load alternate emoji versions. see readme for backwards compatibility and version options
emojiSize = '32';
greedyMatch = false; // set to true for greedy unicode matching
blacklistChars = '';
imagePathPNG;
defaultPathPNG;
fileExtension = '.png';
imageTitleTag = true; // set to false to remove title attribute from img tag
sprites = false; // if this is true then sprite markup will be used
unicodeAlt = true; // use the unicode char as the alt attribute (makes copy and pasting the resulting text better)
ascii = false; // change to true to convert ascii smileys
riskyMatchAscii = false; // set true to match ascii without leading/trailing space char
regAscii;
regAsciiRisky;
regUnicode = new RegExp("<object[^>]*>.*?<\/object>|<span[^>]*>.*?<\/span>|<(?:object|embed|svg|img|div|span|p|a)[^>]*>|(?:\uD83C\uDFF3)\uFE0F?\u200D?(?:\uD83C\uDF08)|(?:\uD83D\uDC41)\uFE0F?\u200D?(?:\uD83D\uDDE8)\uFE0F?|[#-9]\uFE0F?\u20E3|(?:(?:\uD83C\uDFF4)(?:\uDB40[\uDC60-\uDCFF]){1,6})|(?:\uD83C[\uDDE0-\uDDFF]){2}|(?:(?:\uD83D[\uDC68\uDC69]))\uFE0F?(?:\uD83C[\uDFFA-\uDFFF])?\u200D?(?:[\u2695\u2696\u2708]|\uD83C[\uDF3E-\uDFED]|\uD83D[\uDCBB\uDCBC\uDD27\uDD2C\uDE80\uDE92])|(?:\uD83D[\uDC68\uDC69]|\uD83E[\uDDD0-\uDDDF])(?:\uD83C[\uDFFA-\uDFFF])?\u200D?[\u2640\u2642\u2695\u2696\u2708]?\uFE0F?|(?:(?:\u2764|\uD83D[\uDC66-\uDC69\uDC8B])[\u200D\uFE0F]{0,2}){1,3}(?:\u2764|\uD83D[\uDC66-\uDC69\uDC8B])|(?:(?:\u2764|\uD83D[\uDC66-\uDC69\uDC8B])\uFE0F?){2,4}|(?:\uD83D[\uDC68\uDC69\uDC6E\uDC71-\uDC87\uDD75\uDE45-\uDE4E]|\uD83E[\uDD26\uDD37]|\uD83C[\uDFC3-\uDFCC]|\uD83E[\uDD38-\uDD3E]|\uD83D[\uDEA3-\uDEB6]|\u26f9|\uD83D\uDC6F)\uFE0F?(?:\uD83C[\uDFFB-\uDFFF])?\u200D?[\u2640\u2642]?\uFE0F?|(?:[\u261D\u26F9\u270A-\u270D]|\uD83C[\uDF85-\uDFCC]|\uD83D[\uDC42-\uDCAA\uDD74-\uDD96\uDE45-\uDE4F\uDEA3-\uDECC]|\uD83E[\uDD18-\uDD3E])\uFE0F?(?:\uD83C[\uDFFB-\uDFFF])|(?:[\u2194-\u2199\u21a9-\u21aa]\uFE0F?|[\u0023\u002a]|[\u3030\u303d]\uFE0F?|(?:\ud83c[\udd70-\udd71]|\ud83c\udd8e|\ud83c[\udd91-\udd9a])\uFE0F?|\u24c2\uFE0F?|[\u3297\u3299]\uFE0F?|(?:\ud83c[\ude01-\ude02]|\ud83c\ude1a|\ud83c\ude2f|\ud83c[\ude32-\ude3a]|\ud83c[\ude50-\ude51])\uFE0F?|[\u203c\u2049]\uFE0F?|[\u25aa-\u25ab\u25b6\u25c0\u25fb-\u25fe]\uFE0F?|[\u00a9\u00ae]\uFE0F?|[\u2122\u2139]\uFE0F?|\ud83c\udc04\uFE0F?|[\u2b05-\u2b07\u2b1b-\u2b1c\u2b50\u2b55]\uFE0F?|[\u231a-\u231b\u2328\u23cf\u23e9-\u23f3\u23f8-\u23fa]\uFE0F?|\ud83c\udccf|[\u2934\u2935]\uFE0F?)|[\u2700-\u27bf]\uFE0F?|[\ud800-\udbff][\udc00-\udfff]\uFE0F?|[\u2600-\u26FF]\uFE0F?|[\u0030-\u0039]\uFE0F", "g");
emojioneList = { ":kiss_mm:": { "uc_base": "1f468-2764-1f48b-1f468", "uc_output": "1f468-200d-2764-fe0f-200d-1f48b-200d-1f468", "uc_match": "1f468-2764-fe0f-1f48b-1f468", "uc_greedy": "1f468-2764-1f48b-1f468", "shortnames": [":couplekiss_mm:"], "category": "people" }, ":kiss_woman_man:": { "uc_base": "1f469-2764-1f48b-1f468", "uc_output": "1f469-200d-2764-fe0f-200d-1f48b-200d-1f468", "uc_match": "1f469-2764-fe0f-1f48b-1f468", "uc_greedy": "1f469-2764-1f48b-1f468", "shortnames": [], "category": "people" }, ":kiss_ww:": { "uc_base": "1f469-2764-1f48b-1f469", "uc_output": "1f469-200d-2764-fe0f-200d-1f48b-200d-1f469", "uc_match": "1f469-2764-fe0f-1f48b-1f469", "uc_greedy": "1f469-2764-1f48b-1f469", "shortnames": [":couplekiss_ww:"], "category": "people" }, ":england:": { "uc_base": "1f3f4-e0067-e0062-e0065-e006e-e0067-e007f", "uc_output": "1f3f4-e0067-e0062-e0065-e006e-e0067-e007f", "uc_match": "1f3f4-e0067-e0062-e0065-e006e-e0067-e007f", "uc_greedy": "1f3f4-e0067-e0062-e0065-e006e-e0067-e007f", "shortnames": [], "category": "flags" }, ":scotland:": { "uc_base": "1f3f4-e0067-e0062-e0073-e0063-e0074-e007f", "uc_output": "1f3f4-e0067-e0062-e0073-e0063-e0074-e007f", "uc_match": "1f3f4-e0067-e0062-e0073-e0063-e0074-e007f", "uc_greedy": "1f3f4-e0067-e0062-e0073-e0063-e0074-e007f", "shortnames": [], "category": "flags" }, ":wales:": { "uc_base": "1f3f4-e0067-e0062-e0077-e006c-e0073-e007f", "uc_output": "1f3f4-e0067-e0062-e0077-e006c-e0073-e007f", "uc_match": "1f3f4-e0067-e0062-e0077-e006c-e0073-e007f", "uc_greedy": "1f3f4-e0067-e0062-e0077-e006c-e0073-e007f", "shortnames": [], "category": "flags" }, ":family_mmbb:": { "uc_base": "1f468-1f468-1f466-1f466", "uc_output": "1f468-200d-1f468-200d-1f466-200d-1f466", "uc_match": "1f468-1f468-1f466-1f466", "uc_greedy": "1f468-1f468-1f466-1f466", "shortnames": [], "category": "people" }, ":family_mmgb:": { "uc_base": "1f468-1f468-1f467-1f466", "uc_output": "1f468-200d-1f468-200d-1f467-200d-1f466", "uc_match": "1f468-1f468-1f467-1f466", "uc_greedy": "1f468-1f468-1f467-1f466", "shortnames": [], "category": "people" }, ":family_mmgg:": { "uc_base": "1f468-1f468-1f467-1f467", "uc_output": "1f468-200d-1f468-200d-1f467-200d-1f467", "uc_match": "1f468-1f468-1f467-1f467", "uc_greedy": "1f468-1f468-1f467-1f467", "shortnames": [], "category": "people" }, ":family_mwbb:": { "uc_base": "1f468-1f469-1f466-1f466", "uc_output": "1f468-200d-1f469-200d-1f466-200d-1f466", "uc_match": "1f468-1f469-1f466-1f466", "uc_greedy": "1f468-1f469-1f466-1f466", "shortnames": [], "category": "people" }, ":family_mwgb:": { "uc_base": "1f468-1f469-1f467-1f466", "uc_output": "1f468-200d-1f469-200d-1f467-200d-1f466", "uc_match": "1f468-1f469-1f467-1f466", "uc_greedy": "1f468-1f469-1f467-1f466", "shortnames": [], "category": "people" }, ":family_mwgg:": { "uc_base": "1f468-1f469-1f467-1f467", "uc_output": "1f468-200d-1f469-200d-1f467-200d-1f467", "uc_match": "1f468-1f469-1f467-1f467", "uc_greedy": "1f468-1f469-1f467-1f467", "shortnames": [], "category": "people" }, ":family_wwbb:": { "uc_base": "1f469-1f469-1f466-1f466", "uc_output": "1f469-200d-1f469-200d-1f466-200d-1f466", "uc_match": "1f469-1f469-1f466-1f466", "uc_greedy": "1f469-1f469-1f466-1f466", "shortnames": [], "category": "people" }, ":family_wwgb:": { "uc_base": "1f469-1f469-1f467-1f466", "uc_output": "1f469-200d-1f469-200d-1f467-200d-1f466", "uc_match": "1f469-1f469-1f467-1f466", "uc_greedy": "1f469-1f469-1f467-1f466", "shortnames": [], "category": "people" }, ":family_wwgg:": { "uc_base": "1f469-1f469-1f467-1f467", "uc_output": "1f469-200d-1f469-200d-1f467-200d-1f467", "uc_match": "1f469-1f469-1f467-1f467", "uc_greedy": "1f469-1f469-1f467-1f467", "shortnames": [], "category": "people" }, ":couple_mm:": { "uc_base": "1f468-2764-1f468", "uc_output": "1f468-200d-2764-fe0f-200d-1f468", "uc_match": "1f468-2764-fe0f-1f468", "uc_greedy": "1f468-2764-1f468", "shortnames": [":couple_with_heart_mm:"], "category": "people" }, ":couple_with_heart_woman_man:": { "uc_base": "1f469-2764-1f468", "uc_output": "1f469-200d-2764-fe0f-200d-1f468", "uc_match": "1f469-2764-fe0f-1f
}