CodeMirror 是一个强大的基于浏览器的文本编辑器组件,主要用于网页中创建可编辑的源代码区域,特别适用于编写和展示程序代码。它支持多种编程语言的语法高亮、代码折叠、自动补全、查找替换等多种高级编辑特性。
npm install vue- codemirror -- save
或者
npm install codemirror -- save
封装组件
< template>
< div class = "common-editor" >
< textarea ref= "textarea" v- model= "value" > < / textarea>
< / div>
< / template>
< script>
import 'codemirror/lib/codemirror.css'
import 'codemirror/theme/rubyblue.css'
import 'codemirror/theme/idea.css'
import 'codemirror/theme/blackboard.css'
import 'codemirror/theme/dracula.css'
import 'codemirror/addon/hint/show-hint.css'
import 'codemirror/addon/hint/show-hint.js'
import 'codemirror/addon/hint/show-hint'
import 'codemirror/addon/lint/lint.css'
import "codemirror/addon/lint/lint.js" ;
import "codemirror/addon/display/autorefresh" ;
import 'codemirror/mode/python/python'
import 'codemirror/mode/clike/clike'
import 'codemirror/addon/search/match-highlighter'
const CodeMirror = require ( 'codemirror/lib/codemirror' )
export default {
name : 'CommonEditor' ,
props : {
value : {
type : String,
default : ''
} ,
language : {
type : String,
default : null
}
} ,
data ( ) {
return {
CommonEditor : false ,
code : '' ,
coder : null ,
flag : false ,
mode : 'java' ,
theme : 'default' ,
modes : [
{ value : 'x-java' , label : 'Java' } ,
{ value : 'x-python' , label : 'Python' }
]
}
} ,
watch : {
language : {
handler ( language ) {
this . getCoder ( ) . then ( ( ) => {
if ( language) {
const modeObj = this . getLanguage ( language)
if ( modeObj) {
this . mode = modeObj. label
this . coder. setOption ( 'mode' , ` text/ ${ modeObj. value} ` )
}
}
} )
} ,
immediate : true
} ,
value : {
handler ( val ) {
if ( this . $isNotEmpty ( val) ) {
this . flag = true
}
} ,
deep : true ,
immediate : true
}
} ,
computed : {
coderOptions ( ) {
return {
line : true ,
mode : 'application/json' ,
theme : 'dracula' ,
tabSize : 1 ,
lint : true ,
readOnly : true ,
lineNumbers : true ,
cursorHeight : 0.8 ,
autoCloseBrackets : true ,
gutters : [
"CodeMirror-lint-markers" ,
"CodeMirror-linenumbers" ,
"CodeMirror-foldgutter"
] ,
keyMap : 'default' ,
foldGutter : true ,
autoCloseBrackets : true ,
autoCloseTags : true ,
matchTags : { bothTags : true } ,
matchBrackets : true ,
styleActiveLine : true ,
matchBrackets : true ,
lineWrapping : 'wrap' ,
showCursorWhenSelecting : true ,
smartIndent : false ,
completeSingle : false ,
highlightSelectionMatches : {
minChars : 2 ,
trim : true ,
style : "matchhighlight" ,
showToken : false
} ,
}
}
} ,
mounted ( ) {
this . initialize ( )
} ,
methods : {
initialize ( ) {
this . coder = CodeMirror. fromTextArea (
this . $refs. textarea,
this . coderOptions
)
this . coder. on ( 'inputRead' , ( ) => {
this . coder. showHint ( )
} )
if ( this . value || this . code) {
this . setCodeContent ( this . value || this . code)
} else {
this . coder. setValue ( '' )
}
this . coder. on ( 'change' , ( coder ) => {
this . code = coder. getValue ( )
if ( this . $emit) {
this . $emit ( 'input' , this . code)
}
} )
} ,
setCodeContent ( val ) {
setTimeout ( ( ) => {
if ( ! val) {
this . coder. setValue ( '' )
} else {
this . coder. setValue ( val)
}
} , 300 )
} ,
getCoder ( ) {
const that = this
return new Promise ( ( resolve ) => {
; ( function get ( ) {
if ( that. coder) {
resolve ( that. coder)
} else {
setTimeout ( get, 10 )
}
} ) ( )
} )
} ,
getLanguage ( language ) {
return this . modes. find ( ( mode ) => {
const currentLanguage = language. toLowerCase ( )
const currentLabel = mode. label. toLowerCase ( )
const currentValue = mode. value. toLowerCase ( )
return (
currentLabel === currentLanguage || currentValue === currentLanguage
)
} )
} ,
changeMode ( val ) {
this . coder. setOption ( 'mode' , ` text/ ${ val} ` )
const label = this . getLanguage ( val) . label. toLowerCase ( )
this . $emit ( 'language-change' , label)
}
}
}
< / script>
< style lang= "scss" scoped>
. common- editor {
width : 100 % ;
height : 100 % ;
. CodeMirror {
direction : ltr;
line- height: 20px;
width : 100 % ;
height : 100 % ;
}
. CodeMirror- hints {
z- index: 9999 ! important;
}
. custom- class . CodeMirror {
width : 100 % ;
}
}
. CodeMirror- hints {
z- index: 1000 ;
}
< / style>
组件引入
< CommonEditor : value= "value" language= "java" > < / CommonEditor> 在
实现效果