1
0

foldgutter.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://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", updateInViewport);
  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", updateInViewport);
  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.findMarksAt(Pos(line));
  48. for (var i = 0; i < marks.length; ++i)
  49. if (marks[i].__isFold && marks[i].find().from.line == line) return true;
  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. cm.foldCode(Pos(line, 0), opts.rangeFinder);
  92. }
  93. function onChange(cm) {
  94. var state = cm.state.foldGutter;
  95. if (!state) return;
  96. var opts = state.options;
  97. state.from = state.to = 0;
  98. clearTimeout(state.changeUpdate);
  99. state.changeUpdate = setTimeout(function() { updateInViewport(cm); }, opts.foldOnChangeTimeSpan || 600);
  100. }
  101. function onViewportChange(cm) {
  102. var state = cm.state.foldGutter;
  103. if (!state) return;
  104. var opts = state.options;
  105. clearTimeout(state.changeUpdate);
  106. state.changeUpdate = setTimeout(function() {
  107. var vp = cm.getViewport();
  108. if (state.from == state.to || vp.from - state.to > 20 || state.from - vp.to > 20) {
  109. updateInViewport(cm);
  110. } else {
  111. cm.operation(function() {
  112. if (vp.from < state.from) {
  113. updateFoldInfo(cm, vp.from, state.from);
  114. state.from = vp.from;
  115. }
  116. if (vp.to > state.to) {
  117. updateFoldInfo(cm, state.to, vp.to);
  118. state.to = vp.to;
  119. }
  120. });
  121. }
  122. }, opts.updateViewportTimeSpan || 400);
  123. }
  124. function onFold(cm, from) {
  125. var state = cm.state.foldGutter;
  126. if (!state) return;
  127. var line = from.line;
  128. if (line >= state.from && line < state.to)
  129. updateFoldInfo(cm, line, line + 1);
  130. }
  131. });