Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes are documented here. The format follows [Keep a Changelog](h
- Repo-kept content in `content/`: new problems and extra solutions, tests or corrections for existing ones, checked in CI by `make validate-content` and marked "local" in the app.
- The Progress page lists every solved problem, with when it was first accepted and in which languages; the solved counts link to the filtered problem list.
- **Refresh content** never removes problems, solutions or test cases that an earlier import had.
- Optional Vim keybindings in the editor: a **Vim** toggle in the editor toolbar, remembered between sessions, with a mode line under the editor.

### Changed

Expand Down
10 changes: 10 additions & 0 deletions web/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"katex": "^0.19.1",
"marked": "18.1.2",
"monaco-editor": "0.57.0",
"monaco-vim": "0.4.4",
"react": "19.3.1",
"react-dom": "19.3.1",
"react-router-dom": "^7.18.5"
Expand Down
17 changes: 17 additions & 0 deletions web/src/components/CodeEditor.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.wrap {
height: 100%;
display: flex;
flex-direction: column;
}
.wrap > :first-child {
flex: 1;
min-height: 0;
}
.vimStatus {
padding: 2px 12px;
min-height: 20px;
font-size: 11px;
color: var(--fg3);
border-top: 1px solid var(--line);
background: var(--panel);
}
56 changes: 37 additions & 19 deletions web/src/components/CodeEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import Editor, { type OnMount } from "@monaco-editor/react";
import { useEffect, useRef } from "react";
import { initVimMode } from "monaco-vim";
import { useEffect, useRef, useState } from "react";
import type { Lang } from "../api";
import { MONACO_LANG, monaco } from "../monaco";
import { useTheme } from "../useTheme";
import styles from "./CodeEditor.module.css";

type MonacoEditor = Parameters<OnMount>[0];

export default function CodeEditor({
lang,
Expand All @@ -11,13 +15,15 @@ export default function CodeEditor({
fontSize,
onRun,
onSubmit,
vim,
}: {
lang: Lang;
value: string;
onChange: (v: string) => void;
fontSize: number;
onRun: () => void;
onSubmit: () => void;
vim: boolean;
}) {
const theme = useTheme();
// Monaco registers commands once; keep the latest callbacks in refs.
Expand All @@ -26,32 +32,44 @@ export default function CodeEditor({
actions.current = { onRun, onSubmit };
}, [onRun, onSubmit]);

const [editor, setEditor] = useState<MonacoEditor | null>(null);
const vimStatus = useRef<HTMLDivElement>(null);
useEffect(() => {
if (!vim || !editor || !vimStatus.current) return;
const mode = initVimMode(editor, vimStatus.current);
return () => mode.dispose();
}, [vim, editor]);

const onMount: OnMount = (editor) => {
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyCode.Enter, () => actions.current.onRun());
editor.addCommand(monaco.KeyMod.CtrlCmd | monaco.KeyMod.Shift | monaco.KeyCode.Enter, () =>
actions.current.onSubmit(),
);
editor.focus();
setEditor(editor);
};

return (
<Editor
value={value}
language={MONACO_LANG[lang]}
theme={theme === "light" ? "localcode-light" : "localcode-dark"}
onChange={(v) => onChange(v ?? "")}
onMount={onMount}
options={{
fontSize,
fontFamily: "Geist Mono, ui-monospace, monospace",
minimap: { enabled: false },
tabSize: 4,
scrollBeyondLastLine: false,
automaticLayout: true,
padding: { top: 10 },
renderLineHighlight: "line",
smoothScrolling: true,
}}
/>
<div className={styles.wrap}>
<Editor
value={value}
language={MONACO_LANG[lang]}
theme={theme === "light" ? "localcode-light" : "localcode-dark"}
onChange={(v) => onChange(v ?? "")}
onMount={onMount}
options={{
fontSize,
fontFamily: "Geist Mono, ui-monospace, monospace",
minimap: { enabled: false },
tabSize: 4,
scrollBeyondLastLine: false,
automaticLayout: true,
padding: { top: 10 },
renderLineHighlight: "line",
smoothScrolling: true,
}}
/>
{vim && <div ref={vimStatus} className={`mono ${styles.vimStatus}`} aria-live="polite" />}
</div>
);
}
10 changes: 10 additions & 0 deletions web/src/components/EditorToolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ export function EditorToolbar(props: {
onReset: () => void;
fontSize: number;
onFontSize: (delta: number) => void;
vim: boolean;
onVim: () => void;
save: SaveState;
canRun: boolean;
onRun: () => void;
Expand Down Expand Up @@ -45,6 +47,14 @@ export function EditorToolbar(props: {
<button className="btn ghost ibtn" aria-label="Larger font" onClick={() => props.onFontSize(1)}>
A+
</button>
<button
className={props.vim ? "btn" : "btn ghost"}
aria-pressed={props.vim}
onClick={props.onVim}
title="Toggle Vim keybindings"
>
Vim
</button>
<span className={props.save === "error" ? "note bad" : "note"}>{SAVE_LABEL[props.save]}</span>
<div className="spacer" />
<button className="btn" disabled={!props.canRun} onClick={props.onRun} title="Run (⌘↵)">
Expand Down
8 changes: 8 additions & 0 deletions web/src/pages/Workspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default function Workspace() {
const ws = useWorkspace(slug);
const [left, setLeft] = useState<LeftTab>("description");
const [fontSize, setFontSize] = useState<number>(() => pref("fontSize", 14));
const [vim, setVim] = useState<boolean>(() => pref("vim", false));
const {
size: colSize,
container: colsContainer,
Expand Down Expand Up @@ -70,6 +71,10 @@ export default function Workspace() {
setFontSize(size);
setPref("fontSize", size);
};
const toggleVim = () => {
setVim(!vim);
setPref("vim", !vim);
};

return (
<div ref={colsContainer} className={styles.workspace}>
Expand Down Expand Up @@ -128,6 +133,8 @@ export default function Workspace() {
onReset={ws.resetToStarter}
fontSize={fontSize}
onFontSize={changeFont}
vim={vim}
onVim={toggleVim}
save={ws.save}
canRun={canRun}
onRun={ws.run}
Expand All @@ -139,6 +146,7 @@ export default function Workspace() {
value={ws.code}
onChange={ws.setCode}
fontSize={fontSize}
vim={vim}
onRun={() => canRun && ws.run()}
onSubmit={() => canRun && ws.submit()}
/>
Expand Down
2 changes: 2 additions & 0 deletions web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ import react from "@vitejs/plugin-react";

export default defineConfig({
plugins: [react()],
// monaco-vim imports `monaco-editor/esm/vs/...`, which monaco-editor 0.57's exports map no longer allows.
resolve: { alias: [{ find: /^monaco-editor\/esm\/vs\/(.*)$/, replacement: "monaco-editor/$1" }] },
server: { port: 5173, proxy: { "/api": "http://localhost:4173" } },
});