From a05a30c1c18186519002b1367bcd74512a08b89b Mon Sep 17 00:00:00 2001 From: silverwind Date: Thu, 11 Jun 2020 00:16:35 +0200 Subject: [PATCH] Don't add same line code comment box twice (#11837) * Don't add same line code comment box twice Clicking the same '+' button multiple times adds multiple comment boxes to the same line. Prevent this by assigning a unique key to each comment box and checking if it already exists in the DOM. Also cleaned up the code around this a bit. * Update web_src/js/index.js Co-authored-by: Lauris BH Co-authored-by: techknowlogick Co-authored-by: Lauris BH Co-authored-by: techknowlogick --- web_src/js/index.js | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/web_src/js/index.js b/web_src/js/index.js index b8d6578c3..61059b694 100644 --- a/web_src/js/index.js +++ b/web_src/js/index.js @@ -1226,25 +1226,41 @@ function initPullRequestReview() { $(this).closest('tr').removeClass('focus-lines-new focus-lines-old'); }); $('.add-code-comment').on('click', function (e) { - // https://github.com/go-gitea/gitea/issues/4745 - if ($(e.target).hasClass('btn-add-single')) { - return; - } + if ($(e.target).hasClass('btn-add-single')) return; // https://github.com/go-gitea/gitea/issues/4745 e.preventDefault(); + const isSplit = $(this).closest('.code-diff').hasClass('code-diff-split'); const side = $(this).data('side'); const idx = $(this).data('idx'); const path = $(this).data('path'); const form = $('#pull_review_add_comment').html(); const tr = $(this).closest('tr'); + + const oldLineNum = tr.find('.lines-num-old').data('line-num'); + const newLineNum = tr.find('.lines-num-new').data('line-num'); + const addCommentKey = `${oldLineNum}|${newLineNum}`; + if (document.querySelector(`[data-add-comment-key="${addCommentKey}"]`)) return; // don't add same comment box twice + let ntr = tr.next(); if (!ntr.hasClass('add-comment')) { - ntr = $(`${ - isSplit ? '' : - '' - }`); + ntr = $(` + + ${isSplit ? ` + + + + + + + ` : ` + + + + `} + `); tr.after(ntr); } + const td = ntr.find(`.add-comment-${side}`); let commentCloud = td.find('.comment-code-cloud'); if (commentCloud.length === 0) {