annotatescrollbar.min.js (4443B)
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(options) { 15 if (typeof options == "string") options = {className: options}; 16 return new Annotation(this, options); 17 }); 18 19 CodeMirror.defineOption("scrollButtonHeight", 0); 20 21 function Annotation(cm, options) { 22 this.cm = cm; 23 this.options = options; 24 this.buttonHeight = options.scrollButtonHeight || cm.getOption("scrollButtonHeight"); 25 this.annotations = []; 26 this.doRedraw = this.doUpdate = null; 27 this.div = cm.getWrapperElement().appendChild(document.createElement("div")); 28 this.div.style.cssText = "position: absolute; right: 0; top: 0; z-index: 7; pointer-events: none"; 29 this.computeScale(); 30 31 function scheduleRedraw(delay) { 32 clearTimeout(self.doRedraw); 33 self.doRedraw = setTimeout(function() { self.redraw(); }, delay); 34 } 35 36 var self = this; 37 cm.on("refresh", this.resizeHandler = function() { 38 clearTimeout(self.doUpdate); 39 self.doUpdate = setTimeout(function() { 40 if (self.computeScale()) scheduleRedraw(20); 41 }, 100); 42 }); 43 cm.on("markerAdded", this.resizeHandler); 44 cm.on("markerCleared", this.resizeHandler); 45 if (options.listenForChanges !== false) 46 cm.on("change", this.changeHandler = function() { 47 scheduleRedraw(250); 48 }); 49 } 50 51 Annotation.prototype.computeScale = function() { 52 var cm = this.cm; 53 var hScale = (cm.getWrapperElement().clientHeight - cm.display.barHeight - this.buttonHeight * 2) / 54 cm.getScrollerElement().scrollHeight 55 if (hScale != this.hScale) { 56 this.hScale = hScale; 57 return true; 58 } 59 }; 60 61 Annotation.prototype.update = function(annotations) { 62 this.annotations = annotations; 63 this.redraw(); 64 }; 65 66 Annotation.prototype.redraw = function(compute) { 67 if (compute !== false) this.computeScale(); 68 var cm = this.cm, hScale = this.hScale; 69 70 var frag = document.createDocumentFragment(), anns = this.annotations; 71 72 var wrapping = cm.getOption("lineWrapping"); 73 var singleLineH = wrapping && cm.defaultTextHeight() * 1.5; 74 var curLine = null, curLineObj = null; 75 function getY(pos, top) { 76 if (curLine != pos.line) { 77 curLine = pos.line; 78 curLineObj = cm.getLineHandle(curLine); 79 } 80 if ((curLineObj.widgets && curLineObj.widgets.length) || 81 (wrapping && curLineObj.height > singleLineH)) 82 return cm.charCoords(pos, "local")[top ? "top" : "bottom"]; 83 var topY = cm.heightAtLine(curLineObj, "local"); 84 return topY + (top ? 0 : curLineObj.height); 85 } 86 87 var lastLine = cm.lastLine() 88 if (cm.display.barWidth) for (var i = 0, nextTop; i < anns.length; i++) { 89 var ann = anns[i]; 90 if (ann.to.line > lastLine) continue; 91 var top = nextTop || getY(ann.from, true) * hScale; 92 var bottom = getY(ann.to, false) * hScale; 93 while (i < anns.length - 1) { 94 if (anns[i + 1].to.line > lastLine) break; 95 nextTop = getY(anns[i + 1].from, true) * hScale; 96 if (nextTop > bottom + .9) break; 97 ann = anns[++i]; 98 bottom = getY(ann.to, false) * hScale; 99 } 100 if (bottom == top) continue; 101 var height = Math.max(bottom - top, 3); 102 103 var elt = frag.appendChild(document.createElement("div")); 104 elt.style.cssText = "position: absolute; right: 0px; width: " + Math.max(cm.display.barWidth - 1, 2) + "px; top: " 105 + (top + this.buttonHeight) + "px; height: " + height + "px"; 106 elt.className = this.options.className; 107 if (ann.id) { 108 elt.setAttribute("annotation-id", ann.id); 109 } 110 } 111 this.div.textContent = ""; 112 this.div.appendChild(frag); 113 }; 114 115 Annotation.prototype.clear = function() { 116 this.cm.off("refresh", this.resizeHandler); 117 this.cm.off("markerAdded", this.resizeHandler); 118 this.cm.off("markerCleared", this.resizeHandler); 119 if (this.changeHandler) this.cm.off("change", this.changeHandler); 120 this.div.parentNode.removeChild(this.div); 121 }; 122 });