All Projects → react-monaco-editor → React Monaco Editor

react-monaco-editor / React Monaco Editor

Licence: mit
Monaco Editor for React.

Programming Languages

typescript
32286 projects
javascript
184084 projects - #8 most used programming language
HTML
75241 projects

Projects that are alternatives of or similar to React Monaco Editor

Monaco Editor
A browser based code editor
Stars: ✭ 27,382 (+979.73%)
Mutual labels:  editor, monaco-editor
Vue Monaco
MonacoEditor component for Vue.js
Stars: ✭ 471 (-81.43%)
Mutual labels:  editor, monaco-editor
Vue Monaco Editor
Monaco Editor Vue Component
Stars: ✭ 187 (-92.63%)
Mutual labels:  editor, monaco-editor
Orchestra
One language to be RegExp's Successor. Visually readable and rich, technically safe and extended, naturally scalable, advanced, and optimized
Stars: ✭ 103 (-95.94%)
Mutual labels:  editor, monaco-editor
Bim
small terminal text editor with syntax highlighting
Stars: ✭ 174 (-93.14%)
Mutual labels:  editor
Pythonvscode
This extension is now maintained in the Microsoft fork.
Stars: ✭ 2,013 (-20.62%)
Mutual labels:  editor
Vue Markdown Editor
A markdown editor built on Vue
Stars: ✭ 169 (-93.34%)
Mutual labels:  editor
Blocks.js
JavaScript dataflow graph editor
Stars: ✭ 165 (-93.49%)
Mutual labels:  editor
Pixel Editor
An online canvas based Pixel Art creation tool for Lospec.com
Stars: ✭ 180 (-92.9%)
Mutual labels:  editor
Unityheapexplorer
A Memory Profiler, Debugger and Analyzer for Unity 2019.3 and newer.
Stars: ✭ 179 (-92.94%)
Mutual labels:  editor
Ngx Inline Editor
Native UI Inline-editor Angular (4.0+) component
Stars: ✭ 172 (-93.22%)
Mutual labels:  editor
Popcode
An HTML/CSS/JavaScript editor for use in the classroom
Stars: ✭ 169 (-93.34%)
Mutual labels:  editor
Web Code
A text editor for the web based around monaco
Stars: ✭ 174 (-93.14%)
Mutual labels:  monaco-editor
Apubplat
Devops自动化部署、堡垒机开源项目、Web Terminal
Stars: ✭ 167 (-93.41%)
Mutual labels:  monaco-editor
Nib
Wysiwyg / Text editor components built using React and Prosemirror
Stars: ✭ 181 (-92.86%)
Mutual labels:  editor
Monogame.forms
MonoGame.Forms is the easiest way of integrating a MonoGame render window into your Windows Forms project. It should make your life much easier, when you want to create your own editor environment.
Stars: ✭ 165 (-93.49%)
Mutual labels:  editor
Richtexteditor
Rich text editor lib for android. (Img support) - 富文本编辑器(支持图片)
Stars: ✭ 172 (-93.22%)
Mutual labels:  editor
Vscode Chrome Debug
Debug your JavaScript code running in Google Chrome from VS Code.
Stars: ✭ 2,126 (-16.17%)
Mutual labels:  editor
Notepad Plus Plus
🧛🏻‍♂️ Dark theme for Notepad++
Stars: ✭ 170 (-93.3%)
Mutual labels:  editor
Edit Text
Collaborative rich text editor for the web. Written in Rust + WebAssembly.
Stars: ✭ 171 (-93.26%)
Mutual labels:  editor

react-monaco-editor

Monaco Editor for React.

NPM version Downloads Test

Examples

To build the examples locally, run:

yarn
cd example
yarn
yarn start

Then open http://localhost:8886 in a browser.

Installation

yarn add react-monaco-editor

Using with Webpack

import React from 'react';
import { render } from 'react-dom';
import MonacoEditor from 'react-monaco-editor';

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      code: '// type your code...',
    }
  }
  editorDidMount(editor, monaco) {
    console.log('editorDidMount', editor);
    editor.focus();
  }
  onChange(newValue, e) {
    console.log('onChange', newValue, e);
  }
  render() {
    const code = this.state.code;
    const options = {
      selectOnLineNumbers: true
    };
    return (
      <MonacoEditor
        width="800"
        height="600"
        language="javascript"
        theme="vs-dark"
        value={code}
        options={options}
        onChange={::this.onChange}
        editorDidMount={::this.editorDidMount}
      />
    );
  }
}

