-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Saving playground code state #1917
Changes from 7 commits
6fdae32
65e4327
61403a5
58b36a1
9a42d76
23f7592
5a27433
7c51897
8fea0af
55729b0
c12f277
9d03037
ed05702
138fbd7
28fbdd9
8889a48
7a0de16
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,33 @@ | |
} | ||
}); | ||
})(); | ||
|
||
function setCodeToPlayground() { | ||
var codes = JSON.parse(localStorage.getItem(window.location.href)); | ||
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; | ||
}); | ||
} | ||
} | ||
setCodeToPlayground(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor point: this is a bit hard to spot here. Please move the whole thing to an IIFE, like the red-box functionality, and put the immediately-evaluated statements at the bottom: (function savePlaygrounds() {
function setCodeToPlayground() ..
function getCodeFromPlaground() ..
setCodeToPlayground();
setInterval(getCodeFromPlayground, 900);
})() |
||
function getCodeFromPlayground() { | ||
var codes = []; | ||
Array.from(document.querySelectorAll(".playground")).forEach(function ( | ||
pre_block | ||
) { | ||
let code_block = pre_block.querySelector("code"); | ||
let editor = window.ace.edit(code_block); | ||
let code = editor.getValue(); | ||
codes.push(code); | ||
}); | ||
localStorage.setItem(window.location.href, JSON.stringify(codes)); | ||
} | ||
setInterval(getCodeFromPlayground, 900); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like it will wake up the browser frequently even if nothing has changed. I wonder if we could either do this on some event indicating the browser is navigating away (maybe https://developer.mozilla.org/en-US/docs/Web/API/Window/pagehide_event?) or on a timer after the user has modified something in the editor. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, I thought you meant adding a new JS file for this functionality, rather than moving the existing red-box functionality too. Maybe keep
theme/redbox.js
and move this new functionality intotheme/save-playgrounds.js
?