1
0

foldgutter.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. cm.eachLine(from, to, function(line) {
  63. var mark = null;
  64. if (isFolded(cm, cur)) {
  65. mark = marker(opts.indicatorFolded);
  66. } else {
  67. var pos = Pos(cur, 0), func = opts.rangeFinder || CodeMirror.fold.auto;
  68. var range = func && func(cm, pos);
  69. if (range && range.from.line + 1 < range.to.line)
  70. mark = marker(opts.indicatorOpen);
  71. }
  72. cm.setGutterMarker(line, opts.gutter, mark);
  73. ++cur;
  74. });
  75. }
  76. function updateInViewport(cm) {
  77. var vp = cm.getViewport(), state = cm.state.foldGutter;
  78. if (!state) return;
  79. cm.operation(function() {
  80. updateFoldInfo(cm, vp.from, vp.to);
  81. });
  82. state.from = vp.from; state.to = vp.to;
  83. }
  84. function onGutterClick(cm, line, gutter) {
  85. var opts = cm.state.foldGutter.options;
  86. if (gutter != opts.gutter) return;
  87. cm.foldCode(Pos(line, 0), opts.rangeFinder);
  88. }
  89. function onChange(cm) {
  90. var state = cm.state.foldGutter, opts = cm.state.foldGutter.options;
  91. state.from = state.to = 0;
  92. clearTimeout(state.changeUpdate);
  93. state.changeUpdate = setTimeout(function() { updateInViewport(cm); }, opts.foldOnChangeTimeSpan || 600);
  94. }
  95. function onViewportChange(cm) {
  96. var state = cm.state.foldGutter, opts = cm.state.foldGutter.options;
  97. clearTimeout(state.changeUpdate);
  98. state.changeUpdate = setTimeout(function() {
  99. var vp = cm.getViewport();
  100. if (state.from == state.to || vp.from - state.to > 20 || state.from - vp.to > 20) {
  101. updateInViewport(cm);
  102. } else {
  103. cm.operation(function() {
  104. if (vp.from < state.from) {
  105. updateFoldInfo(cm, vp.from, state.from);
  106. state.from = vp.from;
  107. }
  108. if (vp.to > state.to) {
  109. updateFoldInfo(cm, state.to, vp.to);
  110. state.to = vp.to;
  111. }
  112. });
  113. }
  114. }, opts.updateViewportTimeSpan || 400);
  115. }
  116. function onFold(cm, from) {
  117. var state = cm.state.foldGutter, line = from.line;
  118. if (line >= state.from && line < state.to)
  119. updateFoldInfo(cm, line, line + 1);
  120. }
  121. });