本站源代码
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.

123 lines
4.3KB

  1. // CodeMirror, copyright (c) by Marijn Haverbeke and others
  2. // Distributed under an MIT license: https://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("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(curLine);
  68. }
  69. if ((curLineObj.widgets && curLineObj.widgets.length) ||
  70. (wrapping && curLineObj.height > singleLineH))
  71. return cm.charCoords(pos, "local")[top ? "top" : "bottom"];
  72. var topY = cm.heightAtLine(curLineObj, "local");
  73. return topY + (top ? 0 : curLineObj.height);
  74. }
  75. var lastLine = cm.lastLine()
  76. if (cm.display.barWidth) for (var i = 0, nextTop; i < anns.length; i++) {
  77. var ann = anns[i];
  78. if (ann.to.line > lastLine) continue;
  79. var top = nextTop || getY(ann.from, true) * hScale;
  80. var bottom = getY(ann.to, false) * hScale;
  81. while (i < anns.length - 1) {
  82. if (anns[i + 1].to.line > lastLine) break;
  83. nextTop = getY(anns[i + 1].from, true) * hScale;
  84. if (nextTop > bottom + .9) break;
  85. ann = anns[++i];
  86. bottom = getY(ann.to, false) * hScale;
  87. }
  88. if (bottom == top) continue;
  89. var height = Math.max(bottom - top, 3);
  90. var elt = frag.appendChild(document.createElement("div"));
  91. elt.style.cssText = "position: absolute; right: 0px; width: " + Math.max(cm.display.barWidth - 1, 2) + "px; top: "
  92. + (top + this.buttonHeight) + "px; height: " + height + "px";
  93. elt.className = this.options.className;
  94. if (ann.id) {
  95. elt.setAttribute("annotation-id", ann.id);
  96. }
  97. }
  98. this.div.textContent = "";
  99. this.div.appendChild(frag);
  100. };
  101. Annotation.prototype.clear = function() {
  102. this.cm.off("refresh", this.resizeHandler);
  103. this.cm.off("markerAdded", this.resizeHandler);
  104. this.cm.off("markerCleared", this.resizeHandler);
  105. if (this.changeHandler) this.cm.off("changes", this.changeHandler);
  106. this.div.parentNode.removeChild(this.div);
  107. };
  108. });
上海开阖软件有限公司 沪ICP备12045867号-1