|
1 | 1 | (function savePlaygrounds() { |
2 | 2 | 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); |
14 | 14 | editor.clearSelection(); |
15 | | - i += 1; |
16 | | - }); |
17 | | - } |
| 15 | + } |
| 16 | + }); |
18 | 17 | } |
19 | 18 | function getCodeFromPlayground() { |
20 | | - var codes = []; |
21 | 19 | Array.from(document.querySelectorAll(".playground")).forEach(function ( |
22 | | - pre_block |
| 20 | + pre_block, |
| 21 | + index |
23 | 22 | ) { |
24 | 23 | let code_block = pre_block.querySelector("code"); |
25 | 24 | 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 | + }); |
28 | 32 | }); |
29 | | - localStorage.setItem(`${window.location.href}₹code`, JSON.stringify(codes)); |
30 | 33 | } |
31 | 34 | setCodeToPlayground(); |
32 | | - addEventListener("pagehide", getCodeFromPlayground); |
| 35 | + getCodeFromPlayground(); |
33 | 36 | })(); |
34 | 37 |
|
35 | 38 | 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 | + |
36 | 49 | let keys = []; |
37 | 50 | for (var i = 0, len = localStorage.length; i < len; i++) { |
38 | | - if (localStorage.key(i).includes("₹code")) { |
| 51 | + if (localStorage.key(i).includes("₹")) { |
39 | 52 | keys.push(localStorage.key(i)); |
40 | 53 | } |
41 | 54 | } |
|
0 commit comments