本站源代码
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.

153 lines
4.7KB

  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"));
  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: getOption(cm, options, "clearOnEnter"),
  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. } else if (widget) {
  63. widget = widget.cloneNode(true)
  64. }
  65. return widget;
  66. }
  67. // Clumsy backwards-compatible interface
  68. CodeMirror.newFoldFunction = function(rangeFinder, widget) {
  69. return function(cm, pos) { doFold(cm, pos, {rangeFinder: rangeFinder, widget: widget}); };
  70. };
  71. // New-style interface
  72. CodeMirror.defineExtension("foldCode", function(pos, options, force) {
  73. doFold(this, pos, options, force);
  74. });
  75. CodeMirror.defineExtension("isFolded", function(pos) {
  76. var marks = this.findMarksAt(pos);
  77. for (var i = 0; i < marks.length; ++i)
  78. if (marks[i].__isFold) return true;
  79. });
  80. CodeMirror.commands.toggleFold = function(cm) {
  81. cm.foldCode(cm.getCursor());
  82. };
  83. CodeMirror.commands.fold = function(cm) {
  84. cm.foldCode(cm.getCursor(), null, "fold");
  85. };
  86. CodeMirror.commands.unfold = function(cm) {
  87. cm.foldCode(cm.getCursor(), null, "unfold");
  88. };
  89. CodeMirror.commands.foldAll = function(cm) {
  90. cm.operation(function() {
  91. for (var i = cm.firstLine(), e = cm.lastLine(); i <= e; i++)
  92. cm.foldCode(CodeMirror.Pos(i, 0), null, "fold");
  93. });
  94. };
  95. CodeMirror.commands.unfoldAll = function(cm) {
  96. cm.operation(function() {
  97. for (var i = cm.firstLine(), e = cm.lastLine(); i <= e; i++)
  98. cm.foldCode(CodeMirror.Pos(i, 0), null, "unfold");
  99. });
  100. };
  101. CodeMirror.registerHelper("fold", "combine", function() {
  102. var funcs = Array.prototype.slice.call(arguments, 0);
  103. return function(cm, start) {
  104. for (var i = 0; i < funcs.length; ++i) {
  105. var found = funcs[i](cm, start);
  106. if (found) return found;
  107. }
  108. };
  109. });
  110. CodeMirror.registerHelper("fold", "auto", function(cm, start) {
  111. var helpers = cm.getHelpers(start, "fold");
  112. for (var i = 0; i < helpers.length; i++) {
  113. var cur = helpers[i](cm, start);
  114. if (cur) return cur;
  115. }
  116. });
  117. var defaultOptions = {
  118. rangeFinder: CodeMirror.fold.auto,
  119. widget: "\u2194",
  120. minFoldSize: 0,
  121. scanUp: false,
  122. clearOnEnter: true
  123. };
  124. CodeMirror.defineOption("foldOptions", null);
  125. function getOption(cm, options, name) {
  126. if (options && options[name] !== undefined)
  127. return options[name];
  128. var editorOptions = cm.options.foldOptions;
  129. if (editorOptions && editorOptions[name] !== undefined)
  130. return editorOptions[name];
  131. return defaultOptions[name];
  132. }
  133. CodeMirror.defineExtension("foldOption", function(options, name) {
  134. return getOption(this, options, name);
  135. });
  136. });
上海开阖软件有限公司 沪ICP备12045867号-1