Compare commits

...

4 Commits

Author SHA1 Message Date
d798d56dbe fix
Some checks failed
Deploy / build-and-deploy (push) Has been cancelled
2026-06-14 07:18:50 -06:00
504af39ff6 feat: add format code button to editor toolbar 2026-06-14 07:04:38 -06:00
dde90adb46 feat: add format action to code composable 2026-06-14 07:02:23 -06:00
1fd2025fe1 feat: add formatCode API client 2026-06-14 07:00:52 -06:00
4 changed files with 26 additions and 5 deletions

View File

@@ -1,6 +1,6 @@
import axios from "axios"
import { languageToId } from "./templates"
import { Code, Submission } from "./types"
import { Code, LANGUAGE, Submission } from "./types"
function encode(string?: string) {
return btoa(String.fromCharCode(...new TextEncoder().encode(string ?? "")))
@@ -133,3 +133,8 @@ export async function debug(code: string, inputs: string[]) {
})
return res.data
}
export async function formatCode(code: string, language: LANGUAGE) {
const res = await api.post("/format", { code, language })
return res.data.code as string
}

View File

@@ -2,7 +2,7 @@ import { useStorage } from "@vueuse/core"
import copyTextToClipboard from "copy-text-to-clipboard"
import qs from "query-string"
import { reactive, ref, watch } from "vue"
import { getCodeByQuery, submit } from "../api"
import { formatCode, getCodeByQuery, submit } from "../api"
import { sources } from "../templates"
import { Cache, Code, LANGUAGE, Status } from "../types"
import { atou, utoa } from "../utils"
@@ -136,3 +136,7 @@ export function share() {
qs.stringifyUrl({ url: location.href, query: { share: base64 } }),
)
}
export async function format() {
code.value = await formatCode(code.value, code.language)
}

View File

@@ -4,7 +4,7 @@ import copyTextToClipboard from "copy-text-to-clipboard"
import { useMessage } from "naive-ui"
import CodeEditor from "../components/CodeEditor.vue"
import DebugPanel from "../components/DebugPanel.vue"
import { code, input, reset, size } from "../composables/code"
import { code, format, input, reset, size } from "../composables/code"
import { debug } from "../api"
const message = useMessage()
@@ -22,6 +22,17 @@ function copy() {
message.success("已经复制好了")
}
async function handleFormat() {
try {
await format()
message.success("代码已整理")
} catch (err: any) {
message.error(
`整理失败: ${err?.response?.data?.detail ?? err?.message ?? "未知错误"}`,
)
}
}
/**
* trace 末尾停在 raw_input 即说明输入不足
* pg_logger 在缺输入时会立刻 done=Truetrace 中至多只有 1 个 raw_input 事件,
@@ -67,6 +78,7 @@ async function handleDebug() {
>
<template #actions>
<n-button quaternary type="primary" @click="copy">复制</n-button>
<n-button quaternary @click="handleFormat">整理</n-button>
<n-button quaternary @click="reset">清空</n-button>
<n-button
v-if="code.language === 'python'"

View File

@@ -1,7 +1,7 @@
const cSource =
"#include<stdio.h>\r\n\r\nint main()\r\n{\r\n \r\n return 0;\r\n}"
"#include <stdio.h>\r\n\r\nint main() {\r\n \r\n return 0;\r\n}"
const cppSource =
"#include<iostream>\r\n\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n \r\n return 0;\r\n}"
"#include <iostream>\r\n\r\nusing namespace std;\r\n\r\nint main() {\r\n \r\n return 0;\r\n}"
const pythonSource = ""
const javaSource =
"public class Main {\r\n public static void main(String[] args) {\r\n \r\n }\r\n}"