ComunicWeb/assets/js/components/emoji/parser.js

121 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2017-06-20 11:29:15 +00:00
/**
* Emoji parser system
*
* Based on the work of Twitter Emoji
* https://github.com/twitter/twemoji
*
* @author Pierre HUBERT
*/
ComunicWeb.components.emoji.parser = {
/**
* Define twemoji base
*/
__twemojiBase: ComunicWeb.__config.assetsURL + "3rdparty/twemoji/2/",
2018-04-20 14:11:32 +00:00
/**
* EmojiConvertor instance
*/
__emojiConvertor: null,
2017-06-20 11:29:15 +00:00
/**
* Parse emojies
*
* @param {Object} infos Informations about the area to parse
* @info {HTMLElement} element The element to parse
2017-06-20 11:29:15 +00:00
* @return {Boolean} True for a success
*/
parse: function(info){
2017-06-20 11:29:15 +00:00
//Peform string parsing
2020-04-29 15:01:26 +00:00
info.element.innerHTML = this.shortcutToHTMLcode(info.element.innerHTML);
// Parse custom semicolons
if(info.user)
2020-04-03 18:26:34 +00:00
info.element.innerHTML = this.parseCustomEmojis(info.user, info.element.innerHTML)
else
console.error("User information are missing!")
2017-06-20 11:29:15 +00:00
2018-04-20 14:11:32 +00:00
//Perform colon conversion
info.element.innerHTML = this.colonConversion(info.element.innerHTML);
2018-04-20 14:11:32 +00:00
2017-06-20 11:29:15 +00:00
//Perform Twitter parsing
this.twitterEmojiesParsing(info.element);
2017-06-20 11:29:15 +00:00
//Success
return true;
},
2018-04-20 14:11:32 +00:00
/**
* Perform the conversion from colon code to Emoji code
*
* @param {string} string The string to convert
* @return {string} Converted string
*/
colonConversion: function(string){
//Check if the emoji convertor has to be created
if(this.__emojiConvertor == null){
this.__emojiConvertor = new EmojiConvertor();
this.__emojiConvertor.init_env(); // else auto-detection will trigger when we first convert
this.__emojiConvertor.replace_mode = 'unified';
this.__emojiConvertor.allow_native = true;
}
return this.__emojiConvertor.replace_colons(string);
},
2017-06-20 11:29:15 +00:00
/**
* Perform Twitter emojies parsing
*
* @param {Object} target The target of the parsing
* @return {Boolean} True for a success
*/
twitterEmojiesParsing: function(target){
//Call Twitter
twemoji.parse(target, {
base: this.__twemojiBase
});
//Success
return true;
},
/**
2020-04-29 15:01:26 +00:00
* Perform shortcut emoji to HTML code parsing
*
* @param {String} string The input string
* @return {String} The output string
*/
2020-04-29 15:01:26 +00:00
shortcutToHTMLcode: function(string){
//Process all emojie list
var i;
for(i in ComunicWeb.components.emoji.list.translation){
//Change smileys as many time as required
while(string.includes(i))
string = string.replace(i, ComunicWeb.components.emoji.list.translation[i]);
}
//Return result
return string;
2020-04-03 18:26:34 +00:00
},
2020-04-03 18:26:34 +00:00
/**
* Apply custom emojies
*
* @param {User} user Information about the user
* @param {String} input Text to transform
*/
parseCustomEmojis: function(user, input) {
for(const e of user.customEmojis) {
2020-04-29 15:01:26 +00:00
input = input.replace(new RegExp(e.shortcut, "g"), "<img src='"+e.url+"' class='emoji' alt='"+e.shortcut+"' title='"+e.shortcut+"'/>")
2020-04-03 18:26:34 +00:00
}
2020-04-03 18:26:34 +00:00
return input
},
2017-06-20 11:29:15 +00:00
}