Просмотр исходного кода

fix(discussionViewer): make button "Add reply" work

Co-Authored-By: Copilot
rugk 3 месяцев назад
Родитель
Сommit
37a8eff666
1 измененных файлов с 36 добавлено и 13 удалено
  1. 36 13
      js/privatebin.js

+ 36 - 13
js/privatebin.js

@@ -3447,6 +3447,11 @@ window.PrivateBin = (function () {
             // cache elements
             // cache elements
             commentTail = Model.getTemplate('commenttail');
             commentTail = Model.getTemplate('commenttail');
 
 
+            const replyButton = reply.querySelector('#replybutton');
+            if (replyButton) {
+                replyButton.addEventListener('click', PasteEncrypter.sendComment);
+            }
+
             addEventListeners();
             addEventListeners();
         }
         }
 
 
@@ -3459,7 +3464,7 @@ window.PrivateBin = (function () {
          * @param  {Event} event
          * @param  {Event} event
          */
          */
         function openReply(event) {
         function openReply(event) {
-            const source = event.target;
+            const source = event.currentTarget || event.target;
 
 
             // show all reply buttons
             // show all reply buttons
             commentContainer.querySelectorAll('button').forEach(btn => btn.classList.remove('hidden'));
             commentContainer.querySelectorAll('button').forEach(btn => btn.classList.remove('hidden'));
@@ -3475,7 +3480,16 @@ window.PrivateBin = (function () {
             replyCommentId = source.parentElement.id.split('_')[1];
             replyCommentId = source.parentElement.id.split('_')[1];
 
 
             // move to correct position
             // move to correct position
-            source.after(reply);
+            const parent = source.parentNode;
+            if (parent && !reply.contains(source)) {
+                if (typeof source.after === 'function') {
+                    source.after(reply);
+                } else if (source.nextSibling) {
+                    parent.insertBefore(reply, source.nextSibling);
+                } else {
+                    parent.appendChild(reply);
+                }
+            }
 
 
             // show
             // show
             reply.classList.remove('hidden');
             reply.classList.remove('hidden');
@@ -3597,6 +3611,12 @@ window.PrivateBin = (function () {
                 place = parentComment;
                 place = parentComment;
             }
             }
 
 
+            // attach button handler before inserting
+            const replyButton = commentEntry.querySelector('button');
+            if (replyButton) {
+                replyButton.addEventListener('click', openReply);
+            }
+
             // finally append comment
             // finally append comment
             place.appendChild(commentEntry);
             place.appendChild(commentEntry);
         };
         };
@@ -3609,9 +3629,12 @@ window.PrivateBin = (function () {
          */
          */
         me.finishDiscussion = function () {
         me.finishDiscussion = function () {
             // add 'add new comment' area
             // add 'add new comment' area
-            commentContainer.appendChild(commentTail.cloneNode(true));
-
-            addEventListeners();
+            const tail = commentTail.cloneNode(true);
+            const tailButton = tail.querySelector('button');
+            if (tailButton) {
+                tailButton.addEventListener('click', openReply);
+            }
+            commentContainer.appendChild(tail);
 
 
             // show discussions
             // show discussions
             discussion.classList.remove('hidden');
             discussion.classList.remove('hidden');
@@ -3706,9 +3729,6 @@ window.PrivateBin = (function () {
 
 
             document.querySelectorAll('#comment button')
             document.querySelectorAll('#comment button')
                 .forEach(btn => btn.addEventListener('click', openReply));
                 .forEach(btn => btn.addEventListener('click', openReply));
-
-            document.querySelectorAll('#reply button, #reply #replybutton')
-                .forEach(btn => btn.addEventListener('click', PasteEncrypter.sendComment));
         }
         }
 
 
         /**
         /**
@@ -6094,10 +6114,13 @@ window.PrivateBin = (function () {
 })();
 })();
 
 
 // main application start, called when DOM is fully loaded
 // main application start, called when DOM is fully loaded
-window.addEventListener('DOMContentLoaded', function () {
-    'use strict';
-    // run main controller
-    window.PrivateBin.Controller.init();
-});
+// skip auto-initialization in test environments (Node.js)
+if (typeof module === 'undefined' || !module.exports) {
+    window.addEventListener('DOMContentLoaded', function () {
+        'use strict';
+        // run main controller
+        window.PrivateBin.Controller.init();
+    });
+}