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