Skip to content

Commit

Permalink
workflow: improve template explorer hash persistence
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed Jan 4, 2022
1 parent 2b506d7 commit eb721d4
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
59 changes: 44 additions & 15 deletions packages/template-explorer/src/index.ts
@@ -1,7 +1,12 @@
import * as m from 'monaco-editor'
import { compile, CompilerError, CompilerOptions } from '@vue/compiler-dom'
import { compile as ssrCompile } from '@vue/compiler-ssr'
import { compilerOptions, initOptions, ssrMode } from './options'
import {
defaultOptions,
compilerOptions,
initOptions,
ssrMode
} from './options'
import { toRaw, watchEffect } from '@vue/runtime-dom'
import { SourceMapConsumer } from 'source-map'
import theme from './theme'
Expand Down Expand Up @@ -35,19 +40,32 @@ window.init = () => {
monaco.editor.defineTheme('my-theme', theme)
monaco.editor.setTheme('my-theme')

const persistedState: PersistedState = JSON.parse(
decodeURIComponent(window.location.hash.slice(1)) ||
localStorage.getItem('state') ||
`{}`
)
// functions are not persistable, so delete it in case we sometimes need
// to debug with custom nodeTransforms
if (persistedState.options) {
delete persistedState.options.nodeTransforms
let persistedState: PersistedState | undefined

try {
let hash = window.location.hash.slice(1)
try {
hash = escape(atob(hash))
} catch (e) {}
persistedState = JSON.parse(
decodeURIComponent(hash) || localStorage.getItem('state') || `{}`
)
} catch (e: any) {
// bad stored state, clear it
console.warn(
'Persisted state in localStorage seems to be corrupted, please reload.\n' +
e.message
)
localStorage.clear()
}

ssrMode.value = persistedState.ssr
Object.assign(compilerOptions, persistedState.options)
if (persistedState) {
// functions are not persistable, so delete it in case we sometimes need
// to debug with custom nodeTransforms
delete persistedState.options.nodeTransforms
ssrMode.value = persistedState.ssr
Object.assign(compilerOptions, persistedState.options)
}

let lastSuccessfulCode: string
let lastSuccessfulMap: SourceMapConsumer | undefined = undefined
Expand Down Expand Up @@ -99,21 +117,32 @@ window.init = () => {
function reCompile() {
const src = editor.getValue()
// every time we re-compile, persist current state

const optionsToSave = {}
let key: keyof CompilerOptions
for (key in compilerOptions) {
const val = compilerOptions[key]
if (typeof val !== 'object' && val !== defaultOptions[key]) {
// @ts-ignore
optionsToSave[key] = val
}
}

const state = JSON.stringify({
src,
ssr: ssrMode.value,
options: compilerOptions
options: optionsToSave
} as PersistedState)
localStorage.setItem('state', state)
window.location.hash = encodeURIComponent(state)
window.location.hash = btoa(unescape(encodeURIComponent(state)))
const res = compileCode(src)
if (res) {
output.setValue(res)
}
}

const editor = monaco.editor.create(document.getElementById('source')!, {
value: persistedState.src || `<div>Hello World!</div>`,
value: persistedState?.src || `<div>Hello World!</div>`,
language: 'html',
...sharedEditorOptions,
wordWrap: 'bounded'
Expand Down
8 changes: 6 additions & 2 deletions packages/template-explorer/src/options.ts
Expand Up @@ -4,7 +4,7 @@ import { BindingTypes } from '@vue/compiler-core'

export const ssrMode = ref(false)

export const compilerOptions: CompilerOptions = reactive({
export const defaultOptions: CompilerOptions = {
mode: 'module',
filename: 'Foo.vue',
prefixIdentifiers: false,
Expand All @@ -24,7 +24,11 @@ export const compilerOptions: CompilerOptions = reactive({
setupProp: BindingTypes.PROPS,
vMySetupDir: BindingTypes.SETUP_CONST
}
})
}

export const compilerOptions: CompilerOptions = reactive(
Object.assign({}, defaultOptions)
)

const App = {
setup() {
Expand Down

0 comments on commit eb721d4

Please sign in to comment.