Skip to content

Commit 389d255

Browse files
committed
refactor: use useTemplateRef
1 parent 220cfec commit 389d255

File tree

7 files changed

+26
-24
lines changed

7 files changed

+26
-24
lines changed

src/Repl.vue

Lines changed: 3 additions & 3 deletions
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 { type Store, useStore } from './store'
5-
import { computed, provide, ref, toRefs } from 'vue'
5+
import { computed, provide, toRefs, useTemplateRef } from 'vue'
66
import {
77
type EditorComponentType,
88
injectKeyPreviewRef,
@@ -63,7 +63,7 @@ if (!props.editor) {
6363
throw new Error('The "editor" prop is now required.')
6464
}
6565
66-
const outputRef = ref<InstanceType<typeof Output>>()
66+
const outputRef = useTemplateRef('output')
6767
6868
props.store.init()
6969
@@ -94,7 +94,7 @@ defineExpose({ reload })
9494
</template>
9595
<template #[outputSlotName]>
9696
<Output
97-
ref="outputRef"
97+
ref="output"
9898
:editor-component="editor"
9999
:show-compile-output="props.showCompileOutput"
100100
:ssr="!!props.ssr"

src/SplitPane.vue

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<script setup lang="ts">
2-
import { computed, inject, reactive, ref } from 'vue'
2+
import { computed, inject, reactive, useTemplateRef } from 'vue'
33
import { injectKeyPreviewRef, injectKeyProps } from './types'
44
55
const props = defineProps<{ layout?: 'horizontal' | 'vertical' }>()
66
const isVertical = computed(() => props.layout === 'vertical')
77
8-
const container = ref()
8+
const containerRef = useTemplateRef('container')
99
const previewRef = inject(injectKeyPreviewRef)!
1010
1111
// mobile only
@@ -36,11 +36,11 @@ function dragStart(e: MouseEvent) {
3636
}
3737
3838
function dragMove(e: MouseEvent) {
39-
if (state.dragging) {
39+
if (containerRef.value && state.dragging) {
4040
const position = isVertical.value ? e.pageY : e.pageX
4141
const totalSize = isVertical.value
42-
? container.value.offsetHeight
43-
: container.value.offsetWidth
42+
? containerRef.value.offsetHeight
43+
: containerRef.value.offsetWidth
4444
const dp = position - startPosition
4545
state.split = startSplit + +((dp / totalSize) * 100).toFixed(2)
4646

src/codemirror/CodeMirror.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<template>
2-
<div ref="el" class="editor" />
2+
<div ref="container" class="editor" />
33
</template>
44

55
<script setup lang="ts">
66
import type { ModeSpec, ModeSpecOptions } from 'codemirror'
7-
import { inject, onMounted, ref, watchEffect } from 'vue'
7+
import { inject, onMounted, useTemplateRef, watchEffect } from 'vue'
88
import { debounce } from '../utils'
99
import CodeMirror from './codemirror'
1010
import { injectKeyProps } from '../../src/types'
@@ -23,7 +23,7 @@ const props = withDefaults(defineProps<Props>(), {
2323
2424
const emit = defineEmits<(e: 'change', value: string) => void>()
2525
26-
const el = ref()
26+
const el = useTemplateRef('container')
2727
const { autoResize, autoSave } = inject(injectKeyProps)!
2828
2929
onMounted(() => {

src/editor/FileSelector.vue

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<script setup lang="ts">
22
import { injectKeyProps } from '../../src/types'
33
import { importMapFile, stripSrcPrefix, tsconfigFile } from '../store'
4-
import { type VNode, computed, inject, ref } from 'vue'
4+
import { type VNode, computed, inject, ref, useTemplateRef } from 'vue'
55
66
const { store, showTsConfig, showImportMap } = inject(injectKeyProps)!
77
@@ -93,10 +93,10 @@ function editFileName(file: string) {
9393
pending.value = file
9494
}
9595
96-
const fileSel = ref(null)
96+
const fileSelector = useTemplateRef('fileSelector')
9797
function horizontalScroll(e: WheelEvent) {
9898
e.preventDefault()
99-
const el = fileSel.value! as HTMLElement
99+
const el = fileSelector.value!
100100
const direction =
101101
Math.abs(e.deltaX) >= Math.abs(e.deltaY) ? e.deltaX : e.deltaY
102102
const distance = 30 * (direction > 0 ? 1 : -1)
@@ -108,7 +108,7 @@ function horizontalScroll(e: WheelEvent) {
108108

109109
<template>
110110
<div
111-
ref="fileSel"
111+
ref="fileSelector"
112112
class="file-selector"
113113
:class="{ 'has-import-map': showImportMap }"
114114
@wheel="horizontalScroll"

src/monaco/Monaco.vue

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
onMounted,
88
ref,
99
shallowRef,
10+
useTemplateRef,
1011
watch,
1112
} from 'vue'
1213
import * as monaco from 'monaco-editor-core'
@@ -32,7 +33,7 @@ const emit = defineEmits<{
3233
(e: 'change', value: string): void
3334
}>()
3435
35-
const containerRef = ref<HTMLDivElement>()
36+
const containerRef = useTemplateRef('container')
3637
const ready = ref(false)
3738
const editor = shallowRef<monaco.editor.IStandaloneCodeEditor>()
3839
const {
@@ -172,7 +173,7 @@ onBeforeUnmount(() => {
172173
</script>
173174

174175
<template>
175-
<div ref="containerRef" class="editor" />
176+
<div ref="container" class="editor" />
176177
</template>
177178

178179
<style>

src/output/Output.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<script setup lang="ts">
22
import Preview from './Preview.vue'
3-
import { computed, inject, ref } from 'vue'
3+
import { computed, inject, useTemplateRef } from 'vue'
44
import {
55
type EditorComponentType,
66
type OutputModes,
@@ -14,7 +14,7 @@ const props = defineProps<{
1414
}>()
1515
1616
const { store } = inject(injectKeyProps)!
17-
const previewRef = ref<InstanceType<typeof Preview>>()
17+
const previewRef = useTemplateRef('preview')
1818
const modes = computed(() =>
1919
props.showCompileOutput
2020
? (['preview', 'js', 'css', 'ssr'] as const)
@@ -53,7 +53,7 @@ defineExpose({ reload, previewRef })
5353
</div>
5454

5555
<div class="output-container">
56-
<Preview ref="previewRef" :show="mode === 'preview'" :ssr="ssr" />
56+
<Preview ref="preview" :show="mode === 'preview'" :ssr="ssr" />
5757
<props.editorComponent
5858
v-if="mode !== 'preview'"
5959
readonly

src/output/Preview.vue

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
onMounted,
77
onUnmounted,
88
ref,
9+
useTemplateRef,
910
watch,
1011
watchEffect,
1112
} from 'vue'
@@ -19,7 +20,7 @@ const props = defineProps<{ show: boolean; ssr: boolean }>()
1920
const { store, clearConsole, theme, previewTheme, previewOptions } =
2021
inject(injectKeyProps)!
2122
22-
const container = ref<HTMLDivElement>()
23+
const containerRef = useTemplateRef('container')
2324
const runtimeError = ref<string>()
2425
const runtimeWarning = ref<string>()
2526
@@ -68,7 +69,7 @@ function createSandbox() {
6869
// clear prev sandbox
6970
proxy.destroy()
7071
stopUpdateWatcher && stopUpdateWatcher()
71-
container.value?.removeChild(sandbox)
72+
containerRef.value?.removeChild(sandbox)
7273
}
7374
7475
sandbox = document.createElement('iframe')
@@ -101,7 +102,7 @@ function createSandbox() {
101102
previewOptions.value?.placeholderHTML || '',
102103
)
103104
sandbox.srcdoc = sandboxSrc
104-
container.value?.appendChild(sandbox)
105+
containerRef.value?.appendChild(sandbox)
105106
106107
proxy = new PreviewProxy(sandbox, {
107108
on_fetch_progress: (progress: any) => {
@@ -280,7 +281,7 @@ function reload() {
280281
sandbox.contentWindow?.location.reload()
281282
}
282283
283-
defineExpose({ reload, container })
284+
defineExpose({ reload, container: containerRef })
284285
</script>
285286

286287
<template>

0 commit comments

Comments
 (0)