Skip to content

Add pop-up window showing the keyboard shortcuts #2608

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
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
30 changes: 30 additions & 0 deletions src/front-end/css/general.css
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,33 @@ sup {
.result-no-output {
font-style: italic;
}

#help-overlay {
position: fixed;
display: none;
width: 20%;
height: 60%;
top: 10%;
left: 40%;
right: 0;
bottom: 0;
background-color: rgba(225,225,225,1);
color: black;
z-index: 2;
cursor: pointer;
border-width: 3px;
border-color: black;
border-style: solid;
border-radius: 25px;

padding: 10px;
}
#help-overlay kbd {
border-width: 1px;
border-color: black;
border-style: solid;
background-color: white;
}
#help-title {
text-align: center;
}
42 changes: 41 additions & 1 deletion src/front-end/js/book.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ aria-label="Show hidden lines"></button>';

(function chapterNavigation() {
document.addEventListener('keydown', function(e) {
if (e.altKey || e.ctrlKey || e.metaKey || e.shiftKey) {
if (e.altKey || e.ctrlKey || e.metaKey) {
return;
}
if (window.search && window.search.hasFocus()) {
Expand All @@ -643,6 +643,46 @@ aria-label="Show hidden lines"></button>';
window.location.href = previousButton.href;
}
}
function showHelp() {
document.getElementById('help-overlay').style.display = 'block';
let help = '<h2 id="help-title">Keyboard shortcuts</h2>';
help += '<p>Press <kbd>←</kbd> or <kbd>→</kbd> to navigate between chapters</p>';
help += '<p>Press <kbd>s</kbd> to search in the book</p>';
help += '<p>Press <kbd>?</kbd> to show this help</p>';
help += '<p>Press <kbd>Esc</kbd> to hide this help</p>';

document.getElementById('help-overlay').innerHTML = help;

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

switch (e.key) {
case 'Escape':
e.preventDefault();
hideHelp();
break;
}
});
}
function hideHelp() {
document.getElementById('help-overlay').style.display = 'none';
}

// Usually needs the Shift key to be pressed
switch (e.key) {
case '?':
e.preventDefault();
showHelp();
break;
}

// Rest of the keys are only active when the Shift key is not pressed
if (e.shiftKey) {
return;
}

switch (e.key) {
case 'ArrowRight':
e.preventDefault();
Expand Down
1 change: 1 addition & 0 deletions src/front-end/templates/index.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
<script src="{{ resource "toc.js" }}"></script>
</head>
<body>
<div id="help-overlay"></div>
<div id="body-container">
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
Expand Down