Skip to content

WIP: add support for j/k - vi-style navigation #608

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 31 additions & 8 deletions src/theme/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,8 +415,7 @@ function playpen_text(playpen) {
try { localStorage.setItem('mdbook-sidebar', 'hidden'); } catch (e) { }
}

// Toggle sidebar
sidebarToggleButton.addEventListener('click', function sidebarToggle() {
function toggleSidebar() {
if (html.classList.contains("sidebar-hidden")) {
showSidebar();
} else if (html.classList.contains("sidebar-visible")) {
Expand All @@ -428,7 +427,10 @@ function playpen_text(playpen) {
showSidebar();
}
}
});
}

// Toggle sidebar
sidebarToggleButton.addEventListener('click', toggleSidebar);

document.addEventListener('touchstart', function (e) {
firstContact = {
Expand All @@ -455,6 +457,17 @@ function playpen_text(playpen) {
}
}, { passive: true });

document.addEventListener('keydown', function (e) {
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }

switch (e.key) {
case 'F9':
console.log(window.location.href);
toggleSidebar();
break;
}
});

// Scroll sidebar to current active section
var activeSection = sidebar.querySelector(".active");
if (activeSection) {
Expand All @@ -468,18 +481,28 @@ function playpen_text(playpen) {

switch (e.key) {
case 'ArrowRight':
case 'j':
e.preventDefault();
var previousButton = document.querySelector('.nav-chapters.previous');
if (previousButton) {
window.location.href = previousButton.href;
}
break;
case 'ArrowLeft':
case 'k':
e.preventDefault();
var nextButton = document.querySelector('.nav-chapters.next');
if (nextButton) {
window.location.href = nextButton.href;
}
break;
case 'ArrowLeft':
case 'h':
e.preventDefault();
var previousButton = document.querySelector('.nav-chapters.previous');
if (previousButton) {
window.location.href = previousButton.href;
}
// TODO: add code
break;
case 'l':
e.preventDefault();
// TODO: add code
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seems that the author abandoned this PR, I created #1408 to follow up on this.

break;
}
});
Expand Down