Skip to content

Commit e2cad7d

Browse files
mani-chandmgeisler
andauthored
Saving the playground state in local storage (#1942)
#1476 issue. Updated the function call(`getCodeFromPlayground`) in `save-playground.js` file from `pagehide` event to change event in ace editor. --------- Co-authored-by: Martin Geisler <mgeisler@google.com>
1 parent 83b31e1 commit e2cad7d

File tree

3 files changed

+39
-23
lines changed

3 files changed

+39
-23
lines changed

book.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ 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",
5154
]
5255
additional-css = [
5356
"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 = "aspect-ratio box";
27+
redBoxButton.innerHTML = "Toggle 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: 34 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,54 @@
11
(function savePlaygrounds() {
22
function setCodeToPlayground() {
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]);
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);
1414
editor.clearSelection();
15-
i += 1;
16-
});
17-
}
15+
}
16+
});
1817
}
1918
function getCodeFromPlayground() {
20-
var codes = [];
2119
Array.from(document.querySelectorAll(".playground")).forEach(function (
22-
pre_block
20+
pre_block,
21+
index
2322
) {
2423
let code_block = pre_block.querySelector("code");
2524
let editor = window.ace.edit(code_block);
26-
let code = editor.getValue();
27-
codes.push(code);
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+
});
2832
});
29-
localStorage.setItem(`${window.location.href}₹code`, JSON.stringify(codes));
3033
}
3134
setCodeToPlayground();
32-
addEventListener("pagehide", getCodeFromPlayground);
35+
getCodeFromPlayground();
3336
})();
3437

3538
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+
3649
let keys = [];
3750
for (var i = 0, len = localStorage.length; i < len; i++) {
38-
if (localStorage.key(i).includes("₹code")) {
51+
if (localStorage.key(i).includes("₹")) {
3952
keys.push(localStorage.key(i));
4053
}
4154
}

0 commit comments

Comments
 (0)