foldcode.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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"));
  6. else if (typeof define == "function" && define.amd) // AMD
  7. define(["../../lib/codemirror"], mod);
  8. else // Plain browser env
  9. mod(CodeMirror);
  10. })(function(CodeMirror) {
  11. "use strict";
  12. function doFold(cm, pos, options, force) {
  13. if (options && options.call) {
  14. var finder = options;
  15. options = null;
  16. } else {
  17. var finder = getOption(cm, options, "rangeFinder");
  18. }
  19. if (typeof pos == "number") pos = CodeMirror.Pos(pos, 0);
  20. var minSize = getOption(cm, options, "minFoldSize");
  21. function getRange(allowFolded) {
  22. var range = finder(cm, pos);
  23. if (!range || range.to.line - range.from.line < minSize) return null;
  24. var marks = cm.findMarksAt(range.from);
  25. for (var i = 0; i < marks.length; ++i) {
  26. if (marks[i].__isFold && force !== "fold") {
  27. if (!allowFolded) return null;
  28. range.cleared = true;
  29. marks[i].clear();
  30. }
  31. }
  32. return range;
  33. }
  34. var range = getRange(true);
  35. if (getOption(cm, options, "scanUp")) while (!range && pos.line > cm.firstLine()) {
  36. pos = CodeMirror.Pos(pos.line - 1, 0);
  37. range = getRange(false);
  38. }
  39. if (!range || range.cleared || force === "unfold") return;
  40. var myWidget = makeWidget(cm, options);
  41. CodeMirror.on(myWidget, "mousedown", function(e) {
  42. myRange.clear();
  43. CodeMirror.e_preventDefault(e);
  44. });
  45. var myRange = cm.markText(range.from, range.to, {
  46. replacedWith: myWidget,
  47. clearOnEnter: true,
  48. __isFold: true
  49. });
  50. myRange.on("clear", function(from, to) {
  51. CodeMirror.signal(cm, "unfold", cm, from, to);
  52. });
  53. CodeMirror.signal(cm, "fold", cm, range.from, range.to);
  54. }
  55. function makeWidget(cm, options) {
  56. var widget = getOption(cm, options, "widget");
  57. if (typeof widget == "string") {
  58. var text = document.createTextNode(widget);
  59. widget = document.createElement("span");
  60. widget.appendChild(text);
  61. widget.className = "CodeMirror-foldmarker";
  62. }
  63. return widget;
  64. }
  65. // Clumsy backwards-compatible interface
  66. CodeMirror.newFoldFunction = function(rangeFinder, widget) {
  67. return function(cm, pos) { doFold(cm, pos, {rangeFinder: rangeFinder, widget: widget}); };
  68. };
  69. // New-style interface
  70. CodeMirror.defineExtension("foldCode", function(pos, options, force) {
  71. doFold(this, pos, options, force);
  72. });
  73. CodeMirror.defineExtension("isFolded", function(pos) {
  74. var marks = this.findMarksAt(pos);
  75. for (var i = 0; i < marks.length; ++i)
  76. if (marks[i].__isFold) return true;
  77. });
  78. CodeMirror.commands.toggleFold = function(cm) {
  79. cm.foldCode(cm.getCursor());
  80. };
  81. CodeMirror.commands.fold = function(cm) {
  82. cm.foldCode(cm.getCursor(), null, "fold");
  83. };
  84. CodeMirror.commands.unfold = function(cm) {
  85. cm.foldCode(cm.getCursor(), null, "unfold");
  86. };
  87. CodeMirror.commands.foldAll = function(cm) {
  88. cm.operation(function() {
  89. for (var i = cm.firstLine(), e = cm.lastLine(); i <= e; i++)
  90. cm.foldCode(CodeMirror.Pos(i, 0), null, "fold");
  91. });
  92. };
  93. CodeMirror.commands.unfoldAll = function(cm) {
  94. cm.operation(function() {
  95. for (var i = cm.firstLine(), e = cm.lastLine(); i <= e; i++)
  96. cm.foldCode(CodeMirror.Pos(i, 0), null, "unfold");
  97. });
  98. };
  99. CodeMirror.registerHelper("fold", "combine", function() {
  100. var funcs = Array.prototype.slice.call(arguments, 0);
  101. return function(cm, start) {
  102. for (var i = 0; i < funcs.length; ++i) {
  103. var found = funcs[i](cm, start);
  104. if (found) return found;
  105. }
  106. };
  107. });
  108. CodeMirror.registerHelper("fold", "auto", function(cm, start) {
  109. var helpers = cm.getHelpers(start, "fold");
  110. for (var i = 0; i < helpers.length; i++) {
  111. var cur = helpers[i](cm, start);
  112. if (cur) return cur;
  113. }
  114. });
  115. var defaultOptions = {
  116. rangeFinder: CodeMirror.fold.auto,
  117. widget: "\u2194",
  118. minFoldSize: 0,
  119. scanUp: false
  120. };
  121. CodeMirror.defineOption("foldOptions", null);
  122. function getOption(cm, options, name) {
  123. if (options && options[name] !== undefined)
  124. return options[name];
  125. var editorOptions = cm.options.foldOptions;
  126. if (editorOptions && editorOptions[name] !== undefined)
  127. return editorOptions[name];
  128. return defaultOptions[name];
  129. }
  130. });