foldgutter.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. (function() {
  2. "use strict";
  3. CodeMirror.defineOption("foldGutter", false, function(cm, val, old) {
  4. if (old && old != CodeMirror.Init) {
  5. cm.clearGutter(cm.state.foldGutter.options.gutter);
  6. cm.state.foldGutter = null;
  7. cm.off("gutterClick", onGutterClick);
  8. cm.off("change", onChange);
  9. cm.off("viewportChange", onViewportChange);
  10. cm.off("fold", onFold);
  11. cm.off("unfold", onFold);
  12. }
  13. if (val) {
  14. cm.state.foldGutter = new State(parseOptions(val));
  15. updateInViewport(cm);
  16. cm.on("gutterClick", onGutterClick);
  17. cm.on("change", onChange);
  18. cm.on("viewportChange", onViewportChange);
  19. cm.on("fold", onFold);
  20. cm.on("unfold", onFold);
  21. }
  22. });
  23. var Pos = CodeMirror.Pos;
  24. function State(options) {
  25. this.options = options;
  26. this.from = this.to = 0;
  27. }
  28. function parseOptions(opts) {
  29. if (opts === true) opts = {};
  30. if (opts.gutter == null) opts.gutter = "CodeMirror-foldgutter";
  31. if (opts.indicatorOpen == null) opts.indicatorOpen = "CodeMirror-foldgutter-open";
  32. if (opts.indicatorFolded == null) opts.indicatorFolded = "CodeMirror-foldgutter-folded";
  33. return opts;
  34. }
  35. function isFolded(cm, line) {
  36. var marks = cm.findMarksAt(Pos(line));
  37. for (var i = 0; i < marks.length; ++i)
  38. if (marks[i].__isFold && marks[i].find().from.line == line) return true;
  39. }
  40. function marker(spec) {
  41. if (typeof spec == "string") {
  42. var elt = document.createElement("div");
  43. elt.className = spec;
  44. return elt;
  45. } else {
  46. return spec.cloneNode(true);
  47. }
  48. }
  49. function updateFoldInfo(cm, from, to) {
  50. var opts = cm.state.foldGutter.options, cur = from;
  51. cm.eachLine(from, to, function(line) {
  52. var mark = null;
  53. if (isFolded(cm, cur)) {
  54. mark = marker(opts.indicatorFolded);
  55. } else {
  56. var pos = Pos(cur, 0), func = opts.rangeFinder || cm.getHelper(pos, "fold");
  57. var range = func && func(cm, pos);
  58. if (range && range.from.line + 1 < range.to.line)
  59. mark = marker(opts.indicatorOpen);
  60. }
  61. cm.setGutterMarker(line, opts.gutter, mark);
  62. ++cur;
  63. });
  64. }
  65. function updateInViewport(cm) {
  66. var vp = cm.getViewport(), state = cm.state.foldGutter;
  67. if (!state) return;
  68. cm.operation(function() {
  69. updateFoldInfo(cm, vp.from, vp.to);
  70. });
  71. state.from = vp.from; state.to = vp.to;
  72. }
  73. function onGutterClick(cm, line, gutter) {
  74. var opts = cm.state.foldGutter.options;
  75. if (gutter != opts.gutter) return;
  76. cm.foldCode(Pos(line, 0), opts.rangeFinder);
  77. }
  78. function onChange(cm) {
  79. var state = cm.state.foldGutter;
  80. state.from = state.to = 0;
  81. clearTimeout(state.changeUpdate);
  82. state.changeUpdate = setTimeout(function() { updateInViewport(cm); }, 600);
  83. }
  84. function onViewportChange(cm) {
  85. var state = cm.state.foldGutter;
  86. clearTimeout(state.changeUpdate);
  87. state.changeUpdate = setTimeout(function() {
  88. var vp = cm.getViewport();
  89. if (state.from == state.to || vp.from - state.to > 20 || state.from - vp.to > 20) {
  90. updateInViewport(cm);
  91. } else {
  92. cm.operation(function() {
  93. if (vp.from < state.from) {
  94. updateFoldInfo(cm, vp.from, state.from);
  95. state.from = vp.from;
  96. }
  97. if (vp.to > state.to) {
  98. updateFoldInfo(cm, state.to, vp.to);
  99. state.to = vp.to;
  100. }
  101. });
  102. }
  103. }, 400);
  104. }
  105. function onFold(cm, from) {
  106. var state = cm.state.foldGutter, line = from.line;
  107. if (line >= state.from && line < state.to)
  108. updateFoldInfo(cm, line, line + 1);
  109. }
  110. })();