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.

83 lines
2.9 KiB

5 months ago
  1. <!doctype html>
  2. <title>CodeMirror: B-Tree visualization</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. <style>
  8. .lineblock { display: inline-block; margin: 1px; height: 5px; }
  9. .CodeMirror {border: 1px solid #aaa; height: 400px}
  10. </style>
  11. <div id=nav>
  12. <a href="https://codemirror.net/5"><h1>CodeMirror</h1><img id=logo src="../doc/logo.png"></a>
  13. <ul>
  14. <li><a href="../index.html">Home</a>
  15. <li><a href="../doc/manual.html">Manual</a>
  16. <li><a href="https://github.com/codemirror/codemirror5">Code</a>
  17. </ul>
  18. <ul>
  19. <li><a class=active href="#">B-Tree visualization</a>
  20. </ul>
  21. </div>
  22. <article>
  23. <h2>B-Tree visualization</h2>
  24. <form><textarea id="code" name="code">type here, see a summary of the document b-tree below</textarea></form>
  25. <div style="display: inline-block; height: 402px; overflow-y: auto" id="output"></div>
  26. <script id="me">
  27. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  28. lineNumbers: true,
  29. lineWrapping: true
  30. });
  31. var updateTimeout;
  32. editor.on("change", function(cm) {
  33. clearTimeout(updateTimeout);
  34. updateTimeout = setTimeout(updateVisual, 200);
  35. });
  36. updateVisual();
  37. function updateVisual() {
  38. var out = document.getElementById("output");
  39. out.innerHTML = "";
  40. function drawTree(out, node) {
  41. if (node.lines) {
  42. out.appendChild(document.createElement("div")).innerHTML =
  43. "<b>leaf</b>: " + node.lines.length + " lines, " + Math.round(node.height) + " px";
  44. var lines = out.appendChild(document.createElement("div"));
  45. lines.style.lineHeight = "6px"; lines.style.marginLeft = "10px";
  46. for (var i = 0; i < node.lines.length; ++i) {
  47. var line = node.lines[i], lineElt = lines.appendChild(document.createElement("div"));
  48. lineElt.className = "lineblock";
  49. var gray = Math.min(line.text.length * 3, 230), col = gray.toString(16);
  50. if (col.length == 1) col = "0" + col;
  51. lineElt.style.background = "#" + col + col + col;
  52. lineElt.style.width = Math.max(Math.round(line.height / 3), 1) + "px";
  53. }
  54. } else {
  55. out.appendChild(document.createElement("div")).innerHTML =
  56. "<b>node</b>: " + node.size + " lines, " + Math.round(node.height) + " px";
  57. var sub = out.appendChild(document.createElement("div"));
  58. sub.style.paddingLeft = "20px";
  59. for (var i = 0; i < node.children.length; ++i)
  60. drawTree(sub, node.children[i]);
  61. }
  62. }
  63. drawTree(out, editor.getDoc());
  64. }
  65. function fillEditor() {
  66. var sc = document.getElementById("me");
  67. var doc = (sc.textContent || sc.innerText || sc.innerHTML).replace(/^\s*/, "") + "\n";
  68. doc += doc; doc += doc; doc += doc; doc += doc; doc += doc; doc += doc;
  69. editor.setValue(doc);
  70. }
  71. </script>
  72. <p><button onclick="fillEditor()">Add a lot of content</button></p>
  73. </article>