You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
4.5 KiB

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