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.

123 lines
4.1 KiB

5 months ago
  1. <!doctype html>
  2. <title>CodeMirror: merge 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. <link rel=stylesheet href="../addon/merge/merge.css">
  7. <script src="../lib/codemirror.js"></script>
  8. <script src="../mode/xml/xml.js"></script>
  9. <script src="../mode/css/css.js"></script>
  10. <script src="../mode/javascript/javascript.js"></script>
  11. <script src="../mode/htmlmixed/htmlmixed.js"></script>
  12. <script src="https://cdnjs.cloudflare.com/ajax/libs/diff_match_patch/20121119/diff_match_patch.js"></script>
  13. <script src="../addon/merge/merge.js"></script>
  14. <style>
  15. .CodeMirror { line-height: 1.2; }
  16. @media screen and (min-width: 1300px) {
  17. article { max-width: 1000px; }
  18. #nav { border-right: 499px solid transparent; }
  19. }
  20. span.clicky {
  21. cursor: pointer;
  22. background: #d70;
  23. color: white;
  24. padding: 0 3px;
  25. border-radius: 3px;
  26. }
  27. </style>
  28. <div id=nav>
  29. <a href="https://codemirror.net/5"><h1>CodeMirror</h1><img id=logo src="../doc/logo.png"></a>
  30. <ul>
  31. <li><a href="../index.html">Home</a>
  32. <li><a href="../doc/manual.html">Manual</a>
  33. <li><a href="https://github.com/codemirror/codemirror5">Code</a>
  34. </ul>
  35. <ul>
  36. <li><a class=active href="#">merge view</a>
  37. </ul>
  38. </div>
  39. <article>
  40. <h2>merge view demo</h2>
  41. <div id=view></div>
  42. <p>The <a href="../doc/manual.html#addon_merge"><code>merge</code></a>
  43. addon provides an interface for displaying and merging diffs,
  44. either <span class=clicky onclick="panes = 2; initUI()">two-way</span>
  45. or <span class=clicky onclick="panes = 3; initUI()">three-way</span>.
  46. The left (or center) pane is editable, and the differences with the
  47. other pane(s) are <span class=clicky
  48. onclick="toggleDifferences()">optionally</span> shown live as you edit
  49. it. In the two-way configuration, there are also options to pad changed
  50. sections to <span class=clicky onclick="connect = connect ? null :
  51. 'align'; initUI()">align</span> them, and to <span class=clicky
  52. onclick="collapse = !collapse; initUI()">collapse</span> unchanged
  53. stretches of text.</p>
  54. <p>This addon depends on
  55. the <a href="https://code.google.com/p/google-diff-match-patch/">google-diff-match-patch</a>
  56. library to compute the diffs.</p>
  57. <script>
  58. var value, orig1, orig2, dv, panes = 2, highlight = true, connect = "align", collapse = false;
  59. function initUI() {
  60. if (value == null) return;
  61. var target = document.getElementById("view");
  62. target.innerHTML = "";
  63. dv = CodeMirror.MergeView(target, {
  64. value: value,
  65. origLeft: panes == 3 ? orig1 : null,
  66. orig: orig2,
  67. lineNumbers: true,
  68. mode: "text/html",
  69. highlightDifferences: highlight,
  70. connect: connect,
  71. collapseIdentical: collapse
  72. });
  73. }
  74. function toggleDifferences() {
  75. dv.setShowDifferences(highlight = !highlight);
  76. }
  77. window.onload = function() {
  78. value = document.documentElement.innerHTML;
  79. orig1 = "<!doctype html>\n\n" + value.replace(/\.\.\//g, "codemirror/").replace("yellow", "orange");
  80. orig2 = value.replace(/\u003cscript/g, "\u003cscript type=text/javascript ")
  81. .replace("white", "purple;\n font: comic sans;\n text-decoration: underline;\n height: 15em");
  82. initUI();
  83. let d = document.createElement("div"); d.style.cssText = "width: 50px; margin: 7px; height: 14px"; dv.editor().addLineWidget(57, d)
  84. };
  85. function mergeViewHeight(mergeView) {
  86. function editorHeight(editor) {
  87. if (!editor) return 0;
  88. return editor.getScrollInfo().height;
  89. }
  90. return Math.max(editorHeight(mergeView.leftOriginal()),
  91. editorHeight(mergeView.editor()),
  92. editorHeight(mergeView.rightOriginal()));
  93. }
  94. function resize(mergeView) {
  95. var height = mergeViewHeight(mergeView);
  96. for(;;) {
  97. if (mergeView.leftOriginal())
  98. mergeView.leftOriginal().setSize(null, height);
  99. mergeView.editor().setSize(null, height);
  100. if (mergeView.rightOriginal())
  101. mergeView.rightOriginal().setSize(null, height);
  102. var newHeight = mergeViewHeight(mergeView);
  103. if (newHeight >= height) break;
  104. else height = newHeight;
  105. }
  106. mergeView.wrap.style.height = height + "px";
  107. }
  108. </script>
  109. </article>