Skip to content
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

Saving the playground state in local storage #1942

Merged
merged 5 commits into from
May 28, 2024
Merged
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
3 changes: 3 additions & 0 deletions book.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ urlcolor = "red"
curly-quotes = true
additional-js = [
"theme/speaker-notes.js",
"theme/redbox.js",
"theme/save-playgrounds.js",
"theme/instructor-menu.js",
]
additional-css = [
"theme/css/svgbob.css",
Expand Down
4 changes: 2 additions & 2 deletions theme/instructor-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
instructorMenu.title = "Utilities for course instructors";
instructorMenu.innerHTML =
'<i class="fa fa-ellipsis-v" aria-hidden="true"></i>';
redBoxButton.innerHTML = "aspect-ratio box";
redBoxButton.innerHTML = "Toggle aspect-ratio box";
redBoxButton.title =
"Outline the area that fits on one screen while teaching the course.";
playgroundStateButton.innerHTML = "reset all playgrounds";
playgroundStateButton.innerHTML = "Reset all playgrounds";
playgroundStateButton.title =
"Reset code in all playgrounds to its original value.";

Expand Down
55 changes: 34 additions & 21 deletions theme/save-playgrounds.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,54 @@
(function savePlaygrounds() {
function setCodeToPlayground() {
var codes = JSON.parse(
localStorage.getItem(`${window.location.href}₹code`)
);
if (codes) {
var i = 0;
Array.from(document.querySelectorAll(".playground")).forEach(function (
pre_block
) {
let code_block = pre_block.querySelector("code");
let editor = window.ace.edit(code_block);
editor.setValue(codes[i]);
Array.from(document.querySelectorAll(".playground")).forEach(function (
pre_block,
index
) {
let code_block = pre_block.querySelector("code");
let editor = window.ace.edit(code_block);
code = JSON.parse(
localStorage.getItem(`${window.location.href}₹${index}`)
);
if (code) {
editor.setValue(code);
editor.clearSelection();
i += 1;
});
}
}
});
}
function getCodeFromPlayground() {
var codes = [];
Array.from(document.querySelectorAll(".playground")).forEach(function (
pre_block
pre_block,
index
) {
let code_block = pre_block.querySelector("code");
let editor = window.ace.edit(code_block);
let code = editor.getValue();
codes.push(code);
editor.session.on("change", function () {
let code = editor.getValue();
localStorage.setItem(
`${window.location.href}₹${index}`,
JSON.stringify(code)
);
});
});
localStorage.setItem(`${window.location.href}₹code`, JSON.stringify(codes));
}
setCodeToPlayground();
addEventListener("pagehide", getCodeFromPlayground);
getCodeFromPlayground();
})();

function resetPlaygroundsClicked() {
Array.from(document.querySelectorAll(".playground")).forEach(function (
pre_block,
index
) {
let code_block = pre_block.querySelector("code");
let editor = window.ace.edit(code_block);
editor.setValue(editor.originalCode);
editor.clearSelection();
});

let keys = [];
for (var i = 0, len = localStorage.length; i < len; i++) {
if (localStorage.key(i).includes("₹code")) {
if (localStorage.key(i).includes("₹")) {
keys.push(localStorage.key(i));
}
}
Expand Down