Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CodeMirror auto Line-Break

I'm trying to make it so when you go over a certain amount of text and it reaches the max-width of the codemirror it will bring you down to a new line instead of just making a scrollbar and making you go out further.

Checkout the example! http://codeeplus.net/test.php

CSS:

<style>
  .CodeMirror { height: 400px; width: 500px; border: 1px solid #ddd; }
  .CodeMirror-scroll { max-height: 400px; width:500px; }
  .CodeMirror pre { display:inline-block; padding-left: 7px; line-height: 1.25; }
  #drawing { border: 1px solid #555555; float:left; display:inline-block; width:480px; height: 380px; }
</style>

Textarea:

<textarea align="left" style="display:inline-block;" id=demotext name="textarea">

JS:

  <script>
    var editor = CodeMirror.fromTextArea(document.getElementById("demotext"), {
      lineNumbers: true,
      lineWrapping: true,
      mode: "text/html",
      matchBrackets: true
    });
  </script>
like image 321
user2812028 Avatar asked Nov 03 '13 19:11

user2812028


People also ask

How do I use CodeMirror?

Once you import CodeMirror via <script> tag, you get a CodeMirror global that you can use to create a new CodeMirror instance. Just point it at a <div> and CodeMirror does the rest. CodeMirror(document. querySelector('#my-div'), { lineNumbers: true, tabSize: 2, value: 'console.

How do I reset CodeMirror editor?

If you don't want to kill the CodeMirror instance, just change the entire document holding the text, data on markers etc. This can be done by calling cm. swapDoc(doc: CodeMirror. Doc) .

What is CodeMirror CSS?

CodeMirror is a code editor component for the web. It can be used in websites to implement a text input field with support for many editing features, and has a rich programming interface to allow further extension.


2 Answers

In CSS3 they added word-wrap property. Use word-wrap: break-word;

Note that it will create new lines where there is a whitespace. If your string contains no space, it will not work as intended and you'd need to do it programmatically

like image 122
Alexandre TryHard Leblanc Avatar answered Sep 18 '22 23:09

Alexandre TryHard Leblanc


Setting the

lineWrapping: true

in the CodeMirror options and

.CodeMirror-wrap pre { word-break: break-word; }

in the CSS you use to override your editor's CSS will do the job. In the case that the word is longer than the width of the editor it will break at the last character that fits. Something like this:

CodeMirror editor breaking words on width lines

You can check out the example here, just keep in mind I built it for other purposes (and in Angular) but it also demonstrates your use-case.

like image 21
Marko Letic Avatar answered Sep 20 '22 23:09

Marko Letic