CodeMirror:特殊行readonly

Den*_*rat 12 javascript codemirror

我可以将特定数量的行(成功与否)设置为只读模式吗?

例如:我有一个文档,我不希望更改某些部分的内容(例如在Word中,您可以设置页眉和页脚部分,您可以锁定它们).任何人都知道CodeMirror是否支持该功能?

提前致谢!

Mar*_*ijn 15

还有一个markTextreadOnly选项,这可能会更直接地映射到你的使用情况.请参见http://codemirror.net/doc/manual.html#markText


zam*_*uts 10

使用codemirror版本3支持onbeforeChange添加; 只是在变化发生之前捕捉变化并取消应该可以解决问题:

// the line numbers to be "readonly"
var readOnlyLines = [0,1,2,3];

// create the CodeMirror instance
var editor = CodeMirror.fromTextArea(document.getElementById('input'));

// listen for the beforeChange event, test the changed line number, and cancel
editor.on('beforeChange',function(cm,change) {
    if ( ~readOnlyLines.indexOf(change.from.line) ) {
        change.cancel();
    }
});
Run Code Online (Sandbox Code Playgroud)