2024-06-03 20:35:06 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2024-12-04 23:16:50 +01:00
|
|
|
import { useOverrideConfigAtom } from "@/app/store/global";
|
2024-06-03 20:35:06 +02:00
|
|
|
import loader from "@monaco-editor/loader";
|
|
|
|
import { Editor, Monaco } from "@monaco-editor/react";
|
|
|
|
import type * as MonacoTypes from "monaco-editor/esm/vs/editor/editor.api";
|
2024-10-15 03:18:58 +02:00
|
|
|
import { configureMonacoYaml } from "monaco-yaml";
|
2024-09-10 23:27:11 +02:00
|
|
|
import React, { useMemo, useRef } from "react";
|
2024-10-15 03:18:58 +02:00
|
|
|
|
|
|
|
import editorWorker from "monaco-editor/esm/vs/editor/editor.worker?worker";
|
|
|
|
import cssWorker from "monaco-editor/esm/vs/language/css/css.worker?worker";
|
|
|
|
import htmlWorker from "monaco-editor/esm/vs/language/html/html.worker?worker";
|
|
|
|
import jsonWorker from "monaco-editor/esm/vs/language/json/json.worker?worker";
|
|
|
|
import tsWorker from "monaco-editor/esm/vs/language/typescript/ts.worker?worker";
|
|
|
|
import ymlWorker from "./yamlworker?worker";
|
|
|
|
|
2024-11-22 01:05:04 +01:00
|
|
|
import "./codeeditor.scss";
|
2024-06-03 20:35:06 +02:00
|
|
|
|
|
|
|
// there is a global monaco variable (TODO get the correct TS type)
|
|
|
|
declare var monaco: Monaco;
|
|
|
|
|
2024-10-15 03:18:58 +02:00
|
|
|
window.MonacoEnvironment = {
|
|
|
|
getWorker(_, label) {
|
|
|
|
if (label === "json") {
|
|
|
|
return new jsonWorker();
|
|
|
|
}
|
|
|
|
if (label === "css" || label === "scss" || label === "less") {
|
|
|
|
return new cssWorker();
|
|
|
|
}
|
|
|
|
if (label === "yaml" || label === "yml") {
|
|
|
|
return new ymlWorker();
|
|
|
|
}
|
|
|
|
if (label === "html" || label === "handlebars" || label === "razor") {
|
|
|
|
return new htmlWorker();
|
|
|
|
}
|
|
|
|
if (label === "typescript" || label === "javascript") {
|
|
|
|
return new tsWorker();
|
|
|
|
}
|
|
|
|
return new editorWorker();
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-09-03 01:48:10 +02:00
|
|
|
export function loadMonaco() {
|
2024-06-14 01:49:25 +02:00
|
|
|
loader.config({ paths: { vs: "monaco" } });
|
2024-06-03 20:35:06 +02:00
|
|
|
loader
|
|
|
|
.init()
|
|
|
|
.then(() => {
|
|
|
|
monaco.editor.defineTheme("wave-theme-dark", {
|
2024-09-04 03:43:59 +02:00
|
|
|
base: "vs-dark",
|
2024-06-03 20:35:06 +02:00
|
|
|
inherit: true,
|
|
|
|
rules: [],
|
|
|
|
colors: {
|
2024-09-04 03:43:59 +02:00
|
|
|
"editor.background": "#00000000",
|
2024-09-05 08:04:32 +02:00
|
|
|
"editorStickyScroll.background": "#00000055",
|
2024-09-04 03:43:59 +02:00
|
|
|
"minimap.background": "#00000077",
|
|
|
|
focusBorder: "#00000000",
|
2024-06-03 20:35:06 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
monaco.editor.defineTheme("wave-theme-light", {
|
2024-09-04 03:43:59 +02:00
|
|
|
base: "vs",
|
2024-06-03 20:35:06 +02:00
|
|
|
inherit: true,
|
|
|
|
rules: [],
|
|
|
|
colors: {
|
|
|
|
"editor.background": "#fefefe",
|
2024-09-04 03:43:59 +02:00
|
|
|
focusBorder: "#00000000",
|
2024-06-03 20:35:06 +02:00
|
|
|
},
|
|
|
|
});
|
2024-10-15 03:18:58 +02:00
|
|
|
configureMonacoYaml(monaco, {
|
|
|
|
validate: true,
|
|
|
|
schemas: [],
|
|
|
|
});
|
2024-09-04 03:43:59 +02:00
|
|
|
// Disable default validation errors for typescript and javascript
|
|
|
|
monaco.languages.typescript.typescriptDefaults.setDiagnosticsOptions({
|
|
|
|
noSemanticValidation: true,
|
|
|
|
});
|
2024-10-15 03:18:58 +02:00
|
|
|
monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
|
|
|
|
validate: true,
|
|
|
|
allowComments: false, // Set to true if you want to allow comments in JSON
|
|
|
|
schemas: [], // You can specify JSON schemas here if needed
|
|
|
|
});
|
2024-06-03 20:35:06 +02:00
|
|
|
})
|
|
|
|
.catch((e) => {
|
|
|
|
console.error("error loading monaco", e);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function defaultEditorOptions(): MonacoTypes.editor.IEditorOptions {
|
|
|
|
const opts: MonacoTypes.editor.IEditorOptions = {
|
|
|
|
scrollBeyondLastLine: false,
|
|
|
|
fontSize: 12,
|
|
|
|
fontFamily: "Hack",
|
2024-07-18 08:41:33 +02:00
|
|
|
smoothScrolling: true,
|
|
|
|
scrollbar: {
|
|
|
|
useShadows: false,
|
|
|
|
verticalScrollbarSize: 5,
|
|
|
|
horizontalScrollbarSize: 5,
|
|
|
|
},
|
2024-09-04 03:43:59 +02:00
|
|
|
minimap: {
|
|
|
|
enabled: true,
|
|
|
|
},
|
2024-09-05 08:05:33 +02:00
|
|
|
stickyScroll: {
|
|
|
|
enabled: false,
|
|
|
|
},
|
2024-06-03 20:35:06 +02:00
|
|
|
};
|
|
|
|
return opts;
|
|
|
|
}
|
|
|
|
|
2024-07-25 21:00:35 +02:00
|
|
|
interface CodeEditorProps {
|
2024-12-04 23:16:50 +01:00
|
|
|
blockId: string;
|
2024-09-10 23:27:11 +02:00
|
|
|
text: string;
|
2024-06-06 23:52:01 +02:00
|
|
|
filename: string;
|
2024-07-25 21:00:35 +02:00
|
|
|
language?: string;
|
2024-10-31 20:34:30 +01:00
|
|
|
meta?: MetaType;
|
2024-07-18 08:41:33 +02:00
|
|
|
onChange?: (text: string) => void;
|
2024-09-03 01:48:10 +02:00
|
|
|
onMount?: (monacoPtr: MonacoTypes.editor.IStandaloneCodeEditor, monaco: Monaco) => () => void;
|
2024-06-05 02:58:29 +02:00
|
|
|
}
|
|
|
|
|
2024-12-04 23:16:50 +01:00
|
|
|
export function CodeEditor({ blockId, text, language, filename, meta, onChange, onMount }: CodeEditorProps) {
|
2024-07-18 08:41:33 +02:00
|
|
|
const divRef = useRef<HTMLDivElement>(null);
|
2024-09-03 01:48:10 +02:00
|
|
|
const unmountRef = useRef<() => void>(null);
|
2024-12-04 23:16:50 +01:00
|
|
|
const minimapEnabled = useOverrideConfigAtom(blockId, "editor:minimapenabled") ?? false;
|
|
|
|
const stickyScrollEnabled = useOverrideConfigAtom(blockId, "editor:stickyscrollenabled") ?? false;
|
|
|
|
const wordWrap = useOverrideConfigAtom(blockId, "editor:wordwrap") ?? false;
|
2024-07-18 08:41:33 +02:00
|
|
|
const theme = "wave-theme-dark";
|
|
|
|
|
2024-09-03 01:48:10 +02:00
|
|
|
React.useEffect(() => {
|
2024-07-25 21:00:35 +02:00
|
|
|
return () => {
|
2024-09-03 01:48:10 +02:00
|
|
|
// unmount function
|
|
|
|
if (unmountRef.current) {
|
|
|
|
unmountRef.current();
|
|
|
|
}
|
2024-07-25 21:00:35 +02:00
|
|
|
};
|
2024-09-03 01:48:10 +02:00
|
|
|
}, []);
|
2024-06-03 20:35:06 +02:00
|
|
|
|
2024-07-18 08:41:33 +02:00
|
|
|
function handleEditorChange(text: string, ev: MonacoTypes.editor.IModelContentChangedEvent) {
|
2024-07-25 21:00:35 +02:00
|
|
|
if (onChange) {
|
|
|
|
onChange(text);
|
|
|
|
}
|
2024-06-03 20:35:06 +02:00
|
|
|
}
|
|
|
|
|
2024-08-01 04:41:16 +02:00
|
|
|
function handleEditorOnMount(editor: MonacoTypes.editor.IStandaloneCodeEditor, monaco: Monaco) {
|
2024-09-03 01:48:10 +02:00
|
|
|
if (onMount) {
|
|
|
|
unmountRef.current = onMount(editor, monaco);
|
|
|
|
}
|
2024-08-01 04:41:16 +02:00
|
|
|
}
|
|
|
|
|
2024-09-04 23:00:29 +02:00
|
|
|
const editorOpts = useMemo(() => {
|
|
|
|
const opts = defaultEditorOptions();
|
|
|
|
opts.minimap.enabled = minimapEnabled;
|
2024-09-05 08:08:56 +02:00
|
|
|
opts.stickyScroll.enabled = stickyScrollEnabled;
|
2024-12-04 23:16:50 +01:00
|
|
|
opts.wordWrap = wordWrap ? "on" : "off";
|
2024-09-04 23:00:29 +02:00
|
|
|
return opts;
|
2024-12-04 23:16:50 +01:00
|
|
|
}, [minimapEnabled, stickyScrollEnabled, wordWrap]);
|
2024-06-03 20:35:06 +02:00
|
|
|
|
|
|
|
return (
|
2024-07-18 08:41:33 +02:00
|
|
|
<div className="code-editor-wrapper">
|
|
|
|
<div className="code-editor" ref={divRef}>
|
2024-07-25 21:00:35 +02:00
|
|
|
<Editor
|
|
|
|
theme={theme}
|
2024-09-10 23:27:11 +02:00
|
|
|
value={text}
|
2024-07-25 21:00:35 +02:00
|
|
|
options={editorOpts}
|
|
|
|
onChange={handleEditorChange}
|
2024-08-01 04:41:16 +02:00
|
|
|
onMount={handleEditorOnMount}
|
2024-07-25 21:00:35 +02:00
|
|
|
path={filename}
|
|
|
|
language={language}
|
|
|
|
/>
|
2024-06-14 01:49:25 +02:00
|
|
|
</div>
|
2024-06-03 20:35:06 +02:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|