From 3e99191972a84b0a3884fd66dc984c33a4271258 Mon Sep 17 00:00:00 2001 From: Deimos Date: Sun, 3 Mar 2019 14:44:30 -0700 Subject: [PATCH] Quote selected text inside comment reply form If the user has text selected inside a comment when they click the reply button, this will automatically start off the form with that text inside a blockquote. This only works if the selected text is inside another comment (for example it won't work if the text is in the sidebar or the topic itself), and only if the entirety of the selection is inside the same comment. --- .../js/behaviors/comment-reply-button.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/tildes/static/js/behaviors/comment-reply-button.js b/tildes/static/js/behaviors/comment-reply-button.js index 7827464..becbc52 100644 --- a/tildes/static/js/behaviors/comment-reply-button.js +++ b/tildes/static/js/behaviors/comment-reply-button.js @@ -81,6 +81,27 @@ $.onmount('[data-js-comment-reply-button]', function() { replyForm.appendChild(textarea); replyForm.appendChild(buttonDiv); + // If the user has text selected inside a comment when they click the reply + // button, start the comment form off with that text inside a blockquote + if (window.getSelection) { + var selectedText = ""; + + // only capture the selected text if it's all from the same comment + var selection = window.getSelection(); + var $start = $(selection.anchorNode).closest(".comment-text"); + var $end = $(selection.focusNode).closest(".comment-text"); + if ($start.is($end)) { + selectedText = selection.toString(); + } + + if (selectedText.length > 0) { + selectedText = selectedText.replace(/\s+$/g, ""); + selectedText = selectedText.replace(/^/gm, "> "); + textarea.value = selectedText+"\n\n"; + textarea.scrollTop = textarea.scrollHeight; + } + } + // update Intercooler so it knows about this new form Intercooler.processNodes(replyForm);