feat: CodeMirror 6 editor with in-editor search/replace

- bundle CodeMirror 6 (+ langs/search) via scripts/build-editor.mjs into a
  generated TS module (inlined into the WebView; no remote code, no asset
  pipeline)
- CodeEditor now renders CodeMirror 6 with line numbers, language modes and a
  search/replace panel exposed from a toolbar button
- npm run build:editor; language mapping by file extension
This commit is contained in:
JustZoe101 committed 2026-10-09 19:08:38 +02:00
1 parent a104d65978
commit 883440b8be
8 files changed
+1164 -110

No files matched your search

+41
View File
@@ -0,0 +1,41 @@
/**
* Bundles the CodeMirror 6 entry (`scripts/codemirror-entry.mjs`) into a single
* IIFE string inside `src/components/git/codemirror-bundle.ts`.
*
* Run with `npm run build:editor`. The generated module is committed so the app
* builds without a JS bundling step at runtime.
*/
import { writeFileSync } from 'node:fs';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { build } from 'esbuild';
const root = resolve(dirname(fileURLToPath(import.meta.url)), '..');
const entry = resolve(root, 'scripts/codemirror-entry.mjs');
const output = resolve(root, 'src/components/git/codemirror-bundle.ts');
const result = await build({
entryPoints: [entry],
bundle: true,
format: 'iife',
minify: true,
write: false,
platform: 'browser',
target: 'es2020',
legalComments: 'none',
});
const code = result.outputFiles[0].text;
const safe = code.replace(/<\/script/gi, '<\\/script');
const module = `/**
* AUTO-GENERATED by scripts/build-editor.mjs — do not edit by hand.
* Bundled CodeMirror 6 editor (see scripts/codemirror-entry.mjs).
*/
export const CODE_MIRROR_BUNDLE = ${JSON.stringify(safe)};
`;
writeFileSync(output, module);
console.log(`Wrote ${output} (${code.length} bytes)`);
+57
View File
@@ -0,0 +1,57 @@
/**
* CodeMirror 6 entry point bundled by `scripts/build-editor.mjs`.
*
* It exposes `window.createCodeMirror` to the inline WebView HTML. The bundle is
* generated into `src/components/git/codemirror-bundle.ts` so the app carries no
* runtime asset pipeline and never loads remote code.
*/
import { EditorState } from '@codemirror/state';
import { EditorView } from '@codemirror/view';
import { basicSetup } from 'codemirror';
import { openSearchPanel } from '@codemirror/search';
import { javascript } from '@codemirror/lang-javascript';
import { python } from '@codemirror/lang-python';
import { json } from '@codemirror/lang-json';
import { html } from '@codemirror/lang-html';
import { css } from '@codemirror/lang-css';
import { markdown } from '@codemirror/lang-markdown';
import { java } from '@codemirror/lang-java';
import { cpp } from '@codemirror/lang-cpp';
import { rust } from '@codemirror/lang-rust';
import { php } from '@codemirror/lang-php';
import { go } from '@codemirror/lang-go';
const LANGUAGES = { javascript, python, json, html, css, markdown, java, cpp, rust, php, go };
window.createCodeMirror = function createCodeMirror(config) {
const { parent, doc, language, theme, onChange } = config;
const languageExtension = LANGUAGES[language] ? LANGUAGES[language]() : [];
const view = new EditorView({
parent,
state: EditorState.create({
doc,
extensions: [
basicSetup,
languageExtension,
EditorView.theme({
'&': { backgroundColor: theme.bg, color: theme.fg, height: '100%' },
'.cm-content': { fontFamily: 'ui-monospace, Menlo, Consolas, monospace' },
'.cm-gutters': { backgroundColor: theme.bg, color: theme.gutter, border: 'none' },
'.cm-activeLine': { backgroundColor: theme.activeLine },
'.cm-activeLineGutter': { backgroundColor: theme.activeLine },
'&.cm-focused': { outline: 'none' },
}),
EditorView.updateListener.of((update) => {
if (update.docChanged) onChange(update.state.doc.toString());
}),
],
}),
});
return {
openSearch: () => openSearchPanel(view),
focus: () => view.focus(),
};
};