From 881c713cb20d16bade986cdae9e0995bd93dacaf Mon Sep 17 00:00:00 2001 From: Lukas Holecek Date: Aug 02 2020 07:56:12 +0000 Subject: [PATCH 1/5] Omit breaking original comment format in reply This fixes replying to a comment with block quotes and other formatted text by prefixing each line in the original Markdown text with `>` to create additional quotation level. Previously, this was done on the rendered Markdown, but it removed the formatting. In some cases, the Markdown rendering can be still broken, for example code blocks using triple backticks. From Python-Markdown documentation [1]: > Fenced Code Blocks are only supported at the document root level. > Therefore, they cannot be nested inside lists or blockquotes. [1] https://python-markdown.github.io/extensions/fenced_code_blocks/ Fixes #2479 Signed-off-by: Lukas Holecek --- diff --git a/pagure/static/issue_ev.js b/pagure/static/issue_ev.js index 6d7bd11..6289f2b 100644 --- a/pagure/static/issue_ev.js +++ b/pagure/static/issue_ev.js @@ -170,7 +170,8 @@ add_comment = function(data, username) { + '
' + '
' + ' {% endif %} @@ -250,7 +252,7 @@
{% if g.fas_user %} {% endif %} diff --git a/pagure/templates/issue.html b/pagure/templates/issue.html index bb79cf8..a66b9bf 100644 --- a/pagure/templates/issue.html +++ b/pagure/templates/issue.html @@ -799,27 +799,46 @@ function setup_reply_btns() { $(".reply").unbind(); $( ".reply" ).click( function() { - var _section = $(this).closest('.card'); - if (!_section.length) { - var _section = $(this).closest('#original_comment_box'); - } - var _comment = _section.find('.comment_body'); - var _text = _comment.text().split("\n"); - var _output = new Array(); - for (cnt = 0; cnt < _text.length ; cnt ++) { - _output[cnt] = '> ' + $.trim(_text[cnt]); - } - var _prev = $.trim($( "#comment" ).val()); - if (_prev.length > 0){ - _prev += "\n\n"; + var commentid = $( this ).attr('data-comment'); + var _url = "{{ url_for( + 'api_ns.api_view_issue', + repo=repo.name, + username=username, + namespace=repo.namespace, + issueid=issueid) }}"; + if (commentid) { + var _key = 'comment'; + _url = _url + '/comment/' + commentid; + } else { + var _key = 'content'; } - $( "#comment" ).val(_prev + _output.join("\n")); + $.ajax({ + url: _url, + type: 'GET', + dataType: 'json', + success: function(res) { + var _text = res[_key].split("\n"); + var _output = new Array(); + for (cnt = 0; cnt < _text.length ; cnt ++) { + _output[cnt] = '> ' + $.trim(_text[cnt]); + } + var _prev = $.trim($( "#comment" ).val()); + if (_prev.length > 0){ + _prev += "\n\n"; + } + $( "#comment" ).val(_prev + _output.join("\n") + "\n\n"); + $( "#comment" ).focus(); + }, + error: function() { + alert('Failed to retrieve comment text'); + } + }); } ).click(function(){ $('html, body').animate({ scrollTop: $("#comment").offset().top }, 2000); -}); + }); }; $(document).ready(function() { diff --git a/pagure/templates/repo_pull_request.html b/pagure/templates/repo_pull_request.html index 4ef4ea2..ef41537 100644 --- a/pagure/templates/repo_pull_request.html +++ b/pagure/templates/repo_pull_request.html @@ -804,21 +804,47 @@ function setup_reply_btns() { $(".reply").unbind(); $( ".reply" ).click( function() { - var _section = $(this).closest('.card'); - var _comment = _section.find('.comment_body'); - var _text = _comment.text().split("\n"); - var _output = new Array(); - for (var cnt=0; cnt < _text.length; cnt++) { - _output[cnt] = '> ' + _text[cnt]; - } - var _prev = $.trim($( "#comment" ).val()); - if (_prev.length > 0){ - _prev += "\n\n"; - } - $( "#comment" ).val(_prev + _output.join("\n") + "\n\n"); - $( "#comment" ).focus(); + var commentid = $( this ).attr('data-comment'); + var _url = "{{ url_for( + 'api_ns.api_pull_request_view', + repo=repo.name, + requestid=requestid, + username=username, + namespace=repo.namespace) }}"; + $.ajax({ + url: _url, + type: 'GET', + dataType: 'json', + success: function(res) { + var _text; + if (commentid) { + _text = res['comments'].find( + element => element['id'] == commentid)['comment']; + } else { + _text = res['initial_comment']; + } + _text = _text.split("\n"); + var _output = new Array(); + for (cnt = 0; cnt < _text.length ; cnt ++) { + _output[cnt] = '> ' + $.trim(_text[cnt]); + } + var _prev = $.trim($( "#comment" ).val()); + if (_prev.length > 0){ + _prev += "\n\n"; + } + $( "#comment" ).val(_prev + _output.join("\n") + "\n\n"); + $( "#comment" ).focus(); + }, + error: function() { + alert('Failed to retrieve comment text'); + } + }); } - ); + ).click(function(){ + $('html, body').animate({ + scrollTop: $("#comment").offset().top + }, 2000); + }); }; From 376cb2b82f702d794bf1c3b80277d3bf3dd3a9e0 Mon Sep 17 00:00:00 2001 From: Lukas Holecek Date: Aug 02 2020 07:56:12 +0000 Subject: [PATCH 2/5] Move js code to set up reply buttons from html Signed-off-by: Lukas Holecek --- diff --git a/pagure/static/comments.js b/pagure/static/comments.js new file mode 100644 index 0000000..1275b19 --- /dev/null +++ b/pagure/static/comments.js @@ -0,0 +1,70 @@ +function get_comment_text_from_issue_data(data, commentid) { + if (commentid) { + return data['comment']; + } + return data['content']; +} + +function get_comment_text_from_pr_data(data, commentid) { + if (commentid) { + return data['comments'].find( + element => element['id'] == commentid)['comment']; + } + return data['initial_comment']; +} + +function reply(quote) { + var text = $.trim($( "#comment" ).val()); + if (text.length > 0) { + text += "\n\n"; + } + + var lines = quote.split("\n"); + for (var i = 0; i < lines.length ; i++) { + text += '> ' + $.trim(lines[i]) + "\n"; + } + + $( "#comment" ).val(text + "\n"); + $( "#comment" ).focus(); +} + +function setup_reply_btns(url, get_comment_text, comment_url_path) { + $(".reply").unbind(); + $( ".reply" ).click( + function() { + var commentid = $( this ).attr('data-comment'); + + var comment_url = url; + if (comment_url_path && commentid) { + comment_url = comment_url + comment_url_path + commentid; + } + + $.ajax({ + url: comment_url, + type: 'GET', + dataType: 'json', + success: function(res) { + var quote = get_comment_text(res, commentid); + reply(quote) + }, + error: function() { + alert('Failed to retrieve comment text'); + } + }); + } + ).click( + function() { + $('html, body').animate({ + scrollTop: $("#comment").offset().top + }, 2000); + } + ); +}; + +function setup_issue_reply_btns(url) { + setup_reply_btns(url, get_comment_text_from_issue_data, '/comment/'); +} + +function setup_pr_reply_btns(url) { + setup_reply_btns(url, get_comment_text_from_pr_data, ''); +} diff --git a/pagure/templates/issue.html b/pagure/templates/issue.html index a66b9bf..2a2a5fa 100644 --- a/pagure/templates/issue.html +++ b/pagure/templates/issue.html @@ -676,6 +676,9 @@ namespace=repo.namespace, repo=repo.name, issueid=issueid) url_for('static', filename='vendor/jquery.atwho/jquery.atwho.min.js') }}?version={{ g.version}}"> + + diff --git a/pagure/templates/repo_pull_request.html b/pagure/templates/repo_pull_request.html index ef41537..2581c5c 100644 --- a/pagure/templates/repo_pull_request.html +++ b/pagure/templates/repo_pull_request.html @@ -752,6 +752,8 @@ url_for('static', filename='request_ev.js') }}?version={{ g.version}}"> +