use composables.

This commit is contained in:
2023-04-17 11:24:35 +08:00
parent 46d3dcb171
commit 3ff7749395
8 changed files with 130 additions and 134 deletions

View File

@@ -0,0 +1,3 @@
import { Problem } from "~/utils/types"
export const problem = ref<Problem | null>(null)

View File

@@ -1,5 +1,5 @@
<script setup lang="ts"> <script setup lang="ts">
import { NButton, NIcon, NSpace } from "naive-ui" import { NButton, NIcon } from "naive-ui"
import { GoldMedal } from "@element-plus/icons-vue" import { GoldMedal } from "@element-plus/icons-vue"
import Pagination from "~/shared/Pagination.vue" import Pagination from "~/shared/Pagination.vue"
import AcAndSubmission from "../components/AcAndSubmission.vue" import AcAndSubmission from "../components/AcAndSubmission.vue"

View File

@@ -1,29 +1,23 @@
<script lang="ts" setup> <script lang="ts" setup>
import { SOURCES } from "utils/constants" import { SOURCES } from "utils/constants"
import { Problem } from "utils/types"
import { code } from "oj/composables/code" import { code } from "oj/composables/code"
import { isDesktop } from "~/shared/composables/breakpoints" import { isDesktop } from "~/shared/composables/breakpoints"
import Form from "./Form.vue" import { problem } from "oj/composables/problem"
const Form = defineAsyncComponent(() => import("./Form.vue"))
const CodeEditor = defineAsyncComponent(() => import("~/shared/CodeEditor.vue")) const CodeEditor = defineAsyncComponent(() => import("~/shared/CodeEditor.vue"))
interface Props { code.language = problem.value!.languages[0] || "C"
problem: Problem code.value = problem.value!.template[code.language] || SOURCES[code.language]
}
const props = defineProps<Props>()
code.language = props.problem.languages[0] || "C"
code.value = props.problem.template[code.language] || SOURCES[code.language]
const editorHeight = computed(() => const editorHeight = computed(() =>
isDesktop.value ? "calc(100vh - 133px)" : "calc(100vh - 180px)" isDesktop.value ? "calc(100vh - 133px)" : "calc(100vh - 172px)"
) )
</script> </script>
<template> <template>
<n-space vertical> <n-space vertical>
<Form :problem="props.problem" /> <Form />
<CodeEditor <CodeEditor
v-model="code.value" v-model="code.value"
:language="code.language" :language="code.language"

View File

