-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMonacoEditor.tsx
More file actions
122 lines (108 loc) · 3.02 KB
/
MonacoEditor.tsx
File metadata and controls
122 lines (108 loc) · 3.02 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import type { editor } from 'monaco-editor';
import MonacoReactEditor, { type BeforeMount, loader, type Monaco, type OnMount } from '@monaco-editor/react';
import * as monaco from 'monaco-editor';
// eslint-disable-next-line import/default
import EditorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
import { configureMonacoYaml, SchemasSettings } from 'monaco-yaml';
// eslint-disable-next-line import/default
import YamlWorker from 'monaco-yaml/yaml.worker?worker';
import { useRef } from 'react';
import { YAML_SCHEMAS } from '../utils/yaml-schemas';
loader.config({ monaco });
if (typeof globalThis !== 'undefined') {
globalThis.MonacoEnvironment = {
getWorker(_, label) {
switch (label) {
case 'yaml': {
return new YamlWorker();
}
default: {
return new EditorWorker();
}
}
}
};
}
const DEFAULT_MONACO_OPTIONS: editor.IStandaloneEditorConstructionOptions = {
formatOnPaste: true,
formatOnType: true,
minimap: { enabled: false },
quickSuggestions: {
comments: false,
other: false,
strings: false
},
readOnly: false
};
export type MonacoEditorProps = {
beforeMount?: BeforeMount;
className?: string;
customTags?: string[];
height?: number | string;
language?: string;
onChange?: (value: string | undefined) => void;
onMount?: OnMount;
onValidate?: (markers: editor.IMarker[]) => void;
options?: editor.IStandaloneEditorConstructionOptions;
path?: string;
value: string;
};
export function MonacoEditor({
beforeMount,
className,
customTags,
height = '100%',
language = 'yaml',
onChange,
onMount,
onValidate,
options,
path,
value
}: MonacoEditorProps) {
const configuredMonacoRef = useRef<Monaco | null>(null);
const handleBeforeMount: BeforeMount = (monacoInstance: Monaco) => {
const shouldConfigureYaml = configuredMonacoRef.current !== monacoInstance;
if (shouldConfigureYaml) {
const schemas = Object.entries(YAML_SCHEMAS).map(([filename, schema]) => ({
fileMatch: [filename],
schema,
uri: `inmemory://schemas/${filename}.json`
})) as SchemasSettings[];
configureMonacoYaml(monacoInstance, {
completion: true,
customTags: customTags ?? [],
enableSchemaRequest: true,
format: true,
hover: true,
schemas,
validate: true
});
configuredMonacoRef.current = monacoInstance;
}
if (beforeMount) {
beforeMount(monacoInstance);
}
};
const handleMount: OnMount = (editorInstance, monacoInstance) => {
if (onMount) {
onMount(editorInstance, monacoInstance);
}
};
return (
<MonacoReactEditor
beforeMount={handleBeforeMount}
className={className}
defaultLanguage={language}
height={height}
language={language}
onChange={onChange}
onMount={handleMount}
onValidate={onValidate}
options={{ ...DEFAULT_MONACO_OPTIONS, ...options }}
path={path}
theme="vs-dark"
value={value}
/>
);
}