Skip to content
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

feat: add navlinks keyboard shortcuts #10

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions layouts/_default/baseof.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
</main>

{{ partial "docs/inject/body" . }}
<script src="{{ .Site.BaseURL }}js/navlinks.js"></script>
</body>
</html>

Expand Down
4 changes: 2 additions & 2 deletions layouts/partials/docs/navigation-links.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@
<li>
{{ if ne $currentPageIndex $prevLinkIndex }}
{{$prevPage := index $sitePages $prevLinkIndex}}
<a href="{{ $prevPage.Link }}">{{ i18n "prev" }}</a>
<a href="{{ $prevPage.Link }}" id="prev-page">{{ i18n "prev" }}</a>
{{ end }}
</li>
<li>
{{ if ne $currentPageIndex $nextLinkIndex }}
{{$nextPage := index $sitePages $nextLinkIndex}}
<a href="{{ $nextPage.Link }}">{{ i18n "next"}}</a>
<a href="{{ $nextPage.Link }}" id="next-page">{{ i18n "next"}}</a>
{{ end }}
</li>
</ul>
Expand Down
22 changes: 22 additions & 0 deletions static/js/navlinks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* Script to handle the navigation links events.
* When the user presses arrow left or arrow right, the page will be redirected to the previous/next page if corresponding link exists.
*/

// Get the previous (id="prev-page") and next (id="next-page") links
const prevPageLink = document.getElementById("prev-page");
const nextPageLink = document.getElementById("next-page");

// Add event listener to the document
document.addEventListener("keydown", (event) => {
if (!event.ctrlKey && !event.altKey && !event.shiftKey && !event.metaKey) {
return;
} else {
if (event.key === "ArrowLeft" && prevPageLink) {
prevPageLink.click();
}
if (event.key === "ArrowRight" && nextPageLink) {
nextPageLink.click();
}
}
});