@@ -11,12 +11,11 @@ import {
1111 watch ,
1212 watchEffect ,
1313} from ' vue'
14- import LunaConsole from ' luna-console'
1514import srcdoc from ' ./srcdoc.html?raw'
1615import { PreviewProxy } from ' ./PreviewProxy'
1716import { compileModulesForPreview } from ' ./moduleCompiler'
1817import type { Store } from ' ../store'
19- import { injectKeyProps } from ' ../types'
18+ import { injectKeyProps , type LogLevel , type SandboxEmits } from ' ../types'
2019export interface SandboxProps {
2120 store: Store
2221 show? : boolean
@@ -36,7 +35,6 @@ export interface SandboxProps {
3635 }
3736 /** @default true */
3837 autoStoreInit? : boolean
39- lunaConsole? : LunaConsole
4038}
4139
4240const props = withDefaults (defineProps <SandboxProps >(), {
@@ -47,6 +45,9 @@ const props = withDefaults(defineProps<SandboxProps>(), {
4745 previewOptions : () => ({}),
4846 autoStoreInit: true ,
4947})
48+
49+ const emit = defineEmits <SandboxEmits >()
50+
5051const { store, theme, clearConsole, previewOptions } = toRefs (props )
5152
5253const keyProps = inject (injectKeyProps )
@@ -131,7 +132,8 @@ function createSandbox() {
131132 )
132133 sandbox .srcdoc = sandboxSrc
133134 containerRef .value ?.appendChild (sandbox )
134-
135+ const doLog = (logLevel : LogLevel , data ? : any ) =>
136+ emit (' log' , { logLevel , data })
135137 proxy = new PreviewProxy (sandbox , {
136138 on_fetch_progress : (progress : any ) => {
137139 // pending_imports = progress;
@@ -158,34 +160,34 @@ function createSandbox() {
158160 runtimeError .value = ' Uncaught (in promise): ' + error .message
159161 },
160162 on_console : (log : any ) => {
161- const lc = props . lunaConsole
163+ const maybeMsg = log . args [ 0 ]
162164 if (log .level === ' error' ) {
163- if (log . args [ 0 ] instanceof Error ) {
164- runtimeError .value = log . args [ 0 ] .message
165- } else {
166- runtimeError .value = log . args [ 0 ]
165+ if (maybeMsg instanceof Error ) {
166+ runtimeError .value = maybeMsg .message
167+ } else if ( ! maybeMsg . includes ( ' %c Cannot clone the message ' )) {
168+ runtimeError .value = maybeMsg
167169 }
168- lc ?. error ( ... log .args )
170+ doLog ( ' warn ' , log .args )
169171 } else if (log .level === ' warn' ) {
170- if (log . args [ 0 ] .toString ().includes (' [Vue warn]' )) {
172+ if (maybeMsg .toString ().includes (' [Vue warn]' )) {
171173 runtimeWarning .value = log .args
172174 .join (' ' )
173175 .replace (/ \[ Vue warn\] :/ , ' ' )
174176 .trim ()
175177 }
176- lc ?. warn ( ... log .args )
178+ doLog ( ' warn ' , log .args )
177179 } else {
178- lc ?. log ( ... log .args )
180+ doLog ( log . level || ' log ' , log .args )
179181 }
180182 },
181183 on_console_group : (action : any ) => {
182- props . lunaConsole ?. group ( action .label )
184+ doLog ( ' group ' , action .label )
183185 },
184186 on_console_group_end : () => {
185- props . lunaConsole ?. groupEnd ( )
187+ doLog ( ' groupEnd ' )
186188 },
187189 on_console_group_collapsed : (action : any ) => {
188- props . lunaConsole ?. groupCollapsed ( action .label )
190+ doLog ( ' groupCollapsed ' , action .label )
189191 },
190192 })
191193
0 commit comments