Skip to content

Connect, collaborate, and grow with a community of TypeScript developers

vim mode?

pythonKiwi12318d154708d7b
diff --git a/monaco-vim.d.ts b/monaco-vim.d.ts
new file mode 100644
index 00000000..c5bae823
--- /dev/null
+++ b/monaco-vim.d.ts
@@ -0,0 +1,11 @@
+import type * as monaco from 'monaco-editor';
+
+export declare function initVimMode(
+ editor: monaco.editor.IStandaloneCodeEditor,
+ statusbarNode?: Element | null,
+): VimMode;
+
+declare class VimMode {
+ /** removes the attached vim bindings */
+ dispose(): void;
+}
diff --git a/package.json b/package.json
index 8e789e8a..bd5dbb8f 100644
--- a/package.json
+++ b/package.json
@@ -27,6 +27,7 @@
"class-variance-authority": "^0.6.0",
"clsx": "^1.2.1",
"lucide-react": "^0.224.0",
+ "monaco-vim": "^0.4.0",
"next": "^13.4.2",
"next-auth": "^4.22.1",
"next-themes": "^0.2.1",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 3b58af29..3fcb3f1a 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -53,6 +53,9 @@ dependencies:
lucide-react:
specifier: ^0.224.0
version: 0.224.0([email protected])
+ monaco-vim:
+ specifier: ^0.4.0
+ version: 0.4.0([email protected])
next:
specifier: ^13.4.2
@@ -3401,6 +3404,14 @@ packages:
resolution: {integrity: sha512-/CaclMHKQ3A6rnzBzOADfwdSJ25BFoFT0Emxsc4zYVyav5SkK9iA6lEtIeuN/oRYbwPgviJT+t3l+sjFa28jYg==}
+ resolution: {integrity: sha512-+CsW0+Mvx2+eitkXS7OpUXIu57qXlqAL8oVkYhkPCEZ/c6+6gOp/IcG7w+Lb33YiZuTyvJ891+czkeJRPIEwVA==}
+ peerDependencies:
+ monaco-editor: '*'
+ dependencies:
+ monaco-editor: 0.36.1
+ dev: false
+
resolution: {integrity: sha512-tzzskb3bG8LvYGFF/mDTpq3jpI6Q9wc3LEmBaghu+DdCssd1FakN7Bc0hVNmEyGq1bq3RgfkCb3cmQLpNPOroA==}
engines: {node: '>=4'}
diff --git a/src/app/layout.tsx b/src/app/layout.tsx
index abeab08c..be672c22 100644
--- a/src/app/layout.tsx
+++ b/src/app/layout.tsx
@@ -15,10 +15,12 @@ export default function RootLayout({ children }: { children: React.ReactNode })
return (
<html suppressHydrationWarning>
<head />
- <body>
+ <body className="flex flex-col">
<Providers>
<Navigation />
- <main className={`${inter.className} bg-white dark:bg-neutral-900`}>{children}</main>
+ <main className={`${inter.className} flex-1 bg-white dark:bg-neutral-900`}>
+ {children}
+ </main>
</Providers>
</body>
</html>
diff --git a/src/app/providers.tsx b/src/app/providers.tsx
index 1b6e9a09..97a4a4c5 100644
--- a/src/app/providers.tsx
+++ b/src/app/providers.tsx
@@ -9,7 +9,7 @@ interface Props {
export function Providers({ children }: Props) {
return (
<SessionProvider>
- <ThemeProvider attribute="class">{children}</ThemeProvider>;
+ <ThemeProvider attribute="class">{children}</ThemeProvider>
</SessionProvider>
);
}
diff --git a/src/components/challenge/editor/index.tsx b/src/components/challenge/editor/index.tsx
index fbb4f83a..87d4f537 100644
--- a/src/components/challenge/editor/index.tsx
+++ b/src/components/challenge/editor/index.tsx
@@ -1,8 +1,9 @@
'use client';
import Editor from '@monaco-editor/react';
-import type * as monaco from 'monaco-editor';
+import type * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
import React from 'react';
import { createTwoslashInlayProvider } from './twoslash';
+import { VimStatusBar, loadVim } from './vimMode';
declare global {
// eslint-disable-next-line no-var
@@ -11,11 +12,7 @@ declare global {
var model: monaco.editor.ITextModel;
}
-type IGlobalEditorOptions = monaco.editor.IGlobalEditorOptions;
-type IEditorOptions = monaco.editor.IEditorOptions;
-type Monaco = typeof monaco;
-
-const options: IGlobalEditorOptions | IEditorOptions = {
+const options: monaco.editor.IStandaloneEditorConstructionOptions = {
lineNumbers: 'off',
tabSize: 2,
insertSpaces: false,
@@ -25,6 +22,8 @@ const options: IGlobalEditorOptions | IEditorOptions = {
// readOnly: true,
};
+type Monaco = typeof monaco;
+
/// based on: https://github.com/type-challenges/type-challenges/blob/main/utils/index.d.ts
const libSource = `
type Equal<X, Y> =
@@ -89,6 +88,8 @@ const onMount =
globalThis.model = model;
globalThis.editor = editor;
+ loadVim(editor);
+
const ts = await (await monaco.languages.typescript.getTypeScriptWorker())(model.uri);
const filename = model.uri.toString();
@@ -134,14 +135,18 @@ interface Props {
}
export const CodePanel = ({ prompt }: Props) => {
return (
- <Editor
- theme="vs-dark"
- options={options}
- defaultLanguage="typescript"
- // eslint-disable-next-line @typescript-eslint/no-misused-promises
- onMount={onMount(prompt)}
- defaultValue={prompt}
- />
+ <>
+ <Editor
+ theme="vs-dark"
+ options={options}
+ defaultLanguage="typescript"
+ // eslint-disable-next-line @typescript-eslint/no-misused-promises
+ onMount={onMount(prompt)}
+ defaultValue={prompt}
+ className="flex-1"
+ />
+ <VimStatusBar />
+ </>
);
};
diff --git a/src/components/challenge/editor/vimMode.tsx b/src/components/challenge/editor/vimMode.tsx
new file mode 100644
index 00000000..c760a2f5
--- /dev/null
+++ b/src/components/challenge/editor/vimMode.tsx
@@ -0,0 +1,68 @@
+import type * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
+import { type VimMode, initVimMode } from 'monaco-vim';
+
+const vimEnabledKey = 'vim-mode-enabled';
+const statusBarId = 'vim-status-bar';
+
+export const VimStatusBar = () => {
+ return (
+ <div className='flex justify-between flex-row-reverse'>
+ <label className='flex gap-1'>
+ Vim Mode:
+ <input type="checkbox" defaultChecked={localStorage.getItem(vimEnabledKey) === "true"} onChange={function (ev) {
+ if (ev.currentTarget.checked) {
+ activateVimMode(globalThis.editor)
+ } else {
+ deactivateVimMode();
+ }
+ }} />
+ </label>
+ <div id={statusBarId} className="font-mono" />
+ </div>
+ );
+};
+
+export function loadVim(editor: monaco.editor.IStandaloneCodeEditor) {
+ const status = localStorage.getItem(vimEnabledKey) ?? 'false';
+
+ if (status === 'true') {
+ activateVimMode(editor);
+ }
+}
+
+declare global {
+ // eslint-disable-next-line no-var
+ var vimMode: VimMode | undefined;
+}
+
+export function activateVimMode(editor: monaco.editor.IStandaloneCodeEditor) {
+ if (globalThis.vimMode === undefined) {
+ const statusBar = document.getElementById(statusBarId);
+
+ if (statusBar === null) {
+ return;
+ }
+
+ globalThis.vimMode = initVimMode(editor, statusBar);
+
+ localStorage.setItem(vimEnabledKey, 'true');
+ }
+}
+
+export function deactivateVimMode() {
+ if (globalThis.vimMode !== undefined) {
+ const statusBar = document.getElementById(statusBarId);
+
+ if (statusBar === null) {
+ return;
+ }
+
+ globalThis.vimMode.dispose();
+
+ delete globalThis.vimMode;
+
+ localStorage.setItem(vimEnabledKey, 'false');
+
+ statusBar.textContent = '';
+ }
+}
diff --git a/src/components/challenge/index.tsx b/src/components/challenge/index.tsx
index bfc38702..9dbc4fa3 100644
--- a/src/components/challenge/index.tsx
+++ b/src/components/challenge/index.tsx
@@ -20,7 +20,7 @@ export async function Challenge({ id }: Props) {
<div className="flex h-full gap-2 p-4">
<DescriptionPanel description={challenge.description} />
- <div className="h-full flex-1">
+ <div className="h-full flex-1 flex flex-col">
<CodePanel prompt={challenge.prompt} />
</div>
</div>