Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeMirror: Particular lines readonly

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!

like image 207
Denis Brat Avatar asked Jul 01 '13 23:07

Denis Brat


2 Answers

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

like image 79
Marijn Avatar answered Sep 17 '22 21:09

Marijn


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();
    }
});
like image 45
zamnuts Avatar answered Sep 19 '22 21:09

zamnuts