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 badges3 Answers
Reset to default 18There 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