diff --git a/src/admin/problem/detail.vue b/src/admin/problem/detail.vue index 1448c0d..c036514 100644 --- a/src/admin/problem/detail.vue +++ b/src/admin/problem/detail.vue @@ -3,11 +3,10 @@ import TextEditor from "~/shared/TextEditor.vue" import Monaco from "~/shared/Monaco.vue" import { SelectOption } from "naive-ui" -import { unique } from "~/utils/functions" -import { Problem, Tag } from "~/utils/types" +import { encode, unique } from "~/utils/functions" +import { LANGUAGE, Problem, Tag } from "~/utils/types" import { getProblemTagList } from "~/shared/api" -import { LANGUAGE_SHOW_VALUE } from "~/utils/constants" -import { cTemplate, cppTemplate, blankTemplate } from "./templates" +import { LANGUAGE_SHOW_VALUE, CODE_TEMPLATES } from "~/utils/constants" interface AlterProblem { spj_language: string @@ -62,7 +61,7 @@ const problem = reactive & AlterProblem>({ }, }) -const template = shallowRef({}) +const template = reactive(JSON.parse(JSON.stringify(CODE_TEMPLATES))) const existingTags = shallowRef([]) const fromExistingTags = shallowRef([]) @@ -117,6 +116,25 @@ function removeSample(index: number) { 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(() => { listTags() }) @@ -125,13 +143,6 @@ watch([fromExistingTags, newTags], (tags) => { const uniqueTags = unique(tags[0].concat(tags[1])) problem.tags = uniqueTags }) - -watch( - () => problem.languages, - () => { - console.log(111) - } -) diff --git a/src/admin/problem/templates.ts b/src/admin/problem/templates.ts deleted file mode 100644 index 76a1e3d..0000000 --- a/src/admin/problem/templates.ts +++ /dev/null @@ -1,44 +0,0 @@ -export const cTemplate = `//PREPEND BEGIN -#include -//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 -//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` diff --git a/src/utils/constants.ts b/src/utils/constants.ts index cc8c937..b3872d0 100644 --- a/src/utils/constants.ts +++ b/src/utils/constants.ts @@ -231,3 +231,39 @@ export const LANGUAGE_SHOW_VALUE = { JavaScript: "JS", Golang: "Go", } + +const cTemplate = `//TEMPLATE BEGIN +#include + +int main() { + printf("黄岩一职"); + return 0; +} +//TEMPLATE END` + +const cppTemplate = `//TEMPLATE BEGIN +#include + +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, +} diff --git a/src/utils/functions.ts b/src/utils/functions.ts index 06c0c58..39b13ba 100644 --- a/src/utils/functions.ts +++ b/src/utils/functions.ts @@ -138,3 +138,16 @@ export function unique(arr: T[]) { 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) + ) + ) +} diff --git a/src/utils/judge.ts b/src/utils/judge.ts index b538ecc..920f66c 100644 --- a/src/utils/judge.ts +++ b/src/utils/judge.ts @@ -1,22 +1,10 @@ import axios from "axios" import { DEAD_RESULTS } from "./constants" +import { decode, encode } from "./functions" import { Code } from "./types" 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) { const encodedCode = encode(code.value)