add code preset

This commit is contained in:
2024-10-08 23:02:55 +08:00
parent 380f6b9d0a
commit 5d8491ceb6
4 changed files with 131 additions and 17 deletions

View File

@@ -24,7 +24,9 @@ function decode(bytes?: string) {
) )
} }
const http = axios.create({ baseURL: `${protocol}://judge0api.xuyue.cc` }) const judge = axios.create({ baseURL: `${protocol}://judge0api.xuyue.cc` })
const api = axios.create({ baseURL: `${protocol}://codeapi.xuyue.cc` })
// const api = axios.create({ baseURL: `http://localhost:8080` })
export async function submit(code: Code, input: string) { export async function submit(code: Code, input: string) {
const encodedCode = encode(code.value) const encodedCode = encode(code.value)
@@ -42,7 +44,7 @@ export async function submit(code: Code, input: string) {
redirect_stderr_to_stdout: true, redirect_stderr_to_stdout: true,
compiler_options: compilerOptions, compiler_options: compilerOptions,
} }
const response = await http.post<Submission>("/submissions", payload, { const response = await judge.post<Submission>("/submissions", payload, {
params: { base64_encoded: true, wait: true }, params: { base64_encoded: true, wait: true },
}) })
const data = response.data const data = response.data
@@ -54,3 +56,23 @@ export async function submit(code: Code, input: string) {
} }
} }
} }
export async function listCode() {
const res = await api.get("/")
return res.data
}
export async function getCodeByQuery(query: string) {
const res = await api.get("/" + query)
return res.data
}
export async function createCode(data: { code: string; query: string }) {
const res = await api.post("/", data)
return res.data
}
export async function removeCode(id: number) {
const res = await api.delete(`/${id}`)
console.log(res.data)
}

View File

@@ -2,7 +2,7 @@ import { useStorage } from "@vueuse/core"
import copyTextToClipboard from "copy-text-to-clipboard" import copyTextToClipboard from "copy-text-to-clipboard"
import queryString from "query-string" import queryString from "query-string"
import { reactive, ref, watch } from "vue" import { reactive, ref, watch } from "vue"
import { submit } from "../api" import { getCodeByQuery, submit } from "../api"
import { sources } from "../templates" import { sources } from "../templates"
import { Cache, Code, LANGUAGE, Status } from "../types" import { Cache, Code, LANGUAGE, Status } from "../types"
import { atou, utoa } from "../utils" import { atou, utoa } from "../utils"
@@ -56,7 +56,7 @@ watch(input, (value: string) => {
cache.input.value = value cache.input.value = value
}) })
export function init() { export async function init() {
code.language = cache.language.value code.language = cache.language.value
code.value = cache.code[code.language].value code.value = cache.code[code.language].value
input.value = cache.input.value input.value = cache.input.value
@@ -65,13 +65,21 @@ export function init() {
const parsed = queryString.parse(location.search) const parsed = queryString.parse(location.search)
const base64 = parsed.share as string const base64 = parsed.share as string
if (!base64) return if (base64) {
try { try {
const data = JSON.parse(atou(base64)) const data = JSON.parse(atou(base64))
code.language = data.lang code.language = data.lang
code.value = data.code code.value = data.code
input.value = data.input input.value = data.input
} catch (err) {} } catch (err) {}
}
const preset = parsed.query as string
if (preset) {
try {
const result = await getCodeByQuery(preset)
code.value = result.data.code
} catch (err) {}
}
} }
export function clearInput() { export function clearInput() {

69
src/desktop/Query.vue Normal file
View File

@@ -0,0 +1,69 @@
<template>
<n-flex vertical size="large">
<n-flex v-if="code.value">
<n-flex align="center">
<span>复制当前代码输入 Query</span>
<div>
<n-input v-model:value="query" />
</div>
</n-flex>
<n-button type="primary" @click="create" :disabled="!query.length">
新建
</n-button>
</n-flex>
<span v-else style="color: red">当前无代码</span>
<n-flex>
<div style="margin-top: 6px" v-if="codes.length">已有的 Query</div>
<n-tag
v-for="item in codes"
:key="item.query"
closable
size="large"
round
@click="show(item.code)"
@close="remove(item.id)"
>
{{ item.query }}
</n-tag>
</n-flex>
</n-flex>
</template>
<script setup lang="ts">
import { useMessage } from "naive-ui"
import { onMounted, ref } from "vue"
import { createCode, listCode, removeCode } from "../api"
import { code } from "../composables/code"
const message = useMessage()
const query = ref("")
const codes = ref<{ code: string; query: string; id: number }[]>([])
async function create() {
try {
await createCode({
code: code.value,
query: query.value,
})
list()
} catch (err) {
message.error("query重复创建失败")
}
}
async function remove(id: number) {
await removeCode(id)
list()
}
function show(codeStr: string) {
code.value = codeStr
}
async function list() {
const res = await listCode()
codes.value = res.data
}
onMounted(list)
</script>

View File

@@ -2,7 +2,7 @@
<Header /> <Header />
<Content /> <Content />
<n-modal <n-modal
v-model:show="show" v-model:show="file"
preset="card" preset="card"
style="width: 600px" style="width: 600px"
:mask-closable="false" :mask-closable="false"
@@ -10,6 +10,15 @@
> >
<File /> <File />
</n-modal> </n-modal>
<n-modal
v-model:show="query"
preset="card"
style="width: 600px"
:mask-closable="false"
title="代码预设"
>
<Query />
</n-modal>
</template> </template>
<script lang="ts" setup> <script lang="ts" setup>
import { useMagicKeys, whenever } from "@vueuse/core" import { useMagicKeys, whenever } from "@vueuse/core"
@@ -18,19 +27,25 @@ import { run } from "../composables/code"
import Content from "./Content.vue" import Content from "./Content.vue"
import File from "./File.vue" import File from "./File.vue"
import Header from "./Header.vue" import Header from "./Header.vue"
import Query from "./Query.vue"
const show = ref(false) const file = ref(false)
const query = ref(false)
const { alt_shift_p, ctrl_shift_p, ctrl_shift_z } = useMagicKeys() const { alt_shift_p, ctrl_shift_p, ctrl_shift_z, ctrl_shift_m } = useMagicKeys()
whenever(alt_shift_p, () => { whenever(alt_shift_p, () => {
show.value = true file.value = true
}) })
whenever(ctrl_shift_p, () => { whenever(ctrl_shift_p, () => {
show.value = true file.value = true
}) })
whenever(ctrl_shift_z, () => { whenever(ctrl_shift_z, () => {
show.value = true file.value = true
})
whenever(ctrl_shift_m, () => {
query.value = true
}) })
const { ctrl_s } = useMagicKeys({ const { ctrl_s } = useMagicKeys({