indentwrap.html 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <!doctype html>
  2. <title>CodeMirror: Indented wrapped line demo</title>
  3. <meta charset="utf-8"/>
  4. <link rel=stylesheet href="../doc/docs.css">
  5. <link rel="stylesheet" href="../lib/codemirror.css">
  6. <script src="../lib/codemirror.js"></script>
  7. <script src="../mode/xml/xml.js"></script>
  8. <style type="text/css">
  9. .CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}
  10. .CodeMirror pre > * { text-indent: 0px; }
  11. </style>
  12. <div id=nav>
  13. <a href="https://codemirror.net"><h1>CodeMirror</h1><img id=logo src="../doc/logo.png"></a>
  14. <ul>
  15. <li><a href="../index.html">Home</a>
  16. <li><a href="../doc/manual.html">Manual</a>
  17. <li><a href="https://github.com/codemirror/codemirror">Code</a>
  18. </ul>
  19. <ul>
  20. <li><a class=active href="#">Indented wrapped line</a>
  21. </ul>
  22. </div>
  23. <article>
  24. <h2>Indented wrapped line demo</h2>
  25. <form><textarea id="code" name="code">
  26. <!doctype html>
  27. <body>
  28. <h2 id="overview">Overview</h2>
  29. <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>
  30. <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>
  31. </body>
  32. </textarea></form>
  33. <p>This page uses a hack on top of the <code>"renderLine"</code>
  34. event to make wrapped text line up with the base indentation of
  35. the line.</p>
  36. <script>
  37. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  38. lineNumbers: true,
  39. lineWrapping: true,
  40. mode: "text/html"
  41. });
  42. var charWidth = editor.defaultCharWidth(), basePadding = 4;
  43. editor.on("renderLine", function(cm, line, elt) {
  44. var off = CodeMirror.countColumn(line.text, null, cm.getOption("tabSize")) * charWidth;
  45. elt.style.textIndent = "-" + off + "px";
  46. elt.style.paddingLeft = (basePadding + off) + "px";
  47. });
  48. editor.refresh();
  49. </script>
  50. </article>