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.

164 lines
5.2KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/LICENSE
  3. (function(mod) {
  4. if (typeof exports == "object" && typeof module == "object") // CommonJS
  5. mod(require("../../lib/codemirror"), require("./foldcode"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror", "./foldcode"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. CodeMirror.defineOption("foldGutter", false, function(cm, val, old) {
  13. if (old && old != CodeMirror.Init) {
  14. cm.clearGutter(cm.state.foldGutter.options.gutter);
  15. cm.state.foldGutter = null;
  16. cm.off("gutterClick", onGutterClick);
  17. cm.off("changes", onChange);
  18. cm.off("viewportChange", onViewportChange);
  19. cm.off("fold", onFold);
  20. cm.off("unfold", onFold);
  21. cm.off("swapDoc", onChange);
  22. }
  23. if (val) {
  24. cm.state.foldGutter = new State(parseOptions(val));
  25. updateInViewport(cm);
  26. cm.on("gutterClick", onGutterClick);
  27. cm.on("changes", onChange);
  28. cm.on("viewportChange", onViewportChange);
  29. cm.on("fold", onFold);
  30. cm.on("unfold", onFold);
  31. cm.on("swapDoc", onChange);
  32. }
  33. });
  34. var Pos = CodeMirror.Pos;
  35. function State(options) {
  36. this.options = options;
  37. this.from = this.to = 0;
  38. }
  39. function parseOptions(opts) {
  40. if (opts === true) opts = {};
  41. if (opts.gutter == null) opts.gutter = "CodeMirror-foldgutter";
  42. if (opts.indicatorOpen == null) opts.indicatorOpen = "CodeMirror-foldgutter-open";
  43. if (opts.indicatorFolded == null) opts.indicatorFolded = "CodeMirror-foldgutter-folded";
  44. return opts;
  45. }
  46. function isFolded(cm, line) {
  47. var marks = cm.findMarks(Pos(line, 0), Pos(line + 1, 0));
  48. for (var i = 0; i < marks.length; ++i) {
  49. if (marks[i].__isFold) {
  50. var fromPos = marks[i].find(-1);
  51. if (fromPos && fromPos.line === line)
  52. return marks[i];
  53. }
  54. }
  55. }
  56. function marker(spec) {
  57. if (typeof spec == "string") {
  58. var elt = document.createElement("div");
  59. elt.className = spec + " CodeMirror-guttermarker-subtle";
  60. return elt;
  61. } else {
  62. return spec.cloneNode(true);
  63. }
  64. }
  65. function updateFoldInfo(cm, from, to) {
  66. var opts = cm.state.foldGutter.options, cur = from - 1;
  67. var minSize = cm.foldOption(opts, "minFoldSize");
  68. var func = cm.foldOption(opts, "rangeFinder");
  69. // we can reuse the built-in indicator element if its className matches the new state
  70. var clsFolded = typeof opts.indicatorFolded == "string" && classTest(opts.indicatorFolded);
  71. var clsOpen = typeof opts.indicatorOpen == "string" && classTest(opts.indicatorOpen);
  72. cm.eachLine(from, to, function(line) {
  73. ++cur;
  74. var mark = null;
  75. var old = line.gutterMarkers;
  76. if (old) old = old[opts.gutter];
  77. if (isFolded(cm, cur)) {
  78. if (clsFolded && old && clsFolded.test(old.className)) return;
  79. mark = marker(opts.indicatorFolded);
  80. } else {
  81. var pos = Pos(cur, 0);
  82. var range = func && func(cm, pos);
  83. if (range && range.to.line - range.from.line >= minSize) {
  84. if (clsOpen && old && clsOpen.test(old.className)) return;
  85. mark = marker(opts.indicatorOpen);
  86. }
  87. }
  88. if (!mark && !old) return;
  89. cm.setGutterMarker(line, opts.gutter, mark);
  90. });
  91. }
  92. // copied from CodeMirror/src/util/dom.js
  93. function classTest(cls) { return new RegExp("(^|\\s)" + cls + "(?:$|\\s)\\s*") }
  94. function updateInViewport(cm) {
  95. var vp = cm.getViewport(), state = cm.state.foldGutter;
  96. if (!state) return;
  97. cm.operation(function() {
  98. updateFoldInfo(cm, vp.from, vp.to);
  99. });
  100. state.from = vp.from; state.to = vp.to;
  101. }
  102. function onGutterClick(cm, line, gutter) {
  103. var state = cm.state.foldGutter;
  104. if (!state) return;
  105. var opts = state.options;
  106. if (gutter != opts.gutter) return;
  107. var folded = isFolded(cm, line);
  108. if (folded) folded.clear();
  109. else cm.foldCode(Pos(line, 0), opts);
  110. }
  111. function onChange(cm) {
  112. var state = cm.state.foldGutter;
  113. if (!state) return;
  114. var opts = state.options;
  115. state.from = state.to = 0;
  116. clearTimeout(state.changeUpdate);
  117. state.changeUpdate = setTimeout(function() { updateInViewport(cm); }, opts.foldOnChangeTimeSpan || 600);
  118. }
  119. function onViewportChange(cm) {
  120. var state = cm.state.foldGutter;
  121. if (!state) return;
  122. var opts = state.options;
  123. clearTimeout(state.changeUpdate);
  124. state.changeUpdate = setTimeout(function() {
  125. var vp = cm.getViewport();
  126. if (state.from == state.to || vp.from - state.to > 20 || state.from - vp.to > 20) {
  127. updateInViewport(cm);
  128. } else {
  129. cm.operation(function() {
  130. if (vp.from < state.from) {
  131. updateFoldInfo(cm, vp.from, state.from);
  132. state.from = vp.from;
  133. }
  134. if (vp.to > state.to) {
  135. updateFoldInfo(cm, state.to, vp.to);
  136. state.to = vp.to;
  137. }
  138. });
  139. }
  140. }, opts.updateViewportTimeSpan || 400);
  141. }
  142. function onFold(cm, from) {
  143. var state = cm.state.foldGutter;
  144. if (!state) return;
  145. var line = from.line;
  146. if (line >= state.from && line < state.to)
  147. updateFoldInfo(cm, line, line + 1);
  148. }
  149. });