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

@ -9,6 +9,14 @@ This is a static site generated using [Hugo](https://gohugo.io/). There's a publ
[here](https://github.com/alejandro-angulo/alejandr0angul0.dev). Nothing fancy going on. I'm planning on using this site as a sort
of personal documentation. Hopefully what I write here will be useful to whoever stumbles upon it.
For you vim fans, this site supports scrolling vertically with vim bindings (as long as you allow this site to run javascript). As
of now the following are supported:
- `j` (scrolls down)
- `k` (scrolls up)
- `gg` (scrolls to top)
- `G` (scrolls to bottom)
## Me
I'm Alejandro Angulo, a code monkey living in the Los Angeles area. When I'm not ~~introducting bugs~~ writing beautiful,
@ -18,4 +26,3 @@ I like to pass the time binge watching TV shows on Hulu/Netflix and starting (bu
### Contact
I can be reached at `iam@<the domain of this site>`.

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;
});