template for the problem.

This commit is contained in:
2023-03-28 17:09:12 +08:00
parent bcb3d23e37
commit 05c85fd9b0
5 changed files with 95 additions and 87 deletions

View File

@@ -3,11 +3,10 @@ import TextEditor from "~/shared/TextEditor.vue"
import Monaco from "~/shared/Monaco.vue" import Monaco from "~/shared/Monaco.vue"
import { SelectOption } from "naive-ui" import { SelectOption } from "naive-ui"
import { unique } from "~/utils/functions" import { encode, unique } from "~/utils/functions"
import { Problem, Tag } from "~/utils/types" import { LANGUAGE, Problem, Tag } from "~/utils/types"
import { getProblemTagList } from "~/shared/api" import { getProblemTagList } from "~/shared/api"
import { LANGUAGE_SHOW_VALUE } from "~/utils/constants" import { LANGUAGE_SHOW_VALUE, CODE_TEMPLATES } from "~/utils/constants"
import { cTemplate, cppTemplate, blankTemplate } from "./templates"
interface AlterProblem { interface AlterProblem {
spj_language: string spj_language: string
@@ -62,7 +61,7 @@ const problem = reactive<Omit<Problem, ExcludeKeys> & AlterProblem>({
}, },
}) })
const template = shallowRef({}) const template = reactive(JSON.parse(JSON.stringify(CODE_TEMPLATES)))
const existingTags = shallowRef<Tag[]>([]) const existingTags = shallowRef<Tag[]>([])
const fromExistingTags = shallowRef<string[]>([]) const fromExistingTags = shallowRef<string[]>([])
@@ -117,6 +116,25 @@ function removeSample(index: number) {
problem.samples.splice(index, 1) problem.samples.splice(index, 1)
} }
function resetTemplate(language: LANGUAGE) {
template[language] = CODE_TEMPLATES[language]
}
function saveProblem() {
if (!needTemplate.value) {
problem.template = {}
} else {
problem.languages.forEach((lang) => {
if (CODE_TEMPLATES[lang] !== template[lang]) {
problem.template[lang] = template[lang]
} else {
delete problem.template[lang]
}
})
}
console.log(problem.template)
}
onMounted(() => { onMounted(() => {
listTags() listTags()
}) })
@@ -125,13 +143,6 @@ watch([fromExistingTags, newTags], (tags) => {
const uniqueTags = unique<string>(tags[0].concat(tags[1])) const uniqueTags = unique<string>(tags[0].concat(tags[1]))
problem.tags = uniqueTags problem.tags = uniqueTags
}) })
watch(
() => problem.languages,
() => {
console.log(111)
}
)
</script> </script>
<template> <template>
@@ -216,33 +227,29 @@ watch(
添加用例 添加用例
</n-button> </n-button>
<TextEditor v-model:value="problem.hint" title="提示" /> <TextEditor v-model:value="problem.hint" title="提示" />
<n-space class="title"> <n-space class="title" align="center">
<n-checkbox v-model:checked="needTemplate" label="代码模板" /> <n-checkbox v-model:checked="needTemplate" label="代码模板" />
</n-space> </n-space>
<n-tabs type="segment" v-if="needTemplate"> <n-tabs type="segment" class="templateWrapper" v-if="needTemplate">
<n-tab-pane <n-tab-pane
v-for="(lang, index) in problem.languages" v-for="(lang, index) in problem.languages"
:name="LANGUAGE_SHOW_VALUE[lang]" :name="LANGUAGE_SHOW_VALUE[lang]"
:key="index" :key="index"
> >
<Monaco <Monaco
v-if="lang === 'C'" v-model:value="template[lang]"
v-model:value="cTemplate" :language="lang"
:font-size="12" :font-size="16"
height="320px" height="300px"
/>
<Monaco
v-else-if="lang === 'C++'"
v-model:value="cppTemplate"
:font-size="12"
height="320px"
/>
<Monaco
v-else
v-model:value="blankTemplate"
:font-size="12"
height="320px"
/> />
<n-button
size="small"
secondary
type="warning"
@click="resetTemplate(lang)"
>
重置 {{ LANGUAGE_SHOW_VALUE[lang] }} 代码模板
</n-button>
</n-tab-pane> </n-tab-pane>
</n-tabs> </n-tabs>
<n-form> <n-form>
@@ -253,6 +260,9 @@ watch(
/> />
</n-form-item> </n-form-item>
</n-form> </n-form>
<n-space justify="end">
<n-button type="primary" @click="saveProblem">保存</n-button>
</n-space>
</template> </template>
<style scoped> <style scoped>
@@ -281,4 +291,9 @@ watch(
width: 100%; width: 100%;
margin-bottom: 20px; margin-bottom: 20px;
} }
.templateWrapper {
width: 50%;
margin-bottom: 20px;
}
</style> </style>

View File

@@ -1,44 +0,0 @@
export const cTemplate = `//PREPEND BEGIN
#include <stdio.h>
//PREPEND END
//TEMPLATE BEGIN
int add(int a, int b) {
// 请填空
return ___________;
}
//TEMPLATE END
//APPEND BEGIN
int main() {
printf("%d", add(1, 2));
return 0;
}
//APPEND END`
export const cppTemplate = `//PREPEND BEGIN
#include <iostream>
//PREPEND END
//TEMPLATE BEGIN
int add(int a, int b) {
// 请填空
return ___________;
}
//TEMPLATE END
//APPEND BEGIN
int main() {
std::cout << add(1, 2);
return 0;
}
//APPEND END`
export const blankTemplate = `//PREPEND BEGIN
//PREPEND END
//TEMPLATE BEGIN
//TEMPLATE END
//APPEND BEGIN
//APPEND END`

View File

@@ -231,3 +231,39 @@ export const LANGUAGE_SHOW_VALUE = {
JavaScript: "JS", JavaScript: "JS",
Golang: "Go", Golang: "Go",
} }
const cTemplate = `//TEMPLATE BEGIN
#include <stdio.h>
int main() {
printf("黄岩一职");
return 0;
}
//TEMPLATE END`
const cppTemplate = `//TEMPLATE BEGIN
#include <iostream>
int main() {
return 0;
}
//TEMPLATE END`
const blankTemplate = `//PREPEND BEGIN
//PREPEND END
//TEMPLATE BEGIN
//TEMPLATE END
//APPEND BEGIN
//APPEND END`
export const CODE_TEMPLATES = {
C: cTemplate,
"C++": cppTemplate,
Python2: blankTemplate,
Python3: blankTemplate,
Java: blankTemplate,
JavaScript: blankTemplate,
Golang: blankTemplate,
}

