From 58772c62f6a6050dabe1c318f1e24e959e878600 Mon Sep 17 00:00:00 2001 From: Evan You Date: Thu, 10 Oct 2019 12:11:14 -0400 Subject: [PATCH] workflow: persist template explorer state via localStorage --- packages/template-explorer/src/index.ts | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/template-explorer/src/index.ts b/packages/template-explorer/src/index.ts index 5bf7b10bf..4fda5bf5c 100644 --- a/packages/template-explorer/src/index.ts +++ b/packages/template-explorer/src/index.ts @@ -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)