btree.html 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>CodeMirror: B-Tree visualization</title>
  6. <link rel="stylesheet" href="../lib/codemirror.css">
  7. <script src="../lib/codemirror.js"></script>
  8. <link rel="stylesheet" href="../doc/docs.css">
  9. <style type="text/css">
  10. .lineblock { display: inline-block; margin: 1px; height: 5px; }
  11. .CodeMirror {border: 1px solid #aaa; height: 400px}
  12. </style>
  13. </head>
  14. <body>
  15. <h1>CodeMirror: B-Tree visualization</h1>
  16. <p>Shows a visual representation of the b-tree that CodeMirror
  17. uses to store its document. See
  18. the <a href="http://marijnhaverbeke.nl/blog/codemirror-line-tree.html">corresponding
  19. blog post</a> for a description of this format. The gray blocks
  20. under each leaf show the lines it holds (with their width
  21. representing the line height). Add and remove content to see how
  22. the nodes are split and merged to keep the tree balanced.</p>
  23. <div style="position: relative">
  24. <div style="width: 60%; display: inline-block; vertical-align: top">
  25. <form><textarea id="code" name="code">type here, see a summary of the document b-tree to the right</textarea></form>
  26. </div>
  27. <div style="display: inline-block; height: 402px; overflow-y: auto" id="output"></div>
  28. </div>
  29. <script id="me">
  30. var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
  31. lineNumbers: true,
  32. lineWrapping: true
  33. });
  34. var updateTimeout;
  35. editor.on("change", function(cm) {
  36. clearTimeout(updateTimeout);
  37. updateTimeout = setTimeout(updateVisual, 200);
  38. });
  39. updateVisual();
  40. function updateVisual() {
  41. var out = document.getElementById("output");
  42. out.innerHTML = "";
  43. function drawTree(out, node) {
  44. if (node.lines) {
  45. out.appendChild(document.createElement("div")).innerHTML =
  46. "<b>leaf</b>: " + node.lines.length + " lines, " + Math.round(node.height) + " px";
  47. var lines = out.appendChild(document.createElement("div"));
  48. lines.style.lineHeight = "6px"; lines.style.marginLeft = "10px";
  49. for (var i = 0; i < node.lines.length; ++i) {
  50. var line = node.lines[i], lineElt = lines.appendChild(document.createElement("div"));
  51. lineElt.className = "lineblock";
  52. var gray = Math.min(line.text.length * 3, 230), col = gray.toString(16);
  53. if (col.length == 1) col = "0" + col;
  54. lineElt.style.background = "#" + col + col + col;
  55. console.log(line.height, line);
  56. lineElt.style.width = Math.max(Math.round(line.height / 3), 1) + "px";
  57. }
  58. } else {
  59. out.appendChild(document.createElement("div")).innerHTML =
  60. "<b>node</b>: " + node.size + " lines, " + Math.round(node.height) + " px";
  61. var sub = out.appendChild(document.createElement("div"));
  62. sub.style.paddingLeft = "20px";
  63. for (var i = 0; i < node.children.length; ++i)
  64. drawTree(sub, node.children[i]);
  65. }
  66. }
  67. drawTree(out, editor.getDoc());
  68. }
  69. function fillEditor() {
  70. var sc = document.getElementById("me");
  71. var doc = (sc.textContent || sc.innerText || sc.innerHTML).replace(/^\s*/, "") + "\n";
  72. doc += doc; doc += doc; doc += doc; doc += doc; doc += doc; doc += doc;
  73. editor.setValue(doc);
  74. }
  75. </script>
  76. <p><button onclick="fillEditor()">Add a lot of content</button></p>
  77. </body>
  78. </html>