From 4820392226b863c9105b6abb49af4db019a49dc8 Mon Sep 17 00:00:00 2001 From: ryanshanz <92600751+ryanshanz@users.noreply.github.com> Date: Fri, 9 May 2025 20:41:44 -0400 Subject: [PATCH 1/2] feat: add keyboard commands --- src/main.rs | 1 + static/keyboardcommands.js | 56 ++++++++++++++++++++++++++++++++++++++ templates/base.html | 1 + templates/comment.html | 2 +- 4 files changed, 59 insertions(+), 1 deletion(-) create mode 100644 static/keyboardcommands.js diff --git a/src/main.rs b/src/main.rs index e1b010df..6327a100 100644 --- a/src/main.rs +++ b/src/main.rs @@ -259,6 +259,7 @@ async fn main() { .at("/check_update.js") .get(|_| resource(include_str!("../static/check_update.js"), "text/javascript", false).boxed()); app.at("/copy.js").get(|_| resource(include_str!("../static/copy.js"), "text/javascript", false).boxed()); + app.at("/static/keyboardcommands.js").get(|_| resource(include_str!("../static/keyboardcommands.js"), "text/javascript", false).boxed()); app.at("/commits.atom").get(|_| async move { proxy_commit_info().await }.boxed()); app.at("/instances.json").get(|_| async move { proxy_instances().await }.boxed()); diff --git a/static/keyboardcommands.js b/static/keyboardcommands.js new file mode 100644 index 00000000..c9aeb807 --- /dev/null +++ b/static/keyboardcommands.js @@ -0,0 +1,56 @@ +document.addEventListener("DOMContentLoaded", function() { + +allComments = []; +topmostCommentsInThreads = []; +let parentCommentIndex = 0; +let topmostCommentIndex = 0; + +const COMMENT_INDEX_INCREMENT = 1; +const COMMENT_INDEX_DECREMENT = -1; + +document.querySelectorAll('.thread .comment').forEach(element => { + allComments.push(element.getAttribute('data-comment-id')); + +}); + +document.querySelectorAll('.thread').forEach((thread) => { + const allCommentsInThread = thread.querySelectorAll('.comment'); + allCommentsInThread.forEach((comment) => { + if (!comment.closest('blockquote')) { + topmostCommentsInThreads.push(comment); + } + }); + }); + + + document.addEventListener('keypress', (event) => { + if (event.key === 'Enter' && event.shiftKey) { + window.open(post_url.href, "_blank").focus();//open link in new tab, though in Edge it seems to be new window + } else if (event.key === 'Enter') { + window.location.href = post_url.href; + } else if (event.key === 'j') {//next comment + const nextComment = document.getElementById(allComments[parentCommentIndex + COMMENT_INDEX_INCREMENT]); + parentCommentIndex += COMMENT_INDEX_INCREMENT; + nextComment.scrollIntoView({ behavior: "smooth", block: "start" }); + } else if (event.key === 'k') {//previous comment + const previousComment = document.getElementById(allComments[parentCommentIndex + COMMENT_INDEX_DECREMENT]); + parentCommentIndex += COMMENT_INDEX_DECREMENT; + if (parentCommentIndex < 0) { + parentCommentIndex = 0; + } + previousComment.scrollIntoView({ behavior: "smooth", block: "start" }); + } else if (event.key=== 't') { //top of next thread + const nextTopmostComment = topmostCommentsInThreads[topmostCommentIndex + COMMENT_INDEX_INCREMENT]; + topmostCommentIndex += COMMENT_INDEX_INCREMENT; + nextTopmostComment.scrollIntoView({ behavior: "smooth", block: "start" }); + } else if (event.key === 'p') {//top of previous thread + const previousTopmostComment = topmostCommentsInThreads[topmostCommentIndex + COMMENT_INDEX_DECREMENT]; + topmostCommentIndex += COMMENT_INDEX_DECREMENT; + if (topmostCommentIndex < 0) { + topmostCommentIndex = 0; + } + previousTopmostComment.scrollIntoView({ behavior: "smooth", block: "start" }); + } + }); +}); + diff --git a/templates/base.html b/templates/base.html index a1c141bc..7cd42114 100644 --- a/templates/base.html +++ b/templates/base.html @@ -27,6 +27,7 @@ +
{% endblock %} diff --git a/templates/comment.html b/templates/comment.html index 36c9b606..e262d44d 100644 --- a/templates/comment.html +++ b/templates/comment.html @@ -3,7 +3,7 @@ {% if kind == "more" && parent_kind == "t1" %} → More replies ({{ more_count }}) {% else if kind == "t1" %} -
+

{% if prefs.hide_score != "on" %} From f51075f668de32629194d5d986cbf869f059521c Mon Sep 17 00:00:00 2001 From: ryanshanz <92600751+ryanshanz@users.noreply.github.com> Date: Fri, 9 May 2025 20:53:58 -0400 Subject: [PATCH 2/2] fix: cargo formatting --- src/main.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 6327a100..9213540f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -259,7 +259,9 @@ async fn main() { .at("/check_update.js") .get(|_| resource(include_str!("../static/check_update.js"), "text/javascript", false).boxed()); app.at("/copy.js").get(|_| resource(include_str!("../static/copy.js"), "text/javascript", false).boxed()); - app.at("/static/keyboardcommands.js").get(|_| resource(include_str!("../static/keyboardcommands.js"), "text/javascript", false).boxed()); + app + .at("/static/keyboardcommands.js") + .get(|_| resource(include_str!("../static/keyboardcommands.js"), "text/javascript", false).boxed()); app.at("/commits.atom").get(|_| async move { proxy_commit_info().await }.boxed()); app.at("/instances.json").get(|_| async move { proxy_instances().await }.boxed());