Skip to content

Instantly share code, notes, and snippets.

@ericallam
Created November 8, 2011 20:02
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ericallam/1348983 to your computer and use it in GitHub Desktop.
Save ericallam/1348983 to your computer and use it in GitHub Desktop.
Set certain regions of an Ace editor to readOnly
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
)
@KishanV
Copy link

KishanV commented Dec 31, 2015

What is "require('pilot/event')" file

@tripflex
Copy link

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

@macalinao
Copy link

thanks!

@touatily
Copy link

It doesn't work if the user selects many lines, then deletes. (cursor is not in the first or the last line)

@Aman-johri
Copy link

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