一、概念
Monaco Editor
是支持VS Code的代码编辑器 。描述代码编辑器功能的好页面在 这里。 它在 MIT 许可下获得许可,并支持 Edge、Chrome、Firefox、Safari 和 Opera。 移动浏览器或移动 Web 框架不支持 Monaco 编辑 器。 microsoft/monaco-editor代码仓库
二、使用步骤
package.json
, 添加 "monaco-editor": "^0.21.2",
"monaco-editor-webpack-plugin": "^7.0.1",
(简化使用)
找到webpack
的配置文件(别弄到babel
的配置文件里了,启动会造成babel的报错 ),我们这的webpack
是分的多
const HtmlWebpackPlugin = require ( 'html-webpack-plugin' )
const MonacoWebpackPlugin = require ( 'monaco-editor-webpack-plugin' )
plugins: [
new HtmlWebpackPlugin ( {
title: 'Message Bridge'
} ) ,
new MonacoWebpackPlugin ( )
]
三、组件
< template>
< div id = " container" > </ div>
</ template>
< script>
import * as monaco from 'monaco-editor/esm/vs/editor/editor.main'
export default {
props: {
sql: {
type: String,
default ( ) {
return ''
}
}
} ,
data ( ) {
return {
editor: null ,
mysql: ''
}
} ,
watch: {
sql ( val ) {
console. log ( 'sql' ) ;
this . setValue ( val)
} ,
mysql ( val ) {
console. log ( 'my' , val) ;
this . handleChange ( )
}
} ,
mounted ( ) {
this . initEditor ( ) ;
} ,
methods: {
initEditor ( ) {
this . editor = monaco. editor. create ( document. getElementById ( 'container' ) , {
value: '' ,
language: 'sql' ,
automaticLayout: true ,
theme: 'vs-dark'
} ) ;
this . setValue ( this . sql)
this . editor. onDidChangeModelContent ( ( event ) => {
const newValue = this . getValue ( ) ;
this . $emit ( 'changeSql' , newValue)
} )
} ,
getValue ( ) {
return this . editor. getValue ( ) ;
} ,
setValue ( sql ) {
this . editor. setValue ( sql) ;
}
}
}
</ script>
//div无法直接高度为100%,得让所有父级设置高度
< style lang = " scss" scoped >
#container {
height : 97%;
}
</ style>
四、优化
找到我们引入的main
寻找我们需要的sql
语言配置 重新引入,修改组件引入信息
import * as monaco from 'monaco-editor/esm/vs/editor/edcore.main' ;
import 'monaco-editor/esm/vs/basic-languages/sql/sql.contribution'