-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add js to disable solutions on 'F10'
- Loading branch information
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
const HIDE_SOLUTIONS = "hide_solutions"; | ||
|
||
/** | ||
* Change the visibility state of solutions. | ||
* @param {boolean} show whether to show solutions | ||
*/ | ||
function set_solutions(show) { | ||
document.querySelectorAll("details") | ||
.forEach((div) => { | ||
const classes = div.classList; | ||
if (!show) | ||
classes.add("hidden"); | ||
else | ||
classes.remove("hidden"); | ||
}); | ||
} | ||
|
||
/** | ||
* Query whether we are configured to hide solutions. | ||
* @returns {boolean} | ||
*/ | ||
function hide_solutions() { | ||
const hide = localStorage.getItem(HIDE_SOLUTIONS); | ||
return !!hide; | ||
} | ||
|
||
/** | ||
* Toggle the visibility status of solutions | ||
*/ | ||
function toggle_show_solutions() { | ||
if (hide_solutions()) { | ||
localStorage.removeItem(HIDE_SOLUTIONS); | ||
set_solutions(true); | ||
} else { | ||
localStorage.setItem(HIDE_SOLUTIONS, "true"); | ||
set_solutions(false); | ||
} | ||
} | ||
|
||
// Register for keydown and a slightly obscure function key to not interfere | ||
// with mdbook's search bar functionality (in `searcher.js`). | ||
document.addEventListener("keydown", function (event) { | ||
if (event.key == 'F10') { | ||
toggle_show_solutions(); | ||
} | ||
}); |