- 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
58 lines
2.1 KiB
JavaScript
58 lines
2.1 KiB
JavaScript
/**
|
|
* 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(),
|
|
};
|
|
};
|