mirror of
https://github.com/icecoder/ICEcoder.git
synced 2026-03-02 23:03:59 +01:00
initMarks was used for the intial setup of a doc with (-) marks but this idea can be extended to cover not wanting to collapse and only mark lines If a line isn't collapsable now it clears the markers for it (used when we delete text, replace with something else etc)
54 lines
2.0 KiB
JavaScript
54 lines
2.0 KiB
JavaScript
// Modified version of CodeMirror's codefold.js to show guttermarkers
|
|
|
|
CodeMirror.newFoldFunction = function(rangeFinder, widget, markOn, markOff, dontCollapse) {
|
|
if (widget == null) widget = "\u2194";
|
|
if (typeof widget == "string") {
|
|
var text = document.createTextNode(widget);
|
|
widget = document.createElement("span");
|
|
widget.appendChild(text);
|
|
widget.className = "CodeMirror-foldmarker";
|
|
}
|
|
if (markOn == null) markOn = "+";
|
|
if (typeof markOn == "string") {
|
|
var text = document.createTextNode(markOn);
|
|
markOn = document.createElement("span");
|
|
markOn.appendChild(text);
|
|
markOn.className = "CodeMirror-guttermarker CodeMirror-guttermarkerOn";
|
|
}
|
|
if (markOff == null) markOff = "-";
|
|
if (typeof markOff == "string") {
|
|
var text = document.createTextNode(markOff);
|
|
markOff = document.createElement("span");
|
|
markOff.appendChild(text);
|
|
markOff.className = "CodeMirror-guttermarker CodeMirror-guttermarkerOff";
|
|
}
|
|
|
|
return function(cm, pos) {
|
|
if (typeof pos == "number") pos = CodeMirror.Pos(pos, 0);
|
|
var range = rangeFinder(cm, pos);
|
|
foldable = range && (range.from.line != range.to.line || range.from.ch != range.to.ch) ? true : false;
|
|
if (!foldable) cm.setGutterMarker(pos.line, "CodeMirror-linenumbers", null);
|
|
if (!range) return;
|
|
|
|
var present = cm.findMarksAt(range.from), cleared = 0;
|
|
for (var i = 0; i < present.length; ++i) {
|
|
if (present[i].__isFold) {
|
|
++cleared;
|
|
present[i].clear();
|
|
}
|
|
}
|
|
|
|
if (foldable) {
|
|
cm.setGutterMarker(pos.line, "CodeMirror-linenumbers", cleared || dontCollapse ? markOff.cloneNode(true) : markOn.cloneNode(true));
|
|
}
|
|
|
|
if (cleared || dontCollapse) return;
|
|
var myWidget = widget.cloneNode(true);
|
|
CodeMirror.on(myWidget, "mousedown", function() {myRange.clear();cm.setGutterMarker(pos.line, "CodeMirror-linenumbers", markOff.cloneNode(true));});
|
|
var myRange = cm.markText(range.from, range.to, {
|
|
replacedWith: myWidget,
|
|
clearOnEnter: true,
|
|
__isFold: true
|
|
});
|
|
};
|
|
}; |