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