Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50675 views
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"), require("./foldcode"));
7
else if (typeof define == "function" && define.amd) // AMD
8
define(["../../lib/codemirror", "./foldcode"], mod);
9
else // Plain browser env
10
mod(CodeMirror);
11
})(function(CodeMirror) {
12
"use strict";
13
14
CodeMirror.defineOption("foldGutter", false, function(cm, val, old) {
15
if (old && old != CodeMirror.Init) {
16
cm.clearGutter(cm.state.foldGutter.options.gutter);
17
cm.state.foldGutter = null;
18
cm.off("gutterClick", onGutterClick);
19
cm.off("change", onChange);
20
cm.off("viewportChange", onViewportChange);
21
cm.off("fold", onFold);
22
cm.off("unfold", onFold);
23
cm.off("swapDoc", updateInViewport);
24
}
25
if (val) {
26
cm.state.foldGutter = new State(parseOptions(val));
27
updateInViewport(cm);
28
cm.on("gutterClick", onGutterClick);
29
cm.on("change", onChange);
30
cm.on("viewportChange", onViewportChange);
31
cm.on("fold", onFold);
32
cm.on("unfold", onFold);
33
cm.on("swapDoc", updateInViewport);
34
}
35
});
36
37
var Pos = CodeMirror.Pos;
38
39
function State(options) {
40
this.options = options;
41
this.from = this.to = 0;
42
}
43
44
function parseOptions(opts) {
45
if (opts === true) opts = {};
46
if (opts.gutter == null) opts.gutter = "CodeMirror-foldgutter";
47
if (opts.indicatorOpen == null) opts.indicatorOpen = "CodeMirror-foldgutter-open";
48
if (opts.indicatorFolded == null) opts.indicatorFolded = "CodeMirror-foldgutter-folded";
49
return opts;
50
}
51
52
function isFolded(cm, line) {
53
var marks = cm.findMarksAt(Pos(line));
54
for (var i = 0; i < marks.length; ++i)
55
if (marks[i].__isFold && marks[i].find().from.line == line) return true;
56
}
57
58
function marker(spec) {
59
if (typeof spec == "string") {
60
var elt = document.createElement("div");
61
elt.className = spec + " CodeMirror-guttermarker-subtle";
62
return elt;
63
} else {
64
return spec.cloneNode(true);
65
}
66
}
67
68
function updateFoldInfo(cm, from, to) {
69
var opts = cm.state.foldGutter.options, cur = from;
70
cm.eachLine(from, to, function(line) {
71
var mark = null;
72
if (isFolded(cm, cur)) {
73
mark = marker(opts.indicatorFolded);
74
} else {
75
var pos = Pos(cur, 0), func = opts.rangeFinder || CodeMirror.fold.auto;
76
var range = func && func(cm, pos);
77
if (range && range.from.line + 1 < range.to.line)
78
mark = marker(opts.indicatorOpen);
79
}
80
cm.setGutterMarker(line, opts.gutter, mark);
81
++cur;
82
});
83
}
84
85
function updateInViewport(cm) {
86
var vp = cm.getViewport(), state = cm.state.foldGutter;
87
if (!state) return;
88
cm.operation(function() {
89
updateFoldInfo(cm, vp.from, vp.to);
90
});
91
state.from = vp.from; state.to = vp.to;
92
}
93
94
function onGutterClick(cm, line, gutter) {
95
var opts = cm.state.foldGutter.options;
96
if (gutter != opts.gutter) return;
97
cm.foldCode(Pos(line, 0), opts.rangeFinder);
98
}
99
100
function onChange(cm) {
101
var state = cm.state.foldGutter, opts = cm.state.foldGutter.options;
102
state.from = state.to = 0;
103
clearTimeout(state.changeUpdate);
104
state.changeUpdate = setTimeout(function() { updateInViewport(cm); }, opts.foldOnChangeTimeSpan || 600);
105
}
106
107
function onViewportChange(cm) {
108
var state = cm.state.foldGutter, opts = cm.state.foldGutter.options;
109
clearTimeout(state.changeUpdate);
110
state.changeUpdate = setTimeout(function() {
111
var vp = cm.getViewport();
112
if (state.from == state.to || vp.from - state.to > 20 || state.from - vp.to > 20) {
113
updateInViewport(cm);
114
} else {
115
cm.operation(function() {
116
if (vp.from < state.from) {
117
updateFoldInfo(cm, vp.from, state.from);
118
state.from = vp.from;
119
}
120
if (vp.to > state.to) {
121
updateFoldInfo(cm, state.to, vp.to);
122
state.to = vp.to;
123
}
124
});
125
}
126
}, opts.updateViewportTimeSpan || 400);
127
}
128
129
function onFold(cm, from) {
130
var state = cm.state.foldGutter, line = from.line;
131
if (line >= state.from && line < state.to)
132
updateFoldInfo(cm, line, line + 1);
133
}
134
});
135
136