把自测猫搬进来了
This commit is contained in:
117
src/oj/problem/components/EditorWithTest.vue
Normal file
117
src/oj/problem/components/EditorWithTest.vue
Normal file
@@ -0,0 +1,117 @@
|
||||
<script lang="ts" setup>
|
||||
import { SOURCES } from "utils/constants"
|
||||
import { code, input, output } from "oj/composables/code"
|
||||
import { isDesktop } from "~/shared/composables/breakpoints"
|
||||
import { problem } from "oj/composables/problem"
|
||||
import storage from "~/utils/storage"
|
||||
import Form from "./Form.vue"
|
||||
import CodeEditor from "~/shared/components/CodeEditor.vue"
|
||||
|
||||
const route = useRoute()
|
||||
const contestID = !!route.params.contestID ? route.params.contestID : null
|
||||
|
||||
const storageKey = computed(
|
||||
() =>
|
||||
`problem_${problem.value!._id}_contest_${contestID}_lang_${code.language}`,
|
||||
)
|
||||
|
||||
onMounted(() => {
|
||||
if (storage.get(storageKey.value)) {
|
||||
code.value = storage.get(storageKey.value)
|
||||
} else {
|
||||
code.value =
|
||||
problem.value!.template[code.language] || SOURCES[code.language]
|
||||
}
|
||||
})
|
||||
|
||||
const editorHeight = computed(() =>
|
||||
isDesktop.value ? "calc(100vh - 133px)" : "calc(100vh - 172px)",
|
||||
)
|
||||
|
||||
function changeCode(v: string) {
|
||||
storage.set(storageKey.value, v)
|
||||
}
|
||||
|
||||
function changeLanguage(v: string) {
|
||||
if (
|
||||
storage.get(storageKey.value) &&
|
||||
storageKey.value.split("_").pop() === v
|
||||
) {
|
||||
code.value = storage.get(storageKey.value)
|
||||
} else {
|
||||
code.value =
|
||||
problem.value!.template[code.language] || SOURCES[code.language]
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<n-flex vertical>
|
||||
<Form
|
||||
:storage-key="storageKey"
|
||||
@change-language="changeLanguage"
|
||||
with-test
|
||||
/>
|
||||
<n-split direction="horizontal" :min="1 / 3" :max="4 / 5">
|
||||
<template #1>
|
||||
<CodeEditor
|
||||
v-model:value="code.value"
|
||||
@update:model-value="changeCode"
|
||||
:language="code.language"
|
||||
:height="editorHeight"
|
||||
/>
|
||||
</template>
|
||||
<template #2>
|
||||
<n-split
|
||||
direction="vertical"
|
||||
:default-size="1 / 3"
|
||||
:min="1 / 5"
|
||||
:max="3 / 5"
|
||||
>
|
||||
<template #1>
|
||||
<div class="title">输入框</div>
|
||||
<n-input
|
||||
v-model:value="input"
|
||||
type="textarea"
|
||||
:bordered="false"
|
||||
:resizable="false"
|
||||
class="box"
|
||||
/>
|
||||
</template>
|
||||
<template #2>
|
||||
<div class="title">输出框</div>
|
||||
<n-input
|
||||
class="box output"
|
||||
v-model:value="output"
|
||||
placeholder=""
|
||||
type="textarea"
|
||||
:bordered="false"
|
||||
:resizable="false"
|
||||
readonly
|
||||
/>
|
||||
</template>
|
||||
</n-split>
|
||||
</template>
|
||||
</n-split>
|
||||
</n-flex>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.title {
|
||||
height: 40px;
|
||||
line-height: 40px;
|
||||
padding-left: 20px;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
.box {
|
||||
padding-left: 10px;
|
||||
box-sizing: border-box;
|
||||
height: calc(100% - 40px);
|
||||
font-size: 20px;
|
||||
}
|
||||
|
||||
.output {
|
||||
font-family: "Monaco";
|
||||
}
|
||||
</style>
|
||||
@@ -1,7 +1,7 @@
|
||||
<script setup lang="ts">
|
||||
import copy from "copy-text-to-clipboard"
|
||||
import copyText from "copy-text-to-clipboard"
|
||||
import { LANGUAGE_SHOW_VALUE, SOURCES } from "utils/constants"
|
||||
import { code } from "oj/composables/code"
|
||||
import { code, input, output } from "oj/composables/code"
|
||||
import { problem } from "oj/composables/problem"
|
||||
import { isDesktop, isMobile } from "~/shared/composables/breakpoints"
|
||||
import { useUserStore } from "~/shared/store/user"
|
||||
@@ -9,15 +9,29 @@ import Submit from "./Submit.vue"
|
||||
import storage from "~/utils/storage"
|
||||
import { STORAGE_KEY } from "utils/constants"
|
||||
import { LANGUAGE } from "~/utils/types"
|
||||
import { createTestSubmission } from "~/utils/judge"
|
||||
|
||||
interface Props {
|
||||
storageKey: string
|
||||
withTest?: boolean
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
withTest: false,
|
||||
})
|
||||
|
||||
const message = useMessage()
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
const userStore = useUserStore()
|
||||
|
||||
const props = defineProps<{ storageKey: string }>()
|
||||
const emit = defineEmits(["changeLanguage"])
|
||||
|
||||
function copy() {
|
||||
copyText(code.value)
|
||||
message.success("代码复制成功")
|
||||
}
|
||||
|
||||
function reset() {
|
||||
code.value = problem.value!.template[code.language] || SOURCES[code.language]
|
||||
storage.remove(props.storageKey)
|
||||
@@ -34,6 +48,11 @@ function goEdit() {
|
||||
window.open(data.href, "_blank")
|
||||
}
|
||||
|
||||
async function test() {
|
||||
const res = await createTestSubmission(code, input.value)
|
||||
output.value = res.output
|
||||
}
|
||||
|
||||
const menu = computed<DropdownOption[]>(() => [
|
||||
{ label: "提交信息", key: "submissions", show: isMobile.value },
|
||||
{ label: "自测猫", key: "test", show: isMobile.value },
|
||||
@@ -66,8 +85,7 @@ async function select(key: string) {
|
||||
reset()
|
||||
break
|
||||
case "copy":
|
||||
copy(code.value)
|
||||
message.success("代码复制成功")
|
||||
copy()
|
||||
break
|
||||
case "test":
|
||||
window.open("https://code.xuyue.cc", "_blank")
|
||||
@@ -95,19 +113,23 @@ function gotoTestCat() {
|
||||
:size="isDesktop ? 'medium' : 'small'"
|
||||
:options="options"
|
||||
/>
|
||||
<Submit />
|
||||
<n-button v-if="isDesktop" @click="gotoTestCat">自测猫</n-button>
|
||||
<n-button v-if="isDesktop" @click="goSubmissions">提交信息</n-button>
|
||||
<n-dropdown :options="menu" @select="select">
|
||||
<n-button :size="isDesktop ? 'medium' : 'small'">操作</n-button>
|
||||
</n-dropdown>
|
||||
<n-button
|
||||
v-if="isDesktop && userStore.isSuperAdmin"
|
||||
type="warning"
|
||||
@click="goEdit"
|
||||
>
|
||||
编辑
|
||||
</n-button>
|
||||
<n-button v-if="withTest" @click="reset">重置代码</n-button>
|
||||
<n-button v-if="withTest" type="primary" secondary @click="test">运行代码</n-button>
|
||||
<n-flex align="center" v-if="!withTest">
|
||||
<Submit />
|
||||
<n-button v-if="isDesktop" @click="gotoTestCat">自测猫</n-button>
|
||||
<n-button v-if="isDesktop" @click="goSubmissions">提交信息</n-button>
|
||||
<n-dropdown :options="menu" @select="select">
|
||||
<n-button :size="isDesktop ? 'medium' : 'small'">操作</n-button>
|
||||
</n-dropdown>
|
||||
<n-button
|
||||
v-if="isDesktop && userStore.isSuperAdmin"
|
||||
type="warning"
|
||||
@click="goEdit"
|
||||
>
|
||||
编辑
|
||||
</n-button>
|
||||
</n-flex>
|
||||
</n-flex>
|
||||
</template>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user