@@ -14,18 +14,15 @@ import {
1414import LunaConsole from ' luna-console'
1515import srcdoc from ' ./srcdoc.html?raw'
1616import { PreviewProxy } from ' ./PreviewProxy'
17- import SplitPane from ' ../SplitPane.vue'
1817import { compileModulesForPreview } from ' ./moduleCompiler'
1918import { injectKeyProps } from ' ../types'
20-
2119import type { Store } from ' ../store'
2220
2321export interface SandboxProps {
2422 store: Store
2523 show? : boolean
2624 ssr? : boolean
2725 clearConsole? : boolean
28- showConsole? : boolean
2926 theme? : ' dark' | ' light'
3027 previewOptions? : {
3128 headHTML? : string
@@ -40,13 +37,13 @@ export interface SandboxProps {
4037 }
4138 /** @default true */
4239 autoStoreInit? : boolean
40+ lunaConsole? : LunaConsole
4341}
4442
4543const props = withDefaults (defineProps <SandboxProps >(), {
4644 show: true ,
4745 ssr: false ,
4846 theme: ' light' ,
49- showConsole: false ,
5047 clearConsole: true ,
5148 previewOptions : () => ({}),
5249 autoStoreInit: true ,
@@ -59,26 +56,15 @@ if (keyProps === undefined && props.autoStoreInit) {
5956}
6057
6158const containerRef = useTemplateRef (' container' )
62- const consoleContainerRef = useTemplateRef (' console-container' )
6359const runtimeError = ref <string >()
6460const runtimeWarning = ref <string >()
6561
6662let sandbox: HTMLIFrameElement
67- let lunaConsole: LunaConsole
6863let proxy: PreviewProxy
6964let stopUpdateWatcher: WatchStopHandle | undefined
7065
7166// create sandbox on mount
72- onMounted (() => {
73- createSandbox ()
74- if (! consoleContainerRef .value ) return
75- if (props .showConsole ) {
76- lunaConsole = new LunaConsole (consoleContainerRef .value , {
77- theme: keyProps ?.theme .value || ' light' ,
78- })
79- watch (() => store .value .activeFile .code , clearLunaConsole )
80- }
81- })
67+ onMounted (createSandbox )
8268
8369// reset sandbox when import map changes
8470watch (
@@ -173,33 +159,34 @@ function createSandbox() {
173159 runtimeError .value = ' Uncaught (in promise): ' + error .message
174160 },
175161 on_console : (log : any ) => {
162+ const lc = props .lunaConsole
176163 if (log .level === ' error' ) {
177164 if (log .args [0 ] instanceof Error ) {
178165 runtimeError .value = log .args [0 ].message
179166 } else {
180167 runtimeError .value = log .args [0 ]
181168 }
182- lunaConsole .error (... log .args )
169+ lc ? .error (... log .args )
183170 } else if (log .level === ' warn' ) {
184171 if (log .args [0 ].toString ().includes (' [Vue warn]' )) {
185172 runtimeWarning .value = log .args
186173 .join (' ' )
187174 .replace (/ \[ Vue warn\] :/ , ' ' )
188175 .trim ()
189176 }
190- lunaConsole .warn (... log .args )
177+ lc ? .warn (... log .args )
191178 } else {
192- lunaConsole .log (... log .args )
179+ lc ? .log (... log .args )
193180 }
194181 },
195182 on_console_group : (action : any ) => {
196- lunaConsole .group (action .label )
183+ props . lunaConsole ? .group (action .label )
197184 },
198185 on_console_group_end : () => {
199- lunaConsole .groupEnd ()
186+ props . lunaConsole ? .groupEnd ()
200187 },
201188 on_console_group_collapsed : (action : any ) => {
202- lunaConsole .groupCollapsed (action .label )
189+ props . lunaConsole ? .groupCollapsed (action .label )
203190 },
204191 })
205192
@@ -318,38 +305,18 @@ async function updatePreview() {
318305 }
319306}
320307
321- function clearLunaConsole() {
322- lunaConsole ?.clear (true )
323- }
324-
325308/**
326309 * Reload the preview iframe
327310 */
328311function reload() {
329312 sandbox .contentWindow ?.location .reload ()
330- clearLunaConsole ()
331313}
332314
333315defineExpose ({ reload , container: containerRef })
334316 </script >
335317
336318<template >
337- <SplitPane v-if =" show && showConsole" layout =" vertical" >
338- <template #left >
339- <div ref =" container" class =" iframe-container" :class =" theme" />
340- </template >
341- <template #right >
342- <div ref =" console-container" />
343- <button class =" clear-btn" @click =" clearLunaConsole" >clear</button >
344- </template >
345- </SplitPane >
346- <div
347- v-if =" !showConsole"
348- v-show =" props.show"
349- ref =" container"
350- class =" iframe-container"
351- :class =" theme"
352- />
319+ <div v-show =" show" ref =" container" class =" iframe-container" :class =" theme" />
353320 <Message :err =" (previewOptions?.showRuntimeError ?? true) && runtimeError" />
354321 <Message
355322 v-if =" !runtimeError && (previewOptions?.showRuntimeWarning ?? true)"
@@ -368,23 +335,4 @@ defineExpose({ reload, container: containerRef })
368335.iframe-container.dark :deep(iframe ) {
369336 background-color : #1e1e1e ;
370337}
371- .luna-console-theme-dark {
372- background-color : var (--bg ) !important ;
373- }
374- .clear-btn {
375- position : absolute ;
376- font-size : 18px ;
377- font-family : var (--font-code );
378- color : #999 ;
379- top : 10px ;
380- right : 10px ;
381- z-index : 99 ;
382- padding : 8px 10px 6px ;
383- background-color : var (--bg );
384- border-radius : 4px ;
385- border : 1px solid var (--border );
386- &:hover {
387- color : var (--color-branding );
388- }
389- }
390338 </style >
0 commit comments