Have you tried reducing this problem to the minimal script needed to trigger it? That might help reproduce the issue (dynamically changing the mode generally works).
<template>
<div v-bind=" $attrs ">
<div class="vflex scroll-y" style="height:100%;">
<select v-model=" mode " class="float-r" @change=" setOption( 'mode', mode ) ">
<option v-for="(v, k) in modes " :key=" v " :value=" k ">{{ k }}</option>
</select>
</div>
<div class="flex-1">
<textarea ref="ta" />
</div>
</div>
</div>
</template>
<script>
import codemirror from "codemirror"
import mimes from 'dew/src/const/mimes/codes'
export default {
name: 'CodeMirrorEditor',
// model: {
// prop: "value",
// event: "input"
// },
props: {
options: { type: Object, default: {} },
modelValue: { type: String, default: "" },
data: function () {
return {
instance: undefined,
modes: mimes,
mode: this.options.mode || "text/x-markdown"
mounted () {
// window.mimes = mimes
// console.log( codemirror.modes, codemirror.miniModes )
let ops = Object.assign( {}, {
lineWrapping: true,
styleActiveLine: true,
lineNumbers: true,
smartIndent: true,
indentWithTabs: false,
theme: 'idea', //编辑器主题
lint: true,
matchBrackets: true,
foldGutter: true,
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter", "CodeMirror-lint-markers"],
highlightFormatting: true,
emoji: false,
mode: "text/x-markdown",
}, this.options );
console.log( ops )
let x = codemirror.fromTextArea( this.$refs.ta, ops );
// x.setSize( '100%', '100%' );
x.on( "change", ( inst, obj ) => {
// console.log( inst.getValue() )
// 这里竟然可以被@input捕获
this.$emit( "update:modelValue", inst.getValue() );
x.on( "inputRead", ( inst, e ) => {
// console.log( inst.getValue() )
this.$emit( "update:modelValue", inst.getValue() );
// 不知道为什么上面的没有被@input捕获,于是这里重新emit
this.$emit( "input", inst.getValue() )
this.instance = x;
methods: {
getInstance () {
return this.instance;
setOption ( k, v ) {
console.log( k, v )
this.instance.setOption( k, v );
refresh () {
return this.instance.refresh();
getValue () {
return this.instance.getValue();
setValue ( v ) {
return this.instance.setValue( v );
disable ( flag = true ) {
this.instance.setOption( "readOnly", flag )
</script>