-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCodeEditor.tsx
More file actions
177 lines (156 loc) · 6.62 KB
/
Copy pathCodeEditor.tsx
File metadata and controls
177 lines (156 loc) · 6.62 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { Button, Content, Dialog, DialogContainer, Divider, Flex, View, ViewProps } from '@adobe/react-spectrum';
import { useMonaco } from '@monaco-editor/react';
import { ColorVersion } from '@react-types/shared';
import FullScreenExit from '@spectrum-icons/workflow/FullScreenExit';
import { MarkerSeverity, editor } from 'monaco-editor';
import { useEffect, useRef, useState } from 'react';
import { SyntaxError } from '../hooks/code';
import { debounce } from '../utils/debounce';
import { modelStorage } from '../utils/modelStorage';
import { GROOVY_LANGUAGE_ID, registerGroovyLanguage } from '../utils/monaco/groovy';
import { LOG_LANGUAGE_ID, LOG_THEME_ID, registerLogLanguage } from '../utils/monaco/log';
import { DEFAULT_THEME_ID, registerTheme } from '../utils/monaco/theme';
type CodeEditorProps<C extends ColorVersion> = editor.IStandaloneEditorConstructionOptions & {
id: string;
scrollToBottomOnUpdate?: boolean;
initialValue?: string;
containerProps?: ViewProps<C>;
syntaxError?: SyntaxError;
onChange?: (code: string) => void;
};
const SaveViewStateDebounce = 1000;
const SuggestWidgetHeight = 480;
const CodeEditor = <C extends ColorVersion>({ containerProps, syntaxError, onChange, id, language, value, initialValue, readOnly, scrollToBottomOnUpdate, ...props }: CodeEditorProps<C>) => {
const [isOpen, setIsOpen] = useState(false);
const monacoRef = useMonaco();
const containerRef = useRef<HTMLDivElement>(null);
const debouncedViewStateUpdate = debounce((mountedEditor: editor.IStandaloneCodeEditor) => {
modelStorage.updateViewState(id, mountedEditor.saveViewState());
}, SaveViewStateDebounce);
useEffect(() => {
if (value) {
const storedModel = modelStorage.getModel(id);
storedModel?.textModel.setValue(value);
if (scrollToBottomOnUpdate) {
const lines = storedModel?.textModel.getLineCount() ?? 1;
storedModel?.editor.revealLine(lines);
}
}
}, [id, scrollToBottomOnUpdate, value]);
useEffect(() => {
if (!containerRef.current || !monacoRef) {
return;
}
registerTheme(monacoRef);
if (language === GROOVY_LANGUAGE_ID) {
registerGroovyLanguage(monacoRef);
} else if (language === LOG_LANGUAGE_ID) {
registerLogLanguage(monacoRef);
}
const storedModel = modelStorage.getModel(id);
const textModel = storedModel?.textModel || monacoRef.editor.createModel(initialValue ?? value ?? '', language);
if (value !== undefined) {
textModel.setValue(value);
}
const mountedEditor = monacoRef.editor.create(containerRef.current, {
model: textModel,
scrollBeyondLastLine: false,
theme: language === LOG_LANGUAGE_ID ? LOG_THEME_ID : DEFAULT_THEME_ID,
value: initialValue ?? value,
fixedOverflowWidgets: true,
automaticLayout: true,
...props,
});
mountedEditor.addCommand(monacoRef.KeyMod.CtrlCmd | monacoRef.KeyMod.Shift | monacoRef.KeyCode.KeyF, () => setIsOpen((prev) => !prev));
mountedEditor.addCommand(monacoRef.KeyMod.CtrlCmd | monacoRef.KeyCode.Equal, () => mountedEditor.updateOptions({ fontSize: mountedEditor.getOption(monacoRef.editor.EditorOption.fontSize) + 1 }));
mountedEditor.addCommand(monacoRef.KeyMod.CtrlCmd | monacoRef.KeyCode.Minus, () => mountedEditor.updateOptions({ fontSize: mountedEditor.getOption(monacoRef.editor.EditorOption.fontSize) - 1 }));
if (storedModel?.viewState) {
mountedEditor.restoreViewState(storedModel.viewState);
}
// @ts-expect-error: Accessing private API of the suggest widget to modify its behavior
const { widget } = mountedEditor.getContribution('editor.contrib.suggestController');
if (widget) {
const suggestWidget = widget.value;
if (suggestWidget && suggestWidget._setDetailsVisible) {
suggestWidget._setDetailsVisible(true);
if (suggestWidget._details) {
// Height adjusts automatically
suggestWidget._details.widget._size.width = SuggestWidgetHeight;
}
}
}
const changeListener = textModel.onDidChangeContent(() => {
debouncedViewStateUpdate(mountedEditor);
onChange?.(mountedEditor.getValue());
});
modelStorage.updateModel(id, {
textModel,
viewState: mountedEditor.saveViewState(),
editor: mountedEditor,
});
mountedEditor.focus();
return () => {
mountedEditor.dispose();
changeListener?.dispose();
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [monacoRef, isOpen]);
useEffect(() => {
const textModel = modelStorage.getModel(id)?.textModel;
if (monacoRef?.editor && textModel) {
monacoRef?.editor.setModelMarkers(
textModel,
id,
syntaxError
? [
{
startLineNumber: syntaxError.line,
startColumn: syntaxError.column,
endLineNumber: syntaxError.line,
endColumn: syntaxError.column + 10,
message: syntaxError.message,
severity: MarkerSeverity.Error,
},
]
: [],
);
}
}, [id, monacoRef?.editor, syntaxError]);
// Update the 'readOnly' state in the editor when it changes
useEffect(() => {
const storedModel = modelStorage.getModel(id);
if (storedModel?.editor) {
storedModel.editor.updateOptions({ readOnly });
}
}, [id, readOnly]);
return (
<View backgroundColor="gray-800" borderWidth="thin" position="relative" borderColor="dark" height="100%" borderRadius="medium" padding="size-50" {...containerProps}>
{!isOpen && (
<>
<div ref={containerRef} style={{ height: '100%' }} />
<Button variant="primary" style="fill" position="absolute" zIndex={5} bottom={10} right={10} onPress={() => setIsOpen(true)}>
<FullScreenExit />
</Button>
</>
)}
<DialogContainer onDismiss={() => setIsOpen(false)} type="fullscreenTakeover">
{isOpen && (
<Dialog marginTop={8}>
<Content gridColumn="1 / span 5" gridRow="2 / span 4" height="100%">
<Flex height="100%" direction="column">
<Divider size="M" orientation="horizontal" />
<View backgroundColor="gray-800" paddingTop={10} height="100%">
<div ref={containerRef} style={{ height: '100%' }} />
</View>
</Flex>
</Content>
<Button variant="primary" style="fill" position="absolute" zIndex={5} bottom={10} right={10} onPress={() => setIsOpen(false)}>
<FullScreenExit />
</Button>
</Dialog>
)}
</DialogContainer>
</View>
);
};
export default CodeEditor;