最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - CodeMirror: Particular lines readonly - Stack Overflow

programmeradmin1浏览0评论

Can I set a particular number of lines (sucessive or not) to read-only mode?

For example: I have a document where I don't want the contents of some sections to be changed (like in Word, where you can set Header and Footer sections and you can lock them). Anyone know if CodeMirror supports that feature?

Thanks in advance!

Can I set a particular number of lines (sucessive or not) to read-only mode?

For example: I have a document where I don't want the contents of some sections to be changed (like in Word, where you can set Header and Footer sections and you can lock them). Anyone know if CodeMirror supports that feature?

Thanks in advance!

Share Improve this question asked Jul 1, 2013 at 23:12 Denis BratDenis Brat 4975 silver badges14 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 18

There is also markText with the readOnly option, which might map more directly to your use case. See http://codemirror.net/doc/manual.html#markText

With codemirror version 3 support for on and beforeChange was added; simply catching the change before it happens and canceling should do the trick:

// 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();
    }
});

For CodeMirror6

I just released codemirror-readonly-ranges extension that easy allow to work with read-only ranges. For anyone who is interested on the solution:

package: https://www.npmjs.com/package/codemirror-readonly-ranges

full documentation: https://andrebnassis.github.io/codemirror-readonly-ranges

发布评论

评论列表(0)

  1. 暂无评论