1
0

foldgutter.js 5.4 KB

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