codemirror 3.0 format preloaded textarea code

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

codemirror 3.0 format preloaded textarea code

Sujesh Arukil

I am using CodeMirror to create an editor in an HTML5 based presentation. In it's simplest form, the html looks something like this.

 <section class="pattern">
      <textarea id='pattern-view' class='codemirror' data-mode='javascript'>
           var myModule = function(){
              //code goes here
           }
      </textarea>
 </section>

and down in a document ready I have the code

 $(function(){
     var tAreas = document.querySelectorAll('.codemirror');
        for (var i = 0; i < tAreas.length; i++) {
           CodeMirror.fromTextArea(tAreas[i], { theme: 'monokai',  mode: tAreas[i].dataset.mode });
        }
 });

This works as expected, the textarea is replaced with the editor. The trouble is the indentation is maintained and not properly formatted. It only highlights the code, does not re-format the contents.

Is there something I need to add to this? I did find code for formatting.js addon, which is no longer part of codemirror 3.0.

Is there someway to auto-format the code inside the textarea?

--
--
You are receiving this because you are currently a member of the CodeMirror Google group. To send something to list, use [hidden email], to unsubscribe, send a message to [hidden email].
---
You received this message because you are subscribed to the Google Groups "CodeMirror" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.
 
 
Reply | Threaded
Open this post in threaded view
|

Re: codemirror 3.0 format preloaded textarea code

Marijn Haverbeke
Formatting is indeed no longer part of CodeMirror. The old addon as
found in 3.1 might still work, though. If you just want automatic
indentation, that *is* part of the CodeMirror core (given that the
language mode supports it). Simply loop over the lines in the document
and call indentLine [1] for all of them, wrapping the loop in a call
to operation [2] if you care about efficiency.

[1]: http://codemirror.net/doc/manual.html#indentLine
[2]: http://codemirror.net/doc/manual.html#operation

Best,
Marijn

On Wed, May 29, 2013 at 5:15 AM, Sujesh Arukil <[hidden email]> wrote:

> I am using CodeMirror to create an editor in an HTML5 based presentation. In
> it's simplest form, the html looks something like this.
>
>  <section class="pattern">
>       <textarea id='pattern-view' class='codemirror' data-mode='javascript'>
>            var myModule = function(){
>               //code goes here
>            }
>       </textarea>
>  </section>
>
> and down in a document ready I have the code
>
>  $(function(){
>      var tAreas = document.querySelectorAll('.codemirror');
>         for (var i = 0; i < tAreas.length; i++) {
>            CodeMirror.fromTextArea(tAreas[i], { theme: 'monokai',  mode:
> tAreas[i].dataset.mode });
>         }
>  });
>
> This works as expected, the textarea is replaced with the editor. The
> trouble is the indentation is maintained and not properly formatted. It only
> highlights the code, does not re-format the contents.
>
> Is there something I need to add to this? I did find code for formatting.js
> addon, which is no longer part of codemirror 3.0.
>
> Is there someway to auto-format the code inside the textarea?
>
> --
> --
> You are receiving this because you are currently a member of the CodeMirror
> Google group. To send something to list, use [hidden email], to
> unsubscribe, send a message to [hidden email].
> ---
> You received this message because you are subscribed to the Google Groups
> "CodeMirror" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [hidden email].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

--
--
You are receiving this because you are currently a member of the CodeMirror Google group. To send something to list, use [hidden email], to unsubscribe, send a message to [hidden email].
---
You received this message because you are subscribed to the Google Groups "CodeMirror" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/groups/opt_out.