Skip to content

Commit

Permalink
Add js to disable solutions on 'F10'
Browse files Browse the repository at this point in the history
  • Loading branch information
bbannier committed Jan 24, 2025
1 parent 6535922 commit c3c8ef9
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ title = "Introduction to Spicy"
[output.html]
additional-css = ["./src/css/mdbook-admonish.css", "./mdbook-admonish.css"]
additional-js = [
"./src/js/solutions.js",
"./src/js/highlight-evt.js",
"./src/js/highlight-spicy.js",
"./src/js/highlight-spicy_batch.js",
Expand Down
46 changes: 46 additions & 0 deletions src/js/solutions.js
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();
}
});

0 comments on commit c3c8ef9

Please sign in to comment.