Package Exports
- react-monaco-editor
- react-monaco-editor/lib/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-monaco-editor) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
react-monaco-editor
Monaco Editor for React.
Examples
To build the examples locally, run:
npm install
cd examples && npm install
npm start
Then open http://localhost:8886
in a browser.
Installation
npm install react-monaco-editor
Usage
Using with webpack
import React from 'react';
import { render } from 'react-dom';
import MonacoEditor from 'react-monaco-editor';
class App extends React.Component {
onDidMount(editor) {
console.log('onDidMount', editor);
editor.focus();
}
onChange(newValue, e) {
console.log('onChange', newValue, e);
}
render() {
const initialCode = '// type your code...';
const options = {
selectOnLineNumbers: true
};
return (
<MonacoEditor
width="800"
height="600"
language="javascript"
value={initialCode}
options={options}
onChange={::this.onChange}
onDidMount={::this.onDidMount}
/>
);
}
}
render(
<App />,
document.getElementById('root')
);
Add a Webpack plugin copy-webpack-plugin
to your webpack.config.js
:
const CopyWebpackPlugin = require('copy-webpack-plugin');
module.exports = {
plugins: [
new CopyWebpackPlugin([
{
from: 'node_modules/monaco-editor/min/vs',
to: 'vs',
}
])
]
};
Fill from
field with the actual path of monaco-editor
package in node_modules.
Using with require.config (do not need Webpack)
class App extends React.Component {
render() {
const requireConfig = {
url: 'https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.1/require.min.js',
paths: {
'vs': 'https://www.mycdn.com/monaco-editor/0.6.1/min/vs'
}
};
return (
<MonacoEditor
width="800"
height="600"
language="javascript"
value="// type your code..."
requireConfig={requireConfig}
/>
);
}
}
requireConfig
is optional, equal to:
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.1/require.min.js"></script>
<script>
require.config({ paths: { 'vs': 'https://www.mycdn.com/monaco-editor/0.6.1/min/vs' }});
</script>
Both them are valid ways to config loader url and relative module path.
You may need to note the cross domain case.
Properties
width
width of editor. Defaults to100%
.height
height of editor. Defaults to500
.value
the initial value of the auto created model in the editor.language
the initial language of the auto created model in the editor.options
refer to Monaco interface IEditorOptions.onChange
an event emitted when the content of the current model has changed.onDidMount
an event emitted when the editor has been mounted (similar tocomponentDidMount
of React).requireConfig
optional, using to config loader url and relative module path, refer to require.config.
Events & Methods
Refer to Monaco interface IEditor.
Q & A
How to interact with the MonacoEditor instance
Using the first parameter of onDidMount
, or using a ref
(e.g. <MonacoEditor ref="monaco">
) after onDidMount
event has fired.
Then you can invoke instance methods via this.refs.monaco.editor
, e.g. this.refs.monaco.editor.focus()
to focuses the MonacoEditor instance.
How to get value of editor
Using this.refs.monaco.editor.getValue()
or via method of Model
instance:
const model = this.refs.monaco.editor.getModel();
const value = model.getValue();
License
MIT, see the LICENSE file for detail.