Commit | Line | Data |
---|---|---|
ffe0b2f4 JC |
1 | (() => { |
2 | const comments = document.querySelector(".comment-tree > tbody"); | |
3 | ||
4 | let selectedComment = comments.firstElementChild | |
5 | selectedComment.style.outline = '1px dashed black'; | |
6 | ||
7 | const visible = (element) => { | |
8 | let bounds = element.getBoundingClientRect(); | |
9 | return bounds.top >= 0 && bounds.bottom <= window.innerHeight; | |
10 | } | |
11 | ||
12 | const changeWithVisibleCallback = (comment, callback) => { | |
13 | if (comment == null) { | |
14 | return; | |
15 | } | |
16 | selectedComment.style.outline = ''; | |
17 | selectedComment = comment; | |
18 | selectedComment.style.outline = '1px dashed black'; | |
19 | if (!visible(comment)) { | |
20 | callback(); | |
21 | } | |
22 | } | |
23 | ||
24 | const change = (comment) => { | |
25 | changeWithVisibleCallback(comment, () => {}); | |
26 | } | |
27 | ||
28 | // Curry callback for moving downpage | |
29 | const changeDownpage = (comment) => { | |
30 | changeWithVisibleCallback(comment, () => {window.scrollTo(0, window.scrollY + comment.offsetHeight)}); | |
31 | } | |
32 | ||
33 | // Curry callback for moving up page | |
34 | const changeUppage = (comment) => { | |
35 | changeWithVisibleCallback(comment, () => {window.scrollTo(0, window.scrollY - comment.offsetHeight)}); | |
36 | } | |
37 | ||
38 | document.addEventListener("click", (e) => { | |
39 | change(e.target.closest("tr.athing")); | |
40 | }) | |
41 | document.addEventListener("keydown", (e) => { | |
42 | if (e.isComposing) { | |
43 | return; | |
44 | } | |
45 | if (e.key == "j") { | |
46 | do { | |
47 | changeDownpage(selectedComment.nextElementSibling); | |
48 | } while (selectedComment.classList.contains("noshow")); | |
49 | } | |
50 | if (e.key == "k") { | |
51 | do { | |
52 | changeUppage(selectedComment.previousElementSibling); | |
53 | } while (selectedComment.classList.contains("noshow")); | |
54 | } | |
55 | if (e.key == "Enter") { | |
56 | let togg = selectedComment.querySelector(".togg"); | |
57 | let more = selectedComment.querySelector(".morelink"); | |
58 | if (togg) { | |
59 | togg.click(); | |
60 | } | |
61 | else if (more) { | |
62 | more.click(); | |
63 | } | |
64 | } | |
65 | }) | |
66 | })(); |