From 5022b30301528c99163d144f2906d74e3ed96614 Mon Sep 17 00:00:00 2001 From: Silviu Popescu Date: Mon, 16 Nov 2026 23:20:38 +0000 Subject: [PATCH] feat(web): optional Vim keybindings in the editor Add a Vim toggle to the editor toolbar, backed by monaco-vim. The choice is stored with the other editor preferences, and a mode line under the editor shows the current mode and pending commands. monaco-vim imports monaco-editor/esm/vs/... paths, which monaco-editor 0.57's exports map rejects, so vite.config.ts aliases them to the exported paths. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + web/package-lock.json | 10 +++++ web/package.json | 1 + web/src/components/CodeEditor.module.css | 17 +++++++ web/src/components/CodeEditor.tsx | 56 ++++++++++++++++-------- web/src/components/EditorToolbar.tsx | 10 +++++ web/src/pages/Workspace.tsx | 8 ++++ web/vite.config.ts | 2 + 8 files changed, 86 insertions(+), 19 deletions(-) create mode 100644 web/src/components/CodeEditor.module.css diff --git a/CHANGELOG.md b/CHANGELOG.md index 204bb4e..51243b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/web/package-lock.json b/web/package-lock.json index 07c8c08..2ba2f63 100644 --- a/web/package-lock.json +++ b/web/package-lock.json @@ -16,6 +16,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" @@ -2643,6 +2644,15 @@ "node": ">= 18" } }, + "node_modules/monaco-vim": { + "version": "0.4.4", + "resolved": "https://registry.npmjs.org/monaco-vim/-/monaco-vim-0.4.4.tgz", + "integrity": "sha512-LNChAb//WEm/W+eyeHG/0+pdVEHotk2hLTN+M3sQZx5E8cAlSWSgqcxpcRuQnxDybSln7pfHF9i63HmbIQvrWw==", + "license": "MIT", + "peerDependencies": { + "monaco-editor": "*" + } + }, "node_modules/ms": { "version": "2.1.3", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", diff --git a/web/package.json b/web/package.json index 2532eb7..9b6cbb8 100644 --- a/web/package.json +++ b/web/package.json @@ -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" diff --git a/web/src/components/CodeEditor.module.css b/web/src/components/CodeEditor.module.css new file mode 100644 index 0000000..c023991 --- /dev/null +++ b/web/src/components/CodeEditor.module.css @@ -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); +} diff --git a/web/src/components/CodeEditor.tsx b/web/src/components/CodeEditor.tsx index 5b7598c..13a1e33 100644 --- a/web/src/components/CodeEditor.tsx +++ b/web/src/components/CodeEditor.tsx @@ -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[0]; export default function CodeEditor({ lang, @@ -11,6 +15,7 @@ export default function CodeEditor({ fontSize, onRun, onSubmit, + vim, }: { lang: Lang; value: string; @@ -18,6 +23,7 @@ export default function CodeEditor({ fontSize: number; onRun: () => void; onSubmit: () => void; + vim: boolean; }) { const theme = useTheme(); // Monaco registers commands once; keep the latest callbacks in refs. @@ -26,32 +32,44 @@ export default function CodeEditor({ actions.current = { onRun, onSubmit }; }, [onRun, onSubmit]); + const [editor, setEditor] = useState(null); + const vimStatus = useRef(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 ( - 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, - }} - /> +
+ 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 &&
} +
); } diff --git a/web/src/components/EditorToolbar.tsx b/web/src/components/EditorToolbar.tsx index 8af297c..ac68ec4 100644 --- a/web/src/components/EditorToolbar.tsx +++ b/web/src/components/EditorToolbar.tsx @@ -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; @@ -45,6 +47,14 @@ export function EditorToolbar(props: { + {SAVE_LABEL[props.save]}