/
CodeEditor.tsx
72 lines (65 loc) · 2.13 KB
/
CodeEditor.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import { Box } from "@chakra-ui/react";
import { Editor, OnChange, OnMount } from "@monaco-editor/react";
import { editor } from "monaco-editor";
import { useEffect, useState } from "react";
import { isDefined } from "../utils";
type CodeEditorProps = {
text: string;
onTextChange?: (newText: string) => void;
showLineNumbers?: boolean;
};
export const CodeEditor = ({ text, onTextChange, showLineNumbers }: CodeEditorProps) => {
const isReadOnly = !isDefined(onTextChange);
const [editor, setEditor] = useState<editor.IStandaloneCodeEditor>();
const resizeEditorBasedOnContent = () => {
if (isDefined(editor)) {
const contentHeight = Math.min(750, editor.getContentHeight() || 10);
editor.layout({ width: 500, height: contentHeight });
editor.layout();
}
};
const handleMount: OnMount = (editor, monaco) => {
setEditor(editor);
monaco.editor.defineTheme("kurtosis-theme", {
base: "vs-dark",
inherit: true,
rules: [],
colors: {},
});
monaco.editor.setTheme("kurtosis-theme");
};
useEffect(() => resizeEditorBasedOnContent(), [editor]);
const handleChange: OnChange = (value, ev) => {
if (isDefined(value) && onTextChange) {
onTextChange(value);
resizeEditorBasedOnContent();
}
};
return (
<Box width={"100%"} minHeight={`${editor?.getLayoutInfo().height || 10}px`}>
<Editor
onMount={handleMount}
value={text}
onChange={handleChange}
options={{
readOnly: isReadOnly,
lineNumbers: showLineNumbers || (!isDefined(showLineNumbers) && !isReadOnly) ? "on" : "off",
minimap: { enabled: false },
wordWrap: "on",
wrappingStrategy: "advanced",
scrollBeyondLastLine: false,
renderLineHighlight: isReadOnly ? "none" : "line",
selectionHighlight: !isReadOnly,
occurrencesHighlight: !isReadOnly,
overviewRulerLanes: isReadOnly ? 0 : 3,
scrollbar: {
alwaysConsumeMouseWheel: false,
},
}}
defaultLanguage={"json"}
theme={"vs-dark"}
height={"100%"}
/>
</Box>
);
};