2015-02-25 04:05:00 +00:00
|
|
|
// CodeMirror, copyright (c) by Marijn Haverbeke and others
|
2018-08-25 12:11:03 +00:00
|
|
|
// Distributed under an MIT license: https://codemirror.net/LICENSE
|
2015-02-25 04:05:00 +00:00
|
|
|
|
|
|
|
(function(mod) {
|
|
|
|
if (typeof exports == "object" && typeof module == "object") // CommonJS
|
|
|
|
mod(require("../../lib/codemirror"));
|
|
|
|
else if (typeof define == "function" && define.amd) // AMD
|
|
|
|
define(["../../lib/codemirror"], mod);
|
|
|
|
else // Plain browser env
|
|
|
|
mod(CodeMirror);
|
|
|
|
})(function(CodeMirror) {
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
CodeMirror.defineExtension("annotateScrollbar", function(options) {
|
|
|
|
if (typeof options == "string") options = {className: options};
|
|
|
|
return new Annotation(this, options);
|
|
|
|
});
|
|
|
|
|
|
|
|
CodeMirror.defineOption("scrollButtonHeight", 0);
|
|
|
|
|
|
|
|
function Annotation(cm, options) {
|
|
|
|
this.cm = cm;
|
|
|
|
this.options = options;
|
|
|
|
this.buttonHeight = options.scrollButtonHeight || cm.getOption("scrollButtonHeight");
|
|
|
|
this.annotations = [];
|
|
|
|
this.doRedraw = this.doUpdate = null;
|
|
|
|
this.div = cm.getWrapperElement().appendChild(document.createElement("div"));
|
|
|
|
this.div.style.cssText = "position: absolute; right: 0; top: 0; z-index: 7; pointer-events: none";
|
|
|
|
this.computeScale();
|
|
|
|
|
|
|
|
function scheduleRedraw(delay) {
|
|
|
|
clearTimeout(self.doRedraw);
|
|
|
|
self.doRedraw = setTimeout(function() { self.redraw(); }, delay);
|
|
|
|
}
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
cm.on("refresh", this.resizeHandler = function() {
|
|
|
|
clearTimeout(self.doUpdate);
|
|
|
|
self.doUpdate = setTimeout(function() {
|
|
|
|
if (self.computeScale()) scheduleRedraw(20);
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
cm.on("markerAdded", this.resizeHandler);
|
|
|
|
cm.on("markerCleared", this.resizeHandler);
|
|
|
|
if (options.listenForChanges !== false)
|
2020-02-02 17:07:22 +00:00
|
|
|
cm.on("changes", this.changeHandler = function() {
|
2015-02-25 04:05:00 +00:00
|
|
|
scheduleRedraw(250);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Annotation.prototype.computeScale = function() {
|
|
|
|
var cm = this.cm;
|
|
|
|
var hScale = (cm.getWrapperElement().clientHeight - cm.display.barHeight - this.buttonHeight * 2) /
|
2017-02-08 12:35:49 +00:00
|
|
|
cm.getScrollerElement().scrollHeight
|
2015-02-25 04:05:00 +00:00
|
|
|
if (hScale != this.hScale) {
|
|
|
|
this.hScale = hScale;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Annotation.prototype.update = function(annotations) {
|
|
|
|
this.annotations = annotations;
|
|
|
|
this.redraw();
|
|
|
|
};
|
|
|
|
|
|
|
|
Annotation.prototype.redraw = function(compute) {
|
|
|
|
if (compute !== false) this.computeScale();
|
|
|
|
var cm = this.cm, hScale = this.hScale;
|
|
|
|
|
|
|
|
var frag = document.createDocumentFragment(), anns = this.annotations;
|
2015-04-23 02:44:04 +00:00
|
|
|
|
|
|
|
var wrapping = cm.getOption("lineWrapping");
|
|
|
|
var singleLineH = wrapping && cm.defaultTextHeight() * 1.5;
|
|
|
|
var curLine = null, curLineObj = null;
|
2020-08-23 15:44:27 +00:00
|
|
|
|
2015-04-23 02:44:04 +00:00
|
|
|
function getY(pos, top) {
|
|
|
|
if (curLine != pos.line) {
|
2020-08-23 15:44:27 +00:00
|
|
|
curLine = pos.line
|
|
|
|
curLineObj = cm.getLineHandle(pos.line)
|
|
|
|
var visual = cm.getLineHandleVisualStart(curLineObj)
|
|
|
|
if (visual != curLineObj) {
|
|
|
|
curLine = cm.getLineNumber(visual)
|
|
|
|
curLineObj = visual
|
|
|
|
}
|
2015-04-23 02:44:04 +00:00
|
|
|
}
|
2017-03-18 00:24:59 +00:00
|
|
|
if ((curLineObj.widgets && curLineObj.widgets.length) ||
|
|
|
|
(wrapping && curLineObj.height > singleLineH))
|
2015-04-23 02:44:04 +00:00
|
|
|
return cm.charCoords(pos, "local")[top ? "top" : "bottom"];
|
|
|
|
var topY = cm.heightAtLine(curLineObj, "local");
|
|
|
|
return topY + (top ? 0 : curLineObj.height);
|
|
|
|
}
|
|
|
|
|
2017-03-18 00:24:59 +00:00
|
|
|
var lastLine = cm.lastLine()
|
2015-02-25 04:05:00 +00:00
|
|
|
if (cm.display.barWidth) for (var i = 0, nextTop; i < anns.length; i++) {
|
|
|
|
var ann = anns[i];
|
2017-03-18 00:24:59 +00:00
|
|
|
if (ann.to.line > lastLine) continue;
|
2015-04-23 02:44:04 +00:00
|
|
|
var top = nextTop || getY(ann.from, true) * hScale;
|
|
|
|
var bottom = getY(ann.to, false) * hScale;
|
2015-02-25 04:05:00 +00:00
|
|
|
while (i < anns.length - 1) {
|
2017-03-18 00:24:59 +00:00
|
|
|
if (anns[i + 1].to.line > lastLine) break;
|
2015-04-23 02:44:04 +00:00
|
|
|
nextTop = getY(anns[i + 1].from, true) * hScale;
|
2015-02-25 04:05:00 +00:00
|
|
|
if (nextTop > bottom + .9) break;
|
|
|
|
ann = anns[++i];
|
2015-04-23 02:44:04 +00:00
|
|
|
bottom = getY(ann.to, false) * hScale;
|
2015-02-25 04:05:00 +00:00
|
|
|
}
|
|
|
|
if (bottom == top) continue;
|
|
|
|
var height = Math.max(bottom - top, 3);
|
|
|
|
|
|
|
|
var elt = frag.appendChild(document.createElement("div"));
|
|
|
|
elt.style.cssText = "position: absolute; right: 0px; width: " + Math.max(cm.display.barWidth - 1, 2) + "px; top: "
|
|
|
|
+ (top + this.buttonHeight) + "px; height: " + height + "px";
|
|
|
|
elt.className = this.options.className;
|
2017-02-08 12:35:49 +00:00
|
|
|
if (ann.id) {
|
|
|
|
elt.setAttribute("annotation-id", ann.id);
|
|
|
|
}
|
2015-02-25 04:05:00 +00:00
|
|
|
}
|
|
|
|
this.div.textContent = "";
|
|
|
|
this.div.appendChild(frag);
|
|
|
|
};
|
|
|
|
|
|
|
|
Annotation.prototype.clear = function() {
|
|
|
|
this.cm.off("refresh", this.resizeHandler);
|
|
|
|
this.cm.off("markerAdded", this.resizeHandler);
|
|
|
|
this.cm.off("markerCleared", this.resizeHandler);
|
2020-02-02 17:07:22 +00:00
|
|
|
if (this.changeHandler) this.cm.off("changes", this.changeHandler);
|
2015-02-25 04:05:00 +00:00
|
|
|
this.div.parentNode.removeChild(this.div);
|
|
|
|
};
|
|
|
|
});
|