2
0

indentwrap.html 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CodeMirror: Indented wrapped line demo</title>
  6. <link rel="stylesheet" href="../lib/codemirror.css">
  7. <script src="../lib/codemirror.js"></script>
  8. <script src="../mode/xml/xml.js"></script>
  9. <link rel="stylesheet" href="../doc/docs.css">
  10. <style type="text/css">
  11. .CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}
  12. </style>
  13. </head>
  14. <body>
  15. <h1>CodeMirror: Indented wrapped line demo</h1>
  16. <form><textarea id="code" name="code">
  17. <!doctype html>
  18. <body>
  19. <h2 id="overview">Overview</h2>
  20. <p>CodeMirror is a code-editor component that can be embedded in Web pages. The core library provides <em>only</em> the editor component, no accompanying buttons, auto-completion, or other IDE functionality. It does provide a rich API on top of which such functionality can be straightforwardly implemented. See the <a href="#addons">add-ons</a> included in the distribution, and the <a href="https://github.com/jagthedrummer/codemirror-ui">CodeMirror UI</a> project, for reusable implementations of extra features.</p>
  21. <p>CodeMirror works with language-specific modes. Modes are JavaScript programs that help color (and optionally indent) text written in a given language. The distribution comes with a number of modes (see the <a href="../mode/"><code>mode/</code></a> directory), and it isn't hard to <a href="#modeapi">write new ones</a> for other languages.</p>
  22. </body>
  23. </textarea></form>
  24. <p>This page uses a hack on top of the <code>"renderLine"</code>
  25. event to make wrapped text line up with the base indentation of
  26. the line.</p>
  27. <script>
  28. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  29. lineNumbers: true,
  30. lineWrapping: true,
  31. mode: "text/html"
  32. });
  33. var charWidth = editor.defaultCharWidth(), basePadding = 4;
  34. editor.on("renderLine", function(cm, line, elt) {
  35. var off = CodeMirror.countColumn(line.text, null, cm.getOption("tabSize")) * charWidth;
  36. elt.style.textIndent = "-" + off + "px";
  37. elt.style.paddingLeft = (basePadding + off) + "px";
  38. });
  39. editor.refresh();
  40. </script>
  41. </body>
  42. </html>