LaravelTest
128 строк · 4.5 Кб
1// CodeMirror, copyright (c) by Marijn Haverbeke and others
2// Distributed under an MIT license: https://codemirror.net/LICENSE
3
4(function(mod) {5if (typeof exports == "object" && typeof module == "object") // CommonJS6mod(require("../../lib/codemirror"));7else if (typeof define == "function" && define.amd) // AMD8define(["../../lib/codemirror"], mod);9else // Plain browser env10mod(CodeMirror);11})(function(CodeMirror) {12"use strict";13
14CodeMirror.defineExtension("annotateScrollbar", function(options) {15if (typeof options == "string") options = {className: options};16return new Annotation(this, options);17});18
19CodeMirror.defineOption("scrollButtonHeight", 0);20
21function Annotation(cm, options) {22this.cm = cm;23this.options = options;24this.buttonHeight = options.scrollButtonHeight || cm.getOption("scrollButtonHeight");25this.annotations = [];26this.doRedraw = this.doUpdate = null;27this.div = cm.getWrapperElement().appendChild(document.createElement("div"));28this.div.style.cssText = "position: absolute; right: 0; top: 0; z-index: 7; pointer-events: none";29this.computeScale();30
31function scheduleRedraw(delay) {32clearTimeout(self.doRedraw);33self.doRedraw = setTimeout(function() { self.redraw(); }, delay);34}35
36var self = this;37cm.on("refresh", this.resizeHandler = function() {38clearTimeout(self.doUpdate);39self.doUpdate = setTimeout(function() {40if (self.computeScale()) scheduleRedraw(20);41}, 100);42});43cm.on("markerAdded", this.resizeHandler);44cm.on("markerCleared", this.resizeHandler);45if (options.listenForChanges !== false)46cm.on("changes", this.changeHandler = function() {47scheduleRedraw(250);48});49}50
51Annotation.prototype.computeScale = function() {52var cm = this.cm;53var hScale = (cm.getWrapperElement().clientHeight - cm.display.barHeight - this.buttonHeight * 2) /54cm.getScrollerElement().scrollHeight55if (hScale != this.hScale) {56this.hScale = hScale;57return true;58}59};60
61Annotation.prototype.update = function(annotations) {62this.annotations = annotations;63this.redraw();64};65
66Annotation.prototype.redraw = function(compute) {67if (compute !== false) this.computeScale();68var cm = this.cm, hScale = this.hScale;69
70var frag = document.createDocumentFragment(), anns = this.annotations;71
72var wrapping = cm.getOption("lineWrapping");73var singleLineH = wrapping && cm.defaultTextHeight() * 1.5;74var curLine = null, curLineObj = null;75
76function getY(pos, top) {77if (curLine != pos.line) {78curLine = pos.line79curLineObj = cm.getLineHandle(pos.line)80var visual = cm.getLineHandleVisualStart(curLineObj)81if (visual != curLineObj) {82curLine = cm.getLineNumber(visual)83curLineObj = visual84}85}86if ((curLineObj.widgets && curLineObj.widgets.length) ||87(wrapping && curLineObj.height > singleLineH))88return cm.charCoords(pos, "local")[top ? "top" : "bottom"];89var topY = cm.heightAtLine(curLineObj, "local");90return topY + (top ? 0 : curLineObj.height);91}92
93var lastLine = cm.lastLine()94if (cm.display.barWidth) for (var i = 0, nextTop; i < anns.length; i++) {95var ann = anns[i];96if (ann.to.line > lastLine) continue;97var top = nextTop || getY(ann.from, true) * hScale;98var bottom = getY(ann.to, false) * hScale;99while (i < anns.length - 1) {100if (anns[i + 1].to.line > lastLine) break;101nextTop = getY(anns[i + 1].from, true) * hScale;102if (nextTop > bottom + .9) break;103ann = anns[++i];104bottom = getY(ann.to, false) * hScale;105}106if (bottom == top) continue;107var height = Math.max(bottom - top, 3);108
109var elt = frag.appendChild(document.createElement("div"));110elt.style.cssText = "position: absolute; right: 0px; width: " + Math.max(cm.display.barWidth - 1, 2) + "px; top: "111+ (top + this.buttonHeight) + "px; height: " + height + "px";112elt.className = this.options.className;113if (ann.id) {114elt.setAttribute("annotation-id", ann.id);115}116}117this.div.textContent = "";118this.div.appendChild(frag);119};120
121Annotation.prototype.clear = function() {122this.cm.off("refresh", this.resizeHandler);123this.cm.off("markerAdded", this.resizeHandler);124this.cm.off("markerCleared", this.resizeHandler);125if (this.changeHandler) this.cm.off("changes", this.changeHandler);126this.div.parentNode.removeChild(this.div);127};128});129