Monaco Editor เป็นตัวแก้ไขรหัสที่ Powers Vs Code ตอนนี้มีให้บริการเป็นองค์ประกอบ Vue <MonacoEditor> ขอบคุณโครงการนี้
npm install vue-monacoหรือ
yarn add vue-monacoใช้ Monaco-Editor-Webpack-Plugin:
// webpack.config.js
const MonacoEditorPlugin = require ( 'monaco-editor-webpack-plugin' )
module . exports = {
plugins : [
new MonacoEditorPlugin ( {
// https://github.com/Microsoft/monaco-editor-webpack-plugin#options
// Include a subset of languages support
// Some language extensions like typescript are so huge that may impact build performance
// e.g. Build full languages support with webpack 4.0 takes over 80 seconds
// Languages are loaded on demand at runtime
languages : [ 'javascript' , 'css' , 'html' , 'typescript' ]
} )
]
}จากนั้นใช้ส่วนประกอบ:
< template >
< MonacoEditor class = " editor " v-model = " code " language = " javascript " />
</ template >
< script >
import MonacoEditor from ' vue-monaco '
export default {
components : {
MonacoEditor
},
data () {
return {
code : ' const noop = () => {} '
}
}
}
</ script >
< style >
.editor {
width : 600 px ;
height : 800 px ;
}
</ style > <!DOCTYPE html >
< html >
< head >
< meta http-equiv =" X-UA-Compatible " content =" IE=edge " />
< meta http-equiv =" Content-Type " content =" text/html;charset=utf-8 " />
</ head >
< body >
< div id =" app " > </ div >
< script src =" https://unpkg.com/vue " > </ script >
< script src =" https://unpkg.com/vue-monaco " > </ script >
< script src =" monaco-editor/min/vs/loader.js " > </ script >
< script >
require . config ( { paths : { vs : 'monaco-editor/min/vs' } } )
new Vue ( {
el : '#app' ,
template : `
<monaco-editor
style="width:800px;height:600px;border:1px solid grey"
v-model="code"
language="javascript"
:amdRequire="amdRequire"
/>` ,
data : {
code : 'const noop = () => {}'
} ,
methods : {
amdRequire : require
}
} )
</ script >
</ body >
</ html > เมื่อโหลด Monaco-editor จาก CDN คุณจะต้องเปลี่ยน require.config เพื่อให้มีลักษณะเช่นนี้:
require . config ( { paths : { vs : 'http://www.mycdn.com/monaco-editor/min/vs' } } )
// Before loading vs/editor/editor.main, define a global MonacoEnvironment that overwrites
// the default worker url location (used when creating WebWorkers). The problem here is that
// HTML5 does not allow cross-domain web workers, so we need to proxy the instantiation of
// a web worker through a same-domain script
window . MonacoEnvironment = {
getWorkerUrl : function ( workerId , label ) {
return `data:text/javascript;charset=utf-8, ${ encodeURIComponent ( `
self.MonacoEnvironment = {
baseUrl: 'http://www.mycdn.com/monaco-editor/min/'
};
importScripts('http://www.mycdn.com/monaco-editor/min/vs/base/worker/workerMain.js');` ) } `
}
}options : อาร์กิวเมนต์ที่สองของ monaco.editor.createvalue : ทางลัดในการตั้ง options.valuetheme : ทางลัดในการตั้ง options.themelanguage : ทางลัดในการตั้งค่า options.language ภาษาamdRequire : โหลด Monaco-editor โดยใช้ฟังก์ชั่นที่กำหนดสไตล์ AMDdiffEditor : boolean ระบุว่านี่เป็น diffreditor, false โดยค่าเริ่มต้นeditorWillMountmonaco : monaco moduleเรียกก่อนติดตั้งบรรณาธิการ
editorDidMounteditor : IStandaloneCodeEditor สำหรับ Editor ปกติ, IStandaloneDiffEditor สำหรับ Diff Editorเรียกว่าเมื่อติดตั้งบรรณาธิการ
changeค่าตัวแก้ไขได้รับการอัปเดต
value : ค่าตัวแก้ไขใหม่event : event จาก onDidChangeModelContent คุณสามารถฟังกิจกรรมบรรณาธิการได้โดยตรงเช่นนี้:
< template >
< MonacoEditor v-model = " code " @editorDidMount = " editorDidMount " />
</ template >
< script >
export default {
methods : {
editorDidMount ( editor ) {
// Listen to `scroll` event
editor . onDidScrollChange ( e => {
console . log (e)
})
}
},
data () {
return {
code : ' ... '
}
}
}
</ script >อ้างถึงหน้านี้สำหรับกิจกรรมบรรณาธิการทั้งหมด
getEditor(): IStandaloneCodeEditor : ส่งคืนอินสแตนซ์ของตัวแก้ไข ใช้ ref เพื่อโต้ตอบกับส่วนประกอบ MonacoEditor เพื่อเข้าถึงวิธีการ: <MonacoEditor ref="editor" /> สิ่ง this.$refs.editor.getEditor() จะพร้อมใช้งาน
ใช้ prop diffEditor เพื่อระบุว่านี่เป็น diffreditor ใช้เสา original เพื่อตั้งค่าเนื้อหาสำหรับตัวแก้ไขดั้งเดิมให้ใช้ Prop value เพื่อตั้งค่าเนื้อหาสำหรับตัวแก้ไขที่แก้ไขแล้ว
< MonacoEditor
language="javascript"
: diffEditor = " true "
: value = " code "
: original = " originalCode "
/> ในกรณีนี้ getEditor() ของส่วนประกอบจะส่งคืนอินสแตนซ์ IStandaloneDiffEditor ในขณะที่คุณสามารถใช้ getModifiedEditor() เพื่อรับตัวแก้ไขที่แก้ไขแล้วซึ่งเป็นอินสแตนซ์ IStandaloneCodeEditor
git checkout -b my-new-featuregit commit -am 'Add some feature'git push origin my-new-feature Vue-Monaco © Egoist เปิดตัวภายใต้ใบอนุญาต MIT
ประพันธ์และดูแลโดยคนเห็นแก่ตัวด้วยความช่วยเหลือจากผู้มีส่วนร่วม (รายการ)
เว็บไซต์· gitHub @Egoist · Twitter @_egoistlily