Skip to content

feat: add reload function #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Repl.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import SplitPane from './SplitPane.vue'
import Output from './output/Output.vue'
import { Store, ReplStore, SFCOptions } from './store'
import { provide, toRef } from 'vue'
import { provide, ref, toRef } from 'vue'
import type { EditorComponentType } from './editor/types'
import EditorContainer from './editor/EditorContainer.vue'

Expand Down Expand Up @@ -47,6 +47,7 @@ if (!props.editor) {
throw new Error('The "editor" prop is now required.')
}

const outputRef = ref<InstanceType<typeof Output>>()
const { store } = props
const sfcOptions = (store.options = props.sfcOptions || {})
if (!sfcOptions.script) {
Expand All @@ -71,6 +72,15 @@ provide('autoresize', props.autoResize)
provide('import-map', toRef(props, 'showImportMap'))
provide('clear-console', toRef(props, 'clearConsole'))
provide('preview-options', props.previewOptions)

/**
* Reload the preview iframe
*/
function reload() {
outputRef.value?.reload()
}

defineExpose({ reload })
</script>

<template>
Expand All @@ -81,6 +91,7 @@ provide('preview-options', props.previewOptions)
</template>
<template #right>
<Output
ref="outputRef"
:editorComponent="editor"
:showCompileOutput="props.showCompileOutput"
:ssr="!!props.ssr"
Expand Down
9 changes: 8 additions & 1 deletion src/output/Output.vue
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const props = defineProps<{
}>()

const store = inject('store') as Store
const previewRef = ref<InstanceType<typeof Preview>>()
const modes = computed(() =>
props.showCompileOutput
? (['preview', 'js', 'css', 'ssr'] as const)
Expand All @@ -23,6 +24,12 @@ const mode = ref<OutputModes>(
? (store.initialOutputMode as OutputModes)
: 'preview'
)

function reload() {
previewRef.value?.reload()
}

defineExpose({ reload })
</script>

<template>
Expand All @@ -37,7 +44,7 @@ const mode = ref<OutputModes>(
</div>

<div class="output-container">
<Preview :show="mode === 'preview'" :ssr="ssr" />
<Preview ref="previewRef" :show="mode === 'preview'" :ssr="ssr" />
<props.editorComponent
v-if="mode !== 'preview'"
readonly
Expand Down
9 changes: 9 additions & 0 deletions src/output/Preview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,15 @@ async function updatePreview() {
runtimeError.value = (e as Error).message
}
}

/**
* Reload the preview iframe
*/
function reload() {
sandbox.contentWindow?.location.reload()
}

defineExpose({ reload })
</script>

<template>
Expand Down