@@ -11,12 +11,11 @@ import {
11
11
watch ,
12
12
watchEffect ,
13
13
} from ' vue'
14
- import LunaConsole from ' luna-console'
15
14
import srcdoc from ' ./srcdoc.html?raw'
16
15
import { PreviewProxy } from ' ./PreviewProxy'
17
16
import { compileModulesForPreview } from ' ./moduleCompiler'
18
17
import type { Store } from ' ../store'
19
- import { injectKeyProps } from ' ../types'
18
+ import { injectKeyProps , type LogLevel , type SandboxEmits } from ' ../types'
20
19
export interface SandboxProps {
21
20
store: Store
22
21
show? : boolean
@@ -36,7 +35,6 @@ export interface SandboxProps {
36
35
}
37
36
/** @default true */
38
37
autoStoreInit? : boolean
39
- lunaConsole? : LunaConsole
40
38
}
41
39
42
40
const props = withDefaults (defineProps <SandboxProps >(), {
@@ -47,6 +45,9 @@ const props = withDefaults(defineProps<SandboxProps>(), {
47
45
previewOptions : () => ({}),
48
46
autoStoreInit: true ,
49
47
})
48
+
49
+ const emit = defineEmits <SandboxEmits >()
50
+
50
51
const { store, theme, clearConsole, previewOptions } = toRefs (props )
51
52
52
53
const keyProps = inject (injectKeyProps )
@@ -131,7 +132,8 @@ function createSandbox() {
131
132
)
132
133
sandbox .srcdoc = sandboxSrc
133
134
containerRef .value ?.appendChild (sandbox )
134
-
135
+ const doLog = (logLevel : LogLevel , data ? : any ) =>
136
+ emit (' log' , { logLevel , data })
135
137
proxy = new PreviewProxy (sandbox , {
136
138
on_fetch_progress : (progress : any ) => {
137
139
// pending_imports = progress;
@@ -158,34 +160,34 @@ function createSandbox() {
158
160
runtimeError .value = ' Uncaught (in promise): ' + error .message
159
161
},
160
162
on_console : (log : any ) => {
161
- const lc = props . lunaConsole
163
+ const maybeMsg = log . args [ 0 ]
162
164
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
167
169
}
168
- lc ?. error ( ... log .args )
170
+ doLog ( ' warn ' , log .args )
169
171
} else if (log .level === ' warn' ) {
170
- if (log . args [ 0 ] .toString ().includes (' [Vue warn]' )) {
172
+ if (maybeMsg .toString ().includes (' [Vue warn]' )) {
171
173
runtimeWarning .value = log .args
172
174
.join (' ' )
173
175
.replace (/ \[ Vue warn\] :/ , ' ' )
174
176
.trim ()
175
177
}
176
- lc ?. warn ( ... log .args )
178
+ doLog ( ' warn ' , log .args )
177
179
} else {
178
- lc ?. log ( ... log .args )
180
+ doLog ( log . level || ' log ' , log .args )
179
181
}
180
182
},
181
183
on_console_group : (action : any ) => {
182
- props . lunaConsole ?. group ( action .label )
184
+ doLog ( ' group ' , action .label )
183
185
},
184
186
on_console_group_end : () => {
185
- props . lunaConsole ?. groupEnd ( )
187
+ doLog ( ' groupEnd ' )
186
188
},
187
189
on_console_group_collapsed : (action : any ) => {
188
- props . lunaConsole ?. groupCollapsed ( action .label )
190
+ doLog ( ' groupCollapsed ' , action .label )
189
191
},
190
192
})
191
193
0 commit comments