This repository has been archived by the owner on Jul 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.tsx
192 lines (170 loc) · 5.62 KB
/
index.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// /// <reference types="monaco-editor" />
import createElement from 'inferno-create-element';
import InfernoComponent from 'inferno-component';
// XXX currently cannot reference monaco interfaces if using /// directive,
// and cannot pass the monaco module as an object in callbacks.
export type IEditor = any;
export type IModelContentChangedEvent = any;
export interface IEditorOptions {}
export declare type monaco = any;
export declare const monaco;
export interface EditorSettings {
width: string;
height: string;
value: string;
theme: string;
options: IEditorOptions;
language: string;
requireConfig: any;
onMonacoAvailable: { (ns: monaco): void };
onDidMount: { (editor: IEditor): void };
onChange: {
(value: string, evt: IModelContentChangedEvent): void;
};
}
export interface EditorProps {
width?: string;
height?: string;
value?: string;
theme?: string;
options?: IEditorOptions;
language?: string;
requireConfig?: any;
onMonacoAvailable?: { (ns: monaco): void };
onDidMount?: { (editor: IEditor): void };
onChange?: {
(value: string, evt: IModelContentChangedEvent): void;
};
}
export default class MonacoEditor extends InfernoComponent<EditorProps, never> {
private element: HTMLElement;
private editor?: IEditor;
constructor(props: EditorProps) {
super(props);
}
componentDidMount() {
this.afterViewInit();
}
componentWillUnmount() {
this.dispose();
}
render() {
const { width, height } = this.settings;
return (
<div
ref={elem => (this.element = elem)}
style={{ width, height }}
className="inferno-monaco-container"
/>
);
}
/**
* Update the size of the editor to fill its container; call after changing
* the size of the element.
*/
layout() {
this.editor.layout();
}
dispose() {
if (this.editor) {
this.editor.dispose();
}
this.editor = undefined;
}
private afterViewInit() {
const { requireConfig } = this.props;
const loaderUrl = requireConfig.url || 'vs/loader.js';
const w = window as any;
const onGotAmdLoader = () => {
if (w.__REACT_MONACO_EDITOR_LOADER_ISPENDING__) {
// Do not use webpack
if (requireConfig.paths && requireConfig.paths.vs) {
w.require.config(requireConfig);
}
}
// Load monaco
w.require(['vs/editor/editor.main'], () => {
this.initMonaco();
});
// Call the delayed callbacks when AMD loader has been loaded
if (w.__REACT_MONACO_EDITOR_LOADER_ISPENDING__) {
w.__REACT_MONACO_EDITOR_LOADER_ISPENDING__ = false;
let loaderCallbacks =
w.__REACT_MONACO_EDITOR_LOADER_CALLBACKS__;
if (loaderCallbacks && loaderCallbacks.length) {
let currentCallback = loaderCallbacks.shift();
while (currentCallback) {
currentCallback.fn.call(currentCallback.context);
currentCallback = loaderCallbacks.shift();
}
}
}
};
// Load AMD loader if necessary
if (w.__REACT_MONACO_EDITOR_LOADER_ISPENDING__) {
// We need to avoid loading multiple loader.js when there are multiple editors loading concurrently
// delay to call callbacks except the first one
w.__REACT_MONACO_EDITOR_LOADER_CALLBACKS__ =
w.__REACT_MONACO_EDITOR_LOADER_CALLBACKS__ || [];
w.__REACT_MONACO_EDITOR_LOADER_CALLBACKS__.push({
context: this,
fn: onGotAmdLoader,
});
} else {
if (typeof w.require === 'undefined') {
var loaderScript = document.createElement('script');
loaderScript.type = 'text/javascript';
loaderScript.src = loaderUrl;
loaderScript.addEventListener('load', onGotAmdLoader);
document.body.appendChild(loaderScript);
w.__REACT_MONACO_EDITOR_LOADER_ISPENDING__ = true;
} else {
onGotAmdLoader();
}
}
}
private initMonaco() {
const {
value,
language,
theme,
options,
onMonacoAvailable,
} = this.settings;
if (typeof monaco !== 'undefined') {
onMonacoAvailable(monaco);
this.editor = monaco.editor.create(this.element, {
value,
language,
theme,
...options,
});
// After monaco editor has been initialized
this.onDidMount();
}
}
/** Gets the component props with defaults applied. */
private get settings(): EditorSettings {
return this.props as EditorSettings;
}
private onDidMount() {
const { onDidMount, onChange } = this.settings;
onDidMount(this.editor);
this.editor.onDidChangeModelContent(evt =>
onChange(this.editor.getValue(), evt),
);
}
static defaultProps: EditorSettings = {
width: '100%',
height: '500',
value: '',
language: 'javascript',
theme: 'vs',
options: {},
onMonacoAvailable: noop,
onChange: noop,
onDidMount: noop,
requireConfig: {},
};
}
function noop(_: any): void {}