getValue() with lines number

1,671 views
Skip to first unread message

midix

unread,
Sep 26, 2011, 5:26:35 AM9/26/11
to Ajax.org Cloud9 Editor (Ace)
Hi,
When I call editor.getSession().getValue(), it returns the text and
the lines number for each line. Is it possible to have the text
without numbers?

Thanks

Fabian Jakobs

unread,
Sep 26, 2011, 7:38:29 AM9/26/11
to ace-d...@googlegroups.com
Are you sure about that. I just typed
"env.editor.getSession().getValue()" in the console of
<http://ace.ajax.org/build/kitchen-sink.html> and I get the document
without any line numbers. How can I reproduce it?

Best,
Fabian

> --
> 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.
>
>

midix

unread,
Sep 26, 2011, 7:42:23 AM9/26/11
to Ajax.org Cloud9 Editor (Ace)
At the beginning I have

<pre id="editor">
test
</pre>

And when I call editor.getSession().getValue() to just have "test" I
have "1test"

Best
Michel

Fabian Jakobs

unread,
Sep 26, 2011, 8:01:42 AM9/26/11
to ace-d...@googlegroups.com
Strange. I just tried to reproduce it but for me it just works.

midix

unread,
Sep 26, 2011, 8:11:07 AM9/26/11
to Ajax.org Cloud9 Editor (Ace)
Hum strange.
I'm using ace 0.2.0 and you?

Fabian Jakobs

unread,
Sep 28, 2011, 6:03:32 AM9/28/11
to ace-d...@googlegroups.com
I was looking at the master branch on github.

fotkorr

unread,
Oct 11, 2011, 5:06:04 PM10/11/11
to Ajax.org Cloud9 Editor (Ace)
I have the same issue. (Also using source from build folder, from
master branch) If I have 5 lines of code, when saving the content it
removes all line breaks, replaces tabs with spaces and appends 12345
in front of the content...
I noticed that this happens when using:
editor.renderer.setShowGutter(true); if it's set to false, it doesn't
include line numbers (which is obvious)...

Luca

unread,
Oct 18, 2011, 9:35:16 AM10/18/11
to ace-d...@googlegroups.com
Same problem here, when calling the getValue() all the newlines and tabs are removed and the line numbers are added to the text. Tested with github branch and 0.2 version (both on Safari and Chrome).
Any hints on how to solve this? Thanks!

Luca

unread,
Oct 18, 2011, 9:54:00 AM10/18/11
to ace-d...@googlegroups.com
Ok, I found my issue - I post it here in case it can solve the problem for others.

My (wrong) way of accessing the editor:

[...]
 var editor = ace.edit("editor");
 editor.setTheme("ace/theme/twilight");
[...some functions later...]
 var editor = ace.edit("editor");
$("#webeditor-textarea").val( editor.getSession().getValue());

I took this snippet from a tutorial on Google, however I found it not working for me (problem reported in the thread).
Using a shared object solved the problem. Sort to say:

var editor = null;

[...init...]
 editor = ace.edit("editor");
 editor.setTheme("ace/theme/twilight");
[...some functions later...]
$("#webeditor-textarea").val( editor.getSession().getValue());


Derek Davenport

unread,
Nov 22, 2011, 12:29:48 AM11/22/11
to ace-d...@googlegroups.com
I'm having the exact same problem you describe and I am using:

var editor = null;  
editor = ace.edit("editor");
editor.setTheme("ace/theme/textmate");
$("#test_textarea").val( editor.getSession().getValue());

 I am new to javascript, so I could be missing something simple you mentioned in the previous entry, but did not understand.  This is killing me, because I am so close, but the getValue is just NOT doing what I want it to do!

Please help!

seb ruiz

unread,
Dec 15, 2011, 12:47:02 PM12/15/11
to ace-d...@googlegroups.com
I had this problem and here is my solution:

window.onload = function() {
    editor = ace.edit("editor"); //CAREFUL GLOBAL VAR.      
};

Don't add the 'var' bit in front of editor. This makes the variable GLOBAL and so it will be accessible from all javascript functions. 

Somewhere else you can then have:

function getacecontent() {
    var newcontents = editor.getSession().getValue();
}
when someone clicks a button or something, and this will get content without the line numbers. In the function 'getacecontent' please note that I have not re-defined 'editor', as I am accessing the global variable that was initialised on page load.

I was surfing the net for ages to try and find a solution so I hope this helps someone.

Chris Hall

unread,
Dec 16, 2011, 1:34:06 PM12/16/11
to ace-d...@googlegroups.com
Defining global variables like that is generally considered bad coding practice.  It would be better if you defined your getter functions in the same scope as your editor variable definition and made the variable local.

For example:
(Please note, I usually use jQuery for my projects, so my native is a little rusty)

window.onload = function() {
    var editor = ace.edit("editor");

    function saveTheCode() {
        var code = editor.getSession().getValue();

        //save that code!
    }

    //Lets say you have a "save" button
    document.getElementById("save").addEventListener("click", saveTheCode);
}


Cheers,
~Chris

Polymath

unread,
Feb 9, 2012, 1:15:15 PM2/9/12
to ace-d...@googlegroups.com
To fix this, don't call ace.edit twice. Find a way to keep the original editor variable intact to use later when saving the contents.

To save the contents with linebreaks, do the following.

var lines = editor.getSession().getDocument().getAllLines();
var txt = '';var x=0;
for(x=0;x<lines.length;x++){
        if(txt!=''){txt+='\\n';}
        txt+=lines[x];
}
document.getElementById('mytextarea').value = txt;

Then in the server-side code (asp.net, PHP, ruby, etc), replace '\n' with line breaks ( chr[10] + chr[13] )

Reply all
Reply to author
Forward
0 new messages