1
0

foldgutter.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 opts = cm.state.foldGutter.options;
  88. if (gutter != opts.gutter) return;
  89. cm.foldCode(Pos(line, 0), opts.rangeFinder);
  90. }
  91. function onChange(cm) {
  92. var state = cm.state.foldGutter, opts = cm.state.foldGutter.options;
  93. state.from = state.to = 0;
  94. clearTimeout(state.changeUpdate);
  95. state.changeUpdate = setTimeout(function() { updateInViewport(cm); }, opts.foldOnChangeTimeSpan || 600);
  96. }
  97. function onViewportChange(cm) {
  98. var state = cm.state.foldGutter, opts = cm.state.foldGutter.options;
  99. clearTimeout(state.changeUpdate);
  100. state.changeUpdate = setTimeout(function() {
  101. var vp = cm.getViewport();
  102. if (state.from == state.to || vp.from - state.to > 20 || state.from - vp.to > 20) {
  103. updateInViewport(cm);
  104. } else {
  105. cm.operation(function() {
  106. if (vp.from < state.from) {
  107. updateFoldInfo(cm, vp.from, state.from);
  108. state.from = vp.from;
  109. }
  110. if (vp.to > state.to) {
  111. updateFoldInfo(cm, state.to, vp.to);
  112. state.to = vp.to;
  113. }
  114. });
  115. }
  116. }, opts.updateViewportTimeSpan || 400);
  117. }
  118. function onFold(cm, from) {
  119. var state = cm.state.foldGutter, line = from.line;
  120. if (line >= state.from && line < state.to)
  121. updateFoldInfo(cm, line, line + 1);
  122. }
  123. });