เพื่อสร้างตัวอย่างในพื้นที่ให้เรียกใช้:
yarn
cd example
yarn
yarn start จากนั้นเปิด http://localhost:8886 ในเบราว์เซอร์
yarn add react-monaco-editor 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 /> ) ; เพิ่มปลั๊กอิน Monaco 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: Monaco Editor ใช้การนำเข้า 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' ] ,
} คุณสมบัติทั้งหมดด้านล่างเป็นตัวเลือก
width ความกว้างของตัวแก้ไข ค่าเริ่มต้นถึง 100%
height ความสูงของตัวแก้ไข ค่าเริ่มต้นถึง 100%
ค่า value ของโมเดลที่สร้างขึ้นอัตโนมัติในตัวแก้ไข
defaultValue ค่าเริ่มต้นของโมเดลที่สร้างขึ้นอัตโนมัติในตัวแก้ไข
language ภาษาเริ่มต้นของโมเดลที่สร้างขึ้นอัตโนมัติในตัวแก้ไข
theme ธีมของบรรณาธิการ
options อ้างอิงถึงอินเทอร์เฟซโมนาโก istandaloneeditorConstructionOptions
overrideServices อ้างถึงอินเทอร์เฟซ Monaco IeditorOverrideservices ขึ้นอยู่กับการใช้งานภายในของโมนาโกและอาจเปลี่ยนแปลงไปตามกาลเวลาตรวจสอบปัญหา GitHub สำหรับรายละเอียดเพิ่มเติม
onChange(newValue, event) เหตุการณ์ที่ปล่อยออกมาเมื่อเนื้อหาของโมเดลปัจจุบันมีการเปลี่ยนแปลง
editorWillMount(monaco) เหตุการณ์ที่ปล่อยออกมาก่อนที่ตัวแก้ไขจะติดตั้ง (คล้ายกับ componentWillMount ของ React)
editorDidMount(editor, monaco) เหตุการณ์ที่ปล่อยออกมาเมื่อตัวแก้ไขได้รับการติดตั้ง (คล้ายกับ componentDidMount ของ React)
editorWillUnmount(editor, monaco) เหตุการณ์ที่ปล่อยออกมาก่อนที่ Editor Undount (คล้ายกับ componentWillUnmount of React)
อ้างถึงอินเตอร์เฟสโมนาโก IEDitor
อินเทอร์เฟซโมนาโกมีให้โดยนำเข้า
import { monaco } from 'react-monaco-editor' ; ตรวจสอบให้แน่ใจว่าใช้ปลั๊กอิน Monaco Webpack หรือทำตามคำแนะนำเกี่ยวกับวิธีการโหลด Monaco เวอร์ชัน ESM
การใช้พารามิเตอร์แรกของ editorDidMount หรือใช้ ref (เช่น <MonacoEditor ref="monaco"> ) หลังจากเหตุการณ์ editorDidMount ได้ยิง
จากนั้นคุณสามารถเรียกใช้วิธีการอินสแตนซ์ผ่าน this.refs.monaco.editor เช่น this.refs.monaco.editor.focus() เพื่อมุ่งเน้นอินสแตนซ์ของ monacoeditor
ใช้ this.refs.monaco.editor.getValue() หรือผ่านวิธีการของอินสแตนซ์แบบ Model :
const model = this . refs . monaco . editor . getModel ( ) ;
const value = model . getValue ( ) ; ตัวอย่างเช่นคุณอาจต้องการกำหนดค่า schemas 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-editor กับ create-react-app คือการใช้โครงการ React-App-Rewired สำหรับการตั้งค่าจำเป็นต้องมีขั้นตอนต่อไปนี้:
react-app-rewired npm install -D react-app-rewired rewirerreact-scripts โดย react-app-rewired ในส่วนสคริปต์ของ packages.json ของคุณ jsonconfig-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 ที่นี่
MIT ดูรายละเอียดไฟล์ใบอนุญาต