From 29e4130e721e9910dec1e6942fd6348a24b69022 Mon Sep 17 00:00:00 2001 From: Simon Grim Date: Wed, 12 Aug 2015 01:49:30 +0500 Subject: [PATCH] fix mentions thing in htmlFormatMsg() --- js/interface_common.js | 4 ++-- js/options.js | 2 +- js/twister_formatpost.js | 38 ++++++++++++++++++++++---------------- 3 files changed, 25 insertions(+), 19 deletions(-) diff --git a/js/interface_common.js b/js/interface_common.js index c25f340..ec03d7e 100644 --- a/js/interface_common.js +++ b/js/interface_common.js @@ -587,7 +587,7 @@ function posPostPreview(event) { - postPreview.css('padding-left') - postPreview.css('padding-right')); } if (textArea[0].value.length) - postPreview.html(htmlFormatMsg(textArea[0].value, [])).show(); + postPreview.html(htmlFormatMsg(textArea[0].value).html).show(); else postPreview.hide(); textArea.before(postPreview); @@ -698,7 +698,7 @@ function replyTextInput(event) { if ($.Options.postPreview.val) { if (textArea[0].value.length) - textAreaForm.find('#post-preview').html(htmlFormatMsg(textArea[0].value, [])).show(); + textAreaForm.find('#post-preview').html(htmlFormatMsg(textArea[0].value).html).show(); else textAreaForm.find('#post-preview').html('').hide(); } diff --git a/js/options.js b/js/options.js index b047087..6941387 100644 --- a/js/options.js +++ b/js/options.js @@ -373,7 +373,7 @@ function tickOptionsPostPreview() { var imgPreviewCont = elem.find('.preview-container'); elem.children().first().html(htmlFormatMsg( - polyglot.t('post_preview_dummy', {logo: '/img/twister_mini.png', site: 'http://twister.net.co'}), [])); + polyglot.t('post_preview_dummy', {logo: '/img/twister_mini.png', site: 'http://twister.net.co'})).html); if ($.Options.displayPreview.val === 'enable') { imgPreviewCont.empty(); diff --git a/js/twister_formatpost.js b/js/twister_formatpost.js index 5f8cf02..5699170 100644 --- a/js/twister_formatpost.js +++ b/js/twister_formatpost.js @@ -110,20 +110,26 @@ function postToElem(post, kind, promoted) { getAvatar(username, elem.find('.avatar')); elem.find('.post-info-time').text(timeGmtToText(t)).attr('title', timeSincePost(t)); - var mentions = []; - elem.find('.post-text').html(htmlFormatMsg(msg, mentions)); - postData.attr('data-text-mentions', mentions); - - var replyTo = ''; - if (username !== defaultScreenName) - replyTo += '@' + username + ' '; - for (var i = 0; i < mentions.length; i++) { - if (mentions[i] !== username && mentions[i] !== defaultScreenName) - replyTo += '@' + mentions[i] + ' '; + msg = htmlFormatMsg(msg); + elem.find('.post-text').html(msg.html); + postData.attr('data-text-mentions', msg.mentions.join()); // FIXME no idea why do we need this attribute since we don't use it but use data-reply-to instead + + if (username !== defaultScreenName) { + if (msg.mentions.indexOf(username) === -1) + msg.mentions.splice(0, 0, username); + } else { + var i = msg.mentions.indexOf(defaultScreenName); + if (i !== -1) + msg.mentions.splice(i, 1); } + if (msg.mentions.length) + var replyTo = '@' + msg.mentions.join(' @') + ' '; + else + var replyTo = ''; var postTextArea = elem.find('.post-area-new textarea'); postTextArea.attr('data-reply-to', replyTo); + if (!defaultScreenName) postTextArea.attr('placeholder', polyglot.t('You have to log in to post replies.')); else @@ -208,7 +214,7 @@ function dmDataToSnippetItem(dmData, remoteUser) { getGroupChatName( remoteUser, dmItem.find("a.post-info-name") ); else getFullname( remoteUser, dmItem.find("a.post-info-name") ); - dmItem.find(".post-text").html(htmlFormatMsg(dmData.text, [])); + dmItem.find(".post-text").html(htmlFormatMsg(dmData.text).html); dmItem.find(".post-info-time").text(timeGmtToText(dmData.time)).attr("title",timeSincePost(dmData.time)); return dmItem; @@ -226,14 +232,13 @@ function dmDataToConversationItem(dmData, localUser, remoteUser) { getAvatar(from, dmItem.find(".post-photo").find("img") ); dmItem.find(".post-info-time").text(timeGmtToText(dmData.time)).attr("title",timeSincePost(dmData.time)); setPostInfoSent(from,dmData.k,dmItem.find('.post-info-sent')); - var mentions = []; - dmItem.find('.post-text').html(htmlFormatMsg(dmData.text, mentions)); + dmItem.find('.post-text').html(htmlFormatMsg(dmData.text).html); return dmItem; } // convert message text to html, featuring @users and links formating. -function htmlFormatMsg(msg, mentions) { +function htmlFormatMsg(msg) { // TODO: add options for emotions; msg = $.emotions(msg); // TODO make markup optionally mutable ? @@ -534,6 +539,7 @@ function htmlFormatMsg(msg, mentions) { + whiteSpaces; var stopCharsMarkout = '/\\*~_-`.,:;?!%+=&\'"[](){}^|«»…\u201C\u201D\u2026\u2014\u4E00\u3002\uFF0C\uFF1A\uFF1F\uFF01\u3010\u3011\u2047\u2048\u2049'; var i, j, k, str; + var mentions = []; msg = {str: escapeHtmlEntities(msg), htmlEntities: []}; @@ -663,7 +669,7 @@ function htmlFormatMsg(msg, mentions) { break; } str = msg.str.slice(i + 1, j).toLowerCase(); - mentions.push(str); // FIXME + mentions.push(str); msg = msgAddHtmlEntity(msg, i, i + str.length + 1, newHtmlEntityLink(_htmlFormatMsgLinkTemplateUser, $.MAL.userUrl(str), '@' + str) @@ -713,7 +719,7 @@ function htmlFormatMsg(msg, mentions) { if ($.Options.displayLineFeeds.val === 'enable') msg = msg.replace(/\n/g, '
'); - return msg; + return {html: msg, mentions: mentions}; } function proxyURL(url) {