This commit is contained in:
2023-11-14 16:00:55 +08:00
parent dd23d4184d
commit 6481c0bc49
10 changed files with 73 additions and 30 deletions

View File

@@ -12,11 +12,13 @@ import { asyncRun } from "./py"
interface Props {
modelValue: string
readonly: boolean
lang?: "python" | "c"
}
const props = withDefaults(defineProps<Props>(), {
lang: "python",
readonly: false
})
const { isDark } = useData()
@@ -38,21 +40,26 @@ const styleTheme = EditorView.baseTheme({
})
const input = ref("")
const output = ref("")
const code = ref(props.modelValue)
const output = ref<string[]>([])
const error = ref("")
const code = ref(props.modelValue.trim())
async function run() {
const ev = await asyncRun(code.value, input.value)
output.value = ev.result
output.value = ev.results
error.value = ev.error
}
function reset() {
code.value = props.modelValue
code.value = props.modelValue.trim()
error.value = ""
output.value = []
}
</script>
<template>
<p>代码编辑区</p>
<Codemirror
:disabled="props.readonly"
v-model="code"
indentWithTab
:extensions="[styleTheme, lang, isDark ? oneDark : smoothy]"
@@ -65,12 +72,13 @@ function reset() {
:extensions="[styleTheme, isDark ? oneDark : smoothy]"
:tabSize="4"
/>
<p>结果</p>
<p>{{ output }}</p>
<div :class="$style.actions">
<VPButton :class="$style.run" @click="run" text="运行"></VPButton>
<VPButton @click="reset" theme="alt" text="重置"></VPButton>
</div>
<p v-if="output.length || error">运行结果</p>
<pre v-for="(it, index) in output" :key="index">{{ it }}</pre>
<pre>{{ error }}</pre>
</template>
<style module>
.actions {