-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ViewEditor.vue
45 lines (39 loc) · 1.12 KB
/
ViewEditor.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<script setup lang="ts">
import type CodeMirror from 'codemirror'
import { useCodeError } from '../../composables/error'
import { client } from '~/composables/client'
import type { File } from '#types'
const props = defineProps<{
file?: File
}>()
const code = ref('')
watch(() => props.file,
async() => {
if (!props.file || !props.file?.filepath) {
code.value = ''
return
}
code.value = await client.rpc.readFile(props.file.filepath)
},
{ immediate: true },
)
const ext = computed(() => props.file?.filepath?.split(/\./g).pop() || 'js')
const editor = ref<any>()
const cm = computed<CodeMirror.EditorFromTextArea | undefined>(() => editor.value?.cm)
const failed = computed(() => props.file?.tasks.filter(i => i.result?.state === 'fail') || [])
const { hasBeenEdited } = useCodeError(props, cm, failed)
async function onSave(content: string) {
hasBeenEdited.value = true
await client.rpc.writeFile(props.file!.filepath, content)
}
</script>
<template>
<CodeMirror
ref="editor"
v-model="code"
h-full
v-bind="{ lineNumbers: true }"
:mode="ext"
@save="onSave"
/>
</template>