Created
November 8, 2011 20:02
-
-
Save ericallam/1348983 to your computer and use it in GitHub Desktop.
Set certain regions of an Ace editor to readOnly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
event = require('pilot/event') | |
Anchor = require('ace/anchor').Anchor | |
doc = ace_editor.session.getDocument() | |
editablePositions = [[1, 0, 2, 0]] # allow editong only the second row | |
jQuery.each editable, (index, row) -> | |
editablePositions.push [new Anchor(doc, row[0], row[1]), new Anchor(doc, row[2], row[3])] | |
Range = require('ace/range').Range | |
event.addListener(jQuery('#ace textarea')[0], 'keydown', (e) -> | |
# console.log e.type, e.charCode, e.keyCode, e | |
position = ace_editor.getCursorPosition() | |
setReadOnly = false | |
jQuery.each editablePositions, (index, editableAnchors) -> | |
editableRange = Range.fromPoints editableAnchors[0].getPosition(), editableAnchors[1].getPosition() | |
setReadOnly = !editableRange.contains(position.row, position.column) | |
ace_editor.setReadOnly setReadOnly | |
) |
This code snippet will prevent the user from editing the first or last line of the editor:
editor.commands.on("exec", function(e) {
var rowCol = editor.selection.getCursor();
if ((rowCol.row == 0) || ((rowCol.row + 1) == editor.session.getLength())) {
e.preventDefault();
e.stopPropagation();
}
});
https://jsfiddle.net/tripflex/y0huvc1b/
Source:
https://groups.google.com/forum/#!topic/ace-discuss/yffGsSG7GSA
thanks!
It doesn't work if the user selects many lines, then deletes. (cursor is not in the first or the last line)
This code snippet will prevent the user from editing the first or last line of the editor:
editor.commands.on("exec", function(e) { var rowCol = editor.selection.getCursor(); if ((rowCol.row == 0) || ((rowCol.row + 1) == editor.session.getLength())) { e.preventDefault(); e.stopPropagation(); } });
https://jsfiddle.net/tripflex/y0huvc1b/
Source: https://groups.google.com/forum/#!topic/ace-discuss/yffGsSG7GSA
@tripflex Hey , In this e.preventDefault will not throw error?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What is "require('pilot/event')" file