@@ -14,18 +14,15 @@ import {
14
14
import LunaConsole from ' luna-console'
15
15
import srcdoc from ' ./srcdoc.html?raw'
16
16
import { PreviewProxy } from ' ./PreviewProxy'
17
- import SplitPane from ' ../SplitPane.vue'
18
17
import { compileModulesForPreview } from ' ./moduleCompiler'
19
18
import { injectKeyProps } from ' ../types'
20
-
21
19
import type { Store } from ' ../store'
22
20
23
21
export interface SandboxProps {
24
22
store: Store
25
23
show? : boolean
26
24
ssr? : boolean
27
25
clearConsole? : boolean
28
- showConsole? : boolean
29
26
theme? : ' dark' | ' light'
30
27
previewOptions? : {
31
28
headHTML? : string
@@ -40,13 +37,13 @@ export interface SandboxProps {
40
37
}
41
38
/** @default true */
42
39
autoStoreInit? : boolean
40
+ lunaConsole? : LunaConsole
43
41
}
44
42
45
43
const props = withDefaults (defineProps <SandboxProps >(), {
46
44
show: true ,
47
45
ssr: false ,
48
46
theme: ' light' ,
49
- showConsole: false ,
50
47
clearConsole: true ,
51
48
previewOptions : () => ({}),
52
49
autoStoreInit: true ,
@@ -59,26 +56,15 @@ if (keyProps === undefined && props.autoStoreInit) {
59
56
}
60
57
61
58
const containerRef = useTemplateRef (' container' )
62
- const consoleContainerRef = useTemplateRef (' console-container' )
63
59
const runtimeError = ref <string >()
64
60
const runtimeWarning = ref <string >()
65
61
66
62
let sandbox: HTMLIFrameElement
67
- let lunaConsole: LunaConsole
68
63
let proxy: PreviewProxy
69
64
let stopUpdateWatcher: WatchStopHandle | undefined
70
65
71
66
// 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 )
82
68
83
69
// reset sandbox when import map changes
84
70
watch (
@@ -173,33 +159,34 @@ function createSandbox() {
173
159
runtimeError .value = ' Uncaught (in promise): ' + error .message
174
160
},
175
161
on_console : (log : any ) => {
162
+ const lc = props .lunaConsole
176
163
if (log .level === ' error' ) {
177
164
if (log .args [0 ] instanceof Error ) {
178
165
runtimeError .value = log .args [0 ].message
179
166
} else {
180
167
runtimeError .value = log .args [0 ]
181
168
}
182
- lunaConsole .error (... log .args )
169
+ lc ? .error (... log .args )
183
170
} else if (log .level === ' warn' ) {
184
171
if (log .args [0 ].toString ().includes (' [Vue warn]' )) {
185
172
runtimeWarning .value = log .args
186
173
.join (' ' )
187
174
.replace (/ \[ Vue warn\] :/ , ' ' )
188
175
.trim ()
189
176
}
190
- lunaConsole .warn (... log .args )
177
+ lc ? .warn (... log .args )
191
178
} else {
192
- lunaConsole .log (... log .args )
179
+ lc ? .log (... log .args )
193
180
}
194
181
},
195
182
on_console_group : (action : any ) => {
196
- lunaConsole .group (action .label )
183
+ props . lunaConsole ? .group (action .label )
197
184
},
198
185
on_console_group_end : () => {
199
- lunaConsole .groupEnd ()
186
+ props . lunaConsole ? .groupEnd ()
200
187
},
201
188
on_console_group_collapsed : (action : any ) => {
202
- lunaConsole .groupCollapsed (action .label )
189
+ props . lunaConsole ? .groupCollapsed (action .label )
203
190
},
204
191
})
205
192
@@ -318,38 +305,18 @@ async function updatePreview() {
318
305
}
319
306
}
320
307
321
- function clearLunaConsole() {
322
- lunaConsole ?.clear (true )
323
- }
324
-
325
308
/**
326
309
* Reload the preview iframe
327
310
*/
328
311
function reload() {
329
312
sandbox .contentWindow ?.location .reload ()
330
- clearLunaConsole ()
331
313
}
332
314
333
315
defineExpose ({ reload , container: containerRef })
334
316
</script >
335
317
336
318
<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" />
353
320
<Message :err =" (previewOptions?.showRuntimeError ?? true) && runtimeError" />
354
321
<Message
355
322
v-if =" !runtimeError && (previewOptions?.showRuntimeWarning ?? true)"
@@ -368,23 +335,4 @@ defineExpose({ reload, container: containerRef })
368
335
.iframe-container.dark :deep(iframe ) {
369
336
background-color : #1e1e1e ;
370
337
}
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
- }
390
338
</style >
0 commit comments