mirror of
https://github.com/google/comprehensive-rust.git
synced 2025-01-24 22:32:56 +02:00
e2cad7da8d
#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>
60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
(function savePlaygrounds() {
|
|
function setCodeToPlayground() {
|
|
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();
|
|
}
|
|
});
|
|
}
|
|
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)
|
|
);
|
|
});
|
|
});
|
|
}
|
|
setCodeToPlayground();
|
|
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("₹")) {
|
|
keys.push(localStorage.key(i));
|
|
}
|
|
}
|
|
for (let j = 0; j < keys.length; j++) {
|
|
localStorage.removeItem(keys[j]);
|
|
}
|
|
}
|
|
window.resetPlaygroundsClicked = resetPlaygroundsClicked;
|