diff --git a/src/App.vue b/src/App.vue index c25bfd2..affc66a 100644 --- a/src/App.vue +++ b/src/App.vue @@ -1,9 +1,9 @@ @@ -22,4 +22,3 @@ const isDark = useDark() -./themes/breakpoints diff --git a/src/api.ts b/src/api.ts index c0b7d15..069b867 100644 --- a/src/api.ts +++ b/src/api.ts @@ -1,56 +1,56 @@ -import axios from "axios" -import { Code } from "./types" -import { deadResults, languageToId } from "./templates" - -function getChromeVersion() { - var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./) - return raw ? parseInt(raw[2], 10) : 0 -} - -const isLowVersion = getChromeVersion() < 80 - -const protocol = isLowVersion ? "http" : "https" - -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), - ), - ) -} - -const http = axios.create({ baseURL: `${protocol}://judge0api.xuyue.cc` }) - -export async function submit(code: Code, input: string) { - const encodedCode = encode(code.value) - - if (encodedCode === deadResults[code.language].encoded) { - return deadResults[code.language].result - } else { - const id = languageToId[code.language] - let compilerOptions = "" - if (id === 50) compilerOptions = "-lm" // 解决 GCC 的链接问题 - const payload = { - source_code: encodedCode, - language_id: id, - stdin: encode(input), - redirect_stderr_to_stdout: true, - compiler_options: compilerOptions, - } - const response = await http.post("/submissions", payload, { - params: { base64_encoded: true, wait: true }, - }) - const data = response.data - return { - status: data.status && data.status.id, - output: [decode(data.compile_output), decode(data.stdout)] - .join("\n") - .trim(), - } - } -} +import axios from "axios" +import { Code } from "./types" +import { deadResults, languageToId } from "./templates" + +function getChromeVersion() { + var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./) + return raw ? parseInt(raw[2], 10) : 0 +} + +const isLowVersion = getChromeVersion() < 80 + +const protocol = isLowVersion ? "http" : "https" + +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), + ), + ) +} + +const http = axios.create({ baseURL: `${protocol}://judge0api.xuyue.cc` }) + +export async function submit(code: Code, input: string) { + const encodedCode = encode(code.value) + + if (encodedCode === deadResults[code.language].encoded) { + return deadResults[code.language].result + } else { + const id = languageToId[code.language] + let compilerOptions = "" + if (id === 50) compilerOptions = "-lm" // 解决 GCC 的链接问题 + const payload = { + source_code: encodedCode, + language_id: id, + stdin: encode(input), + redirect_stderr_to_stdout: true, + compiler_options: compilerOptions, + } + const response = await http.post("/submissions", payload, { + params: { base64_encoded: true, wait: true }, + }) + const data = response.data + return { + status: data.status && data.status.id, + output: [decode(data.compile_output), decode(data.stdout)] + .join("\n") + .trim(), + } + } +} diff --git a/src/components/CodeEditor.vue b/src/components/CodeEditor.vue index dd0574b..e4c455c 100644 --- a/src/components/CodeEditor.vue +++ b/src/components/CodeEditor.vue @@ -1,81 +1,81 @@ - - - + + + diff --git a/src/composables/code.ts b/src/composables/code.ts index 1f5dff5..feb17c7 100644 --- a/src/composables/code.ts +++ b/src/composables/code.ts @@ -1,40 +1,40 @@ -import { ref } from "vue" -import copyTextToClipboard from "copy-text-to-clipboard" -import { Code, LANGUAGE } from "../types" -import { sources } from "../templates" -import { submit } from "../api" - -export const code = ref({ - value: sources["python"], - language: "python", -}) -export const input = ref("") -export const output = ref("") -export const loading = ref(false) - -export function copy() { - copyTextToClipboard(code.value.value) -} - -export function reset() { - code.value.value = sources["python"] - output.value = "" -} - -export function changeLanguage(language: LANGUAGE) { - code.value.value = sources[language] - output.value = "" -} - -export async function run() { - loading.value = true - const cleanCode = code.value.value.trim() - if (!cleanCode) return - output.value = "" - const result = await submit( - { value: cleanCode, language: code.value.language }, - input.value.trim(), - ) - output.value = result.output || "" - loading.value = false -} +import { ref } from "vue" +import copyTextToClipboard from "copy-text-to-clipboard" +import { Code, LANGUAGE } from "../types" +import { sources } from "../templates" +import { submit } from "../api" + +export const code = ref({ + value: sources["python"], + language: "python", +}) +export const input = ref("") +export const output = ref("") +export const loading = ref(false) + +export function copy() { + copyTextToClipboard(code.value.value) +} + +export function reset() { + code.value.value = sources["python"] + output.value = "" +} + +export function changeLanguage(language: LANGUAGE) { + code.value.value = sources[language] + output.value = "" +} + +export async function run() { + loading.value = true + const cleanCode = code.value.value.trim() + if (!cleanCode) return + output.value = "" + const result = await submit( + { value: cleanCode, language: code.value.language }, + input.value.trim(), + ) + output.value = result.output || "" + loading.value = false +} diff --git a/src/desktop/Content.vue b/src/desktop/Content.vue index 5001963..26df10b 100644 --- a/src/desktop/Content.vue +++ b/src/desktop/Content.vue @@ -1,37 +1,37 @@ - - - + + + diff --git a/src/desktop/Header.vue b/src/desktop/Header.vue index fb09d88..f492b1b 100644 --- a/src/desktop/Header.vue +++ b/src/desktop/Header.vue @@ -1,69 +1,69 @@ - - - - - + + + + + diff --git a/src/desktop/index.vue b/src/desktop/index.vue index f10286a..a5a8828 100644 --- a/src/desktop/index.vue +++ b/src/desktop/index.vue @@ -1,8 +1,8 @@ - - + + diff --git a/src/icons/Play.vue b/src/icons/Play.vue index 02c3f3e..31e893f 100644 --- a/src/icons/Play.vue +++ b/src/icons/Play.vue @@ -1,15 +1,15 @@ - + diff --git a/src/mobile/index.vue b/src/mobile/index.vue index fe406dd..cc340bc 100644 --- a/src/mobile/index.vue +++ b/src/mobile/index.vue @@ -1 +1 @@ - + diff --git a/src/templates.ts b/src/templates.ts index 9174b57..67dd73b 100644 --- a/src/templates.ts +++ b/src/templates.ts @@ -1,55 +1,55 @@ -const cSource = - '#include\r\n\r\nint main()\r\n{\r\n printf("黄岩一职");\r\n return 0;\r\n}' -const cppSource = - '#include\r\n\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n cout<<"黄岩一职"<\r\n\r\nint main()\r\n{\r\n printf("黄岩一职");\r\n return 0;\r\n}' +const cppSource = + '#include\r\n\r\nusing namespace std;\r\n\r\nint main()\r\n{\r\n cout<<"黄岩一职"<