/
editor.tsx
178 lines (157 loc) · 4.77 KB
/
editor.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
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
178
import * as monaco from "monaco-editor/esm/vs/editor/editor.api";
import * as PropTypes from "prop-types";
import * as React from "react";
import { MonacoEditorProps } from "./types";
import { noop, processSize } from "./utils";
class MonacoEditor extends React.Component<MonacoEditorProps> {
static propTypes = {
width: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
height: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
value: PropTypes.string,
defaultValue: PropTypes.string,
language: PropTypes.string,
theme: PropTypes.string,
options: PropTypes.object,
overrideServices: PropTypes.object,
editorDidMount: PropTypes.func,
editorWillMount: PropTypes.func,
onChange: PropTypes.func,
className: PropTypes.string,
};
static defaultProps = {
width: "100%",
height: "100%",
value: null,
defaultValue: "",
language: "javascript",
theme: null,
options: {},
overrideServices: {},
editorDidMount: noop,
editorWillMount: noop,
onChange: noop,
className: null,
};
editor?: monaco.editor.IStandaloneCodeEditor;
private containerElement?: HTMLDivElement;
private _subscription: monaco.IDisposable;
private __prevent_trigger_change_event?: boolean;
constructor(props: MonacoEditorProps) {
super(props);
this.containerElement = undefined;
}
componentDidMount() {
this.initMonaco();
}
componentDidUpdate(prevProps: MonacoEditorProps) {
const { value, language, theme, height, options, width, className } =
this.props;
const { editor } = this;
const model = editor.getModel();
if (this.props.value != null && this.props.value !== model.getValue()) {
this.__prevent_trigger_change_event = true;
this.editor.pushUndoStop();
// pushEditOperations says it expects a cursorComputer, but doesn't seem to need one.
// @ts-expect-error
model.pushEditOperations(
[],
[
{
range: model.getFullModelRange(),
text: value,
},
]
);
this.editor.pushUndoStop();
this.__prevent_trigger_change_event = false;
}
if (prevProps.language !== language) {
monaco.editor.setModelLanguage(model, language);
}
if (prevProps.theme !== theme) {
monaco.editor.setTheme(theme);
}
if (editor && (width !== prevProps.width || height !== prevProps.height)) {
editor.layout();
}
if (prevProps.options !== options) {
// Don't pass in the model on update because monaco crashes if we pass the model
// a second time. See https://github.com/microsoft/monaco-editor/issues/2027
const { model: _model, ...optionsWithoutModel } = options;
editor.updateOptions({
...(className ? { extraEditorClassName: className } : {}),
...optionsWithoutModel,
});
}
}
componentWillUnmount() {
this.destroyMonaco();
}
assignRef = (component: HTMLDivElement) => {
this.containerElement = component;
};
destroyMonaco() {
if (this.editor) {
this.editor.dispose();
const model = this.editor.getModel();
if (model) {
model.dispose();
}
}
if (this._subscription) {
this._subscription.dispose();
}
}
initMonaco() {
const value =
this.props.value != null ? this.props.value : this.props.defaultValue;
const { language, theme, overrideServices, className } = this.props;
if (this.containerElement) {
// Before initializing monaco editor
const options = { ...this.props.options, ...this.editorWillMount() };
this.editor = monaco.editor.create(
this.containerElement,
{
value,
language,
...(className ? { extraEditorClassName: className } : {}),
...options,
...(theme ? { theme } : {}),
},
overrideServices
);
// After initializing monaco editor
this.editorDidMount(this.editor);
}
}
editorWillMount() {
const { editorWillMount } = this.props;
const options = editorWillMount(monaco);
return options || {};
}
editorDidMount(editor: monaco.editor.IStandaloneCodeEditor) {
this.props.editorDidMount(editor, monaco);
this._subscription = editor.onDidChangeModelContent((event) => {
if (!this.__prevent_trigger_change_event) {
this.props.onChange(editor.getValue(), event);
}
});
}
render() {
const { width, height } = this.props;
const fixedWidth = processSize(width);
const fixedHeight = processSize(height);
const style = {
width: fixedWidth,
height: fixedHeight,
};
return (
<div
ref={this.assignRef}
style={style}
className="react-monaco-editor-container"
/>
);
}
}
export default MonacoEditor;