Added vim-like navigation (#11)

This commit is contained in:
Alejandro Angulo 2021-10-02 12:02:00 -07:00 committed by GitHub
parent 797fb83258
commit 8d2abf9b3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 54 additions and 1 deletions

View file

@ -0,0 +1 @@
<script src="/js/keynav.js"></script>

View file

@ -0,0 +1,45 @@
/*
* Adds vim-like keyboard navigation.
*
* Currently supports:
* - j (scroll down)
* - k (scroll up)
* - gg (scroll to top)
* - G (scroll to bottom)
*/
const scroll_vertical = 100;
const scroll_behavior = "smooth";
let last_key = null;
document.addEventListener("keydown", (e) => {
console.log(e);
let scroll_factor = 1;
if (e.key === "k") {
scroll_factor *= -1;
}
switch (e.key) {
case "j":
case "k":
window.scrollBy({
top: scroll_factor * scroll_vertical,
behavior: scroll_behavior,
});
break;
case "g":
if (last_key === "g") {
window.scroll(0, 0);
}
break;
case "G":
window.scroll(0, document.body.scrollHeight);
break;
}
last_key = e.key;
});