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.

159 lines
4.9 KiB

7 months ago
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://codemirror.net/5/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. if (force === "fold") return range;
  25. var marks = cm.findMarksAt(range.from);
  26. for (var i = 0; i < marks.length; ++i) {
  27. if (marks[i].__isFold) {
  28. if (!allowFolded) return null;
  29. range.cleared = true;
  30. marks[i].clear();
  31. }
  32. }
  33. return range;
  34. }
  35. var range = getRange(true);
  36. if (getOption(cm, options, "scanUp")) while (!range && pos.line > cm.firstLine()) {
  37. pos = CodeMirror.Pos(pos.line - 1, 0);
  38. range = getRange(false);
  39. }
  40. if (!range || range.cleared || force === "unfold") return;
  41. var myWidget = makeWidget(cm, options, range);
  42. CodeMirror.on(myWidget, "mousedown", function(e) {
  43. myRange.clear();
  44. CodeMirror.e_preventDefault(e);
  45. });
  46. var myRange = cm.markText(range.from, range.to, {
  47. replacedWith: myWidget,
  48. clearOnEnter: getOption(cm, options, "clearOnEnter"),
  49. __isFold: true
  50. });
  51. myRange.on("clear", function(from, to) {
  52. CodeMirror.signal(cm, "unfold", cm, from, to);
  53. });
  54. CodeMirror.signal(cm, "fold", cm, range.from, range.to);
  55. }
  56. function makeWidget(cm, options, range) {
  57. var widget = getOption(cm, options, "widget");
  58. if (typeof widget == "function") {
  59. widget = widget(range.from, range.to);
  60. }
  61. if (typeof widget == "string") {
  62. var text = document.createTextNode(widget);
  63. widget = document.createElement("span");
  64. widget.appendChild(text);
  65. widget.className = "CodeMirror-foldmarker";
  66. } else if (widget) {
  67. widget = widget.cloneNode(true)
  68. }
  69. return widget;
  70. }
  71. // Clumsy backwards-compatible interface
  72. CodeMirror.newFoldFunction = function(rangeFinder, widget) {
  73. return function(cm, pos) { doFold(cm, pos, {rangeFinder: rangeFinder, widget: widget}); };
  74. };
  75. // New-style interface
  76. CodeMirror.defineExtension("foldCode", function(pos, options, force) {
  77. doFold(this, pos, options, force);
  78. });
  79. CodeMirror.defineExtension("isFolded", function(pos) {
  80. var marks = this.findMarksAt(pos);
  81. for (var i = 0; i < marks.length; ++i)
  82. if (marks[i].__isFold) return true;
  83. });
  84. CodeMirror.commands.toggleFold = function(cm) {
  85. cm.foldCode(cm.getCursor());
  86. };
  87. CodeMirror.commands.fold = function(cm) {
  88. cm.foldCode(cm.getCursor(), null, "fold");
  89. };
  90. CodeMirror.commands.unfold = function(cm) {
  91. cm.foldCode(cm.getCursor(), { scanUp: false }, "unfold");
  92. };
  93. CodeMirror.commands.foldAll = 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), { scanUp: false }, "fold");
  97. });
  98. };
  99. CodeMirror.commands.unfoldAll = function(cm) {
  100. cm.operation(function() {
  101. for (var i = cm.firstLine(), e = cm.lastLine(); i <= e; i++)
  102. cm.foldCode(CodeMirror.Pos(i, 0), { scanUp: false }, "unfold");
  103. });
  104. };
  105. CodeMirror.registerHelper("fold", "combine", function() {
  106. var funcs = Array.prototype.slice.call(arguments, 0);
  107. return function(cm, start) {
  108. for (var i = 0; i < funcs.length; ++i) {
  109. var found = funcs[i](cm, start);
  110. if (found) return found;
  111. }
  112. };
  113. });
  114. CodeMirror.registerHelper("fold", "auto", function(cm, start) {
  115. var helpers = cm.getHelpers(start, "fold");
  116. for (var i = 0; i < helpers.length; i++) {
  117. var cur = helpers[i](cm, start);
  118. if (cur) return cur;
  119. }
  120. });
  121. var defaultOptions = {
  122. rangeFinder: CodeMirror.fold.auto,
  123. widget: "\u2194",
  124. minFoldSize: 0,
  125. scanUp: false,
  126. clearOnEnter: true
  127. };
  128. CodeMirror.defineOption("foldOptions", null);
  129. function getOption(cm, options, name) {
  130. if (options && options[name] !== undefined)
  131. return options[name];
  132. var editorOptions = cm.options.foldOptions;
  133. if (editorOptions && editorOptions[name] !== undefined)
  134. return editorOptions[name];
  135. return defaultOptions[name];
  136. }
  137. CodeMirror.defineExtension("foldOption", function(options, name) {
  138. return getOption(this, options, name);
  139. });
  140. });