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.

100 lines
3.6 KiB

пре 3 година
  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: http://codemirror.net/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("change", 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.heightAtLine(cm.lastLine() + 1, "local");
  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. if (cm.display.barWidth) for (var i = 0, nextTop; i < anns.length; i++) {
  62. var ann = anns[i];
  63. var top = nextTop || cm.charCoords(ann.from, "local").top * hScale;
  64. var bottom = cm.charCoords(ann.to, "local").bottom * hScale;
  65. while (i < anns.length - 1) {
  66. nextTop = cm.charCoords(anns[i + 1].from, "local").top * hScale;
  67. if (nextTop > bottom + .9) break;
  68. ann = anns[++i];
  69. bottom = cm.charCoords(ann.to, "local").bottom * hScale;
  70. }
  71. if (bottom == top) continue;
  72. var height = Math.max(bottom - top, 3);
  73. var elt = frag.appendChild(document.createElement("div"));
  74. elt.style.cssText = "position: absolute; right: 0px; width: " + Math.max(cm.display.barWidth - 1, 2) + "px; top: "
  75. + (top + this.buttonHeight) + "px; height: " + height + "px";
  76. elt.className = this.options.className;
  77. }
  78. this.div.textContent = "";
  79. this.div.appendChild(frag);
  80. };
  81. Annotation.prototype.clear = function() {
  82. this.cm.off("refresh", this.resizeHandler);
  83. this.cm.off("markerAdded", this.resizeHandler);
  84. this.cm.off("markerCleared", this.resizeHandler);
  85. if (this.changeHandler) this.cm.off("change", this.changeHandler);
  86. this.div.parentNode.removeChild(this.div);
  87. };
  88. });