scrollbars.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import { addClass, elt, rmClass } from "../util/dom.js"
  2. import { on } from "../util/event.js"
  3. import { scrollGap, paddingVert } from "../measurement/position_measurement.js"
  4. import { ie, ie_version, mac, mac_geMountainLion } from "../util/browser.js"
  5. import { updateHeightsInViewport } from "./update_lines.js"
  6. import { Delayed } from "../util/misc.js"
  7. import { setScrollLeft, updateScrollTop } from "./scrolling.js"
  8. // SCROLLBARS
  9. // Prepare DOM reads needed to update the scrollbars. Done in one
  10. // shot to minimize update/measure roundtrips.
  11. export function measureForScrollbars(cm) {
  12. let d = cm.display, gutterW = d.gutters.offsetWidth
  13. let docH = Math.round(cm.doc.height + paddingVert(cm.display))
  14. return {
  15. clientHeight: d.scroller.clientHeight,
  16. viewHeight: d.wrapper.clientHeight,
  17. scrollWidth: d.scroller.scrollWidth, clientWidth: d.scroller.clientWidth,
  18. viewWidth: d.wrapper.clientWidth,
  19. barLeft: cm.options.fixedGutter ? gutterW : 0,
  20. docHeight: docH,
  21. scrollHeight: docH + scrollGap(cm) + d.barHeight,
  22. nativeBarWidth: d.nativeBarWidth,
  23. gutterWidth: gutterW
  24. }
  25. }
  26. class NativeScrollbars {
  27. constructor(place, scroll, cm) {
  28. this.cm = cm
  29. let vert = this.vert = elt("div", [elt("div", null, null, "min-width: 1px")], "CodeMirror-vscrollbar")
  30. let horiz = this.horiz = elt("div", [elt("div", null, null, "height: 100%; min-height: 1px")], "CodeMirror-hscrollbar")
  31. vert.tabIndex = horiz.tabIndex = -1
  32. place(vert); place(horiz)
  33. on(vert, "scroll", () => {
  34. if (vert.clientHeight) scroll(vert.scrollTop, "vertical")
  35. })
  36. on(horiz, "scroll", () => {
  37. if (horiz.clientWidth) scroll(horiz.scrollLeft, "horizontal")
  38. })
  39. this.checkedZeroWidth = false
  40. // Need to set a minimum width to see the scrollbar on IE7 (but must not set it on IE8).
  41. if (ie && ie_version < 8) this.horiz.style.minHeight = this.vert.style.minWidth = "18px"
  42. }
  43. update(measure) {
  44. let needsH = measure.scrollWidth > measure.clientWidth + 1
  45. let needsV = measure.scrollHeight > measure.clientHeight + 1
  46. let sWidth = measure.nativeBarWidth
  47. if (needsV) {
  48. this.vert.style.display = "block"
  49. this.vert.style.bottom = needsH ? sWidth + "px" : "0"
  50. let totalHeight = measure.viewHeight - (needsH ? sWidth : 0)
  51. // A bug in IE8 can cause this value to be negative, so guard it.
  52. this.vert.firstChild.style.height =
  53. Math.max(0, measure.scrollHeight - measure.clientHeight + totalHeight) + "px"
  54. } else {
  55. this.vert.style.display = ""
  56. this.vert.firstChild.style.height = "0"
  57. }
  58. if (needsH) {
  59. this.horiz.style.display = "block"
  60. this.horiz.style.right = needsV ? sWidth + "px" : "0"
  61. this.horiz.style.left = measure.barLeft + "px"
  62. let totalWidth = measure.viewWidth - measure.barLeft - (needsV ? sWidth : 0)
  63. this.horiz.firstChild.style.width =
  64. Math.max(0, measure.scrollWidth - measure.clientWidth + totalWidth) + "px"
  65. } else {
  66. this.horiz.style.display = ""
  67. this.horiz.firstChild.style.width = "0"
  68. }
  69. if (!this.checkedZeroWidth && measure.clientHeight > 0) {
  70. if (sWidth == 0) this.zeroWidthHack()
  71. this.checkedZeroWidth = true
  72. }
  73. return {right: needsV ? sWidth : 0, bottom: needsH ? sWidth : 0}
  74. }
  75. setScrollLeft(pos) {
  76. if (this.horiz.scrollLeft != pos) this.horiz.scrollLeft = pos
  77. if (this.disableHoriz) this.enableZeroWidthBar(this.horiz, this.disableHoriz, "horiz")
  78. }
  79. setScrollTop(pos) {
  80. if (this.vert.scrollTop != pos) this.vert.scrollTop = pos
  81. if (this.disableVert) this.enableZeroWidthBar(this.vert, this.disableVert, "vert")
  82. }
  83. zeroWidthHack() {
  84. let w = mac && !mac_geMountainLion ? "12px" : "18px"
  85. this.horiz.style.height = this.vert.style.width = w
  86. this.horiz.style.pointerEvents = this.vert.style.pointerEvents = "none"
  87. this.disableHoriz = new Delayed
  88. this.disableVert = new Delayed
  89. }
  90. enableZeroWidthBar(bar, delay, type) {
  91. bar.style.pointerEvents = "auto"
  92. function maybeDisable() {
  93. // To find out whether the scrollbar is still visible, we
  94. // check whether the element under the pixel in the bottom
  95. // right corner of the scrollbar box is the scrollbar box
  96. // itself (when the bar is still visible) or its filler child
  97. // (when the bar is hidden). If it is still visible, we keep
  98. // it enabled, if it's hidden, we disable pointer events.
  99. let box = bar.getBoundingClientRect()
  100. let elt = type == "vert" ? document.elementFromPoint(box.right - 1, (box.top + box.bottom) / 2)
  101. : document.elementFromPoint((box.right + box.left) / 2, box.bottom - 1)
  102. if (elt != bar) bar.style.pointerEvents = "none"
  103. else delay.set(1000, maybeDisable)
  104. }
  105. delay.set(1000, maybeDisable)
  106. }
  107. clear() {
  108. let parent = this.horiz.parentNode
  109. parent.removeChild(this.horiz)
  110. parent.removeChild(this.vert)
  111. }
  112. }
  113. class NullScrollbars {
  114. update() { return {bottom: 0, right: 0} }
  115. setScrollLeft() {}
  116. setScrollTop() {}
  117. clear() {}
  118. }
  119. export function updateScrollbars(cm, measure) {
  120. if (!measure) measure = measureForScrollbars(cm)
  121. let startWidth = cm.display.barWidth, startHeight = cm.display.barHeight
  122. updateScrollbarsInner(cm, measure)
  123. for (let i = 0; i < 4 && startWidth != cm.display.barWidth || startHeight != cm.display.barHeight; i++) {
  124. if (startWidth != cm.display.barWidth && cm.options.lineWrapping)
  125. updateHeightsInViewport(cm)
  126. updateScrollbarsInner(cm, measureForScrollbars(cm))
  127. startWidth = cm.display.barWidth; startHeight = cm.display.barHeight
  128. }
  129. }
  130. // Re-synchronize the fake scrollbars with the actual size of the
  131. // content.
  132. function updateScrollbarsInner(cm, measure) {
  133. let d = cm.display
  134. let sizes = d.scrollbars.update(measure)
  135. d.sizer.style.paddingRight = (d.barWidth = sizes.right) + "px"
  136. d.sizer.style.paddingBottom = (d.barHeight = sizes.bottom) + "px"
  137. d.heightForcer.style.borderBottom = sizes.bottom + "px solid transparent"
  138. if (sizes.right && sizes.bottom) {
  139. d.scrollbarFiller.style.display = "block"
  140. d.scrollbarFiller.style.height = sizes.bottom + "px"
  141. d.scrollbarFiller.style.width = sizes.right + "px"
  142. } else d.scrollbarFiller.style.display = ""
  143. if (sizes.bottom && cm.options.coverGutterNextToScrollbar && cm.options.fixedGutter) {
  144. d.gutterFiller.style.display = "block"
  145. d.gutterFiller.style.height = sizes.bottom + "px"
  146. d.gutterFiller.style.width = measure.gutterWidth + "px"
  147. } else d.gutterFiller.style.display = ""
  148. }
  149. export let scrollbarModel = {"native": NativeScrollbars, "null": NullScrollbars}
  150. export function initScrollbars(cm) {
  151. if (cm.display.scrollbars) {
  152. cm.display.scrollbars.clear()
  153. if (cm.display.scrollbars.addClass)
  154. rmClass(cm.display.wrapper, cm.display.scrollbars.addClass)
  155. }
  156. cm.display.scrollbars = new scrollbarModel[cm.options.scrollbarStyle](node => {
  157. cm.display.wrapper.insertBefore(node, cm.display.scrollbarFiller)
  158. // Prevent clicks in the scrollbars from killing focus
  159. on(node, "mousedown", () => {
  160. if (cm.state.focused) setTimeout(() => cm.display.input.focus(), 0)
  161. })
  162. node.setAttribute("cm-not-content", "true")
  163. }, (pos, axis) => {
  164. if (axis == "horizontal") setScrollLeft(cm, pos)
  165. else updateScrollTop(cm, pos)
  166. }, cm)
  167. if (cm.display.scrollbars.addClass)
  168. addClass(cm.display.wrapper, cm.display.scrollbars.addClass)
  169. }