Skip to content

Commit

Permalink
feat: support for sandbox page customization (vuejs#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackman99 committed Jun 14, 2023
1 parent 29263d8 commit a22b969
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 9 deletions.
19 changes: 18 additions & 1 deletion src/Repl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ export interface Props {
sfcOptions?: SFCOptions
layout?: string
ssr?: boolean
previewOptions?: {
headHTML?: string
bodyHTML?: string
customCode?: {
importCode?: string
useCode?: string
}
}
}
const props = withDefaults(defineProps<Props>(), {
Expand All @@ -22,7 +30,15 @@ const props = withDefaults(defineProps<Props>(), {
showCompileOutput: true,
showImportMap: true,
clearConsole: true,
ssr: false
ssr: false,
previewOptions: () => ({
headHTML: '',
bodyHTML: '',
customCode: {
importCode: '',
useCode: '',
},
}),
})
const { store } = props
Expand All @@ -48,6 +64,7 @@ provide('store', store)
provide('autoresize', props.autoResize)
provide('import-map', toRef(props, 'showImportMap'))
provide('clear-console', toRef(props, 'clearConsole'))
provide('preview-options', props.previewOptions)
</script>

<template>
Expand Down
30 changes: 22 additions & 8 deletions src/output/Preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ import srcdoc from './srcdoc.html?raw'
import { PreviewProxy } from './PreviewProxy'
import { compileModulesForPreview } from './moduleCompiler'
import { Store } from '../store'
import { Props } from '../Repl.vue'
const props = defineProps<{ show: boolean; ssr: boolean }>()
const store = inject('store') as Store
const clearConsole = inject('clear-console') as Ref<boolean>
const previewOptions = inject('preview-options') as Props['previewOptions']
const container = ref()
const runtimeError = ref()
const runtimeWarning = ref()
Expand Down Expand Up @@ -85,10 +89,12 @@ function createSandbox() {
if (!importMap.imports.vue) {
importMap.imports.vue = store.state.vueRuntimeURL
}
const sandboxSrc = srcdoc.replace(
/<!--IMPORT_MAP-->/,
JSON.stringify(importMap)
)
const sandboxSrc = srcdoc
.replace(/<!--IMPORT_MAP-->/, JSON.stringify(importMap))
.replace(
/<!-- PREVIEW-OPTIONS-HEAD-HTML -->/,
previewOptions?.headHTML || ''
)
sandbox.srcdoc = sandboxSrc
container.value.appendChild(sandbox)
Expand Down Expand Up @@ -196,7 +202,9 @@ async function updatePreview() {
}
app.config.warnHandler = () => {}
window.__ssr_promise__ = _renderToString(app).then(html => {
document.body.innerHTML = '<div id="app">' + html + '</div>'
document.body.innerHTML = '<div id="app">' + html + '</div>' + \`${
previewOptions?.bodyHTML || ''
}\`
}).catch(err => {
console.error("SSR Error", err)
})
Expand All @@ -213,9 +221,13 @@ async function updatePreview() {
)
const codeToEval = [
`window.__modules__ = {}\nwindow.__css__ = ''\n` +
`if (window.__app__) window.__app__.unmount()\n` +
(isSSR ? `` : `document.body.innerHTML = '<div id="app"></div>'`),
`window.__modules__ = {};window.__css__ = '';` +
`if (window.__app__) window.__app__.unmount();` +
(isSSR
? ``
: `document.body.innerHTML = '<div id="app"></div>' + \`${
previewOptions?.bodyHTML || ''
}\``),
...modules,
`document.getElementById('__sfc-styles').innerHTML = window.__css__`
]
Expand All @@ -226,6 +238,7 @@ async function updatePreview() {
`import { ${
isSSR ? `createSSRApp` : `createApp`
} as _createApp } from "vue"
${previewOptions?.customCode?.importCode || ''}
const _mount = () => {
const AppComponent = __modules__["${mainFile}"].default
AppComponent.name = 'Repl'
Expand All @@ -234,6 +247,7 @@ async function updatePreview() {
app.config.unwrapInjectedRef = true
}
app.config.errorHandler = e => console.error(e)
${previewOptions?.customCode?.useCode || ''}
app.mount('#app')
}
if (window.__ssr_promise__) {
Expand Down
1 change: 1 addition & 0 deletions src/output/srcdoc.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
}
</style>
<!-- PREVIEW-OPTIONS-HEAD-HTML -->
<style id="__sfc-styles"></style>
<script>
(() => {
Expand Down

0 comments on commit a22b969

Please sign in to comment.