forked from codesandbox/codesandbox-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonacoReactComponent.js
More file actions
142 lines (118 loc) · 3.46 KB
/
MonacoReactComponent.js
File metadata and controls
142 lines (118 loc) · 3.46 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
import FontFaceObserver from 'fontfaceobserver';
import React from 'react';
function noop() {}
class MonacoEditor extends React.PureComponent {
constructor(props) {
super(props);
this.containerElement = undefined;
}
componentDidMount() {
this.afterViewInit();
}
componentWillUnmount() {
this.destroyMonaco();
}
editorWillMount = monaco => {
const { editorWillMount } = this.props;
editorWillMount(monaco);
};
editorDidMount = (editor, monaco) => {
this.props.editorDidMount(editor, monaco);
};
afterViewInit = () => {
const context = this.props.context || window;
if (context.monaco !== undefined) {
this.initMonaco();
return;
}
// eslint-disable-next-line global-require
require('app/overmind/effects/vscode/vscode-script-loader').default(false, [
'vs/editor/editor.main',
])(() => {
this.initMonaco();
});
};
initMonaco = () => {
const { theme, options, diffEditor = false } = this.props;
const context = this.props.context || window;
if (this.containerElement && typeof context.monaco !== 'undefined') {
// Before initializing monaco editor
this.editorWillMount(context.monaco);
window.monacoCodeSandbox = {
openModel: model => this.props.openReference(model),
};
const appliedOptions = { ...options };
const fonts = appliedOptions.fontFamily.split(',').map(x => x.trim());
// We first just set the default fonts for the editor. When the custom font has loaded
// we set that one so that Monaco doesn't get confused.
// https://github.com/Microsoft/monaco-editor/issues/392
let firstFont = fonts[0];
if (firstFont.startsWith('"')) {
// Font is eg. '"aaaa"'
firstFont = JSON.parse(firstFont);
}
if (firstFont === 'dm') {
const font = new FontFaceObserver(firstFont);
font.load().then(
() => {
if (this.editor && this.props.getEditorOptions) {
this.editor.updateOptions(this.props.getEditorOptions());
}
},
() => {
// Font was not loaded in 3s, do nothing
}
);
appliedOptions.fontFamily = fonts.slice(1).join(', ');
}
this.editor = context.monaco.editor[
diffEditor ? 'createDiffEditor' : 'create'
](this.containerElement, appliedOptions);
if (theme) {
context.monaco.editor.setTheme(theme);
}
// After initializing monaco editor
this.editorDidMount(this.editor, context.monaco);
}
};
destroyMonaco = () => {
if (typeof this.editor !== 'undefined') {
this.editor.dispose();
}
};
assignRef = component => {
this.containerElement = component;
};
render() {
const { width, height } = this.props;
const fixedWidth = width.toString().includes('%') ? width : `${width}px`;
const fixedHeight = height.toString().includes('%')
? height
: `${height}px`;
const style = {
width: fixedWidth,
height: fixedHeight,
overflow: 'hidden',
position: 'absolute',
};
return (
<div
ref={this.assignRef}
style={style}
className="react-monaco-editor-container"
/>
);
}
}
MonacoEditor.defaultProps = {
width: '100%',
height: '100%',
theme: null,
options: {},
editorDidMount: noop,
editorWillMount: noop,
onChange: noop,
template: '',
requireConfig: {},
};
export default MonacoEditor;