Browse Source

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.
merge-requests/64/head
Deimos 6 years ago
parent
commit
3e99191972
  1. 21
      tildes/static/js/behaviors/comment-reply-button.js

21
tildes/static/js/behaviors/comment-reply-button.js

@ -81,6 +81,27 @@ $.onmount('[data-js-comment-reply-button]', function() {
replyForm.appendChild(textarea); replyForm.appendChild(textarea);
replyForm.appendChild(buttonDiv); 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 // update Intercooler so it knows about this new form
Intercooler.processNodes(replyForm); Intercooler.processNodes(replyForm);

Loading…
Cancel
Save