From 83ab6bf5b33a6189c49156ab75571eaaa6958560 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 13 May 2017 16:37:06 +0100 Subject: [PATCH] Fix the reply box --- build/index.php | 47 ++++++++++++++++++++++------ module_index.json | 2 +- modules/feature-comments.php | 60 ++++++++++++++++++++---------------- 3 files changed, 72 insertions(+), 37 deletions(-) diff --git a/build/index.php b/build/index.php index 3e67f81..0477e16 100644 --- a/build/index.php +++ b/build/index.php @@ -1565,7 +1565,7 @@ class page_renderer // Math rendering support if(!empty($settings->enable_math_rendering)) { - page_renderer::AddJSLink("https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-MML-AM_CHTML"); + page_renderer::AddJSLink("https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML"); } // alt+enter suport in the search box page_renderer::AddJSSnippet('// Alt + Enter support in the top search box @@ -2179,15 +2179,15 @@ register_module([ page_renderer::register_part_preprocessor(function(&$parts) { global $env; $comments_filename = get_comment_filename($env->page); - $comments_data = file_exists($comments_filename) ? json_decode(file_get_contents($comment_filename)) : []; + $comments_data = file_exists($comments_filename) ? json_decode(file_get_contents($comments_filename)) : []; $comments_html = "\n"; $parts["{extra}"] = $comments_html . $parts["{extra}"]; }); + + $reply_js_snippet = <<<'REPLYJS' +/////////////////////////////////// +///////// Commenting Form ///////// +/////////////////////////////////// +window.addEventListener("load", function(event) { + var replyButtons = document.querySelectorAll(".reply-button"); + for(let i = 0; i < replyButtons.length; i++) { + replyButtons[i].addEventListener("click", display_reply_form); + replyButtons[i].addEventListener("touchend", display_reply_form); + } +}); + +function display_reply_form(event) +{ + // Deep-clone the comment form + var replyForm = document.querySelector(".comment-reply-form").cloneNode(true); + replyForm.classList.add("nested"); + // Set the comment we're replying to + replyForm.querySelector("[name=reply-to]").value = event.target.parentElement.parentElement.dataset.commentId; + // Display the newly-cloned commenting form + event.target.parentElement.parentElement.querySelector(".reply-box-container").appendChild(replyForm); +} + +REPLYJS; + page_renderer::AddJSSnippet($reply_js_snippet); + } } ]); @@ -2230,7 +2259,7 @@ function get_comment_filename($pagename) function generate_comment_id() { $result = base64_encode(random_bytes(16)); - $result = str_replace(["+", "/"], ["-", "_"], $result); + $result = str_replace(["+", "/", "="], ["-", "_"], $result); return $result; } @@ -2314,7 +2343,7 @@ function render_comments($comments_data, $depth = 0) $result = "
"; foreach($comments_data as $comment) { - $result .= "\t
\n"; + $result .= "\t
\n"; $result .= "\t

$comment->username said:

"; $result .= "\t
\n"; $result .= "\t\t" . parse_page_source($comment->message); @@ -2322,11 +2351,11 @@ function render_comments($comments_data, $depth = 0) $result .= "\t
\n"; $result .= "\t\n"; $result .= "\t" . render_comments($comment->replies) . "\n"; - $ersult .= "\t
"; + $result .= "\t
"; } $result .= "
"; diff --git a/module_index.json b/module_index.json index 4ccafe6..ed8c321 100755 --- a/module_index.json +++ b/module_index.json @@ -50,7 +50,7 @@ "author": "Starbeamrainbowlabs", "description": "Adds threaded comments to the bottom of every page.", "id": "feature-comments", - "lastupdate": 1494686069, + "lastupdate": 1494689720, "optional": false }, { diff --git a/modules/feature-comments.php b/modules/feature-comments.php index b526cdd..67398f4 100644 --- a/modules/feature-comments.php +++ b/modules/feature-comments.php @@ -124,11 +124,11 @@ register_module([ page_renderer::register_part_preprocessor(function(&$parts) { global $env; $comments_filename = get_comment_filename($env->page); - $comments_data = file_exists($comments_filename) ? json_decode(file_get_contents($comment_filename)) : []; + $comments_data = file_exists($comments_filename) ? json_decode(file_get_contents($comments_filename)) : []; $comments_html = "\n"; + + $parts["{extra}"] = $comments_html . $parts["{extra}"]; + }); + + $reply_js_snippet = <<<'REPLYJS' /////////////////////////////////// ///////// Commenting Form ///////// /////////////////////////////////// -document.addEventListener("load", function(event) { +window.addEventListener("load", function(event) { var replyButtons = document.querySelectorAll(".reply-button"); for(let i = 0; i < replyButtons.length; i++) { - replyButtons + replyButtons[i].addEventListener("click", display_reply_form); + replyButtons[i].addEventListener("touchend", display_reply_form); } }); @@ -153,27 +170,16 @@ function display_reply_form(event) { // Deep-clone the comment form var replyForm = document.querySelector(".comment-reply-form").cloneNode(true); + replyForm.classList.add("nested"); // Set the comment we're replying to - replyForm.querySelector("[name=reply-to]").value = event.target.parentNode.dataset.commentId; + replyForm.querySelector("[name=reply-to]").value = event.target.parentElement.parentElement.dataset.commentId; // Display the newly-cloned commenting form - event.target.parentElement.querySelector(".reply-box-container").appendChild(replyForm); + event.target.parentElement.parentElement.querySelector(".reply-box-container").appendChild(replyForm); } + REPLYJS; -) - } - else { - $comments_html .= "\n"; - } - $comments_html .= render_comments($comments_data); - - $comments_html .= "\n"; - - $parts["{extra}"] = $comments_html . $parts["{extra}"]; - }); + page_renderer::AddJSSnippet($reply_js_snippet); + } } ]); @@ -198,7 +204,7 @@ function get_comment_filename($pagename) function generate_comment_id() { $result = base64_encode(random_bytes(16)); - $result = str_replace(["+", "/"], ["-", "_"], $result); + $result = str_replace(["+", "/", "="], ["-", "_"], $result); return $result; } @@ -290,11 +296,11 @@ function render_comments($comments_data, $depth = 0) $result .= "\t
\n"; $result .= "\t\n"; $result .= "\t" . render_comments($comment->replies) . "\n"; - $ersult .= "\t
"; + $result .= "\t"; } $result .= "";