@@ -1,16 +1,11 @@
<script setup lang="ts"> <script setup lang="ts">
import { LANGUAGE_SHOW_VALUE, SOURCES } from "utils/constants" import { LANGUAGE_SHOW_VALUE, SOURCES } from "utils/constants"
import { Problem } from "utils/types"
import { code } from "oj/composables/code" import { code } from "oj/composables/code"
import { problem } from "oj/composables/problem"
import { isDesktop, isMobile } from "~/shared/composables/breakpoints" import { isDesktop, isMobile } from "~/shared/composables/breakpoints"
import Submit from "./Submit.vue"
import { useUserStore } from "~/shared/store/user" import { useUserStore } from "~/shared/store/user"
import Submit from "./Submit.vue"
interface Props {
problem: Problem
}
const props = defineProps<Props>()
const route = useRoute() const route = useRoute()
const router = useRouter() const router = useRouter()
const userStore = useUserStore() const userStore = useUserStore()
@@ -18,12 +13,12 @@ const userStore = useUserStore()
watch(() => code.language, reset) watch(() => code.language, reset)
function reset() { function reset() {
code.value = props.problem.template[code.language] || SOURCES[code.language] code.value = problem.value!.template[code.language] || SOURCES[code.language]
} }
function goSubmissions() { function goSubmissions() {
const name = !!route.params.contestID ? "contest submissions" : "submissions" const name = !!route.params.contestID ? "contest submissions" : "submissions"
router.push({ name, query: { problem: props.problem._id } }) router.push({ name, query: { problem: problem.value!._id } })
} }
function goTestCat() { function goTestCat() {
@@ -32,17 +27,16 @@ function goTestCat() {
} }
function goEdit() { function goEdit() {
const data = router.resolve("/admin/problem/edit/" + props.problem.id) const data = router.resolve("/admin/problem/edit/" + problem.value!.id)
window.open(data.href, "_blank") window.open(data.href, "_blank")
} }
const menu: DropdownOption[] = [ const menu: DropdownOption[] = [
{ label: "重置", key: "reset" },
{ label: "提交信息", key: "submissions" }, { label: "提交信息", key: "submissions" },
{ label: "自测猫", key: "testcat" }, { label: "自测猫", key: "testcat" },
] ]
const options: DropdownOption[] = props.problem.languages.map((it) => ({ const options: DropdownOption[] = problem.value!.languages.map((it) => ({
label: () => [ label: () => [
h("img", { h("img", {
src: `/${it}.svg`, src: `/${it}.svg`,
@@ -60,9 +54,6 @@ const options: DropdownOption[] = props.problem.languages.map((it) => ({
function select(key: string) { function select(key: string) {
switch (key) { switch (key) {
case "reset":
reset()
break
case "submissions": case "submissions":
goSubmissions() goSubmissions()
break break
@@ -77,17 +68,21 @@ function select(key: string) {
<n-space> <n-space>
<n-select <n-select
class="language" class="language"
:size="isDesktop ? 'medium' : 'small'"
v-model:value="code.language" v-model:value="code.language"
:options="options" :options="options"
/> />
<Submit /> <Submit />
<n-button :size="isDesktop ? 'medium' : 'small'" @click="reset">
重置
</n-button>
<n-dropdown <n-dropdown
v-if="isMobile" v-if="isMobile"
trigger="click" trigger="click"
:options="menu" :options="menu"
@select="select" @select="select"
> >
<n-button> <n-button :size="isDesktop ? 'medium' : 'small'">
<template #icon> <template #icon>
<n-icon> <n-icon>
<i-ep-more-filled /> <i-ep-more-filled />
@@ -95,11 +90,16 @@ function select(key: string) {
</template> </template>
</n-button> </n-button>
</n-dropdown> </n-dropdown>
<n-button v-if="isDesktop" @click="reset">重置</n-button> <n-button
<n-button v-if="isDesktop" @click="goSubmissions">提交信息</n-button> :size="isDesktop ? 'medium' : 'small'"
<n-button v-if="isDesktop" type="info" @click="goTestCat">自测猫</n-button> v-if="isDesktop"
@click="goSubmissions"
>
提交信息
</n-button>
<n-button <n-button
type="warning" type="warning"
:size="isDesktop ? 'medium' : 'small'"
v-if="isDesktop && userStore.isSuperAdmin" v-if="isDesktop && userStore.isSuperAdmin"
@click="goEdit" @click="goEdit"
> >
@@ -110,6 +110,6 @@ function select(key: string) {
<style scoped> <style scoped>
.language { .language {
width: 140px; width: 120px;
} }
</style> </style>

View File

@@ -1,13 +1,11 @@
<script setup lang="ts"> <script setup lang="ts">
import Copy from "~/shared/Copy.vue" import Copy from "~/shared/Copy.vue"
import { code } from "oj/composables/code" import { code } from "oj/composables/code"
import { problem } from "oj/composables/problem"
import { Problem, ProblemStatus } from "utils/types" import { Problem, ProblemStatus } from "utils/types"
import { createTestSubmission } from "utils/judge" import { createTestSubmission } from "utils/judge"
import { useThemeVars } from "naive-ui" import { useThemeVars } from "naive-ui"
interface Props {
problem: Problem
}
type Sample = Problem["samples"][number] & { type Sample = Problem["samples"][number] & {
id: number id: number
msg: string msg: string
@@ -15,12 +13,11 @@ type Sample = Problem["samples"][number] & {
loading: boolean loading: boolean
} }
const props = defineProps<Props>()
const theme = useThemeVars() const theme = useThemeVars()
const style = computed(() => "color: " + theme.value.primaryColor) const style = computed(() => "color: " + theme.value.primaryColor)
const samples = ref<Sample[]>( const samples = ref<Sample[]>(
props.problem.samples.map((sample, index) => ({ problem.value!.samples.map((sample, index) => ({
...sample, ...sample,
id: index, id: index,
msg: "", msg: "",
@@ -90,74 +87,76 @@ function type(status: ProblemStatus) {
</script> </script>
<template> <template>
<n-alert <div v-if="problem">
class="success" <n-alert
v-if="problem.my_status === 0" class="success"
type="success" v-if="problem.my_status === 0"
title="🎉 本 题 已 经 被 你 解 决 啦" type="success"
/> title="🎉 本 题 已 经 被 你 解 决 啦"
/>
<n-space align="center">
<n-tag>{{ problem._id }}</n-tag>
<h1 class="problemTitle">{{ problem.title }}</h1>
</n-space>
<p class="title" :style="style">描述</p>
<div class="content" v-html="problem.description"></div>
<p class="title" :style="style">输入</p>
<div class="content" v-html="problem.input_description"></div>
<p class="title" :style="style">输出</p>
<div class="content" v-html="problem.output_description"></div>
<div v-if="problem.hint">
<p class="title" :style="style">提示</p>
<div class="content" v-html="problem.hint"></div>
</div>
<div v-for="(sample, index) of samples" :key="index">
<n-space align="center"> <n-space align="center">
<p class="title testcaseTitle" :style="style">测试用例 {{ index + 1 }}</p> <n-tag>{{ problem._id }}</n-tag>
<n-button <h2 class="problemTitle">{{ problem.title }}</h2>
size="small"
:type="type(sample.status)"
@click="test(sample, index)"
>
{{ label(sample.status, sample.loading) }}
</n-button>
</n-space> </n-space>
<n-descriptions <p class="title" :style="style">描述</p>
bordered <div class="content" v-html="problem.description"></div>
:column="2"
label-style="width: 50%; min-width: 100px"
>
<n-descriptions-item>
<template #label>
<n-space>
<span>输入</span>
<Copy :value="sample.input" />
</n-space>
</template>
<div class="testcase">{{ sample.input }}</div>
</n-descriptions-item>
<n-descriptions-item>
<template #label>
<n-space>
<span>输出</span>
<Copy :value="sample.output" />
</n-space>
</template>
<div class="testcase">{{ sample.output }}</div>
</n-descriptions-item>
<n-descriptions-item label="运行结果" v-if="sample.msg">
<div class="testcase">{{ sample.msg }}</div>
</n-descriptions-item>
</n-descriptions>
</div>
<div v-if="problem.source"> <p class="title" :style="style">输入</p>
<p class="title" :style="style">来源</p> <div class="content" v-html="problem.input_description"></div>
<div class="content" v-html="problem.source"></div>
<p class="title" :style="style">输出</p>
<div class="content" v-html="problem.output_description"></div>
<div v-if="problem.hint">
<p class="title" :style="style">提示</p>
<div class="content" v-html="problem.hint"></div>
</div>
<div v-for="(sample, index) of samples" :key="index">
<n-space align="center">
<p class="title" :style="style">测试用例 {{ index + 1 }}</p>
<n-button
size="small"
:type="type(sample.status)"
@click="test(sample, index)"
>
{{ label(sample.status, sample.loading) }}
</n-button>
</n-space>
<n-descriptions
bordered
:column="2"
label-style="width: 50%; min-width: 100px"
>
<n-descriptions-item>
<template #label>
<n-space>
<span>输入</span>
<Copy :value="sample.input" />
</n-space>
</template>
<div class="testcase">{{ sample.input }}</div>
</n-descriptions-item>
<n-descriptions-item>
<template #label>
<n-space>
<span>输出</span>
<Copy :value="sample.output" />
</n-space>
</template>
<div class="testcase">{{ sample.output }}</div>
</n-descriptions-item>
<n-descriptions-item label="运行结果" v-if="sample.msg">
<div class="testcase">{{ sample.msg }}</div>
</n-descriptions-item>
</n-descriptions>
</div>
<div v-if="problem.source">
<p class="title" :style="style">来源</p>
<div class="content" v-html="problem.source"></div>
</div>
</div> </div>
</template> </template>
@@ -167,14 +166,10 @@ function type(status: ProblemStatus) {
} }
.title { .title {
font-size: 20px; font-size: 16px;
margin: 12px 0; margin: 12px 0;
} }
.testcaseTitle {
margin-bottom: 24px;
}
.content { .content {
font-size: 16px; font-size: 16px;
line-height: 2; line-height: 2;

View File

@@ -2,16 +2,11 @@
import { Pie } from "vue-chartjs" import { Pie } from "vue-chartjs"
import { DIFFICULTY, JUDGE_STATUS } from "utils/constants" import { DIFFICULTY, JUDGE_STATUS } from "utils/constants"
import { getACRate, getTagColor, parseTime } from "utils/functions" import { getACRate, getTagColor, parseTime } from "utils/functions"
import { problem } from "oj/composables/problem"
import { isDesktop } from "~/shared/composables/breakpoints" import { isDesktop } from "~/shared/composables/breakpoints"
import { Problem } from "utils/types"
interface Props {
problem: Problem
}
const props = defineProps<Props>()
const data = computed(() => { const data = computed(() => {
const status = props.problem.statistic_info const status = problem.value!.statistic_info
const labels = [] const labels = []
for (let i in status) { for (let i in status) {
if (status[i] !== 0) { if (status[i] !== 0) {
@@ -33,7 +28,12 @@ const options = {
</script> </script>
<template> <template>
<n-descriptions bordered label-placement="left" :column="isDesktop ? 3 : 1"> <n-descriptions
bordered
label-placement="left"
:column="isDesktop ? 3 : 1"
v-if="problem"
>
<n-descriptions-item label="编号"> <n-descriptions-item label="编号">
{{ problem._id }} {{ problem._id }}
</n-descriptions-item> </n-descriptions-item>
@@ -71,7 +71,7 @@ const options = {
</n-space> </n-space>
</n-descriptions-item> </n-descriptions-item>
</n-descriptions> </n-descriptions>
<div class="pie" v-if="problem.submission_number > 0"> <div class="pie" v-if="problem && problem.submission_number > 0">
<Pie :data="data" :options="options" /> <Pie :data="data" :options="options" />
</div> </div>
</template> </template>

View File

@@ -1,10 +1,11 @@
<script setup lang="ts"> <script setup lang="ts">
import { code } from "oj/composables/code"
import party from "party-js" import party from "party-js"
import { Ref } from "vue" import { code } from "oj/composables/code"
import { problem } from "oj/composables/problem"
import { isDesktop } from "~/shared/composables/breakpoints"
import { JUDGE_STATUS, SubmissionStatus } from "utils/constants" import { JUDGE_STATUS, SubmissionStatus } from "utils/constants"
import { submissionMemoryFormat, submissionTimeFormat } from "utils/functions" import { submissionMemoryFormat, submissionTimeFormat } from "utils/functions"
import { Problem, Submission, SubmitCodePayload } from "utils/types" import { Submission, SubmitCodePayload } from "utils/types"
import { getSubmission, submitCode } from "oj/api" import { getSubmission, submitCode } from "oj/api"
import SubmissionResultTag from "~/shared/SubmissionResultTag.vue" import SubmissionResultTag from "~/shared/SubmissionResultTag.vue"
import { useUserStore } from "~/shared/store/user" import { useUserStore } from "~/shared/store/user"
@@ -12,7 +13,6 @@ import { useUserStore } from "~/shared/store/user"
const userStore = useUserStore() const userStore = useUserStore()
const route = useRoute() const route = useRoute()
const problem = inject<Ref<Problem>>("problem")
const contestID = <string>route.params.contestID ?? "" const contestID = <string>route.params.contestID ?? ""
const submissionId = ref("") const submissionId = ref("")
@@ -92,7 +92,7 @@ const submitLabel = computed(() => {
if (isPending.value) { if (isPending.value) {
return "运行结果" return "运行结果"
} }
return "点击提交" return "提交评测"
}) })
const msg = computed(() => { const msg = computed(() => {
@@ -160,7 +160,7 @@ async function submit() {
return return
} }
const data: SubmitCodePayload = { const data: SubmitCodePayload = {
problem_id: problem!.value.id, problem_id: problem.value!.id,
language: code.language, language: code.language,
code: code.value, code: code.value,
} }
@@ -199,7 +199,12 @@ watch(
style="max-height: 600px" style="max-height: 600px"
> >
<template #trigger> <template #trigger>
<n-button type="primary" :disabled="submitDisabled" @click="submit"> <n-button
:size="isDesktop ? 'medium' : 'small'"
type="primary"
:disabled="submitDisabled"
@click="submit"
>
<template #icon> <template #icon>
<n-icon> <n-icon>
<i-ep-loading v-if="judging || pending || submitting" /> <i-ep-loading v-if="judging || pending || submitting" />

View File

@@ -1,7 +1,7 @@
<script setup lang="ts"> <script setup lang="ts">
import { getProblem } from "oj/api" import { getProblem } from "oj/api"
import { isDesktop } from "~/shared/composables/breakpoints" import { isDesktop } from "~/shared/composables/breakpoints"
import { Problem } from "utils/types" import { problem } from "../composables/problem"
const Editor = defineAsyncComponent(() => import("./components/Editor.vue")) const Editor = defineAsyncComponent(() => import("./components/Editor.vue"))
const ProblemContent = defineAsyncComponent( const ProblemContent = defineAsyncComponent(
@@ -19,7 +19,7 @@ interface Props {
const props = withDefaults(defineProps<Props>(), { const props = withDefaults(defineProps<Props>(), {
contestID: "", contestID: "",
}) })
const problem = ref<Problem | null>(null)
const errMsg = ref("无数据") const errMsg = ref("无数据")
async function init() { async function init() {
@@ -34,7 +34,6 @@ async function init() {
} }
} }
onMounted(init) onMounted(init)
provide("problem", readonly(problem))
</script> </script>
<template> <template>
@@ -43,27 +42,27 @@ provide("problem", readonly(problem))
<n-scrollbar v-if="isDesktop" style="max-height: calc(100vh - 92px)"> <n-scrollbar v-if="isDesktop" style="max-height: calc(100vh - 92px)">
<n-tabs default-value="content" type="segment"> <n-tabs default-value="content" type="segment">
<n-tab-pane name="content" tab="题目描述"> <n-tab-pane name="content" tab="题目描述">
<ProblemContent :problem="problem" /> <ProblemContent />
</n-tab-pane> </n-tab-pane>
<n-tab-pane name="info" tab="题目信息"> <n-tab-pane name="info" tab="题目统计">
<ProblemInfo :problem="problem" /> <ProblemInfo />
</n-tab-pane> </n-tab-pane>
</n-tabs> </n-tabs>
</n-scrollbar> </n-scrollbar>
<n-tabs v-else default-value="content" type="segment"> <n-tabs v-else default-value="content" type="segment">
<n-tab-pane name="content" tab="题目描述"> <n-tab-pane name="content" tab="题目描述">
<ProblemContent :problem="problem" /> <ProblemContent />
</n-tab-pane> </n-tab-pane>
<n-tab-pane name="editor" tab="代码编辑"> <n-tab-pane name="editor" tab="代码编辑">
<Editor :problem="problem" /> <Editor />
</n-tab-pane> </n-tab-pane>
<n-tab-pane name="info" tab="题目信息"> <n-tab-pane name="info" tab="题目统计">
<ProblemInfo :problem="problem" /> <ProblemInfo />
</n-tab-pane> </n-tab-pane>
</n-tabs> </n-tabs>
</n-gi> </n-gi>
<n-gi v-if="isDesktop"> <n-gi v-if="isDesktop">
<Editor :problem="problem" /> <Editor />
</n-gi> </n-gi>
</n-grid> </n-grid>
<n-empty v-else :description="errMsg"></n-empty> <n-empty v-else :description="errMsg"></n-empty>