首頁>建站資源>其他資源

例子

要在本地構建示例,請運行:

yarn
cd example
yarn
yarn start

然後在瀏覽器中打開http://localhost:8886

安裝

yarn add react-monaco-editor

與WebPack一起使用

 import React from 'react' ;
import { createRoot } from "react-dom/client" ;
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 }
      />
    ) ;
  }
}

const container = document . getElementById ( 'root' ) ;
const root = createRoot ( container ) ;
root . render ( < App /> ) ;

將摩納哥Webpack插件monaco-editor-webpack-plugin添加到您的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/blob/main/webpack-plugin/README.md#options
      languages : [ 'json' ]
    } )
  ]
} ;

Sidenote:摩納哥編輯器在內部使用CSS導入,因此,如果您在項目中使用CSS模塊 - 默認情況下可能會發生衝突。為了避免這種情況 - 應用程序和摩納哥編輯包的CSS-Loader分開:

 // 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' ] ,
} 

特性

以下所有屬性都是可選的。

事件和方法

請參閱摩納哥界面IEDITOR。

摩納哥界面可通過導入可用

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

問答

我沒有語法突出顯示 /自動完成 /驗證。

確保使用摩納哥Webpack插件或遵循有關如何加載摩納哥ESM版本的說明。

如何與Monacoditor實例互動

使用editorDidMount的第一個參數,或使用ref (例如<MonacoEditor ref="monaco"> )在editorDidMount事件發生後。

然後,您可以通過this.refs.monaco.editor調用實例方法,例如this.refs.monaco.editor.focus()以焦點摩納哥了腳步。

如何獲得編輯的價值

使用this.refs.monaco.editor.getValue()或通過Model實例的方法:

 const model = this . refs . monaco . editor . getModel ( ) ;
const value = model . getValue ( ) ;

在編輯之前做某事

例如,您可能需要在編輯器安裝之前配置一些JSON架構,然後您可以使用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 } />
        ) ;
    }
}

使用多個主題

摩納哥只支持一個主題。

如何使用差異編輯器

 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 }
      />
    ) ;
  }
}

使用create-react-app

使用react-monaco-editorcreate-react-app使用的最簡單方法是使用React App-Rewired項目。為了設置它,需要以下步驟:

  1. 安裝react-app-rewirednpm install -D react-app-rewired
  2. packages.json的腳本部分中的react-app-rewired替換react-scripts
  3. 在您的項目的根目錄中創建一個config-overrides.js並具有以下內容:
 const MonacoWebpackPlugin = require ( 'monaco-editor-webpack-plugin' ) ;

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

有關更多信息,請在此處查看react-app-rewired的文檔。

執照

麻省理工學院,請參閱許可證文件以獲取詳細信息。

展開
附加信息