From e4bae8cdc501771349ea05bada004b357b88a211 Mon Sep 17 00:00:00 2001 From: Matt Pass Date: Sat, 25 May 2013 18:24:47 +0100 Subject: [PATCH] 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 --- lib/foldcode.js | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/lib/foldcode.js b/lib/foldcode.js index e0f4a36..6abb353 100644 --- a/lib/foldcode.js +++ b/lib/foldcode.js @@ -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 }); }; -}; +}; \ No newline at end of file