Skip to content

Commit 4134945

Browse files
committed
Add pop-up window showing the keyboard shortcuts
Make it display when the user presses `?`. Implements #2607
1 parent 43281c8 commit 4134945

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

src/theme/book.js

+37-1
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ function playground_text(playground, hidden = true) {
543543

544544
(function chapterNavigation() {
545545
document.addEventListener('keydown', function (e) {
546-
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
546+
if (e.altKey || e.ctrlKey || e.metaKey) { return; }
547547
if (window.search && window.search.hasFocus()) { return; }
548548
var html = document.querySelector('html');
549549

@@ -559,6 +559,42 @@ function playground_text(playground, hidden = true) {
559559
window.location.href = previousButton.href;
560560
}
561561
}
562+
function showHelp() {
563+
document.getElementById("help-overlay").style.display = "block";
564+
let help = `<h2 id="help-title">Keyboard shortcuts</h2>`;
565+
help += "<p>Press <kbd>←</kbd> or <kbd>→</kbd> to navigate between chapters</p>";
566+
help += "<p>Press <kbd>s</kbd> to search in the book</p>";
567+
help += "<p>Press <kbd>?</kbd> to show this help</p>";
568+
help += "<p>Press <kbd>Esc</kbd> to hide this help</p>";
569+
570+
document.getElementById("help-overlay").innerHTML = help;
571+
572+
document.addEventListener('keydown', function (e) {
573+
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) { return; }
574+
575+
switch (e.key) {
576+
case 'Escape':
577+
e.preventDefault();
578+
hideHelp();
579+
break;
580+
}
581+
});
582+
}
583+
function hideHelp() {
584+
document.getElementById("help-overlay").style.display = "none";
585+
}
586+
587+
if (e.shiftKey) {
588+
switch (e.key) {
589+
case '?':
590+
e.preventDefault();
591+
showHelp();
592+
break;
593+
}
594+
595+
return;
596+
}
597+
562598
switch (e.key) {
563599
case 'ArrowRight':
564600
e.preventDefault();

src/theme/css/general.css

+23
Original file line numberDiff line numberDiff line change
@@ -242,3 +242,26 @@ sup {
242242
.result-no-output {
243243
font-style: italic;
244244
}
245+
246+
#help-overlay {
247+
position: fixed;
248+
display: none;
249+
width: 20%;
250+
height: 60%;
251+
top: 10%;
252+
left: 40%;
253+
right: 0;
254+
bottom: 0;
255+
background-color: rgba(225,225,225,1);
256+
z-index: 2;
257+
cursor: pointer;
258+
border-width: 3px;
259+
border-color: black;
260+
border-style: solid;
261+
border-radius: 25px;
262+
263+
padding: 10px;
264+
}
265+
#help-title {
266+
text-align: center;
267+
}

src/theme/index.hbs

+1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
<script src="{{ resource "toc.js" }}"></script>
6363
</head>
6464
<body>
65+
<div id="help-overlay"></div>
6566
<div id="body-container">
6667
<!-- Work around some values being stored in localStorage wrapped in quotes -->
6768
<script>

0 commit comments

Comments
 (0)