make some parts of the code readOnly

1,932 views
Skip to first unread message

alex

unread,
Mar 23, 2011, 6:21:40 AM3/23/11
to Ajax.org Cloud9 Editor (Ace)
I need a js code editor to fill some code templates. There is any
"easy way" to make some parts of the code in ACE editor readOnly.

Fabian Jakobs

unread,
Mar 24, 2011, 5:23:46 AM3/24/11
to ace-d...@googlegroups.com
Hi Alex,

I'm pretty sure it is possible but it's sure not easy. My first idea
to implement is is like this:

1. Attach floating anchors
<https://github.com/ajaxorg/ace/blob/master/lib/ace/anchor.js> at the
places in your code where the writable parts begin/end.
2. Attach a listener to the keydown event. If the cursor is in a
readonly section call setReadonly(true)

This is just a rough sketch but it should work

Best,
Fabian

On Wed, Mar 23, 2011 at 11:21 AM, alex <alex.ma...@gmail.com> wrote:
> I need a js code editor to fill some code templates. There is any
> "easy way" to make some parts of the code in ACE editor readOnly.
>

> --
> You received this message because you are subscribed to the Google Groups "Ajax.org Cloud9 Editor (Ace)" group.
> To post to this group, send email to ace-d...@googlegroups.com.
> To unsubscribe from this group, send email to ace-discuss...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/ace-discuss?hl=en.
>
>

alex

unread,
Apr 2, 2011, 12:10:55 PM4/2/11
to Ajax.org Cloud9 Editor (Ace)
Thanks for your help. I'll try your suggestion.

On Mar 24, 11:23 am, Fabian Jakobs <fabian.jak...@web.de> wrote:
> HiAlex,

Matthew Sudol

unread,
Jul 24, 2014, 4:05:29 PM7/24/14
to ace-d...@googlegroups.com, alex.ma...@gmail.com
Did you ever get this concept to work?  I'm trying to do something similar.

Thanks,

Saravanan Ganesh

unread,
Aug 6, 2014, 6:21:08 PM8/6/14
to ace-d...@googlegroups.com, alex.ma...@gmail.com

I got this working. I added a div on top of the editor with a high z index. 
This means underlying code cannot be reached using a click. Nevertheless, you can always arrow key into that area. 

Here's how it looked: 

Matthew Sudol

unread,
Sep 16, 2014, 8:59:50 AM9/16/14
to ace-d...@googlegroups.com, alex.ma...@gmail.com
I too got this to work, what I did is created an object in my app called myAnchors in which I keep track of references to all the anchors I've set for readOnly areas. Then as part of of the onChange event I check to see if the current position matches a position saved in myAnchors.. if so, I set the editor to readOnly mode (without need for a div over the top).  It works quite well... only now I have another issue...

That would be highlighting..  I'd really like to give "anchors" a class so that the readOnly areas of the document are highlighted as a way to indicate to the user that it IS a readOnly area.

Any idea how to give lines with anchors a class?

Harutyun Amirjanyan

unread,
Sep 18, 2014, 5:22:56 AM9/18/14
to ace-d...@googlegroups.com, alex.ma...@gmail.com
You can use 

var Range = require("ace/range").Range
r = new Range()
r.start = anchor1; 
r.end = anchor2;
range.id = session.addMarker(range, "readonly-highlight")
/*to remove call*/ // session.removeMarker(range.id)

.readonly-highlight{
    background-color: red;
    opacity: 0.2;
    position: absolute;
}


flex capacitor

unread,
Aug 9, 2015, 7:07:27 PM8/9/15
to Ajax.org Cloud9 Editor (Ace), alex.ma...@gmail.com
Do you have a code example for this? Does it work with paste and multiple line selection?
Reply all
Reply to author
Forward
0 new messages