comparison .cms/lib/codemirror/addon/scroll/annotatescrollbar.js @ 0:78edf6b517a0 draft

24.10
author Coffee CMS <info@coffee-cms.ru>
date Fri, 11 Oct 2024 22:40:23 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:78edf6b517a0
1 // CodeMirror, copyright (c) by Marijn Haverbeke and others
2 // Distributed under an MIT license: https://codemirror.net/5/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("changes", 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
76 function getY(pos, top) {
77 if (curLine != pos.line) {
78 curLine = pos.line
79 curLineObj = cm.getLineHandle(pos.line)
80 var visual = cm.getLineHandleVisualStart(curLineObj)
81 if (visual != curLineObj) {
82 curLine = cm.getLineNumber(visual)
83 curLineObj = visual
84 }
85 }
86 if ((curLineObj.widgets && curLineObj.widgets.length) ||
87 (wrapping && curLineObj.height > singleLineH))
88 return cm.charCoords(pos, "local")[top ? "top" : "bottom"];
89 var topY = cm.heightAtLine(curLineObj, "local");
90 return topY + (top ? 0 : curLineObj.height);
91 }
92
93 var lastLine = cm.lastLine()
94 if (cm.display.barWidth) for (var i = 0, nextTop; i < anns.length; i++) {
95 var ann = anns[i];
96 if (ann.to.line > lastLine) continue;
97 var top = nextTop || getY(ann.from, true) * hScale;
98 var bottom = getY(ann.to, false) * hScale;
99 while (i < anns.length - 1) {
100 if (anns[i + 1].to.line > lastLine) break;
101 nextTop = getY(anns[i + 1].from, true) * hScale;
102 if (nextTop > bottom + .9) break;
103 ann = anns[++i];
104 bottom = getY(ann.to, false) * hScale;
105 }
106 if (bottom == top) continue;
107 var height = Math.max(bottom - top, 3);
108
109 var elt = frag.appendChild(document.createElement("div"));
110 elt.style.cssText = "position: absolute; right: 0px; width: " + Math.max(cm.display.barWidth - 1, 2) + "px; top: "
111 + (top + this.buttonHeight) + "px; height: " + height + "px";
112 elt.className = this.options.className;
113 if (ann.id) {
114 elt.setAttribute("annotation-id", ann.id);
115 }
116 }
117 this.div.textContent = "";
118 this.div.appendChild(frag);
119 };
120
121 Annotation.prototype.clear = function() {
122 this.cm.off("refresh", this.resizeHandler);
123 this.cm.off("markerAdded", this.resizeHandler);
124 this.cm.off("markerCleared", this.resizeHandler);
125 if (this.changeHandler) this.cm.off("changes", this.changeHandler);
126 this.div.parentNode.removeChild(this.div);
127 };
128 });