workflow: persist template explorer state via localStorage

This commit is contained in:
Evan You 2019-10-10 12:11:14 -04:00
parent 1139368dc2
commit 58772c62f6

View File

@ -15,7 +15,9 @@ declare global {
window.init = () => {
const monaco = window.monaco
const persistedState = JSON.parse(
decodeURIComponent(window.location.hash.slice(1)) || `{}`
decodeURIComponent(window.location.hash.slice(1)) ||
localStorage.getItem('state') ||
`{}`
)
Object.assign(compilerOptions, persistedState.options)
@ -64,13 +66,13 @@ window.init = () => {
function reCompile() {
const src = editor.getValue()
// every time we re-compile, persist current state to URL
window.location.hash = encodeURIComponent(
JSON.stringify({
src,
options: compilerOptions
})
)
// every time we re-compile, persist current state
const state = JSON.stringify({
src,
options: compilerOptions
})
localStorage.setItem('state', state)
window.location.hash = encodeURIComponent(state)
const res = compileCode(src)
if (res) {
output.setValue(res)