foldgutter.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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("change", 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("change", 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 && marks[i].find().from.line == line) return marks[i];
  50. }
  51. function marker(spec) {
  52. if (typeof spec == "string") {
  53. var elt = document.createElement("div");
  54. elt.className = spec + " CodeMirror-guttermarker-subtle";
  55. return elt;
  56. } else {
  57. return spec.cloneNode(true);
  58. }
  59. }
  60. function updateFoldInfo(cm, from, to) {
  61. var opts = cm.state.foldGutter.options, cur = from;
  62. var minSize = cm.foldOption(opts, "minFoldSize");
  63. var func = cm.foldOption(opts, "rangeFinder");
  64. cm.eachLine(from, to, function(line) {
  65. var mark = null;
  66. if (isFolded(cm, cur)) {
  67. mark = marker(opts.indicatorFolded);
  68. } else {
  69. var pos = Pos(cur, 0);
  70. var range = func && func(cm, pos);
  71. if (range && range.to.line - range.from.line >= minSize)
  72. mark = marker(opts.indicatorOpen);
  73. }
  74. cm.setGutterMarker(line, opts.gutter, mark);
  75. ++cur;
  76. });
  77. }
  78. function updateInViewport(cm) {
  79. var vp = cm.getViewport(), state = cm.state.foldGutter;
  80. if (!state) return;
  81. cm.operation(function() {
  82. updateFoldInfo(cm, vp.from, vp.to);
  83. });
  84. state.from = vp.from; state.to = vp.to;
  85. }
  86. function onGutterClick(cm, line, gutter) {
  87. var state = cm.state.foldGutter;
  88. if (!state) return;
  89. var opts = state.options;
  90. if (gutter != opts.gutter) return;
  91. var folded = isFolded(cm, line);
  92. if (folded) folded.clear();
  93. else cm.foldCode(Pos(line, 0), opts.rangeFinder);
  94. }
  95. function onChange(cm) {
  96. var state = cm.state.foldGutter;
  97. if (!state) return;
  98. var opts = state.options;
  99. state.from = state.to = 0;
  100. clearTimeout(state.changeUpdate);
  101. state.changeUpdate = setTimeout(function() { updateInViewport(cm); }, opts.foldOnChangeTimeSpan || 600);
  102. }
  103. function onViewportChange(cm) {
  104. var state = cm.state.foldGutter;
  105. if (!state) return;
  106. var opts = state.options;
  107. clearTimeout(state.changeUpdate);
  108. state.changeUpdate = setTimeout(function() {
  109. var vp = cm.getViewport();
  110. if (state.from == state.to || vp.from - state.to > 20 || state.from - vp.to > 20) {
  111. updateInViewport(cm);
  112. } else {
  113. cm.operation(function() {
  114. if (vp.from < state.from) {
  115. updateFoldInfo(cm, vp.from, state.from);
  116. state.from = vp.from;
  117. }
  118. if (vp.to > state.to) {
  119. updateFoldInfo(cm, state.to, vp.to);
  120. state.to = vp.to;
  121. }
  122. });
  123. }
  124. }, opts.updateViewportTimeSpan || 400);
  125. }
  126. function onFold(cm, from) {
  127. var state = cm.state.foldGutter;
  128. if (!state) return;
  129. var line = from.line;
  130. if (line >= state.from && line < state.to)
  131. updateFoldInfo(cm, line, line + 1);
  132. }
  133. });