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"));
7
else if (typeof define == "function" && define.amd) // AMD
8
define(["../../lib/codemirror"], mod);
9
else // Plain browser env
10
mod(CodeMirror);
11
})(function(CodeMirror) {
12
"use strict";
13
14
CodeMirror.defineExtension("annotateScrollbar", function(className) {
15
return new Annotation(this, className);
16
});
17
18
function Annotation(cm, className) {
19
this.cm = cm;
20
this.className = className;
21
this.annotations = [];
22
this.div = cm.getWrapperElement().appendChild(document.createElement("div"));
23
this.div.style.cssText = "position: absolute; right: 0; top: 0; z-index: 7; pointer-events: none";
24
this.computeScale();
25
26
var self = this;
27
cm.on("refresh", this.resizeHandler = function(){
28
if (self.computeScale()) self.redraw();
29
});
30
}
31
32
Annotation.prototype.computeScale = function() {
33
var cm = this.cm;
34
var hScale = (cm.getWrapperElement().clientHeight - cm.display.barHeight) /
35
cm.heightAtLine(cm.lastLine() + 1, "local");
36
if (hScale != this.hScale) {
37
this.hScale = hScale;
38
return true;
39
}
40
};
41
42
Annotation.prototype.update = function(annotations) {
43
this.annotations = annotations;
44
this.redraw();
45
};
46
47
Annotation.prototype.redraw = function() {
48
var cm = this.cm, hScale = this.hScale;
49
if (!cm.display.barWidth) return;
50
51
var frag = document.createDocumentFragment(), anns = this.annotations;
52
for (var i = 0, nextTop; i < anns.length; i++) {
53
var ann = anns[i];
54
var top = nextTop || cm.charCoords(ann.from, "local").top * hScale;
55
var bottom = cm.charCoords(ann.to, "local").bottom * hScale;
56
while (i < anns.length - 1) {
57
nextTop = cm.charCoords(anns[i + 1].from, "local").top * hScale;
58
if (nextTop > bottom + .9) break;
59
ann = anns[++i];
60
bottom = cm.charCoords(ann.to, "local").bottom * hScale;
61
}
62
var height = Math.max(bottom - top, 3);
63
64
var elt = frag.appendChild(document.createElement("div"));
65
elt.style.cssText = "position: absolute; right: 0px; width: " + Math.max(cm.display.barWidth - 1, 2) + "px; top: " + top + "px; height: " + height + "px";
66
elt.className = this.className;
67
}
68
this.div.textContent = "";
69
this.div.appendChild(frag);
70
};
71
72
Annotation.prototype.clear = function() {
73
this.cm.off("refresh", this.resizeHandler);
74
this.div.parentNode.removeChild(this.div);
75
};
76
});
77
78