1
0

foldcode.js 956 B

1234567891011121314151617181920212223242526272829303132
  1. CodeMirror.newFoldFunction = function(rangeFinder, widget) {
  2. if (widget == null) widget = "\u2194";
  3. if (typeof widget == "string") {
  4. var text = document.createTextNode(widget);
  5. widget = document.createElement("span");
  6. widget.appendChild(text);
  7. widget.className = "CodeMirror-foldmarker";
  8. }
  9. return function(cm, pos) {
  10. if (typeof pos == "number") pos = CodeMirror.Pos(pos, 0);
  11. var range = rangeFinder(cm, pos);
  12. if (!range) return;
  13. var present = cm.findMarksAt(range.from), cleared = 0;
  14. for (var i = 0; i < present.length; ++i) {
  15. if (present[i].__isFold) {
  16. ++cleared;
  17. present[i].clear();
  18. }
  19. }
  20. if (cleared) return;
  21. var myWidget = widget.cloneNode(true);
  22. CodeMirror.on(myWidget, "mousedown", function() {myRange.clear();});
  23. var myRange = cm.markText(range.from, range.to, {
  24. replacedWith: myWidget,
  25. clearOnEnter: true,
  26. __isFold: true
  27. });
  28. };
  29. };