From 3bd93526cd93ed384b67d5c9fb4cc830eca62eba Mon Sep 17 00:00:00 2001 From: barisusakli <barisusakli@gmail.com> Date: Mon, 20 Jun 2016 13:06:08 +0300 Subject: [PATCH] closes #4768 --- public/src/client/topic/postTools.js | 79 ++++++++++++++++------------ 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/public/src/client/topic/postTools.js b/public/src/client/topic/postTools.js index 0195c00417..ff6016c5ab 100644 --- a/public/src/client/topic/postTools.js +++ b/public/src/client/topic/postTools.js @@ -228,24 +228,7 @@ define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator function onReplyClicked(button, tid) { showStaleWarning(function(proceed) { if (!proceed) { - var selectionText = ''; - var selection = window.getSelection ? window.getSelection() : document.selection.createRange(); - var content = button.parents('[component="post"]').find('[component="post/content"]').get(0); - - if (selection && selection.containsNode && content && selection.containsNode(content, true)) { - var bounds = document.createRange(); - bounds.selectNodeContents(content); - var range = selection.getRangeAt(0).cloneRange(); - if (range.compareBoundaryPoints(Range.START_TO_START, bounds) < 0) { - range.setStart(bounds.startContainer, bounds.startOffset); - } - if (range.compareBoundaryPoints(Range.END_TO_END, bounds) > 0) { - range.setEnd(bounds.endContainer, bounds.endOffset); - } - bounds.detach(); - selectionText = range.toString(); - range.detach(); - } + var selectedText = getSelectedText(button); var username = getUserName(button); if (getData(button, 'data-uid') === '0' || !getData(button, 'data-userslug')) { @@ -254,7 +237,7 @@ define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator var toPid = button.is('[component="post/reply"]') ? getData(button, 'data-pid') : null; - if (selectionText.length) { + if (selectedText) { $(window).trigger('action:composer.addQuote', { tid: tid, slug: ajaxify.data.slug, @@ -262,7 +245,7 @@ define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator pid: toPid, topicName: ajaxify.data.titleRaw, username: username, - text: selectionText + text: selectedText }); } else { $(window).trigger('action:composer.post.new', { @@ -278,29 +261,59 @@ define('forum/topic/postTools', ['share', 'navigator', 'components', 'translator function onQuoteClicked(button, tid) { showStaleWarning(function(proceed) { - if (!proceed) { - var username = getUserName(button), - pid = getData(button, 'data-pid'); + function quote(text) { + $(window).trigger('action:composer.addQuote', { + tid: tid, + slug: ajaxify.data.slug, + index: getData(button, 'data-index'), + pid: pid, + username: username, + topicName: ajaxify.data.titleRaw, + text: text + }); + } + + if (!proceed) { + var username = getUserName(button); + var pid = getData(button, 'data-pid'); + var selectedText = getSelectedText(button); + if (selectedText) { + return quote(selectedText); + } socket.emit('posts.getRawPost', pid, function(err, post) { - if(err) { + if (err) { return app.alertError(err.message); } - $(window).trigger('action:composer.addQuote', { - tid: tid, - slug: ajaxify.data.slug, - index: getData(button, 'data-index'), - pid: pid, - username: username, - topicName: ajaxify.data.titleRaw, - text: post - }); + quote(post); }); } }); } + function getSelectedText(button) { + var selectionText = ''; + var selection = window.getSelection ? window.getSelection() : document.selection.createRange(); + var content = button.parents('[component="post"]').find('[component="post/content"]').get(0); + + if (selection && selection.containsNode && content && selection.containsNode(content, true)) { + var bounds = document.createRange(); + bounds.selectNodeContents(content); + var range = selection.getRangeAt(0).cloneRange(); + if (range.compareBoundaryPoints(Range.START_TO_START, bounds) < 0) { + range.setStart(bounds.startContainer, bounds.startOffset); + } + if (range.compareBoundaryPoints(Range.END_TO_END, bounds) > 0) { + range.setEnd(bounds.endContainer, bounds.endOffset); + } + bounds.detach(); + selectionText = range.toString(); + range.detach(); + } + return selectionText; + } + function favouritePost(button, pid) { var method = button.attr('data-favourited') === 'false' ? 'posts.favourite' : 'posts.unfavourite';