mirror of
https://github.com/codeninjasllc/discourse.git
synced 2024-11-30 19:08:10 -05:00
e6e7948617
Added following emoji sets - Apple/International - Emoji One (default) - Android/Google - Twitter FIX: translations from plugins weren't properly merged with default translations FEATURE: new 'site_setting_changed' event
191 lines
4.7 KiB
Text
191 lines
4.7 KiB
Text
// TODO @robin to move this whole thing to es6
|
|
Discourse.Emoji = {};
|
|
// bump up this number to expire all emojis
|
|
Discourse.Emoji.ImageVersion = "0"
|
|
|
|
var _emoji = <%= Emoji.all.map { |e| e["aliases"] }.flatten.inspect %>;
|
|
|
|
var _extendedEmoji = {};
|
|
Discourse.Dialect.registerEmoji = function(code, url) {
|
|
_extendedEmoji[code] = url;
|
|
};
|
|
|
|
Discourse.Emoji.list = function(){
|
|
var copy = _emoji.slice(0);
|
|
_.each(_extendedEmoji, function(v,k){
|
|
copy.push(k);
|
|
});
|
|
return copy;
|
|
};
|
|
|
|
var _toSearch;
|
|
|
|
var search = function(term, options) {
|
|
var maxResults = (options && options["maxResults"]) || -1;
|
|
|
|
_toSearch = _toSearch || _emoji.concat(Object.keys(_extendedEmoji));
|
|
|
|
if(maxResults === 0) { return []; }
|
|
|
|
var i, results = [];
|
|
|
|
var done = function(){
|
|
return maxResults > 0 && results.length >= maxResults;
|
|
}
|
|
|
|
for (i=0; i < _toSearch.length; i++) {
|
|
if (_toSearch[i].indexOf(term) === 0) {
|
|
results.push(_toSearch[i]);
|
|
if(done()) { break; }
|
|
}
|
|
}
|
|
|
|
if(!done()){
|
|
for (i=0; i < _toSearch.length; i++) {
|
|
if (_toSearch[i].indexOf(term) > 0) {
|
|
results.push(_toSearch[i]);
|
|
if(done()) { break; }
|
|
}
|
|
}
|
|
}
|
|
|
|
return results;
|
|
}
|
|
|
|
Discourse.Emoji.search = search;
|
|
|
|
var _emojiHash = {};
|
|
_emoji.forEach(function(code){
|
|
_emojiHash[code] = true;
|
|
});
|
|
|
|
var urlFor = function(code) {
|
|
var url, set = Discourse.SiteSettings.emoji_set;
|
|
|
|
if(_extendedEmoji.hasOwnProperty(code)) {
|
|
url = _extendedEmoji[code];
|
|
}
|
|
|
|
if(!url && _emojiHash.hasOwnProperty(code)) {
|
|
url = Discourse.getURL('/plugins/emoji/images/' + set + '/' + code + '.png');
|
|
}
|
|
|
|
if(url && url[0] !== 'h' && Discourse.CDN) {
|
|
url = Discourse.CDN + url;
|
|
}
|
|
|
|
if(url){
|
|
url = url + "?v=" + Discourse.Emoji.ImageVersion;
|
|
}
|
|
|
|
return url;
|
|
}
|
|
|
|
Discourse.Emoji.urlFor = urlFor;
|
|
|
|
Discourse.Emoji.exists = function(code){
|
|
return !!(_extendedEmoji.hasOwnProperty(code) || _emojiHash.hasOwnProperty(code));
|
|
}
|
|
|
|
function imageFor(code) {
|
|
var url = urlFor(code);
|
|
if (url) {
|
|
return ['img', { href: url, title: ':' + code + ':', 'class': 'emoji', alt: code }];
|
|
}
|
|
}
|
|
|
|
// Also support default emotions
|
|
var translations = {
|
|
':)' : 'smile',
|
|
':-)' : 'smile',
|
|
':(' : 'frowning',
|
|
':-(' : 'frowning',
|
|
';)' : 'wink',
|
|
';-)' : 'wink',
|
|
':\'(' : 'cry',
|
|
':\'-(' : 'cry',
|
|
':-\'(' : 'cry',
|
|
':p' : 'stuck_out_tongue',
|
|
':P' : 'stuck_out_tongue',
|
|
':-P' : 'stuck_out_tongue',
|
|
':O' : 'open_mouth',
|
|
':-O' : 'open_mouth',
|
|
':D' : 'smiley',
|
|
':-D' : 'smiley',
|
|
':|' : 'expressionless',
|
|
':-|' : 'expressionless',
|
|
";P" : 'stuck_out_tongue_winking_eye',
|
|
";-P" : 'stuck_out_tongue_winking_eye',
|
|
":$" : 'blush',
|
|
":-$" : 'blush'
|
|
};
|
|
|
|
Discourse.Emoji.translations = translations;
|
|
|
|
function checkPrev(prev) {
|
|
if (prev && prev.length) {
|
|
var lastToken = prev[prev.length-1];
|
|
if (lastToken && lastToken.charAt) {
|
|
var lastChar = lastToken.charAt(lastToken.length-1);
|
|
if (lastChar !== ' ' && lastChar !== "\n") return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
var translationsWithColon = {};
|
|
Object.keys(translations).forEach(function (t) {
|
|
if (t[0] === ':') {
|
|
translationsWithColon[t] = translations[t];
|
|
} else {
|
|
var replacement = translations[t];
|
|
Discourse.Dialect.inlineReplace(t, function (token, match, prev) {
|
|
if (!Discourse.SiteSettings.enable_emoji) { return token; }
|
|
return checkPrev(prev) ? imageFor(replacement) : token;
|
|
});
|
|
}
|
|
});
|
|
|
|
function escapeRegExp(s) {
|
|
return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&');
|
|
}
|
|
|
|
var translationColonRegexp = new RegExp(Object.keys(translationsWithColon).map(function (t) {
|
|
return "(" + escapeRegExp(t) + ")";
|
|
}).join("|"));
|
|
|
|
Discourse.Dialect.registerInline(':', function(text, match, prev) {
|
|
if (!Discourse.SiteSettings.enable_emoji) { return; }
|
|
|
|
var endPos = text.indexOf(':', 1),
|
|
firstSpace = text.search(/\s/),
|
|
contents;
|
|
|
|
if (!checkPrev(prev)) { return; }
|
|
|
|
// If there is no trailing colon, check our translations that begin with colons
|
|
if (endPos === -1 || (firstSpace !== -1 && endPos > firstSpace)) {
|
|
translationColonRegexp.lastIndex = 0;
|
|
var m = translationColonRegexp.exec(text);
|
|
if (m && m[0] && text.indexOf(m[0]) === 0) {
|
|
|
|
// Check outer edge
|
|
var lastChar = text.charAt(m[0].length);
|
|
if (lastChar && (lastChar !== ' ' && lastChar !== "\n")) return;
|
|
contents = imageFor(translationsWithColon[m[0]]);
|
|
if (contents) {
|
|
return [m[0].length, contents];
|
|
}
|
|
}
|
|
return;
|
|
}
|
|
|
|
// Simple find and replace from our array
|
|
var between = text.slice(1, endPos);
|
|
contents = imageFor(between);
|
|
if (contents) {
|
|
return [endPos+1, contents];
|
|
}
|
|
});
|
|
|
|
Discourse.Markdown.whiteListTag('img', 'class', 'emoji');
|