You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
5.7 KiB

2 months ago
  1. <!doctype html>
  2. <head>
  3. <title>CodeMirror: Code Folding Demo</title>
  4. <meta charset="utf-8"/>
  5. <link rel=stylesheet href="../doc/docs.css">
  6. <style>
  7. .some-css {
  8. color: red;
  9. line-height: 2;
  10. }
  11. </style>
  12. <link rel="stylesheet" href="../lib/codemirror.css">
  13. <link rel="stylesheet" href="../addon/fold/foldgutter.css" />
  14. <script src="../lib/codemirror.js"></script>
  15. <script src="../addon/fold/foldcode.js"></script>
  16. <script src="../addon/fold/foldgutter.js"></script>
  17. <script src="../addon/fold/brace-fold.js"></script>
  18. <script src="../addon/fold/xml-fold.js"></script>
  19. <script src="../addon/fold/indent-fold.js"></script>
  20. <script src="../addon/fold/markdown-fold.js"></script>
  21. <script src="../addon/fold/comment-fold.js"></script>
  22. <script src="../mode/javascript/javascript.js"></script>
  23. <script src="../mode/xml/xml.js"></script>
  24. <script src="../mode/css/css.js"></script>
  25. <script src="../mode/htmlmixed/htmlmixed.js"></script>
  26. <script src="../mode/python/python.js"></script>
  27. <script src="../mode/markdown/markdown.js"></script>
  28. <style>
  29. .CodeMirror {border-top: 1px solid black; border-bottom: 1px solid black;}
  30. </style>
  31. </head>
  32. <body>
  33. <div id=nav>
  34. <a href="https://codemirror.net/5"><h1>CodeMirror</h1><img id=logo src="../doc/logo.png"></a>
  35. <ul>
  36. <li><a href="../index.html">Home</a>
  37. <li><a href="../doc/manual.html">Manual</a>
  38. <li><a href="https://github.com/codemirror/codemirror5">Code</a>
  39. </ul>
  40. <ul>
  41. <li><a class=active href="#">Code Folding</a>
  42. </ul>
  43. </div>
  44. <article>
  45. <h2>Code Folding Demo</h2>
  46. <form>
  47. <div style="max-width: 50em; margin-bottom: 1em">JavaScript:<br>
  48. <textarea id="code" name="code"></textarea></div>
  49. <div style="max-width: 50em; margin-bottom: 1em">HTML:<br>
  50. <textarea id="code-html" name="code-html"></textarea></div>
  51. <div style="max-width: 50em; margin-bottom: 1em">JSON with custom widget:<br>
  52. <textarea id="code-json" name="code-json">
  53. {
  54. "menu": {
  55. "id": "file",
  56. "value": "File",
  57. "popup": {
  58. "menuitem": [
  59. {"value": "New", "onclick": "CreateNewDoc()"},
  60. {"value": "Open", "onclick": "OpenDoc()"},
  61. {"value": "Close", "onclick": "CloseDoc()"}
  62. ]
  63. }
  64. }
  65. }
  66. </textarea></div>
  67. <div style="max-width: 50em">Python:<br>
  68. <textarea id="code-python" name="code">
  69. def foo():
  70. do_some_stuff()
  71. here
  72. return None
  73. class Bar:
  74. __init__(self):
  75. if True:
  76. print("True")
  77. else:
  78. print("False")
  79. this_code_makes_no_sense():
  80. pass
  81. # A comment</textarea></div>
  82. <div style="max-width: 50em">Markdown:<br>
  83. <textarea id="code-markdown" name="code"></textarea></div>
  84. </form>
  85. <script id="script">
  86. /*
  87. * Demonstration of code folding
  88. */
  89. window.onload = function() {
  90. var te = document.getElementById("code");
  91. var sc = document.getElementById("script");
  92. te.value = (sc.textContent || sc.innerText || sc.innerHTML).replace(/^\s*/, "");
  93. sc.innerHTML = "";
  94. var te_html = document.getElementById("code-html");
  95. te_html.value = document.documentElement.innerHTML;
  96. var te_python = document.getElementById("code-python");
  97. var te_markdown = document.getElementById("code-markdown");
  98. te_markdown.value = "# Foo\n## Bar\n\nblah blah\n\n## Baz\n\nblah blah\n\n# Quux\n\nblah blah\n"
  99. var te_json = document.getElementById("code-json");
  100. window.editor = CodeMirror.fromTextArea(te, {
  101. mode: "javascript",
  102. lineNumbers: true,
  103. lineWrapping: true,
  104. extraKeys: {"Ctrl-Q": function(cm){ cm.foldCode(cm.getCursor()); }},
  105. foldGutter: true,
  106. gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
  107. });
  108. editor.foldCode(CodeMirror.Pos(13, 0));
  109. window.editor_json = CodeMirror.fromTextArea(te_json, {
  110. mode: {name: "javascript", json: true},
  111. lineNumbers: true,
  112. lineWrapping: true,
  113. extraKeys: {"Ctrl-Q": function(cm){ cm.foldCode(cm.getCursor()); }},
  114. foldGutter: true,
  115. gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
  116. foldOptions: {
  117. widget: (from, to) => {
  118. var count = undefined;
  119. // Get open / close token
  120. var startToken = '{', endToken = '}';
  121. var prevLine = window.editor_json.getLine(from.line);
  122. if (prevLine.lastIndexOf('[') > prevLine.lastIndexOf('{')) {
  123. startToken = '[', endToken = ']';
  124. }
  125. // Get json content
  126. var internal = window.editor_json.getRange(from, to);
  127. var toParse = startToken + internal + endToken;
  128. // Get key count
  129. try {
  130. var parsed = JSON.parse(toParse);
  131. count = Object.keys(parsed).length;
  132. } catch(e) { }
  133. return count ? `\u21A4${count}\u21A6` : '\u2194';
  134. }
  135. }
  136. });
  137. editor_json.foldCode(CodeMirror.Pos(5, 0));
  138. window.editor_html = CodeMirror.fromTextArea(te_html, {
  139. mode: "text/html",
  140. lineNumbers: true,
  141. lineWrapping: true,
  142. extraKeys: {"Ctrl-Q": function(cm){ cm.foldCode(cm.getCursor()); }},
  143. foldGutter: true,
  144. gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
  145. });
  146. editor_html.foldCode(CodeMirror.Pos(0, 0));
  147. editor_html.foldCode(CodeMirror.Pos(34, 0));
  148. window.editor_python = CodeMirror.fromTextArea(te_python, {
  149. mode: "python",
  150. lineNumbers: true,
  151. extraKeys: {"Ctrl-Q": function(cm){ cm.foldCode(cm.getCursor()); }},
  152. foldGutter: true,
  153. gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
  154. });
  155. window.editor_markdown = CodeMirror.fromTextArea(te_markdown, {
  156. mode: "markdown",
  157. lineNumbers: true,
  158. lineWrapping: true,
  159. extraKeys: {"Ctrl-Q": function(cm){ cm.foldCode(cm.getCursor()); }},
  160. foldGutter: true,
  161. gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
  162. });
  163. };
  164. </script>
  165. </article>
  166. </body>