Skip to content

Commit 8e668cd

Browse files
authored
Revert "Saving the playground state in local storage (#1942)" (#2106)
This reverts commit e2cad7d. Fixes #2105, #2104.
1 parent 13d6900 commit 8e668cd

File tree

3 files changed

+23
-39
lines changed

3 files changed

+23
-39
lines changed

book.toml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ urlcolor = "red"
4848
curly-quotes = true
4949
additional-js = [
5050
"theme/speaker-notes.js",
51-
"theme/redbox.js",
52-
"theme/save-playgrounds.js",
53-
"theme/instructor-menu.js",
5451
]
5552
additional-css = [
5653
"theme/css/svgbob.css",

theme/instructor-menu.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@
2424
instructorMenu.title = "Utilities for course instructors";
2525
instructorMenu.innerHTML =
2626
'<i class="fa fa-ellipsis-v" aria-hidden="true"></i>';
27-
redBoxButton.innerHTML = "Toggle aspect-ratio box";
27+
redBoxButton.innerHTML = "aspect-ratio box";
2828
redBoxButton.title =
2929
"Outline the area that fits on one screen while teaching the course.";
30-
playgroundStateButton.innerHTML = "Reset all playgrounds";
30+
playgroundStateButton.innerHTML = "reset all playgrounds";
3131
playgroundStateButton.title =
3232
"Reset code in all playgrounds to its original value.";
3333

theme/save-playgrounds.js

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,41 @@
11
(function savePlaygrounds() {
22
function setCodeToPlayground() {
3-
Array.from(document.querySelectorAll(".playground")).forEach(function (
4-
pre_block,
5-
index
6-
) {
7-
let code_block = pre_block.querySelector("code");
8-
let editor = window.ace.edit(code_block);
9-
code = JSON.parse(
10-
localStorage.getItem(`${window.location.href}${index}`)
11-
);
12-
if (code) {
13-
editor.setValue(code);
3+
var codes = JSON.parse(
4+
localStorage.getItem(`${window.location.href}₹code`)
5+
);
6+
if (codes) {
7+
var i = 0;
8+
Array.from(document.querySelectorAll(".playground")).forEach(function (
9+
pre_block
10+
) {
11+
let code_block = pre_block.querySelector("code");
12+
let editor = window.ace.edit(code_block);
13+
editor.setValue(codes[i]);
1414
editor.clearSelection();
15-
}
16-
});
15+
i += 1;
16+
});
17+
}
1718
}
1819
function getCodeFromPlayground() {
20+
var codes = [];
1921
Array.from(document.querySelectorAll(".playground")).forEach(function (
20-
pre_block,
21-
index
22+
pre_block
2223
) {
2324
let code_block = pre_block.querySelector("code");
2425
let editor = window.ace.edit(code_block);
25-
editor.session.on("change", function () {
26-
let code = editor.getValue();
27-
localStorage.setItem(
28-
`${window.location.href}${index}`,
29-
JSON.stringify(code)
30-
);
31-
});
26+
let code = editor.getValue();
27+
codes.push(code);
3228
});
29+
localStorage.setItem(`${window.location.href}₹code`, JSON.stringify(codes));
3330
}
3431
setCodeToPlayground();
35-
getCodeFromPlayground();
32+
addEventListener("pagehide", getCodeFromPlayground);
3633
})();
3734

3835
function resetPlaygroundsClicked() {
39-
Array.from(document.querySelectorAll(".playground")).forEach(function (
40-
pre_block,
41-
index
42-
) {
43-
let code_block = pre_block.querySelector("code");
44-
let editor = window.ace.edit(code_block);
45-
editor.setValue(editor.originalCode);
46-
editor.clearSelection();
47-
});
48-
4936
let keys = [];
5037
for (var i = 0, len = localStorage.length; i < len; i++) {
51-
if (localStorage.key(i).includes("₹")) {
38+
if (localStorage.key(i).includes("₹code")) {
5239
keys.push(localStorage.key(i));
5340
}
5441
}

0 commit comments

Comments
 (0)