Skip to content

Commit 2452d49

Browse files
committed
feat: encapsulate monaco-editor as react component
1 parent 07d1260 commit 2452d49

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import * as React from 'react';
2+
import { Component } from 'react';
3+
import * as monaco from 'monaco-editor';
4+
import { APP_PREFIX } from 'mo/common/const';
5+
6+
export const SYMBOL_MONACO_EDITOR = `${APP_PREFIX}-monaco-editor`;
7+
8+
export interface IMonacoEditorProps extends React.ComponentProps<any> {
9+
/**
10+
* The value of monaco editor
11+
*/
12+
value?: string;
13+
/**
14+
* The option of monaco editor
15+
*/
16+
options?: monaco.editor.IStandaloneEditorConstructionOptions;
17+
/**
18+
* The override for monaco editor
19+
*/
20+
override?: monaco.editor.IEditorOverrideServices;
21+
editorInstanceRef?: (instance: monaco.editor.IStandaloneCodeEditor) => void;
22+
}
23+
24+
export default class MonacoEditor extends Component<IMonacoEditorProps> {
25+
/**
26+
* The instance of monaco
27+
*/
28+
private monacoInstance!: monaco.editor.IStandaloneCodeEditor;
29+
/**
30+
* The dom element of editor container
31+
*/
32+
private monacoDom!: HTMLDivElement;
33+
34+
constructor(props) {
35+
super(props);
36+
}
37+
38+
componentDidMount() {
39+
const { options = {}, override, editorInstanceRef } = this.props;
40+
this.monacoInstance = monaco.editor.create(this.monacoDom, options, override);
41+
if (editorInstanceRef) {
42+
editorInstanceRef(this.monacoInstance);
43+
}
44+
}
45+
46+
componentWillUnmount() {
47+
if (this.monacoInstance) {
48+
this.monacoInstance.dispose();
49+
}
50+
}
51+
52+
render() {
53+
const { style } = this.props;
54+
let renderStyle: any = {
55+
position: 'relative',
56+
minHeight: '400px',
57+
height: '100%',
58+
width: '100%',
59+
};
60+
61+
renderStyle = style ? Object.assign(renderStyle, style) : renderStyle;
62+
63+
return (
64+
<div
65+
style={renderStyle}
66+
className={SYMBOL_MONACO_EDITOR}
67+
ref={(domIns: HTMLDivElement) => {
68+
this.monacoDom = domIns;
69+
}}
70+
/>
71+
);
72+
}
73+
}

0 commit comments

Comments
 (0)