|
1 | 1 | import * as m from 'monaco-editor'
|
2 | 2 | import { compile, CompilerError, CompilerOptions } from '@vue/compiler-dom'
|
3 | 3 | 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' |
5 | 10 | import { toRaw, watchEffect } from '@vue/runtime-dom'
|
6 | 11 | import { SourceMapConsumer } from 'source-map'
|
7 | 12 | import theme from './theme'
|
@@ -35,19 +40,32 @@ window.init = () => {
|
35 | 40 | monaco.editor.defineTheme('my-theme', theme)
|
36 | 41 | monaco.editor.setTheme('my-theme')
|
37 | 42 |
|
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() |
47 | 60 | }
|
48 | 61 |
|
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 | + } |
51 | 69 |
|
52 | 70 | let lastSuccessfulCode: string
|
53 | 71 | let lastSuccessfulMap: SourceMapConsumer | undefined = undefined
|
@@ -99,21 +117,32 @@ window.init = () => {
|
99 | 117 | function reCompile() {
|
100 | 118 | const src = editor.getValue()
|
101 | 119 | // 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 | + |
102 | 131 | const state = JSON.stringify({
|
103 | 132 | src,
|
104 | 133 | ssr: ssrMode.value,
|
105 |
| - options: compilerOptions |
| 134 | + options: optionsToSave |
106 | 135 | } as PersistedState)
|
107 | 136 | localStorage.setItem('state', state)
|
108 |
| - window.location.hash = encodeURIComponent(state) |
| 137 | + window.location.hash = btoa(unescape(encodeURIComponent(state))) |
109 | 138 | const res = compileCode(src)
|
110 | 139 | if (res) {
|
111 | 140 | output.setValue(res)
|
112 | 141 | }
|
113 | 142 | }
|
114 | 143 |
|
115 | 144 | const editor = monaco.editor.create(document.getElementById('source')!, {
|
116 |
| - value: persistedState.src || `<div>Hello World!</div>`, |
| 145 | + value: persistedState?.src || `<div>Hello World!</div>`, |
117 | 146 | language: 'html',
|
118 | 147 | ...sharedEditorOptions,
|
119 | 148 | wordWrap: 'bounded'
|
|
0 commit comments