本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

152 lines
4.6KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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("changes", onChange);
  18. cm.off("viewportChange", onViewportChange);
  19. cm.off("fold", onFold);
  20. cm.off("unfold", onFold);
  21. cm.off("swapDoc", onChange);
  22. }
  23. if (val) {
  24. cm.state.foldGutter = new State(parseOptions(val));
  25. updateInViewport(cm);
  26. cm.on("gutterClick", onGutterClick);
  27. cm.on("changes", onChange);
  28. cm.on("viewportChange", onViewportChange);
  29. cm.on("fold", onFold);
  30. cm.on("unfold", onFold);
  31. cm.on("swapDoc", onChange);
  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.findMarks(Pos(line, 0), Pos(line + 1, 0));
  48. for (var i = 0; i < marks.length; ++i) {
  49. if (marks[i].__isFold) {
  50. var fromPos = marks[i].find(-1);
  51. if (fromPos && fromPos.line === line)
  52. return marks[i];
  53. }
  54. }
  55. }
  56. function marker(spec) {
  57. if (typeof spec == "string") {
  58. var elt = document.createElement("div");
  59. elt.className = spec + " CodeMirror-guttermarker-subtle";
  60. return elt;
  61. } else {
  62. return spec.cloneNode(true);
  63. }
  64. }
  65. function updateFoldInfo(cm, from, to) {
  66. var opts = cm.state.foldGutter.options, cur = from;
  67. var minSize = cm.foldOption(opts, "minFoldSize");
  68. var func = cm.foldOption(opts, "rangeFinder");
  69. cm.eachLine(from, to, function(line) {
  70. var mark = null;
  71. if (isFolded(cm, cur)) {
  72. mark = marker(opts.indicatorFolded);
  73. } else {
  74. var pos = Pos(cur, 0);
  75. var range = func && func(cm, pos);
  76. if (range && range.to.line - range.from.line >= minSize)
  77. mark = marker(opts.indicatorOpen);
  78. }
  79. cm.setGutterMarker(line, opts.gutter, mark);
  80. ++cur;
  81. });
  82. }
  83. function updateInViewport(cm) {
  84. var vp = cm.getViewport(), state = cm.state.foldGutter;
  85. if (!state) return;
  86. cm.operation(function() {
  87. updateFoldInfo(cm, vp.from, vp.to);
  88. });
  89. state.from = vp.from; state.to = vp.to;
  90. }
  91. function onGutterClick(cm, line, gutter) {
  92. var state = cm.state.foldGutter;
  93. if (!state) return;
  94. var opts = state.options;
  95. if (gutter != opts.gutter) return;
  96. var folded = isFolded(cm, line);
  97. if (folded) folded.clear();
  98. else cm.foldCode(Pos(line, 0), opts);
  99. }
  100. function onChange(cm) {
  101. var state = cm.state.foldGutter;
  102. if (!state) return;
  103. var opts = state.options;
  104. state.from = state.to = 0;
  105. clearTimeout(state.changeUpdate);
  106. state.changeUpdate = setTimeout(function() { updateInViewport(cm); }, opts.foldOnChangeTimeSpan || 600);
  107. }
  108. function onViewportChange(cm) {
  109. var state = cm.state.foldGutter;
  110. if (!state) return;
  111. var opts = state.options;
  112. clearTimeout(state.changeUpdate);
  113. state.changeUpdate = setTimeout(function() {
  114. var vp = cm.getViewport();
  115. if (state.from == state.to || vp.from - state.to > 20 || state.from - vp.to > 20) {
  116. updateInViewport(cm);
  117. } else {
  118. cm.operation(function() {
  119. if (vp.from < state.from) {
  120. updateFoldInfo(cm, vp.from, state.from);
  121. state.from = vp.from;
  122. }
  123. if (vp.to > state.to) {
  124. updateFoldInfo(cm, state.to, vp.to);
  125. state.to = vp.to;
  126. }
  127. });
  128. }
  129. }, opts.updateViewportTimeSpan || 400);
  130. }
  131. function onFold(cm, from) {
  132. var state = cm.state.foldGutter;
  133. if (!state) return;
  134. var line = from.line;
  135. if (line >= state.from && line < state.to)
  136. updateFoldInfo(cm, line, line + 1);
  137. }
  138. });
上海开阖软件有限公司 沪ICP备12045867号-1