Skip to content

Commit 12ebcea

Browse files
feat: add reload function (#103)
1 parent d29d1de commit 12ebcea

File tree

3 files changed

+29
-2
lines changed

3 files changed

+29
-2
lines changed

src/Repl.vue

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import SplitPane from './SplitPane.vue'
33
import Output from './output/Output.vue'
44
import { Store, ReplStore, SFCOptions } from './store'
5-
import { provide, toRef } from 'vue'
5+
import { provide, ref, toRef } from 'vue'
66
import type { EditorComponentType } from './editor/types'
77
import EditorContainer from './editor/EditorContainer.vue'
88
@@ -49,6 +49,7 @@ if (!props.editor) {
4949
throw new Error('The "editor" prop is now required.')
5050
}
5151
52+
const outputRef = ref<InstanceType<typeof Output>>()
5253
const { store } = props
5354
const sfcOptions = (store.options = props.sfcOptions || {})
5455
if (!sfcOptions.script) {
@@ -74,6 +75,15 @@ provide('import-map', toRef(props, 'showImportMap'))
7475
provide('tsconfig', toRef(props, 'showTsConfig'))
7576
provide('clear-console', toRef(props, 'clearConsole'))
7677
provide('preview-options', props.previewOptions)
78+
79+
/**
80+
* Reload the preview iframe
81+
*/
82+
function reload() {
83+
outputRef.value?.reload()
84+
}
85+
86+
defineExpose({ reload })
7787
</script>
7888

7989
<template>
@@ -84,6 +94,7 @@ provide('preview-options', props.previewOptions)
8494
</template>
8595
<template #right>
8696
<Output
97+
ref="outputRef"
8798
:editorComponent="editor"
8899
:showCompileOutput="props.showCompileOutput"
89100
:ssr="!!props.ssr"

src/output/Output.vue

+8-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const props = defineProps<{
1212
}>()
1313
1414
const store = inject('store') as Store
15+
const previewRef = ref<InstanceType<typeof Preview>>()
1516
const modes = computed(() =>
1617
props.showCompileOutput
1718
? (['preview', 'js', 'css', 'ssr'] as const)
@@ -23,6 +24,12 @@ const mode = ref<OutputModes>(
2324
? (store.initialOutputMode as OutputModes)
2425
: 'preview'
2526
)
27+
28+
function reload() {
29+
previewRef.value?.reload()
30+
}
31+
32+
defineExpose({ reload })
2633
</script>
2734

2835
<template>
@@ -37,7 +44,7 @@ const mode = ref<OutputModes>(
3744
</div>
3845

3946
<div class="output-container">
40-
<Preview :show="mode === 'preview'" :ssr="ssr" />
47+
<Preview ref="previewRef" :show="mode === 'preview'" :ssr="ssr" />
4148
<props.editorComponent
4249
v-if="mode !== 'preview'"
4350
readonly

src/output/Preview.vue

+9
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,15 @@ async function updatePreview() {
259259
runtimeError.value = (e as Error).message
260260
}
261261
}
262+
263+
/**
264+
* Reload the preview iframe
265+
*/
266+
function reload() {
267+
sandbox.contentWindow?.location.reload()
268+
}
269+
270+
defineExpose({ reload })
262271
</script>
263272

264273
<template>

0 commit comments

Comments
 (0)