foldgutter.js 4.1 KB

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