Skip to content

Commit 4573f4d

Browse files
szabgabehuss
authored andcommitted
Add pop-up window showing the keyboard shortcuts
Make it display when the user presses `?`. Implements #2607
1 parent 63ae0d5 commit 4573f4d

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

src/front-end/css/general.css

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,3 +277,26 @@ sup {
277277
.result-no-output {
278278
font-style: italic;
279279
}
280+
281+
#help-overlay {
282+
position: fixed;
283+
display: none;
284+
width: 20%;
285+
height: 60%;
286+
top: 10%;
287+
left: 40%;
288+
right: 0;
289+
bottom: 0;
290+
background-color: rgba(225,225,225,1);
291+
z-index: 2;
292+
cursor: pointer;
293+
border-width: 3px;
294+
border-color: black;
295+
border-style: solid;
296+
border-radius: 25px;
297+
298+
padding: 10px;
299+
}
300+
#help-title {
301+
text-align: center;
302+
}

src/front-end/js/book.js

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ aria-label="Show hidden lines"></button>';
623623

624624
(function chapterNavigation() {
625625
document.addEventListener('keydown', function(e) {
626-
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) {
626+
if (e.altKey || e.ctrlKey || e.metaKey) {
627627
return;
628628
}
629629
if (window.search && window.search.hasFocus()) {
@@ -643,6 +643,42 @@ aria-label="Show hidden lines"></button>';
643643
window.location.href = previousButton.href;
644644
}
645645
}
646+
function showHelp() {
647+
document.getElementById("help-overlay").style.display = "block";
648+
let help = `<h2 id="help-title">Keyboard shortcuts</h2>`;
649+
help += "<p>Press <kbd>←</kbd> or <kbd>→</kbd> to navigate between chapters</p>";
650+
help += "<p>Press <kbd>s</kbd> to search in the book</p>";
651+
help += "<p>Press <kbd>?</kbd> to show this help</p>";
652+
help += "<p>Press <kbd>Esc</kbd> to hide this help</p>";
653+
654+
document.getElementById("help-overlay").innerHTML = help;
655+
656+
document.addEventListener('keydown', function (e) {
657+
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
658+
659+
switch (e.key) {
660+
case 'Escape':
661+
e.preventDefault();
662+
hideHelp();
663+
break;
664+
}
665+
});
666+
}
667+
function hideHelp() {
668+
document.getElementById("help-overlay").style.display = "none";
669+
}
670+
671+
if (e.shiftKey) {
672+
switch (e.key) {
673+
case '?':
674+
e.preventDefault();
675+
showHelp();
676+
break;
677+
}
678+
679+
return;
680+
}
681+
646682
switch (e.key) {
647683
case 'ArrowRight':
648684
e.preventDefault();

src/front-end/templates/index.hbs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<script src="{{ resource "toc.js" }}"></script>
6464
</head>
6565
<body>
66+
<div id="help-overlay"></div>
6667
<div id="body-container">
6768
<!-- Work around some values being stored in localStorage wrapped in quotes -->
6869
<script>

0 commit comments

Comments
 (0)