You've already forked comprehensive-rust
mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-06-17 22:57:35 +02:00
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>
This commit is contained in:
committed by
GitHub
parent
83b31e19af
commit
e2cad7da8d
@ -48,6 +48,9 @@ urlcolor = "red"
|
|||||||
curly-quotes = true
|
curly-quotes = true
|
||||||
additional-js = [
|
additional-js = [
|
||||||
"theme/speaker-notes.js",
|
"theme/speaker-notes.js",
|
||||||
|
"theme/redbox.js",
|
||||||
|
"theme/save-playgrounds.js",
|
||||||
|
"theme/instructor-menu.js",
|
||||||
]
|
]
|
||||||
additional-css = [
|
additional-css = [
|
||||||
"theme/css/svgbob.css",
|
"theme/css/svgbob.css",
|
||||||
|
@ -24,10 +24,10 @@
|
|||||||
instructorMenu.title = "Utilities for course instructors";
|
instructorMenu.title = "Utilities for course instructors";
|
||||||
instructorMenu.innerHTML =
|
instructorMenu.innerHTML =
|
||||||
'<i class="fa fa-ellipsis-v" aria-hidden="true"></i>';
|
'<i class="fa fa-ellipsis-v" aria-hidden="true"></i>';
|
||||||
redBoxButton.innerHTML = "aspect-ratio box";
|
redBoxButton.innerHTML = "Toggle aspect-ratio box";
|
||||||
redBoxButton.title =
|
redBoxButton.title =
|
||||||
"Outline the area that fits on one screen while teaching the course.";
|
"Outline the area that fits on one screen while teaching the course.";
|
||||||
playgroundStateButton.innerHTML = "reset all playgrounds";
|
playgroundStateButton.innerHTML = "Reset all playgrounds";
|
||||||
playgroundStateButton.title =
|
playgroundStateButton.title =
|
||||||
"Reset code in all playgrounds to its original value.";
|
"Reset code in all playgrounds to its original value.";
|
||||||
|
|
||||||
|
@ -1,41 +1,54 @@
|
|||||||
(function savePlaygrounds() {
|
(function savePlaygrounds() {
|
||||||
function setCodeToPlayground() {
|
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]);
|
|
||||||
editor.clearSelection();
|
|
||||||
i += 1;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
function getCodeFromPlayground() {
|
|
||||||
var codes = [];
|
|
||||||
Array.from(document.querySelectorAll(".playground")).forEach(function (
|
Array.from(document.querySelectorAll(".playground")).forEach(function (
|
||||||
pre_block
|
pre_block,
|
||||||
|
index
|
||||||
) {
|
) {
|
||||||
let code_block = pre_block.querySelector("code");
|
let code_block = pre_block.querySelector("code");
|
||||||
let editor = window.ace.edit(code_block);
|
let editor = window.ace.edit(code_block);
|
||||||
let code = editor.getValue();
|
code = JSON.parse(
|
||||||
codes.push(code);
|
localStorage.getItem(`${window.location.href}₹${index}`)
|
||||||
|
);
|
||||||
|
if (code) {
|
||||||
|
editor.setValue(code);
|
||||||
|
editor.clearSelection();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
function getCodeFromPlayground() {
|
||||||
|
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.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();
|
setCodeToPlayground();
|
||||||
addEventListener("pagehide", getCodeFromPlayground);
|
getCodeFromPlayground();
|
||||||
})();
|
})();
|
||||||
|
|
||||||
function resetPlaygroundsClicked() {
|
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 = [];
|
let keys = [];
|
||||||
for (var i = 0, len = localStorage.length; i < len; i++) {
|
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));
|
keys.push(localStorage.key(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user