Skip to content

Commit eb721d4

Browse files
committed
workflow: improve template explorer hash persistence
1 parent 2b506d7 commit eb721d4

File tree

2 files changed

+50
-17
lines changed

2 files changed

+50
-17
lines changed

packages/template-explorer/src/index.ts

Lines changed: 44 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import * as m from 'monaco-editor'
22
import { compile, CompilerError, CompilerOptions } from '@vue/compiler-dom'
33
import { compile as ssrCompile } from '@vue/compiler-ssr'
4-
import { compilerOptions, initOptions, ssrMode } from './options'
4+
import {
5+
defaultOptions,
6+
compilerOptions,
7+
initOptions,
8+
ssrMode
9+
} from './options'
510
import { toRaw, watchEffect } from '@vue/runtime-dom'
611
import { SourceMapConsumer } from 'source-map'
712
import theme from './theme'
@@ -35,19 +40,32 @@ window.init = () => {
3540
monaco.editor.defineTheme('my-theme', theme)
3641
monaco.editor.setTheme('my-theme')
3742

38-
const persistedState: PersistedState = JSON.parse(
39-
decodeURIComponent(window.location.hash.slice(1)) ||
40-
localStorage.getItem('state') ||
41-
`{}`
42-
)
43-
// functions are not persistable, so delete it in case we sometimes need
44-
// to debug with custom nodeTransforms
45-
if (persistedState.options) {
46-
delete persistedState.options.nodeTransforms
43+
let persistedState: PersistedState | undefined
44+
45+
try {
46+
let hash = window.location.hash.slice(1)
47+
try {
48+
hash = escape(atob(hash))
49+
} catch (e) {}
50+
persistedState = JSON.parse(
51+
decodeURIComponent(hash) || localStorage.getItem('state') || `{}`
52+
)
53+
} catch (e: any) {
54+
// bad stored state, clear it
55+
console.warn(
56+
'Persisted state in localStorage seems to be corrupted, please reload.\n' +
57+
e.message
58+
)
59+
localStorage.clear()
4760
}
4861

49-
ssrMode.value = persistedState.ssr
50-
Object.assign(compilerOptions, persistedState.options)
62+
if (persistedState) {
63+
// functions are not persistable, so delete it in case we sometimes need
64+
// to debug with custom nodeTransforms
65+
delete persistedState.options.nodeTransforms
66+
ssrMode.value = persistedState.ssr
67+
Object.assign(compilerOptions, persistedState.options)
68+
}
5169

5270
let lastSuccessfulCode: string
5371
let lastSuccessfulMap: SourceMapConsumer | undefined = undefined
@@ -99,21 +117,32 @@ window.init = () => {
99117
function reCompile() {
100118
const src = editor.getValue()
101119
// every time we re-compile, persist current state
120+
121+
const optionsToSave = {}
122+
let key: keyof CompilerOptions
123+
for (key in compilerOptions) {
124+
const val = compilerOptions[key]
125+
if (typeof val !== 'object' && val !== defaultOptions[key]) {
126+
// @ts-ignore
127+
optionsToSave[key] = val
128+
}
129+
}
130+
102131
const state = JSON.stringify({
103132
src,
104133
ssr: ssrMode.value,
105-
options: compilerOptions
134+
options: optionsToSave
106135
} as PersistedState)
107136
localStorage.setItem('state', state)
108-
window.location.hash = encodeURIComponent(state)
137+
window.location.hash = btoa(unescape(encodeURIComponent(state)))
109138
const res = compileCode(src)
110139
if (res) {
111140
output.setValue(res)
112141
}
113142
}
114143

115144
const editor = monaco.editor.create(document.getElementById('source')!, {
116-
value: persistedState.src || `<div>Hello World!</div>`,
145+
value: persistedState?.src || `<div>Hello World!</div>`,
117146
language: 'html',
118147
...sharedEditorOptions,
119148
wordWrap: 'bounded'

packages/template-explorer/src/options.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { BindingTypes } from '@vue/compiler-core'
44

55
export const ssrMode = ref(false)
66

7-
export const compilerOptions: CompilerOptions = reactive({
7+
export const defaultOptions: CompilerOptions = {
88
mode: 'module',
99
filename: 'Foo.vue',
1010
prefixIdentifiers: false,
@@ -24,7 +24,11 @@ export const compilerOptions: CompilerOptions = reactive({
2424
setupProp: BindingTypes.PROPS,
2525
vMySetupDir: BindingTypes.SETUP_CONST
2626
}
27-
})
27+
}
28+
29+
export const compilerOptions: CompilerOptions = reactive(
30+
Object.assign({}, defaultOptions)
31+
)
2832

2933
const App = {
3034
setup() {

0 commit comments

Comments
 (0)