Run and lint solutions on the site
A new admin-only solve page, /p/<name>/solve, edits a problem's code in
C++ or Python (a dropdown switches between them), runs it on custom
input or against the saved tests with per-test verdicts (AC, WA, RE,
TLE, unchecked) and compile errors, marks lint diagnostics in the
editor, and saves the code back as a commit ("Edit <name>"). Drafts
stay in the browser per problem and language until saved. LeetCode
problems are C++ only, since their tests run through driver.h.
Code never runs in the site, which holds the Claude, Gitea and S3
secrets. A separate runner service (cmd/usacorunner, Dockerfile.runner)
does it in a container meant to have no environment, an internal-only
network, a read-only root apart from /tmp, and memory, CPU and pids
limits, with tini to reap orphans. Runs copy the CLI: the same g++
flags with -DLOCAL and the repo's headers, <io>.in on disk and on
stdin, <io>.out or stdout as the output, and usaco test's comparison.
Unlike the CLI they have CPU, memory, file and wall-clock limits.
C++ lint is g++'s warnings plus clang-tidy with a new .clang-tidy
(bugprone, performance and a few readability checks; template.cpp is
clean). Python lint is a syntax check plus ruff. template.py is the
Python starter, with a stdio() helper like template.rs's.
CI builds, smoke-tests and rolls out the runner image before the site.
The web_server stack needs a usaco-runner service before that rollout
can succeed.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Generated
+198
@@ -8,7 +8,16 @@
|
||||
"name": "usaco-site-web",
|
||||
"version": "0.0.0",
|
||||
"dependencies": {
|
||||
"@codemirror/commands": "^6.11.1",
|
||||
"@codemirror/lang-cpp": "^6.0.3",
|
||||
"@codemirror/lang-python": "^6.2.1",
|
||||
"@codemirror/language": "^6.12.4",
|
||||
"@codemirror/lint": "^6.9.7",
|
||||
"@codemirror/state": "^6.7.6",
|
||||
"@codemirror/view": "^6.43.13",
|
||||
"@lezer/highlight": "^1.2.5",
|
||||
"@vscode/markdown-it-katex": "^1.1.2",
|
||||
"codemirror": "^6.0.2",
|
||||
"dompurify": "^3.4.17",
|
||||
"highlight.js": "^11.11.1",
|
||||
"katex": "^0.16.25",
|
||||
@@ -72,6 +81,110 @@
|
||||
"node": ">=6.9.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@codemirror/autocomplete": {
|
||||
"version": "6.20.3",
|
||||
"resolved": "https://registry.npmjs.org/@codemirror/autocomplete/-/autocomplete-6.20.3.tgz",
|
||||
"integrity": "sha512-tlosUqb+3BbxCxZdu4tKeRghPFC+QM7q4X5YhKV2eCmPG+1r2F3f4AaSz5sCrFqUtX4Jh20VFTKecl16MgiV9g==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@codemirror/language": "^6.0.0",
|
||||
"@codemirror/state": "^6.0.0",
|
||||
"@codemirror/view": "^6.17.0",
|
||||
"@lezer/common": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@codemirror/commands": {
|
||||
"version": "6.11.1",
|
||||
"resolved": "https://registry.npmjs.org/@codemirror/commands/-/commands-6.11.1.tgz",
|
||||
"integrity": "sha512-O/4hG3SC1YwcmQ0d2UVNDs+AsaNWd1iHVxbTeEBuqH+6bExAiPK3iS/BvpY6rZGURALv4ZD3sIgcCmRvw3ehBg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@codemirror/language": "^6.0.0",
|
||||
"@codemirror/state": "^6.7.0",
|
||||
"@codemirror/view": "^6.27.0",
|
||||
"@lezer/common": "^1.1.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@codemirror/lang-cpp": {
|
||||
"version": "6.0.3",
|
||||
"resolved": "https://registry.npmjs.org/@codemirror/lang-cpp/-/lang-cpp-6.0.3.tgz",
|
||||
"integrity": "sha512-URM26M3vunFFn9/sm6rzqrBzDgfWuDixp85uTY49wKudToc2jTHUrKIGGKs+QWND+YLofNNZpxcNGRynFJfvgA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@codemirror/language": "^6.0.0",
|
||||
"@lezer/cpp": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@codemirror/lang-python": {
|
||||
"version": "6.2.1",
|
||||
"resolved": "https://registry.npmjs.org/@codemirror/lang-python/-/lang-python-6.2.1.tgz",
|
||||
"integrity": "sha512-IRjC8RUBhn9mGR9ywecNhB51yePWCGgvHfY1lWN/Mrp3cKuHr0isDKia+9HnvhiWNnMpbGhWrkhuWOc09exRyw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@codemirror/autocomplete": "^6.3.2",
|
||||
"@codemirror/language": "^6.8.0",
|
||||
"@codemirror/state": "^6.0.0",
|
||||
"@lezer/common": "^1.2.1",
|
||||
"@lezer/python": "^1.1.4"
|
||||
}
|
||||
},
|
||||
"node_modules/@codemirror/language": {
|
||||
"version": "6.12.4",
|
||||
"resolved": "https://registry.npmjs.org/@codemirror/language/-/language-6.12.4.tgz",
|
||||
"integrity": "sha512-1q4PaT+o6PbgpkJt4Q8Fv5XJxTy4FUZ4MWETtyiDw3J0Pyr9E2vqcKL+k9wcvjNTIsauxvE7OfmWj3FRPHQ76A==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@codemirror/state": "^6.0.0",
|
||||
"@codemirror/view": "^6.23.0",
|
||||
"@lezer/common": "^1.5.0",
|
||||
"@lezer/highlight": "^1.0.0",
|
||||
"@lezer/lr": "^1.0.0",
|
||||
"style-mod": "^4.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@codemirror/lint": {
|
||||
"version": "6.9.7",
|
||||
"resolved": "https://registry.npmjs.org/@codemirror/lint/-/lint-6.9.7.tgz",
|
||||
"integrity": "sha512-28/+iWLYxKxsvGYhSYL7zaCZqLz5+FFFDq9tVsvGv9kv8RY4fFAchJ5WX9M3YrrRlTIsECjsXPqeNgnSmNP2dg==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@codemirror/state": "^6.0.0",
|
||||
"@codemirror/view": "^6.42.0",
|
||||
"crelt": "^1.0.5"
|
||||
}
|
||||
},
|
||||
"node_modules/@codemirror/search": {
|
||||
"version": "6.7.2",
|
||||
"resolved": "https://registry.npmjs.org/@codemirror/search/-/search-6.7.2.tgz",
|
||||
"integrity": "sha512-gUYkYhT2+n/+VGZ+8EzE5WFkYZUZYm1VOKDudIsNqh42uRVQJ0a6Yss9sdKT3MeOYfuL1N6AZA57oza0Oyr0LA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@codemirror/state": "^6.0.0",
|
||||
"@codemirror/view": "^6.37.0",
|
||||
"crelt": "^1.0.5"
|
||||
}
|
||||
},
|
||||
"node_modules/@codemirror/state": {
|
||||
"version": "6.7.6",
|
||||
"resolved": "https://registry.npmjs.org/@codemirror/state/-/state-6.7.6.tgz",
|
||||
"integrity": "sha512-kAz+AncRtKuIknedxT1bq4XwXv4UowhbkHU1myPrtVb/jZtImWuV5BXzv5vK6i3kYACsdiZiQKFQQ5Mq7elW8w==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@marijn/find-cluster-break": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@codemirror/view": {
|
||||
"version": "6.43.13",
|
||||
"resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.43.13.tgz",
|
||||
"integrity": "sha512-sihaFrUzAsYBQsL9J2t69y8nfMQGwcYmggAZsk+kjPbjYZMyuf2hU8tUNTZ+P+isb6XRr8JE22TZlJxBoVdH1A==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@codemirror/state": "^6.7.0",
|
||||
"crelt": "^1.0.6",
|
||||
"style-mod": "^4.1.0",
|
||||
"w3c-keyname": "^2.2.4"
|
||||
}
|
||||
},
|
||||
"node_modules/@esbuild/aix-ppc64": {
|
||||
"version": "0.28.2",
|
||||
"resolved": "https://registry.npmjs.org/@esbuild/aix-ppc64/-/aix-ppc64-0.28.2.tgz",
|
||||
@@ -563,6 +676,58 @@
|
||||
"@jridgewell/sourcemap-codec": "^1.4.14"
|
||||
}
|
||||
},
|
||||
"node_modules/@lezer/common": {
|
||||
"version": "1.5.3",
|
||||
"resolved": "https://registry.npmjs.org/@lezer/common/-/common-1.5.3.tgz",
|
||||
"integrity": "sha512-H0iErY4e43LpXbYDyBci5W4v/RwTgGV3YzYOlJPiGZ8RY8w51kVE+xLn1tg3Z6UmSfyETvtsUK3r7YsgDQEI7Q==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@lezer/cpp": {
|
||||
"version": "1.1.6",
|
||||
"resolved": "https://registry.npmjs.org/@lezer/cpp/-/cpp-1.1.6.tgz",
|
||||
"integrity": "sha512-vh9gWWJOXFVY8HBHK3Twzq8MgwG2iN4GSyzBP9sCGTe37P15x2R14VaBQk0VA0ezTRN1KHYBBsHhvpGZ2Xy/pA==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@lezer/common": "^1.2.0",
|
||||
"@lezer/highlight": "^1.0.0",
|
||||
"@lezer/lr": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@lezer/highlight": {
|
||||
"version": "1.2.5",
|
||||
"resolved": "https://registry.npmjs.org/@lezer/highlight/-/highlight-1.2.5.tgz",
|
||||
"integrity": "sha512-O1GMVKgtf5YspFaRzpmqkVgMtIk2HG9uMAbH6wdAIdNh3TVzCsch/+cRi+6l2//UGDKe6NB9BW3cvuR5ezMQ0w==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@lezer/common": "^1.3.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@lezer/lr": {
|
||||
"version": "1.4.10",
|
||||
"resolved": "https://registry.npmjs.org/@lezer/lr/-/lr-1.4.10.tgz",
|
||||
"integrity": "sha512-rnCpTIBafOx4mRp43xOxDJbFipJm/c0cia/V5TiGlhmMa+wsSdoGmUN3w5Bqrks/09Q/D4tNAmWaT8p6NRi77A==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@lezer/common": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@lezer/python": {
|
||||
"version": "1.1.19",
|
||||
"resolved": "https://registry.npmjs.org/@lezer/python/-/python-1.1.19.tgz",
|
||||
"integrity": "sha512-MhQIURHRytsNzP/YXnqpYKW6la6voAH3kyplTOOiCdjyFY6cWWGFVmYVdHIPrElqSDf4iCDktQCockB9FxuhzQ==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@lezer/common": "^1.2.0",
|
||||
"@lezer/highlight": "^1.0.0",
|
||||
"@lezer/lr": "^1.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/@marijn/find-cluster-break": {
|
||||
"version": "1.0.4",
|
||||
"resolved": "https://registry.npmjs.org/@marijn/find-cluster-break/-/find-cluster-break-1.0.4.tgz",
|
||||
"integrity": "sha512-Wy0V7+SGUjnF9/TkiM1hKVDPj7jKXduPNboMVtHTA8dySMURWqfg/JZ9E2Sq8JgSJmkl7k7Qe9FLeMSrSraWmQ==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/@napi-rs/lzma-linux-x64-gnu": {
|
||||
"version": "1.5.1",
|
||||
"resolved": "https://registry.npmjs.org/@napi-rs/lzma-linux-x64-gnu/-/lzma-linux-x64-gnu-1.5.1.tgz",
|
||||
@@ -1425,6 +1590,21 @@
|
||||
"integrity": "sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==",
|
||||
"license": "Python-2.0"
|
||||
},
|
||||
"node_modules/codemirror": {
|
||||
"version": "6.0.2",
|
||||
"resolved": "https://registry.npmjs.org/codemirror/-/codemirror-6.0.2.tgz",
|
||||
"integrity": "sha512-VhydHotNW5w1UGK0Qj96BwSk/Zqbp9WbnyK2W/eVMv4QyF41INRGpjUhFJY7/uDNuudSc33a/PKr4iDqRduvHw==",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@codemirror/autocomplete": "^6.0.0",
|
||||
"@codemirror/commands": "^6.0.0",
|
||||
"@codemirror/language": "^6.0.0",
|
||||
"@codemirror/lint": "^6.0.0",
|
||||
"@codemirror/search": "^6.0.0",
|
||||
"@codemirror/state": "^6.0.0",
|
||||
"@codemirror/view": "^6.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/commander": {
|
||||
"version": "8.3.0",
|
||||
"resolved": "https://registry.npmjs.org/commander/-/commander-8.3.0.tgz",
|
||||
@@ -1434,6 +1614,12 @@
|
||||
"node": ">= 12"
|
||||
}
|
||||
},
|
||||
"node_modules/crelt": {
|
||||
"version": "1.0.7",
|
||||
"resolved": "https://registry.npmjs.org/crelt/-/crelt-1.0.7.tgz",
|
||||
"integrity": "sha512-aK6BbWfhf4U/wCcLHKPJl/xa6VkVstRaPywWtMKGwuOLc/wZTyQYuoxgvZnNsBvv7Kg3YTBQYYBCggcviQczuA==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/csstype": {
|
||||
"version": "3.2.3",
|
||||
"resolved": "https://registry.npmjs.org/csstype/-/csstype-3.2.3.tgz",
|
||||
@@ -2071,6 +2257,12 @@
|
||||
"node": ">=0.10.0"
|
||||
}
|
||||
},
|
||||
"node_modules/style-mod": {
|
||||
"version": "4.1.4",
|
||||
"resolved": "https://registry.npmjs.org/style-mod/-/style-mod-4.1.4.tgz",
|
||||
"integrity": "sha512-XXWIQt633/EpAFx8aZDOTjBzrCaGmhvEQlQo6MVPfa2OzO2cWo+4hV9h+6UkHYlXGfy+ODXKUdP7Pthmcu5ATw==",
|
||||
"license": "MIT"
|
||||
},
|
||||
"node_modules/tailwindcss": {
|
||||
"version": "4.3.3",
|
||||
"resolved": "https://registry.npmjs.org/tailwindcss/-/tailwindcss-4.3.3.tgz",
|
||||
@@ -2225,6 +2417,12 @@
|
||||
"peerDependencies": {
|
||||
"vue": "^3.5.0"
|
||||
}
|
||||
},
|
||||
"node_modules/w3c-keyname": {
|
||||
"version": "2.2.8",
|
||||
"resolved": "https://registry.npmjs.org/w3c-keyname/-/w3c-keyname-2.2.8.tgz",
|
||||
"integrity": "sha512-dpojBhNsCNN7T82Tm7k26A6G9ML3NkhDsnw9n/eoxSRlVBB4CEtIQ/KTCLI2Fwf3ataSXRhYFkQi3SlnFwPvPQ==",
|
||||
"license": "MIT"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,16 @@
|
||||
"preview": "vite preview"
|
||||
},
|
||||
"dependencies": {
|
||||
"@codemirror/commands": "^6.11.1",
|
||||
"@codemirror/lang-cpp": "^6.0.3",
|
||||
"@codemirror/lang-python": "^6.2.1",
|
||||
"@codemirror/language": "^6.12.4",
|
||||
"@codemirror/lint": "^6.9.7",
|
||||
"@codemirror/state": "^6.7.6",
|
||||
"@codemirror/view": "^6.43.13",
|
||||
"@lezer/highlight": "^1.2.5",
|
||||
"@vscode/markdown-it-katex": "^1.1.2",
|
||||
"codemirror": "^6.0.2",
|
||||
"dompurify": "^3.4.17",
|
||||
"highlight.js": "^11.11.1",
|
||||
"katex": "^0.16.25",
|
||||
|
||||
+27
-2
@@ -1,12 +1,18 @@
|
||||
<script setup>
|
||||
/**
|
||||
* Root: the header with the page links, and the outlet.
|
||||
*
|
||||
* A route with meta.wide (the solve page) gets the whole window instead of
|
||||
* the reading column, and on a desktop the shell is exactly the viewport
|
||||
* tall so the page can split the space under the header into panes.
|
||||
*/
|
||||
import { RouterLink, RouterView } from "vue-router";
|
||||
import { RouterLink, RouterView, useRoute } from "vue-router";
|
||||
|
||||
const route = useRoute();
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="shell">
|
||||
<div class="shell" :class="{ wide: route.meta.wide }">
|
||||
<header class="top">
|
||||
<RouterLink to="/" class="brand">USACO practice</RouterLink>
|
||||
<nav>
|
||||
@@ -29,6 +35,25 @@ import { RouterLink, RouterView } from "vue-router";
|
||||
margin: 0 auto;
|
||||
padding: 0 16px 48px;
|
||||
}
|
||||
.shell.wide {
|
||||
max-width: none;
|
||||
padding-bottom: 16px;
|
||||
}
|
||||
.shell.wide .top {
|
||||
margin-bottom: 12px;
|
||||
}
|
||||
@media (min-width: 900px) {
|
||||
.shell.wide {
|
||||
height: 100vh;
|
||||
height: 100dvh;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
.shell.wide main {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
}
|
||||
}
|
||||
.top {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
|
||||
@@ -51,3 +51,12 @@ export const deleteReview = (name, id) =>
|
||||
// For an <audio> src: the browser fetches it, with ranges for seeking.
|
||||
export const reviewAudioUrl = (name, id) =>
|
||||
`${BASE}/problems/${encodeURIComponent(name)}/reviews/${encodeURIComponent(id)}/audio`;
|
||||
|
||||
// The solve page (admin only). run and lint go to the code runner
|
||||
// (RUNNER_URL) and answer when it does: a run can take a minute.
|
||||
export const getCode = (name) => request("GET", `/problems/${encodeURIComponent(name)}/code`);
|
||||
// body: { lang, source, mode: "tests" | "custom", input? }
|
||||
export const runCode = (name, body) => request("POST", `/problems/${encodeURIComponent(name)}/run`, body);
|
||||
export const lintCode = (name, body) => request("POST", `/problems/${encodeURIComponent(name)}/lint`, body);
|
||||
// Commits the source as the problem's solution file in that language.
|
||||
export const saveCode = (name, body) => request("PUT", `/problems/${encodeURIComponent(name)}/code`, body);
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
<script setup>
|
||||
/**
|
||||
* The solve page's editor: CodeMirror 6 behind a v-model, in the paper
|
||||
* palette (the colours are CodeBlock's highlight.js ones, so code reads the
|
||||
* same in both).
|
||||
*
|
||||
* Each language keeps its own EditorState, rather than one state whose
|
||||
* language sits in a Compartment: reconfiguring a shared state would give
|
||||
* the two languages one undo history, so Ctrl+Z after a switch would bring
|
||||
* back the other language's code. With a state each, switching back also
|
||||
* restores the cursor and history where they were.
|
||||
*
|
||||
* Diagnostics come in as a prop, already fetched by the page (its lint call
|
||||
* is remote and debounced by the page), and are pushed with setDiagnostics
|
||||
* rather than through a linter() source the editor would schedule itself.
|
||||
* CodeMirror maps them through later edits until the next set arrives.
|
||||
*/
|
||||
import { onBeforeUnmount, onMounted, ref, watch } from "vue";
|
||||
import { basicSetup } from "codemirror";
|
||||
import { EditorState, Prec } from "@codemirror/state";
|
||||
import { EditorView, keymap } from "@codemirror/view";
|
||||
import { indentWithTab } from "@codemirror/commands";
|
||||
import { HighlightStyle, indentUnit, syntaxHighlighting } from "@codemirror/language";
|
||||
import { lintGutter, setDiagnostics } from "@codemirror/lint";
|
||||
import { cpp } from "@codemirror/lang-cpp";
|
||||
import { python } from "@codemirror/lang-python";
|
||||
import { tags as t } from "@lezer/highlight";
|
||||
|
||||
const props = defineProps({
|
||||
modelValue: { type: String, required: true },
|
||||
lang: { type: String, required: true },
|
||||
// The server's: 1-based line/col, endLine/endCol optional.
|
||||
diagnostics: { type: Array, default: () => [] },
|
||||
label: { type: String, default: "Code" },
|
||||
});
|
||||
const emit = defineEmits(["update:modelValue", "run", "submit", "save"]);
|
||||
|
||||
// The workspace's styles: clang-format's two spaces for C++, PEP 8's four.
|
||||
const LANGS = {
|
||||
cpp: () => [cpp(), indentUnit.of(" ")],
|
||||
c: () => [cpp(), indentUnit.of(" ")],
|
||||
python: () => [python(), indentUnit.of(" ")],
|
||||
};
|
||||
|
||||
const highlight = HighlightStyle.define([
|
||||
{ tag: t.comment, color: "#6a737d", fontStyle: "italic" },
|
||||
{ tag: [t.keyword, t.controlKeyword, t.operatorKeyword, t.definitionKeyword, t.moduleKeyword, t.modifier, t.self], color: "#a3268d" },
|
||||
{ tag: [t.typeName, t.className, t.namespace, t.standard(t.typeName)], color: "#0b6e99" },
|
||||
{ tag: [t.string, t.special(t.string), t.character, t.escape], color: "#1a7f37" },
|
||||
{ tag: [t.number, t.bool, t.null, t.atom, t.literal], color: "#b35900" },
|
||||
{
|
||||
tag: [t.function(t.variableName), t.function(t.definition(t.variableName)), t.function(t.propertyName)],
|
||||
color: "#5a3fc0",
|
||||
},
|
||||
{ tag: [t.processingInstruction, t.meta, t.special(t.name)], color: "#8250df" },
|
||||
{ tag: t.invalid, color: "var(--color-ink-danger)" },
|
||||
]);
|
||||
|
||||
const theme = EditorView.theme(
|
||||
{
|
||||
"&": {
|
||||
height: "100%",
|
||||
fontSize: "0.8125rem",
|
||||
color: "var(--color-ink-soft)",
|
||||
backgroundColor: "var(--color-paper)",
|
||||
},
|
||||
// The wrapper draws focus, and the site's global :focus-visible ring
|
||||
// would otherwise land on the contenteditable inside.
|
||||
"&.cm-focused": { outline: "none" },
|
||||
".cm-content:focus-visible": { outline: "none" },
|
||||
".cm-scroller": { fontFamily: "var(--font-mono)", lineHeight: "1.55" },
|
||||
".cm-content": { caretColor: "var(--color-ink)", padding: "12px 0" },
|
||||
".cm-cursor, .cm-dropCursor": { borderLeftColor: "var(--color-ink)" },
|
||||
".cm-gutters": {
|
||||
backgroundColor: "var(--color-paper)",
|
||||
color: "var(--color-ink-faint)",
|
||||
borderRight: "1px solid var(--color-line-soft)",
|
||||
},
|
||||
".cm-lineNumbers .cm-gutterElement": { padding: "0 10px 0 12px" },
|
||||
".cm-activeLine": { backgroundColor: "rgba(0, 0, 0, 0.03)" },
|
||||
".cm-activeLineGutter": { backgroundColor: "transparent", color: "var(--color-ink)" },
|
||||
"&.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground, .cm-selectionBackground, .cm-content ::selection":
|
||||
{ backgroundColor: "#d7e6fb" },
|
||||
".cm-selectionMatch": { backgroundColor: "#eef3e2" },
|
||||
"&.cm-focused .cm-matchingBracket": { backgroundColor: "var(--color-paper-shade)", outline: "1px solid var(--color-line)" },
|
||||
".cm-tooltip": {
|
||||
border: "1px solid var(--color-line)",
|
||||
borderRadius: "4px",
|
||||
backgroundColor: "var(--color-paper)",
|
||||
color: "var(--color-ink)",
|
||||
},
|
||||
".cm-diagnostic": { fontFamily: "var(--font-mono)", fontSize: "0.8rem" },
|
||||
".cm-diagnostic-error": { borderLeftColor: "var(--color-ink-danger)" },
|
||||
".cm-diagnostic-warning": { borderLeftColor: "var(--color-solving)" },
|
||||
".cm-diagnostic-info": { borderLeftColor: "var(--color-ink-faint)" },
|
||||
".cm-diagnosticSource": { color: "var(--color-ink-muted)" },
|
||||
".cm-panels": { backgroundColor: "var(--color-paper-tint)", color: "var(--color-ink)" },
|
||||
},
|
||||
{ dark: false },
|
||||
);
|
||||
|
||||
// The page also listens for these on the window, for when the editor is not
|
||||
// focused; stopping them here keeps one press from running twice, and keeps
|
||||
// basicSetup's Mod-Enter (insert a blank line) out of the way.
|
||||
const pageKeys = Prec.highest(
|
||||
keymap.of([
|
||||
{ key: "Mod-Shift-Enter", run: () => (emit("submit"), true), stopPropagation: true },
|
||||
{ key: "Mod-Enter", run: () => (emit("run"), true), stopPropagation: true },
|
||||
{ key: "Mod-s", run: () => (emit("save"), true), preventDefault: true, stopPropagation: true },
|
||||
]),
|
||||
);
|
||||
|
||||
const host = ref(null);
|
||||
let view = null;
|
||||
const states = new Map();
|
||||
|
||||
function newState(doc, lang) {
|
||||
return EditorState.create({
|
||||
doc,
|
||||
extensions: [
|
||||
pageKeys,
|
||||
basicSetup,
|
||||
keymap.of([indentWithTab]),
|
||||
(LANGS[lang] || (() => []))(),
|
||||
syntaxHighlighting(highlight),
|
||||
lintGutter(),
|
||||
theme,
|
||||
EditorView.contentAttributes.of({ "aria-label": props.label }),
|
||||
EditorView.updateListener.of((u) => {
|
||||
if (u.docChanged) emit("update:modelValue", u.state.doc.toString());
|
||||
}),
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
// The server's positions as document offsets. Columns are taken as
|
||||
// characters; anything past the end of a line or the document is clamped,
|
||||
// since the source may have moved on since the lint ran. Without an end
|
||||
// the mark covers the word at the start, or the rest of the line.
|
||||
function toDiagnostic(d, doc) {
|
||||
const lineAt = (n) => doc.line(Math.min(Math.max(n || 1, 1), doc.lines));
|
||||
const at = (lineNo, col) => {
|
||||
const line = lineAt(lineNo);
|
||||
return line.from + Math.min(Math.max((col || 1) - 1, 0), line.length);
|
||||
};
|
||||
const from = at(d.line, d.col);
|
||||
let to = d.endLine > 0 ? at(d.endLine, d.endCol || 1) : from;
|
||||
if (to <= from) {
|
||||
const line = doc.lineAt(from);
|
||||
const word = /^\w+/.exec(line.text.slice(from - line.from));
|
||||
to = word ? from + word[0].length : line.to;
|
||||
}
|
||||
const severity = ["error", "warning", "info"].includes(d.severity) ? d.severity : "info";
|
||||
const source = [d.source, d.rule].filter(Boolean).join(" ");
|
||||
return { from, to, severity, message: d.message, ...(source ? { source } : {}) };
|
||||
}
|
||||
|
||||
function pushDiagnostics() {
|
||||
const doc = view.state.doc;
|
||||
view.dispatch(setDiagnostics(view.state, props.diagnostics.map((d) => toDiagnostic(d, doc))));
|
||||
}
|
||||
|
||||
onMounted(() => {
|
||||
view = new EditorView({ state: newState(props.modelValue, props.lang), parent: host.value });
|
||||
states.set(props.lang, view.state);
|
||||
});
|
||||
onBeforeUnmount(() => view?.destroy());
|
||||
|
||||
// The page sets the language and its source in one go, so they are watched
|
||||
// together: a new language swaps in that language's state; a new value in
|
||||
// the same language (Reset, Save's commit) replaces the text as an edit,
|
||||
// so Ctrl+Z can bring the old text back.
|
||||
watch(
|
||||
() => [props.lang, props.modelValue],
|
||||
([lang, value], [oldLang]) => {
|
||||
if (!view) return;
|
||||
if (lang !== oldLang) {
|
||||
states.set(oldLang, view.state);
|
||||
const kept = states.get(lang);
|
||||
view.setState(kept && kept.doc.toString() === value ? kept : newState(value, lang));
|
||||
pushDiagnostics();
|
||||
} else if (value !== view.state.doc.toString()) {
|
||||
view.dispatch({ changes: { from: 0, to: view.state.doc.length, insert: value } });
|
||||
}
|
||||
},
|
||||
);
|
||||
watch(() => props.diagnostics, () => view && pushDiagnostics());
|
||||
|
||||
defineExpose({ focus: () => view?.focus() });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div ref="host" class="editor"></div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.editor {
|
||||
height: 100%;
|
||||
min-height: 0;
|
||||
border: 1px solid var(--color-line-soft);
|
||||
border-radius: 6px;
|
||||
overflow: hidden;
|
||||
background: var(--color-paper);
|
||||
}
|
||||
.editor:focus-within {
|
||||
border-color: var(--color-ink-muted);
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,314 @@
|
||||
<script setup>
|
||||
/**
|
||||
* The solve page's bottom panel: the custom input box, and what the last run
|
||||
* came back with, a tab per test (or one for a custom run). A compile
|
||||
* failure gets a tab of its own and there are no results; warnings from a
|
||||
* build that worked get one too, so they are not lost behind the verdicts.
|
||||
*
|
||||
* After a run the panel opens the tab worth reading first: the compile
|
||||
* error, else the first test that failed, else the first test.
|
||||
*/
|
||||
import { computed, ref, watch } from "vue";
|
||||
import CodeBlock from "@/components/CodeBlock.vue";
|
||||
import { MOD_KEY } from "@/format";
|
||||
|
||||
const props = defineProps({
|
||||
// The run response, plus the mode it was run in.
|
||||
result: { type: Object, default: null },
|
||||
// "custom" or "tests" while a run is in flight.
|
||||
running: { type: String, default: "" },
|
||||
error: { type: String, default: "" },
|
||||
modelValue: { type: String, default: "" },
|
||||
});
|
||||
defineEmits(["update:modelValue"]);
|
||||
|
||||
const VERDICTS = {
|
||||
AC: "Accepted",
|
||||
WA: "Wrong answer",
|
||||
RE: "Runtime error",
|
||||
TLE: "Time limit exceeded",
|
||||
unchecked: "Ran",
|
||||
};
|
||||
|
||||
const tab = ref("custom");
|
||||
|
||||
const compile = computed(() => props.result?.compile);
|
||||
const results = computed(() => props.result?.results || []);
|
||||
const warnings = computed(() => compile.value?.ok && compile.value.output?.trim());
|
||||
|
||||
const tabs = computed(() => {
|
||||
const out = [{ id: "custom", label: "Custom input" }];
|
||||
if (compile.value && !compile.value.ok) out.push({ id: "compile", label: "Compile error", verdict: "bad" });
|
||||
if (warnings.value) out.push({ id: "warnings", label: "Warnings" });
|
||||
results.value.forEach((r, i) => {
|
||||
const label = props.result.mode === "custom" ? "Output" : `Test ${r.name}`;
|
||||
out.push({ id: `r${i}`, label, verdict: r.verdict });
|
||||
});
|
||||
return out;
|
||||
});
|
||||
|
||||
watch(
|
||||
() => props.result,
|
||||
(r) => {
|
||||
if (!r) return;
|
||||
if (!r.compile?.ok) {
|
||||
tab.value = "compile";
|
||||
return;
|
||||
}
|
||||
const failed = r.results.findIndex((x) => x.verdict !== "AC" && x.verdict !== "unchecked");
|
||||
tab.value = r.results.length ? `r${Math.max(failed, 0)}` : "custom";
|
||||
},
|
||||
);
|
||||
|
||||
const current = computed(() => (tab.value.startsWith("r") ? results.value[Number(tab.value.slice(1))] : null));
|
||||
|
||||
const summary = computed(() => {
|
||||
const c = compile.value;
|
||||
if (!c) return "";
|
||||
const build = `${c.timeMs} ms compile`;
|
||||
if (!c.ok) return `Compile error · ${build}`;
|
||||
const rs = results.value;
|
||||
if (props.result.mode === "custom") {
|
||||
return rs.length ? `${VERDICTS[rs[0].verdict] || rs[0].verdict} in ${rs[0].timeMs} ms · ${build}` : build;
|
||||
}
|
||||
if (!rs.length) return `No saved tests · ${build}`;
|
||||
const passed = rs.filter((r) => r.verdict === "AC").length;
|
||||
return `${passed}/${rs.length} passed · ${build}`;
|
||||
});
|
||||
|
||||
const verdictClass = (v) => (v === "AC" ? "ok" : v === "unchecked" || !v ? "plain" : "bad");
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="results card">
|
||||
<div class="bar">
|
||||
<div class="tabs" role="tablist" aria-label="Run results">
|
||||
<button
|
||||
v-for="tb in tabs"
|
||||
:id="`tab-${tb.id}`"
|
||||
:key="tb.id"
|
||||
type="button"
|
||||
role="tab"
|
||||
:aria-selected="tab === tb.id"
|
||||
aria-controls="run-panel"
|
||||
:class="{ on: tab === tb.id }"
|
||||
@click="tab = tb.id"
|
||||
>
|
||||
<span v-if="tb.verdict" class="dot" :class="verdictClass(tb.verdict)" aria-hidden="true"></span>
|
||||
{{ tb.label }}
|
||||
</button>
|
||||
</div>
|
||||
<span v-if="running" class="working" role="status">
|
||||
{{ running === "tests" ? "Running the saved tests…" : "Running on your input…" }}
|
||||
</span>
|
||||
<span v-else-if="summary" class="summary muted" role="status">{{ summary }}</span>
|
||||
</div>
|
||||
|
||||
<div id="run-panel" class="body" role="tabpanel" :aria-labelledby="`tab-${tab}`">
|
||||
<p v-if="error" class="error">{{ error }}</p>
|
||||
|
||||
<label v-if="tab === 'custom'" class="custom">
|
||||
<span class="muted">
|
||||
Input for Run ({{ MOD_KEY }}+Enter). Submit ({{ MOD_KEY }}+Shift+Enter) runs the saved tests instead.
|
||||
</span>
|
||||
<textarea
|
||||
:value="modelValue"
|
||||
rows="6"
|
||||
spellcheck="false"
|
||||
placeholder="stdin for your program"
|
||||
@input="$emit('update:modelValue', $event.target.value)"
|
||||
></textarea>
|
||||
</label>
|
||||
|
||||
<template v-else-if="tab === 'compile'">
|
||||
<CodeBlock :content="compile.output || 'The compiler said nothing.'" />
|
||||
</template>
|
||||
|
||||
<template v-else-if="tab === 'warnings'">
|
||||
<CodeBlock :content="compile.output" />
|
||||
</template>
|
||||
|
||||
<div v-else-if="current" class="result">
|
||||
<p class="facts">
|
||||
<span class="badge verdict" :class="verdictClass(current.verdict)">
|
||||
{{ VERDICTS[current.verdict] || current.verdict }}
|
||||
</span>
|
||||
<span class="muted">{{ current.timeMs }} ms</span>
|
||||
<span v-if="current.exitCode" class="muted">exit code {{ current.exitCode }}</span>
|
||||
<span v-if="current.message" class="error">{{ current.message }}</span>
|
||||
</p>
|
||||
<p v-if="current.truncated" class="muted note">The output was cut short by the runner.</p>
|
||||
<div>
|
||||
<p class="filename muted">Input</p>
|
||||
<CodeBlock v-if="current.input" :content="current.input" />
|
||||
<p v-else class="muted empty">(empty)</p>
|
||||
</div>
|
||||
<div class="io">
|
||||
<div>
|
||||
<p class="filename muted">Output</p>
|
||||
<CodeBlock v-if="current.output" :content="current.output" />
|
||||
<p v-else class="muted empty">(empty)</p>
|
||||
</div>
|
||||
<div v-if="current.expected !== undefined && current.expected !== null">
|
||||
<p class="filename muted">Expected</p>
|
||||
<CodeBlock v-if="current.expected" :content="current.expected" />
|
||||
<p v-else class="muted empty">(empty)</p>
|
||||
</div>
|
||||
</div>
|
||||
<div v-if="current.stderr">
|
||||
<p class="filename muted">stderr</p>
|
||||
<CodeBlock :content="current.stderr" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.results {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-height: 0;
|
||||
height: 100%;
|
||||
}
|
||||
.bar {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 4px 12px;
|
||||
padding: 6px 8px;
|
||||
border-bottom: 1px solid var(--color-line-soft);
|
||||
}
|
||||
.tabs {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 4px;
|
||||
min-width: 0;
|
||||
}
|
||||
.tabs button {
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
padding: 3px 10px;
|
||||
border: 1px solid var(--color-line-soft);
|
||||
border-radius: 4px;
|
||||
font-size: 0.8rem;
|
||||
color: var(--color-ink-muted);
|
||||
background: var(--color-paper);
|
||||
cursor: pointer;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.tabs button:hover {
|
||||
color: var(--color-ink);
|
||||
}
|
||||
.tabs button.on {
|
||||
border-color: var(--color-ink);
|
||||
color: var(--color-ink);
|
||||
background: var(--color-paper-shade);
|
||||
}
|
||||
.dot {
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
border-radius: 50%;
|
||||
background: var(--color-skipped);
|
||||
}
|
||||
.dot.ok {
|
||||
background: var(--color-solved);
|
||||
}
|
||||
.dot.bad {
|
||||
background: var(--color-ink-danger);
|
||||
}
|
||||
.summary,
|
||||
.working {
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
/* The pulsing dot of the problem page's jobs: the request is still open. */
|
||||
.working::before {
|
||||
content: "";
|
||||
display: inline-block;
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
margin-right: 8px;
|
||||
border-radius: 50%;
|
||||
background: var(--color-solving);
|
||||
animation: pulse 1.2s ease-in-out infinite;
|
||||
}
|
||||
@keyframes pulse {
|
||||
50% {
|
||||
opacity: 0.25;
|
||||
}
|
||||
}
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.working::before {
|
||||
animation: none;
|
||||
}
|
||||
}
|
||||
.body {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
overflow: auto;
|
||||
padding: 10px 12px 12px;
|
||||
display: grid;
|
||||
align-content: start;
|
||||
gap: 10px;
|
||||
}
|
||||
.custom {
|
||||
display: grid;
|
||||
gap: 6px;
|
||||
font-size: 0.8rem;
|
||||
}
|
||||
.custom textarea {
|
||||
width: 100%;
|
||||
padding: 8px 10px;
|
||||
border: 1px solid var(--color-line);
|
||||
border-radius: 4px;
|
||||
font-family: var(--font-mono);
|
||||
font-size: 0.8125rem;
|
||||
line-height: 1.55;
|
||||
color: var(--color-ink);
|
||||
background: var(--color-paper);
|
||||
resize: vertical;
|
||||
}
|
||||
.result {
|
||||
display: grid;
|
||||
gap: 10px;
|
||||
min-width: 0;
|
||||
}
|
||||
.result > div {
|
||||
min-width: 0;
|
||||
}
|
||||
.facts {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 4px 12px;
|
||||
font-size: 0.875rem;
|
||||
}
|
||||
.verdict.ok {
|
||||
color: var(--color-solved);
|
||||
border-color: currentColor;
|
||||
}
|
||||
.verdict.bad {
|
||||
color: var(--color-ink-danger);
|
||||
border-color: currentColor;
|
||||
}
|
||||
.note,
|
||||
.empty {
|
||||
font-size: 0.8rem;
|
||||
}
|
||||
.io {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
|
||||
gap: 12px;
|
||||
min-width: 0;
|
||||
}
|
||||
.io > div {
|
||||
min-width: 0;
|
||||
}
|
||||
.filename {
|
||||
font-family: var(--font-mono);
|
||||
font-size: 0.8rem;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
</style>
|
||||
@@ -23,3 +23,6 @@ export function dayKey(date) {
|
||||
const p = (n) => String(n).padStart(2, "0");
|
||||
return `${d.getFullYear()}-${p(d.getMonth() + 1)}-${p(d.getDate())}`;
|
||||
}
|
||||
|
||||
// The modifier the editor's shortcuts use, as the keyboard labels it.
|
||||
export const MOD_KEY = /Mac|iPhone|iPad/.test(navigator.platform || navigator.userAgent) ? "⌘" : "Ctrl";
|
||||
|
||||
@@ -12,6 +12,14 @@ export default createRouter({
|
||||
routes: [
|
||||
{ path: "/", name: "list", component: () => import("@/views/ProblemList.vue") },
|
||||
{ path: "/p/:name", name: "problem", component: () => import("@/views/ProblemPage.vue"), props: true },
|
||||
// wide: App drops its column width so the editor gets the whole window.
|
||||
{
|
||||
path: "/p/:name/solve",
|
||||
name: "solve",
|
||||
component: () => import("@/views/SolvePage.vue"),
|
||||
props: true,
|
||||
meta: { wide: true },
|
||||
},
|
||||
{ path: "/stats", name: "stats", component: () => import("@/views/StatsPage.vue") },
|
||||
{ path: "/cli", name: "cli", component: () => import("@/views/CliPage.vue") },
|
||||
{ path: "/:pathMatch(.*)*", redirect: "/" },
|
||||
|
||||
@@ -332,6 +332,7 @@ const moves = computed(() => {
|
||||
>
|
||||
{{ t.label }}
|
||||
</button>
|
||||
<RouterLink :to="`/p/${problem.name}/solve`" class="btn solve">Solve</RouterLink>
|
||||
</div>
|
||||
<div v-if="saving || busy.length" class="admin-row status" role="status">
|
||||
<span v-if="saving" class="muted">Saving to git…</span>
|
||||
@@ -581,6 +582,9 @@ h2 {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
.btn.solve {
|
||||
text-decoration: none;
|
||||
}
|
||||
.btn.tool.on {
|
||||
border-color: var(--color-ink);
|
||||
background: var(--color-paper-shade);
|
||||
|
||||
@@ -0,0 +1,653 @@
|
||||
<script setup>
|
||||
/**
|
||||
* Solve a problem in the browser (admin only): the statement and saved
|
||||
* tests on the left, an editor on the right with Run (the custom input),
|
||||
* Submit (the saved tests) and Save (a commit, like the CLI's).
|
||||
*
|
||||
* What is typed is kept as a draft in this browser per problem and language
|
||||
* until it is saved or reset, so leaving the page loses nothing and there
|
||||
* is no "unsaved changes" prompt. Runs and lints are plain requests to the
|
||||
* server, which passes them to the code runner: a run answers when it has
|
||||
* finished, which can take up to a minute.
|
||||
*/
|
||||
import { computed, onMounted, onUnmounted, ref, watch } from "vue";
|
||||
import { RouterLink } from "vue-router";
|
||||
import CodeBlock from "@/components/CodeBlock.vue";
|
||||
import CodeEditor from "@/components/CodeEditor.vue";
|
||||
import RunResults from "@/components/RunResults.vue";
|
||||
import { markdown } from "@/markdown";
|
||||
import { getCode, getMe, getProblem, lintCode, runCode, saveCode } from "@/api";
|
||||
import { LANG_LABELS, MOD_KEY, STATUS_LABELS, title } from "@/format";
|
||||
|
||||
const props = defineProps({ name: { type: String, required: true } });
|
||||
|
||||
const md = markdown();
|
||||
|
||||
const problem = ref(null);
|
||||
const admin = ref(false);
|
||||
const loading = ref(true);
|
||||
const error = ref("");
|
||||
// GET /code: the languages, and per language the repo's file or a starter.
|
||||
const info = ref(null);
|
||||
const lang = ref("");
|
||||
const source = ref("");
|
||||
const customInput = ref("");
|
||||
|
||||
const running = ref("");
|
||||
const result = ref(null);
|
||||
const runError = ref("");
|
||||
const saving = ref(false);
|
||||
const saveError = ref("");
|
||||
const notice = ref("");
|
||||
|
||||
const diagnostics = ref([]);
|
||||
const linted = ref(false);
|
||||
const lintError = ref("");
|
||||
|
||||
const statementHtml = computed(() => (problem.value?.statement ? md.render(problem.value.statement) : ""));
|
||||
const current = computed(() => info.value?.code[lang.value]);
|
||||
const dirty = computed(() => !!current.value && source.value !== current.value.source);
|
||||
const busy = computed(() => !!running.value || saving.value);
|
||||
const canRun = computed(() => !!info.value?.runner && !busy.value);
|
||||
// A starter can be saved as it is: that is how a language gets its file.
|
||||
const canSave = computed(() => !!current.value && !busy.value && (dirty.value || !current.value.saved));
|
||||
|
||||
const codeStatus = computed(() => {
|
||||
if (!current.value) return "";
|
||||
if (dirty.value) return "Unsaved draft";
|
||||
if (current.value.saved) return `Saved in repo as ${current.value.path}`;
|
||||
return "Starter template — not in the repo yet";
|
||||
});
|
||||
|
||||
async function load() {
|
||||
flushDraft();
|
||||
clearTimeout(lintTimer);
|
||||
const name = props.name;
|
||||
loading.value = true;
|
||||
error.value = "";
|
||||
info.value = null;
|
||||
result.value = null;
|
||||
running.value = "";
|
||||
runError.value = "";
|
||||
saveError.value = "";
|
||||
notice.value = "";
|
||||
diagnostics.value = [];
|
||||
linted.value = false;
|
||||
try {
|
||||
const [p, me] = await Promise.all([getProblem(name), getMe().catch(() => ({ admin: false }))]);
|
||||
if (name !== props.name) return;
|
||||
problem.value = p;
|
||||
admin.value = me.admin;
|
||||
customInput.value = p.sample?.content || "";
|
||||
// The code endpoints are admin-only; asking anyway would send the
|
||||
// visitor to the login page instead of explaining.
|
||||
if (!me.admin) return;
|
||||
const c = await getCode(name);
|
||||
if (name !== props.name) return;
|
||||
info.value = c;
|
||||
open(c.code[c.default] ? c.default : c.languages[0]);
|
||||
} catch (e) {
|
||||
if (name !== props.name) return;
|
||||
problem.value = null;
|
||||
error.value = e.status === 404 ? "No such problem." : e.message;
|
||||
} finally {
|
||||
if (name === props.name) loading.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// Shows a language: its draft if there is one, else the repo's file or the
|
||||
// starter.
|
||||
function open(l) {
|
||||
lang.value = l;
|
||||
source.value = readDraft(props.name, l) ?? info.value.code[l].source;
|
||||
diagnostics.value = [];
|
||||
linted.value = false;
|
||||
lintError.value = "";
|
||||
notice.value = "";
|
||||
saveError.value = "";
|
||||
lintSoon();
|
||||
}
|
||||
|
||||
function switchLang(l) {
|
||||
if (l === lang.value || busy.value) return;
|
||||
flushDraft();
|
||||
open(l);
|
||||
}
|
||||
|
||||
function reset() {
|
||||
const c = current.value;
|
||||
const to = c.saved ? "the saved code" : "the starter template";
|
||||
if (dirty.value && !window.confirm(`Discard your draft and go back to ${to}?`)) return;
|
||||
source.value = c.source;
|
||||
dropDraft(props.name, lang.value);
|
||||
}
|
||||
|
||||
// ---- drafts ----
|
||||
// Written half a second after typing stops. The pending write carries its
|
||||
// own problem and language, so a switch or navigation flushes it to the
|
||||
// right key.
|
||||
const draftKey = (name, l) => `usaco:draft:${name}:${l}`;
|
||||
let draftTimer = null;
|
||||
let pendingDraft = null;
|
||||
|
||||
function readDraft(name, l) {
|
||||
try {
|
||||
return localStorage.getItem(draftKey(name, l));
|
||||
} catch {
|
||||
return null; // Storage blocked (private mode, site data off): no drafts.
|
||||
}
|
||||
}
|
||||
|
||||
function scheduleDraft() {
|
||||
if (!current.value) return;
|
||||
pendingDraft = { name: props.name, lang: lang.value, source: source.value, saved: current.value.source };
|
||||
clearTimeout(draftTimer);
|
||||
draftTimer = setTimeout(flushDraft, 500);
|
||||
}
|
||||
|
||||
function flushDraft() {
|
||||
clearTimeout(draftTimer);
|
||||
const d = pendingDraft;
|
||||
pendingDraft = null;
|
||||
if (!d) return;
|
||||
try {
|
||||
// Typing back to what is saved is not a draft.
|
||||
if (d.source === d.saved) localStorage.removeItem(draftKey(d.name, d.lang));
|
||||
else localStorage.setItem(draftKey(d.name, d.lang), d.source);
|
||||
} catch {
|
||||
// As in readDraft.
|
||||
}
|
||||
}
|
||||
|
||||
function dropDraft(name, l) {
|
||||
if (pendingDraft?.name === name && pendingDraft.lang === l) {
|
||||
clearTimeout(draftTimer);
|
||||
pendingDraft = null;
|
||||
}
|
||||
try {
|
||||
localStorage.removeItem(draftKey(name, l));
|
||||
} catch {
|
||||
// As in readDraft.
|
||||
}
|
||||
}
|
||||
|
||||
// ---- lint ----
|
||||
// 1.5 s after typing stops, and at once on Run and Submit. Only the newest
|
||||
// request counts, and only while the text is still what was linted: an
|
||||
// edit since then has already scheduled another.
|
||||
let lintTimer = null;
|
||||
let lintSeq = 0;
|
||||
|
||||
function lintSoon() {
|
||||
clearTimeout(lintTimer);
|
||||
if (info.value?.runner) lintTimer = setTimeout(lintNow, 1500);
|
||||
}
|
||||
|
||||
async function lintNow() {
|
||||
clearTimeout(lintTimer);
|
||||
if (!info.value?.runner) return;
|
||||
const seq = ++lintSeq;
|
||||
const name = props.name;
|
||||
const l = lang.value;
|
||||
const src = source.value;
|
||||
try {
|
||||
const r = await lintCode(name, { lang: l, source: src });
|
||||
if (seq !== lintSeq || name !== props.name || l !== lang.value || src !== source.value) return;
|
||||
diagnostics.value = r.diagnostics || [];
|
||||
linted.value = true;
|
||||
lintError.value = "";
|
||||
} catch (e) {
|
||||
if (seq === lintSeq) lintError.value = e.message;
|
||||
}
|
||||
}
|
||||
|
||||
const lintSummary = computed(() => {
|
||||
const count = (s) => diagnostics.value.filter((d) => d.severity === s).length;
|
||||
const plural = (n, word) => `${n} ${word}${n === 1 ? "" : "s"}`;
|
||||
const [e, w, i] = [count("error"), count("warning"), count("info")];
|
||||
return {
|
||||
errors: e ? plural(e, "error") : "",
|
||||
rest: [w && plural(w, "warning"), i && plural(i, "note")].filter(Boolean).join(" · "),
|
||||
};
|
||||
});
|
||||
|
||||
watch(source, () => {
|
||||
notice.value = "";
|
||||
scheduleDraft();
|
||||
lintSoon();
|
||||
});
|
||||
|
||||
// ---- run, submit, save ----
|
||||
async function run(mode) {
|
||||
if (!canRun.value) return;
|
||||
const name = props.name;
|
||||
const body = { lang: lang.value, source: source.value, mode };
|
||||
if (mode === "custom") body.input = customInput.value;
|
||||
running.value = mode;
|
||||
runError.value = "";
|
||||
lintNow();
|
||||
try {
|
||||
const r = await runCode(name, body);
|
||||
if (name === props.name) result.value = { ...r, mode };
|
||||
} catch (e) {
|
||||
if (name === props.name) runError.value = e.message;
|
||||
} finally {
|
||||
if (name === props.name) running.value = "";
|
||||
}
|
||||
}
|
||||
|
||||
async function save() {
|
||||
if (!canSave.value) return;
|
||||
const name = props.name;
|
||||
const l = lang.value;
|
||||
const src = source.value;
|
||||
saving.value = true;
|
||||
saveError.value = "";
|
||||
notice.value = "";
|
||||
try {
|
||||
const { path } = await saveCode(name, { lang: l, source: src });
|
||||
if (name !== props.name) return;
|
||||
info.value.code[l] = { ...info.value.code[l], path, source: src, saved: true };
|
||||
// Typing during the commit made a newer draft; that one stays.
|
||||
if (source.value === src) dropDraft(name, l);
|
||||
notice.value = `Committed ${path}`;
|
||||
} catch (e) {
|
||||
if (name === props.name) saveError.value = e.message;
|
||||
} finally {
|
||||
if (name === props.name) saving.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
// The editor handles these itself while it has focus (and stops them);
|
||||
// this is for everywhere else on the page, the custom input included.
|
||||
function onKey(e) {
|
||||
if (!info.value || !(e.ctrlKey || e.metaKey) || e.altKey) return;
|
||||
if (e.key === "Enter") {
|
||||
e.preventDefault();
|
||||
run(e.shiftKey ? "tests" : "custom");
|
||||
} else if (e.key.toLowerCase() === "s" && !e.shiftKey) {
|
||||
e.preventDefault();
|
||||
save();
|
||||
}
|
||||
}
|
||||
|
||||
// A draft still in its half second would be lost with the tab.
|
||||
onMounted(() => {
|
||||
window.addEventListener("keydown", onKey);
|
||||
window.addEventListener("pagehide", flushDraft);
|
||||
});
|
||||
onUnmounted(() => {
|
||||
window.removeEventListener("keydown", onKey);
|
||||
window.removeEventListener("pagehide", flushDraft);
|
||||
clearTimeout(lintTimer);
|
||||
flushDraft();
|
||||
});
|
||||
|
||||
// ---- the divider between the panes ----
|
||||
// A share of the width, remembered in this browser.
|
||||
const SPLIT_KEY = "usaco:solve:split";
|
||||
const panes = ref(null);
|
||||
const split = ref(readSplit());
|
||||
|
||||
function readSplit() {
|
||||
try {
|
||||
const v = Number(localStorage.getItem(SPLIT_KEY));
|
||||
return v >= 20 && v <= 75 ? v : 40;
|
||||
} catch {
|
||||
return 40;
|
||||
}
|
||||
}
|
||||
|
||||
function setSplit(v) {
|
||||
split.value = Math.min(75, Math.max(20, v));
|
||||
}
|
||||
|
||||
function keepSplit() {
|
||||
try {
|
||||
localStorage.setItem(SPLIT_KEY, String(Math.round(split.value)));
|
||||
} catch {
|
||||
// As in readDraft.
|
||||
}
|
||||
}
|
||||
|
||||
function drag(e) {
|
||||
e.preventDefault();
|
||||
const handle = e.currentTarget;
|
||||
const box = panes.value.getBoundingClientRect();
|
||||
handle.setPointerCapture(e.pointerId);
|
||||
const move = (ev) => setSplit(((ev.clientX - box.left) / box.width) * 100);
|
||||
const up = () => {
|
||||
handle.removeEventListener("pointermove", move);
|
||||
handle.removeEventListener("pointerup", up);
|
||||
handle.removeEventListener("pointercancel", up);
|
||||
keepSplit();
|
||||
};
|
||||
handle.addEventListener("pointermove", move);
|
||||
handle.addEventListener("pointerup", up);
|
||||
handle.addEventListener("pointercancel", up);
|
||||
}
|
||||
|
||||
function nudge(e) {
|
||||
const step = { ArrowLeft: -2, ArrowRight: 2 }[e.key];
|
||||
if (!step) return;
|
||||
e.preventDefault();
|
||||
setSplit(split.value + step);
|
||||
keepSplit();
|
||||
}
|
||||
|
||||
watch(() => props.name, load, { immediate: true });
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<p v-if="loading" class="muted">Loading…</p>
|
||||
<div v-else-if="error">
|
||||
<p class="error">{{ error }}</p>
|
||||
<RouterLink to="/">Back to the list</RouterLink>
|
||||
</div>
|
||||
<div v-else-if="problem && !admin" class="denied card">
|
||||
<p>Solving in the browser is for the site's admin.</p>
|
||||
<RouterLink :to="`/p/${name}`">Back to {{ title(problem.name) }}</RouterLink>
|
||||
</div>
|
||||
|
||||
<div v-else-if="problem && info" ref="panes" class="solve" :style="{ '--left': `${split}%` }">
|
||||
<aside class="left card">
|
||||
<RouterLink :to="`/p/${problem.name}`" class="back">← Problem page</RouterLink>
|
||||
<h1>{{ title(problem.name) }}</h1>
|
||||
<div class="facts">
|
||||
<span class="badge" :class="problem.status">{{ STATUS_LABELS[problem.status] }}</span>
|
||||
<a v-if="problem.url" :href="problem.url" target="_blank" rel="noopener noreferrer">
|
||||
{{ problem.source || "Problem statement" }} ↗
|
||||
</a>
|
||||
<span v-if="info.io" class="muted">File I/O: <code>{{ info.io }}.in</code></span>
|
||||
<span v-if="info.leetcode" class="muted">LeetCode</span>
|
||||
</div>
|
||||
|
||||
<div v-if="statementHtml" class="statement prose" v-html="statementHtml"></div>
|
||||
<p v-else class="muted">
|
||||
No statement saved. Add it under Edit details on the
|
||||
<RouterLink :to="`/p/${problem.name}`">problem page</RouterLink>.
|
||||
</p>
|
||||
|
||||
<section v-if="problem.testCases.length">
|
||||
<h2>Tests</h2>
|
||||
<details v-for="t in problem.testCases" :key="t.name" class="test">
|
||||
<summary>Test {{ t.name }}</summary>
|
||||
<div class="io">
|
||||
<div>
|
||||
<p class="filename muted">Input</p>
|
||||
<CodeBlock :content="t.input.content" :truncated="t.input.truncated" />
|
||||
</div>
|
||||
<div>
|
||||
<p class="filename muted">Expected output</p>
|
||||
<CodeBlock v-if="t.output" :content="t.output.content" :truncated="t.output.truncated" />
|
||||
<p v-else class="muted">Not saved.</p>
|
||||
</div>
|
||||
</div>
|
||||
</details>
|
||||
</section>
|
||||
<section v-else-if="problem.sample">
|
||||
<h2>Sample input</h2>
|
||||
<CodeBlock :content="problem.sample.content" :truncated="problem.sample.truncated" />
|
||||
</section>
|
||||
</aside>
|
||||
|
||||
<div
|
||||
class="split"
|
||||
role="separator"
|
||||
tabindex="0"
|
||||
aria-orientation="vertical"
|
||||
aria-label="Resize the panes"
|
||||
aria-valuemin="20"
|
||||
aria-valuemax="75"
|
||||
:aria-valuenow="Math.round(split)"
|
||||
@pointerdown="drag"
|
||||
@keydown="nudge"
|
||||
></div>
|
||||
|
||||
<div class="right">
|
||||
<div class="toolbar">
|
||||
<select aria-label="Language" :value="lang" :disabled="busy" @change="switchLang($event.target.value)">
|
||||
<option v-for="l in info.languages" :key="l" :value="l">{{ LANG_LABELS[l] || l }}</option>
|
||||
</select>
|
||||
<button class="btn" type="button" :disabled="busy || !dirty" @click="reset">
|
||||
Reset to {{ current.saved ? "saved" : "starter" }}
|
||||
</button>
|
||||
<span class="grow"></span>
|
||||
<button
|
||||
class="btn"
|
||||
type="button"
|
||||
:disabled="!canRun"
|
||||
:title="`Run on the custom input (${MOD_KEY}+Enter)`"
|
||||
@click="run('custom')"
|
||||
>
|
||||
{{ running === "custom" ? "Running…" : "Run" }}
|
||||
</button>
|
||||
<button
|
||||
class="btn primary"
|
||||
type="button"
|
||||
:disabled="!canRun"
|
||||
:title="`Run the saved tests (${MOD_KEY}+Shift+Enter)`"
|
||||
@click="run('tests')"
|
||||
>
|
||||
{{ running === "tests" ? "Submitting…" : "Submit" }}
|
||||
</button>
|
||||
<button class="btn" type="button" :disabled="!canSave" :title="`Commit to the repo (${MOD_KEY}+S)`" @click="save">
|
||||
{{ saving ? "Saving…" : "Save" }}
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="status" role="status">
|
||||
<span :class="{ draft: dirty }">{{ codeStatus }}</span>
|
||||
<span v-if="saving" class="muted">Committing to git…</span>
|
||||
<span v-if="notice" class="done">{{ notice }}</span>
|
||||
<span v-if="saveError" class="error">Save: {{ saveError }}</span>
|
||||
<span class="grow"></span>
|
||||
<span v-if="lintError" class="error" :title="lintError">Lint failed</span>
|
||||
<span v-else-if="linted" class="lint">
|
||||
<span v-if="lintSummary.errors" class="error">{{ lintSummary.errors }}</span>
|
||||
<span v-if="lintSummary.errors && lintSummary.rest" class="muted"> · </span>
|
||||
<span v-if="lintSummary.rest" class="warn">{{ lintSummary.rest }}</span>
|
||||
<span v-if="!lintSummary.errors && !lintSummary.rest" class="muted">No lint issues</span>
|
||||
</span>
|
||||
</div>
|
||||
<p v-if="!info.runner" class="muted note">
|
||||
The code runner isn't configured (RUNNER_URL), so Run, Submit and lint are off. Save still works.
|
||||
</p>
|
||||
|
||||
<CodeEditor
|
||||
v-model="source"
|
||||
class="code"
|
||||
:lang="lang"
|
||||
:diagnostics="diagnostics"
|
||||
:label="`${LANG_LABELS[lang] || lang} solution`"
|
||||
@run="run('custom')"
|
||||
@submit="run('tests')"
|
||||
@save="save"
|
||||
/>
|
||||
|
||||
<RunResults v-model="customInput" class="out" :result="result" :running="running" :error="runError" />
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.solve {
|
||||
height: 100%;
|
||||
min-height: 0;
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, var(--left)) 12px minmax(0, 1fr);
|
||||
}
|
||||
.left {
|
||||
min-height: 0;
|
||||
overflow: auto;
|
||||
padding: 14px 18px 20px;
|
||||
display: grid;
|
||||
align-content: start;
|
||||
gap: 16px;
|
||||
}
|
||||
.left > * {
|
||||
min-width: 0;
|
||||
}
|
||||
.back {
|
||||
font-size: 0.875rem;
|
||||
color: var(--color-ink-muted);
|
||||
text-decoration: none;
|
||||
}
|
||||
.back:hover {
|
||||
color: var(--color-ink);
|
||||
}
|
||||
h1 {
|
||||
font-size: 2.25rem;
|
||||
margin: -8px 0 0;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
h2 {
|
||||
font-size: 1.4rem;
|
||||
margin-bottom: 6px;
|
||||
}
|
||||
.facts {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 6px 16px;
|
||||
font-size: 0.9rem;
|
||||
margin-top: -8px;
|
||||
}
|
||||
.facts code {
|
||||
font-family: var(--font-mono);
|
||||
font-size: 0.85em;
|
||||
}
|
||||
.statement {
|
||||
padding-top: 12px;
|
||||
border-top: 1px solid var(--color-line-soft);
|
||||
}
|
||||
.test {
|
||||
border: 1px solid var(--color-line-soft);
|
||||
border-radius: 6px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
.test summary {
|
||||
padding: 6px 12px;
|
||||
cursor: pointer;
|
||||
}
|
||||
.io {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
||||
gap: 12px;
|
||||
padding: 0 12px 12px;
|
||||
min-width: 0;
|
||||
}
|
||||
.io > div {
|
||||
min-width: 0;
|
||||
}
|
||||
.filename {
|
||||
font-family: var(--font-mono);
|
||||
font-size: 0.8rem;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
/* The divider: a line in the middle of a gap wide enough to grab. */
|
||||
.split {
|
||||
cursor: col-resize;
|
||||
touch-action: none;
|
||||
position: relative;
|
||||
}
|
||||
.split::after {
|
||||
content: "";
|
||||
position: absolute;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
left: 5px;
|
||||
width: 2px;
|
||||
border-radius: 1px;
|
||||
background: transparent;
|
||||
}
|
||||
.split:hover::after,
|
||||
.split:focus-visible::after {
|
||||
background: var(--color-line);
|
||||
}
|
||||
.right {
|
||||
min-width: 0;
|
||||
min-height: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
}
|
||||
.toolbar {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
.toolbar select {
|
||||
padding: 6px 10px;
|
||||
border: 1px solid var(--color-line);
|
||||
border-radius: 4px;
|
||||
background: var(--color-paper);
|
||||
color: var(--color-ink);
|
||||
cursor: pointer;
|
||||
}
|
||||
.toolbar select:disabled {
|
||||
opacity: 0.5;
|
||||
cursor: default;
|
||||
}
|
||||
.grow {
|
||||
flex: 1;
|
||||
}
|
||||
.status {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: baseline;
|
||||
gap: 4px 14px;
|
||||
font-size: 0.85rem;
|
||||
color: var(--color-ink-muted);
|
||||
}
|
||||
.status .draft {
|
||||
color: var(--color-solving);
|
||||
}
|
||||
.status .done {
|
||||
color: var(--color-solved);
|
||||
}
|
||||
.status .warn {
|
||||
color: var(--color-solving);
|
||||
}
|
||||
.note {
|
||||
font-size: 0.85rem;
|
||||
}
|
||||
.code {
|
||||
flex: 1 1 0;
|
||||
min-height: 160px;
|
||||
}
|
||||
.out {
|
||||
flex: 0 0 38%;
|
||||
min-height: 170px;
|
||||
}
|
||||
.denied {
|
||||
padding: 16px 20px;
|
||||
display: grid;
|
||||
gap: 8px;
|
||||
justify-items: start;
|
||||
}
|
||||
/* A phone or a narrow window: the panes stack and the page scrolls. */
|
||||
@media (max-width: 899px) {
|
||||
.solve {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 16px;
|
||||
height: auto;
|
||||
}
|
||||
.split {
|
||||
display: none;
|
||||
}
|
||||
.left {
|
||||
overflow: visible;
|
||||
}
|
||||
.code {
|
||||
flex: none;
|
||||
height: 60vh;
|
||||
}
|
||||
.out {
|
||||
flex: none;
|
||||
height: auto;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user