render(
  <App />,
  document.getElementById('root')
);

Add the Monaco Webpack plugin monaco-editor-webpack-plugin to your webpack.config.js:

const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');
module.exports = {
  plugins: [
    new MonacoWebpackPlugin({
      // available options are documented at https://github.com/Microsoft/monaco-editor-webpack-plugin#options
      languages: ['json']
    })
  ]
};

Sidenote: Monaco Editor uses CSS imports internally, so if you're using CSS Modules in your project - you're likely to get conflict by default. In order to avoid that - separate css-loader for app and monaco-editor package:

// Specify separate paths
const path = require('path');
const APP_DIR = path.resolve(__dirname, './src');
const MONACO_DIR = path.resolve(__dirname, './node_modules/monaco-editor');

{
  test: /\.css$/,
  include: APP_DIR,
  use: [{
    loader: 'style-loader',
  }, {
    loader: 'css-loader',
    options: {
      modules: true,
      namedExport: true,
    },
  }],
}, {
  test: /\.css$/,
  include: MONACO_DIR,
  use: ['style-loader', 'css-loader'],
}

Properties

All the properties below are optional.

  • width width of editor. Defaults to 100%.

  • height height of editor. Defaults to 100%.

  • value value of the auto created model in the editor.

  • defaultValue the initial value of the auto created model in the editor.

  • language the initial language of the auto created model in the editor.

  • theme the theme of the editor

  • options refer to Monaco interface IStandaloneEditorConstructionOptions.

  • overrideServices refer to Monaco Interface IEditorOverrideServices. It depends on Monaco's internal implementations and may change over time, check github issue for more details.

  • onChange(newValue, event) an event emitted when the content of the current model has changed.

  • editorWillMount(monaco) an event emitted before the editor mounted (similar to componentWillMount of React).

  • editorDidMount(editor, monaco) an event emitted when the editor has been mounted (similar to componentDidMount of React).

Events & Methods

Refer to Monaco interface IEditor.

The monaco interfaces available by import

import { monaco } from 'react-monaco-editor';

Q & A

I don't get syntax highlighting / autocomplete / validation.

Make sure to use the Monaco Webpack plugin or follow the instructions on how to load the ESM version of Monaco.

How to interact with the MonacoEditor instance

Using the first parameter of editorDidMount, or using a ref (e.g. <MonacoEditor ref="monaco">) after editorDidMount 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();

Do something before editor mounted

For example, you may want to configure some JSON schemas before editor mounted, then you can go with editorWillMount(monaco):

class App extends React.Component {
    editorWillMount(monaco) {
        monaco.languages.json.jsonDefaults.setDiagnosticsOptions({
            validate: true,
            schemas: [{
                uri: "http://myserver/foo-schema.json",
                fileMatch: ['*'],
                schema: {
                    type: "object",
                    properties: {
                        p1: {
                            enum: [ "v1", "v2"]
                        },
                        p2: {
                            $ref: "http://myserver/bar-schema.json"
                        }
                    }
                }
            }]
        });
    }
    render() {
        return (
          <MonacoEditor language="json" editorWillMount={this.editorWillMount} />
        );
    }
}

Use multiple themes

Monaco only supports one theme.

How to use the diff editor

import React from 'react';
import { MonacoDiffEditor } from 'react-monaco-editor';

class App extends React.Component {
  render() {
    const code1 = "// your original code...";
    const code2 = "// a different version...";
    const options = {
      //renderSideBySide: false
    };
    return (
      <MonacoDiffEditor
        width="800"
        height="600"
        language="javascript"
        original={code1}
        value={code2}
        options={options}
      />
    );
  }
}

Usage with create-react-app

The easiest way to use the react-monaco-editor with create-react-app is to use the react-app-rewired project. For setting it up, the following steps are required:

  1. Install react-app-rewired: npm install -D react-app-rewired
  2. Replace react-scripts by react-app-rewired in the scripts section of your packages.json
  3. Create a config-overrides.js in the root directory of your project with the following content:
const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin');

module.exports = function override(config, env) {
  config.plugins.push(new MonacoWebpackPlugin({
    languages: ['json']
  }));
  return config;
}

For more information checkout the documentation of react-app-rewired here.

License

MIT, see the LICENSE file for detail.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].