Skip to content

Commit bbf1943

Browse files
committed
improve console
1 parent c3f3fba commit bbf1943

File tree

5 files changed

+117
-71
lines changed

5 files changed

+117
-71
lines changed

pnpm-lock.yaml

Lines changed: 38 additions & 35 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Repl.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export interface Props {
2525
showCompileOutput?: boolean
2626
showImportMap?: boolean
2727
showTsConfig?: boolean
28+
showConsole?: boolean
2829
clearConsole?: boolean
2930
layout?: 'horizontal' | 'vertical'
3031
layoutReverse?: boolean
@@ -132,8 +133,9 @@ defineExpose({ reload })
132133
margin: 0;
133134
overflow: hidden;
134135
font-size: 13px;
135-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen,
136-
Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
136+
font-family:
137+
-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu,
138+
Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
137139
background-color: var(--bg-soft);
138140
}
139141

src/output/Preview.vue

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,14 @@ import Sandbox from './Sandbox.vue'
55
66
const props = defineProps<{ show: boolean; ssr: boolean }>()
77
8-
const { store, clearConsole, theme, previewTheme, previewOptions } =
9-
inject(injectKeyProps)!
8+
const {
9+
store,
10+
clearConsole,
11+
showConsole,
12+
theme,
13+
previewTheme,
14+
previewOptions,
15+
} = inject(injectKeyProps)!
1016
1117
const sandboxTheme = computed(() =>
1218
previewTheme.value ? theme.value : undefined,
@@ -29,6 +35,7 @@ defineExpose({
2935
:theme="sandboxTheme"
3036
:preview-options="previewOptions"
3137
:ssr="props.ssr"
38+
:show-console="showConsole"
3239
:clear-console="clearConsole"
3340
/>
3441
</template>

src/output/Sandbox.vue

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,21 @@ import {
1111
watch,
1212
watchEffect,
1313
} from 'vue'
14+
import LunaConsole from 'luna-console'
1415
import srcdoc from './srcdoc.html?raw'
1516
import { PreviewProxy } from './PreviewProxy'
17+
import SplitPane from '../SplitPane.vue'
1618
import { compileModulesForPreview } from './moduleCompiler'
17-
import type { Store } from '../store'
1819
import { injectKeyProps } from '../types'
1920
21+
import type { Store } from '../store'
22+
2023
export interface SandboxProps {
2124
store: Store
2225
show?: boolean
2326
ssr?: boolean
2427
clearConsole?: boolean
28+
showConsole?: boolean
2529
theme?: 'dark' | 'light'
2630
previewOptions?: {
2731
headHTML?: string
@@ -42,6 +46,7 @@ const props = withDefaults(defineProps<SandboxProps>(), {
4246
show: true,
4347
ssr: false,
4448
theme: 'light',
49+
showConsole: false,
4550
clearConsole: true,
4651
previewOptions: () => ({}),
4752
autoStoreInit: true,
@@ -54,15 +59,26 @@ if (keyProps === undefined && props.autoStoreInit) {
5459
}
5560
5661
const containerRef = useTemplateRef('container')
62+
const consoleContainerRef = useTemplateRef('console-container')
5763
const runtimeError = ref<string>()
5864
const runtimeWarning = ref<string>()
5965
6066
let sandbox: HTMLIFrameElement
67+
let lunaConsole: LunaConsole
6168
let proxy: PreviewProxy
6269
let stopUpdateWatcher: WatchStopHandle | undefined
6370
6471
// create sandbox on mount
65-
onMounted(createSandbox)
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+
})
6682
6783
// reset sandbox when import map changes
6884
watch(
@@ -157,32 +173,33 @@ function createSandbox() {
157173
runtimeError.value = 'Uncaught (in promise): ' + error.message
158174
},
159175
on_console: (log: any) => {
160-
if (log.duplicate) {
161-
return
162-
}
163176
if (log.level === 'error') {
164177
if (log.args[0] instanceof Error) {
165178
runtimeError.value = log.args[0].message
166179
} else {
167180
runtimeError.value = log.args[0]
168181
}
182+
lunaConsole.error(...log.args)
169183
} else if (log.level === 'warn') {
170184
if (log.args[0].toString().includes('[Vue warn]')) {
171185
runtimeWarning.value = log.args
172186
.join('')
173187
.replace(/\[Vue warn\]:/, '')
174188
.trim()
175189
}
190+
lunaConsole.warn(...log.args)
191+
} else {
192+
lunaConsole.log(...log.args)
176193
}
177194
},
178195
on_console_group: (action: any) => {
179-
// group_logs(action.label, false);
196+
lunaConsole.group(action.label)
180197
},
181198
on_console_group_end: () => {
182-
// ungroup_logs();
199+
lunaConsole.groupEnd()
183200
},
184201
on_console_group_collapsed: (action: any) => {
185-
// group_logs(action.label, true);
202+
lunaConsole.groupCollapsed(action.label)
186203
},
187204
})
188205
@@ -301,18 +318,33 @@ async function updatePreview() {
301318
}
302319
}
303320
321+
function clearLunaConsole() {
322+
lunaConsole?.clear(true)
323+
}
324+
304325
/**
305326
* Reload the preview iframe
306327
*/
307328
function reload() {
308329
sandbox.contentWindow?.location.reload()
330+
clearLunaConsole()
309331
}
310332
311333
defineExpose({ reload, container: containerRef })
312334
</script>
313335

314336
<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>
315346
<div
347+
v-if="!showConsole"
316348
v-show="props.show"
317349
ref="container"
318350
class="iframe-container"
@@ -336,4 +368,23 @@ defineExpose({ reload, container: containerRef })
336368
.iframe-container.dark :deep(iframe) {
337369
background-color: #1e1e1e;
338370
}
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+
}
339390
</style>

src/output/srcdoc.html

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
color-scheme: dark;
77
}
88
body {
9-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto,
10-
Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
9+
font-family:
10+
-apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu,
11+
Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
1112
}
1213
</style>
1314
<!-- PREVIEW-OPTIONS-HEAD-HTML -->
@@ -152,9 +153,6 @@
152153
)
153154
}
154155
})
155-
156-
let previous = { level: null, args: null }
157-
158156
;['clear', 'log', 'info', 'dir', 'warn', 'error', 'table'].forEach(
159157
(level) => {
160158
const original = console[level]
@@ -170,28 +168,13 @@
170168
}
171169

172170
original(...args)
173-
174-
const stringifiedArgs = stringify(args)
175-
if (
176-
previous.level === level &&
177-
previous.args &&
178-
previous.args === stringifiedArgs
179-
) {
171+
try {
172+
parent.postMessage({ action: 'console', level, args }, '*')
173+
} catch (err) {
180174
parent.postMessage(
181-
{ action: 'console', level, duplicate: true },
175+
{ action: 'console', level, args: args.map(toString) },
182176
'*',
183177
)
184-
} else {
185-
previous = { level, args: stringifiedArgs }
186-
187-
try {
188-
parent.postMessage({ action: 'console', level, args }, '*')
189-
} catch (err) {
190-
parent.postMessage(
191-
{ action: 'console', level, args: args.map(toString) },
192-
'*',
193-
)
194-
}
195178
}
196179
}
197180
},

0 commit comments

Comments
 (0)