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