Now shows guttermarks

Takes new params for markOn, markOff and initMarks
Creates new span DOM elems for markOn and markOff
Determin if a line is foldable by there being a range with chars in
between (ie, start & end line's & ch's not the same)
If it's foldable and we're clearing or setting up the initMarks, show
markOff marker, otherwise markOn marker (as cloned nodes)
Also set to the markOff marker when clicking the inline widget
This commit is contained in:
Matt Pass
2013-05-25 18:24:47 +01:00
parent e0e679d436
commit e4bae8cdc5

View File

@@ -1,4 +1,6 @@
CodeMirror.newFoldFunction = function(rangeFinder, widget) {
// Modified version of CodeMirror's codefold.js to show guttermarkers
CodeMirror.newFoldFunction = function(rangeFinder, widget, markOn, markOff, initMarks) {
if (widget == null) widget = "\u2194";
if (typeof widget == "string") {
var text = document.createTextNode(widget);
@@ -6,10 +8,25 @@
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 (!range) return;
var present = cm.findMarksAt(range.from), cleared = 0;
@@ -19,14 +36,18 @@
present[i].clear();
}
}
if (cleared) return;
if (foldable) {
cm.setGutterMarker(pos.line, "CodeMirror-linenumbers", cleared || initMarks ? markOff.cloneNode(true) : markOn.cloneNode(true));
}
if (cleared || initMarks) return;
var myWidget = widget.cloneNode(true);
CodeMirror.on(myWidget, "mousedown", function() {myRange.clear();});
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
});
};
};
};