npm install codemirror下载组件
引入核心配置包
import * as CodeMirror from 'codemirror/lib/codemirror'
import 'codemirror/lib/codemirror.css'
let myTextarea = document.getElementById('editor');
this.CodeMirrorEditor = CodeMirror.fromTextArea(myTextarea, {
mode:'javascript'//编辑器语言
theme:'monokai', //编辑器主题
extraKeys: {"Ctrl": "autocomplete"},//ctrl可以弹出选择项
lineNumbers: true//显示行号
});
5.根据设置的主题,引入相应的主题包,主题包存储在theme下,使用其他主题包时设置option中theme为对应主题
import 'codemirror/theme/monokai.css'
6.根据设置的编辑器语言,引入相应工具包,以下为常用语言包
import 'codemirror/mode/javascript/javascript'
import 'codemirror/mode/clike/clike'
import 'codemirror/mode/go/go'
import 'codemirror/mode/htmlmixed/htmlmixed'
import 'codemirror/mode/http/http'
import 'codemirror/mode/php/php'
import 'codemirror/mode/python/python'
import 'codemirror/mode/http/http'
import 'codemirror/mode/sql/sql'
import 'codemirror/mode/vue/vue'
import 'codemirror/mode/xml/xml'
7.初始化编译器时可设置属性:
import codemirror/keymap/sublime.js
import codemirror/keymap/emacs.js
import codemirror/keymap/vim.js
import 'codemirror/addon/hint/show-hint'
import 'codemirror/addon/hint/javascript-hint'
import 'codemirror/addon/hint/sql-hint'
import 'codemirror/addon/hint/html-hint'
import 'codemirror/addon/hint/xml-hint'
import 'codemirror/addon/hint/anyword-hint'
import 'codemirror/addon/hint/css-hint'
import 'codemirror/addon/hint/show-hint'
extraKeys的快捷键绑定命令:
scrollbarStyle: 设置滚动条,默认为"null"为不显示的滚动条,可以使用提供的其他滚动条:“simple”,"overlay"选择内侧与外侧的滚动条,使用需引入以下工具包:
import 'codemirror/addon/scroll/simplescrollbars.css'
import 'codemirror/addon/scroll/simplescrollbars'
import 'codemirror/addon/selection/active-line'
8.动态设置CodeMirror属性:(以设置支持语言mode为例)
this.CodeMirrorEditor.setOption("mode",this.something)
触发器使用方法:(change事件为例)
this.CodeMirrorEditor.on("change",function(){
//事件触发后执行事件
alert("change")
});
取消触发器方法:
this.CodeMirrorEditor.off("change")
实现代码:
setting.ts
import Vue from 'vue'
import { Component, Prop, Watch} from 'vue-property-decorator'
import * as WithRender from './setting.html?style=./setting.less'
import * as CodeMirror from 'codemirror/lib/codemirror'
import 'codemirror/theme/monokai.css'
import 'codemirror/lib/codemirror.css'
import 'codemirror/mode/javascript/javascript'
import 'codemirror/mode/clike/clike'
import 'codemirror/mode/go/go'
import 'codemirror/mode/htmlmixed/htmlmixed'
import 'codemirror/mode/http/http'
import 'codemirror/mode/php/php'
import 'codemirror/mode/python/python'
import 'codemirror/mode/http/http'
import 'codemirror/mode/sql/sql'
import 'codemirror/mode/vue/vue'
import 'codemirror/mode/xml/xml'
import 'codemirror/addon/scroll/simplescrollbars.css'
import 'codemirror/addon/scroll/simplescrollbars'
import 'codemirror/addon/hint/show-hint'
import 'codemirror/addon/hint/javascript-hint'
import 'codemirror/addon/hint/sql-hint'
import 'codemirror/addon/hint/html-hint'
import 'codemirror/addon/hint/xml-hint'
import 'codemirror/addon/hint/anyword-hint'
import 'codemirror/addon/hint/css-hint'
import 'codemirror/addon/hint/show-hint.css'
import 'codemirror/addon/fold/markdown-fold'
import 'codemirror/mode/meta'
import "codemirror/addon/fold/foldgutter.css"
import "codemirror/addon/fold/foldcode"
import "codemirror/addon/fold/brace-fold"
import 'codemirror/addon/selection/active-line'
@WithRender
@Component
export class Setting extends Vue{
nameArray=new Array();
modeArray=new Array();
mapArray=new Array();
CodeMirrorEditor;
something="";
mounted(){
console.log(CodeMirror)
for(var i=0;i {
this.CodeMirrorEditor.setOption("mode",this.something)
}
}
setmirror(){
let myTextarea = document.getElementById('editor');
this.CodeMirrorEditor = CodeMirror.fromTextArea(myTextarea, {
theme:'monokai',
styleActiveLine: true,
mode:'javascript',
extraKeys: {"Ctrl": "autocomplete"},//输入s然后ctrl就可以弹出选择项
// lineNumbers: true,
tabSize:10,
// readOnly:"nocursor",
smartIndent:true,
scrollbarStyle:"overlay"
// keymap:"defaule"
});
// this.CodeMirrorEditor.setOption('lineWrapping', true);
this.CodeMirrorEditor.on("keypress",() =>{
//编译器内容更改事件
console.log(this.CodeMirrorEditor)
this.CodeMirrorEditor.showHint();
});
this.CodeMirrorEditor.setValue("Hello Kitty\nHello Tony\nHow are you\nFine thank you and you \nI love you")
this.CodeMirrorEditor.setOption("lineNumbers","true")
// this.CodeMirrorEditor.addOverlay("coconut");
// this.CodeMirrorEditor.markText({line:0,ch:0},{line:0,ch:0})
this.CodeMirrorEditor.setBookmark({line:0,ch:0},{line:0,ch:1},{readOnly:true});
this.CodeMirrorEditor.setCursor(0)
this.CodeMirrorEditor.setSize(600,600)
}
}
setting.html
setting.less
.setting{
margin: 0;
width: 1398px;
height: 600px;
background-color: rgb(248, 248, 248);
h1{
margin: 0;
}
}