buffers.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <!doctype html>
  2. <title>CodeMirror: Multiple Buffer & Split View 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/javascript/javascript.js"></script>
  8. <script src="../mode/css/css.js"></script>
  9. <style id=style>
  10. .CodeMirror {border: 1px solid black; height: 250px;}
  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="#">Multiple Buffer & Split View</a>
  21. </ul>
  22. </div>
  23. <article>
  24. <h2>Multiple Buffer & Split View Demo</h2>
  25. <div id=code_top></div>
  26. <div>
  27. Select buffer: <select id=buffers_top></select>
  28. &nbsp; &nbsp; <button onclick="newBuf('top')">New buffer</button>
  29. </div>
  30. <div id=code_bot></div>
  31. <div>
  32. Select buffer: <select id=buffers_bot></select>
  33. &nbsp; &nbsp; <button onclick="newBuf('bot')">New buffer</button>
  34. </div>
  35. <script id=script>
  36. var sel_top = document.getElementById("buffers_top");
  37. CodeMirror.on(sel_top, "change", function() {
  38. selectBuffer(ed_top, sel_top.options[sel_top.selectedIndex].value);
  39. });
  40. var sel_bot = document.getElementById("buffers_bot");
  41. CodeMirror.on(sel_bot, "change", function() {
  42. selectBuffer(ed_bot, sel_bot.options[sel_bot.selectedIndex].value);
  43. });
  44. var buffers = {};
  45. function openBuffer(name, text, mode) {
  46. buffers[name] = CodeMirror.Doc(text, mode);
  47. var opt = document.createElement("option");
  48. opt.appendChild(document.createTextNode(name));
  49. sel_top.appendChild(opt);
  50. sel_bot.appendChild(opt.cloneNode(true));
  51. }
  52. function newBuf(where) {
  53. var name = prompt("Name for the buffer", "*scratch*");
  54. if (name == null) return;
  55. if (buffers.hasOwnProperty(name)) {
  56. alert("There's already a buffer by that name.");
  57. return;
  58. }
  59. openBuffer(name, "", "javascript");
  60. selectBuffer(where == "top" ? ed_top : ed_bot, name);
  61. var sel = where == "top" ? sel_top : sel_bot;
  62. sel.value = name;
  63. }
  64. function selectBuffer(editor, name) {
  65. var buf = buffers[name];
  66. if (buf.getEditor()) buf = buf.linkedDoc({sharedHist: true});
  67. var old = editor.swapDoc(buf);
  68. var linked = old.iterLinkedDocs(function(doc) {linked = doc;});
  69. if (linked) {
  70. // Make sure the document in buffers is the one the other view is looking at
  71. for (var name in buffers) if (buffers[name] == old) buffers[name] = linked;
  72. old.unlinkDoc(linked);
  73. }
  74. editor.focus();
  75. }
  76. function nodeContent(id) {
  77. var node = document.getElementById(id), val = node.textContent || node.innerText;
  78. val = val.slice(val.match(/^\s*/)[0].length, val.length - val.match(/\s*$/)[0].length) + "\n";
  79. return val;
  80. }
  81. openBuffer("js", nodeContent("script"), "javascript");
  82. openBuffer("css", nodeContent("style"), "css");
  83. var ed_top = CodeMirror(document.getElementById("code_top"), {lineNumbers: true});
  84. selectBuffer(ed_top, "js");
  85. var ed_bot = CodeMirror(document.getElementById("code_bot"), {lineNumbers: true});
  86. selectBuffer(ed_bot, "js");
  87. </script>
  88. <p>Demonstration of
  89. using <a href="../doc/manual.html#linkedDoc">linked documents</a>
  90. to provide a split view on a document, and
  91. using <a href="../doc/manual.html#swapDoc"><code>swapDoc</code></a>
  92. to use a single editor to display multiple documents.</p>
  93. </article>