View File

@@ -138,3 +138,16 @@ export function unique<T>(arr: T[]) {
return prev return prev
}, []) }, [])
} }
export function encode(string?: string) {
return btoa(String.fromCharCode(...new TextEncoder().encode(string ?? "")))
}
export function decode(bytes?: string) {
const latin = atob(bytes ?? "")
return new TextDecoder("utf-8").decode(
Uint8Array.from({ length: latin.length }, (_, index) =>
latin.charCodeAt(index)
)
)
}

View File

@@ -1,22 +1,10 @@
import axios from "axios" import axios from "axios"
import { DEAD_RESULTS } from "./constants" import { DEAD_RESULTS } from "./constants"
import { decode, encode } from "./functions"
import { Code } from "./types" import { Code } from "./types"
const http = axios.create({ baseURL: "https://judge0api.hyyz.izhai.net" }) const http = axios.create({ baseURL: "https://judge0api.hyyz.izhai.net" })
function encode(string?: string) {
return btoa(String.fromCharCode(...new TextEncoder().encode(string ?? "")))
}
function decode(bytes?: string) {
const latin = atob(bytes ?? "")
return new TextDecoder("utf-8").decode(
Uint8Array.from({ length: latin.length }, (_, index) =>
latin.charCodeAt(index)
)
)
}
export async function createTestSubmission(code: Code, input: string) { export async function createTestSubmission(code: Code, input: string) {
const encodedCode = encode(code.value) const encodedCode = encode(code.value)