c43fba7cd7f74b3c7dd0c4c5c50184300a367e353f019f80df90f26815512f72647d9ee0c44ae7959199f2d199041b466d3d26fb24e4432ea08a66c5e35f0c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import { Pos } from "../line/pos.js"
  2. import { cursorCoords, displayHeight, displayWidth, estimateCoords, paddingTop, paddingVert, scrollGap, textHeight } from "../measurement/position_measurement.js"
  3. import { gecko, phantom } from "../util/browser.js"
  4. import { elt } from "../util/dom.js"
  5. import { signalDOMEvent } from "../util/event.js"
  6. import { startWorker } from "./highlight_worker.js"
  7. import { alignHorizontally } from "./line_numbers.js"
  8. import { updateDisplaySimple } from "./update_display.js"
  9. // SCROLLING THINGS INTO VIEW
  10. // If an editor sits on the top or bottom of the window, partially
  11. // scrolled out of view, this ensures that the cursor is visible.
  12. export function maybeScrollWindow(cm, rect) {
  13. if (signalDOMEvent(cm, "scrollCursorIntoView")) return
  14. let display = cm.display, box = display.sizer.getBoundingClientRect(), doScroll = null
  15. let doc = display.wrapper.ownerDocument
  16. if (rect.top + box.top < 0) doScroll = true
  17. else if (rect.bottom + box.top > (doc.defaultView.innerHeight || doc.documentElement.clientHeight)) doScroll = false
  18. if (doScroll != null && !phantom) {
  19. let scrollNode = elt("div", "\u200b", null, `position: absolute;
  20. top: ${rect.top - display.viewOffset - paddingTop(cm.display)}px;
  21. height: ${rect.bottom - rect.top + scrollGap(cm) + display.barHeight}px;
  22. left: ${rect.left}px; width: ${Math.max(2, rect.right - rect.left)}px;`)
  23. cm.display.lineSpace.appendChild(scrollNode)
  24. scrollNode.scrollIntoView(doScroll)
  25. cm.display.lineSpace.removeChild(scrollNode)
  26. }
  27. }
  28. // Scroll a given position into view (immediately), verifying that
  29. // it actually became visible (as line heights are accurately
  30. // measured, the position of something may 'drift' during drawing).
  31. export function scrollPosIntoView(cm, pos, end, margin) {
  32. if (margin == null) margin = 0
  33. let rect
  34. if (!cm.options.lineWrapping && pos == end) {
  35. // Set pos and end to the cursor positions around the character pos sticks to
  36. // If pos.sticky == "before", that is around pos.ch - 1, otherwise around pos.ch
  37. // If pos == Pos(_, 0, "before"), pos and end are unchanged
  38. end = pos.sticky == "before" ? Pos(pos.line, pos.ch + 1, "before") : pos
  39. pos = pos.ch ? Pos(pos.line, pos.sticky == "before" ? pos.ch - 1 : pos.ch, "after") : pos
  40. }
  41. for (let limit = 0; limit < 5; limit++) {
  42. let changed = false
  43. let coords = cursorCoords(cm, pos)
  44. let endCoords = !end || end == pos ? coords : cursorCoords(cm, end)
  45. rect = {left: Math.min(coords.left, endCoords.left),
  46. top: Math.min(coords.top, endCoords.top) - margin,
  47. right: Math.max(coords.left, endCoords.left),
  48. bottom: Math.max(coords.bottom, endCoords.bottom) + margin}
  49. let scrollPos = calculateScrollPos(cm, rect)
  50. let startTop = cm.doc.scrollTop, startLeft = cm.doc.scrollLeft
  51. if (scrollPos.scrollTop != null) {
  52. updateScrollTop(cm, scrollPos.scrollTop)
  53. if (Math.abs(cm.doc.scrollTop - startTop) > 1) changed = true
  54. }
  55. if (scrollPos.scrollLeft != null) {
  56. setScrollLeft(cm, scrollPos.scrollLeft)
  57. if (Math.abs(cm.doc.scrollLeft - startLeft) > 1) changed = true
  58. }
  59. if (!changed) break
  60. }
  61. return rect
  62. }
  63. // Scroll a given set of coordinates into view (immediately).
  64. export function scrollIntoView(cm, rect) {
  65. let scrollPos = calculateScrollPos(cm, rect)
  66. if (scrollPos.scrollTop != null) updateScrollTop(cm, scrollPos.scrollTop)
  67. if (scrollPos.scrollLeft != null) setScrollLeft(cm, scrollPos.scrollLeft)
  68. }
  69. // Calculate a new scroll position needed to scroll the given
  70. // rectangle into view. Returns an object with scrollTop and
  71. // scrollLeft properties. When these are undefined, the
  72. // vertical/horizontal position does not need to be adjusted.
  73. function calculateScrollPos(cm, rect) {
  74. let display = cm.display, snapMargin = textHeight(cm.display)
  75. if (rect.top < 0) rect.top = 0
  76. let screentop = cm.curOp && cm.curOp.scrollTop != null ? cm.curOp.scrollTop : display.scroller.scrollTop
  77. let screen = displayHeight(cm), result = {}
  78. if (rect.bottom - rect.top > screen) rect.bottom = rect.top + screen
  79. let docBottom = cm.doc.height + paddingVert(display)
  80. let atTop = rect.top < snapMargin, atBottom = rect.bottom > docBottom - snapMargin
  81. if (rect.top < screentop) {
  82. result.scrollTop = atTop ? 0 : rect.top
  83. } else if (rect.bottom > screentop + screen) {
  84. let newTop = Math.min(rect.top, (atBottom ? docBottom : rect.bottom) - screen)
  85. if (newTop != screentop) result.scrollTop = newTop
  86. }
  87. let gutterSpace = cm.options.fixedGutter ? 0 : display.gutters.offsetWidth
  88. let screenleft = cm.curOp && cm.curOp.scrollLeft != null ? cm.curOp.scrollLeft : display.scroller.scrollLeft - gutterSpace
  89. let screenw = displayWidth(cm) - display.gutters.offsetWidth
  90. let tooWide = rect.right - rect.left > screenw
  91. if (tooWide) rect.right = rect.left + screenw
  92. if (rect.left < 10)
  93. result.scrollLeft = 0
  94. else if (rect.left < screenleft)
  95. result.scrollLeft = Math.max(0, rect.left + gutterSpace - (tooWide ? 0 : 10))
  96. else if (rect.right > screenw + screenleft - 3)
  97. result.scrollLeft = rect.right + (tooWide ? 0 : 10) - screenw
  98. return result
  99. }
  100. // Store a relative adjustment to the scroll position in the current
  101. // operation (to be applied when the operation finishes).
  102. export function addToScrollTop(cm, top) {
  103. if (top == null) return
  104. resolveScrollToPos(cm)
  105. cm.curOp.scrollTop = (cm.curOp.scrollTop == null ? cm.doc.scrollTop : cm.curOp.scrollTop) + top
  106. }
  107. // Make sure that at the end of the operation the current cursor is
  108. // shown.
  109. export function ensureCursorVisible(cm) {
  110. resolveScrollToPos(cm)
  111. let cur = cm.getCursor()
  112. cm.curOp.scrollToPos = {from: cur, to: cur, margin: cm.options.cursorScrollMargin}
  113. }
  114. export function scrollToCoords(cm, x, y) {
  115. if (x != null || y != null) resolveScrollToPos(cm)
  116. if (x != null) cm.curOp.scrollLeft = x
  117. if (y != null) cm.curOp.scrollTop = y
  118. }
  119. export function scrollToRange(cm, range) {
  120. resolveScrollToPos(cm)
  121. cm.curOp.scrollToPos = range
  122. }
  123. // When an operation has its scrollToPos property set, and another
  124. // scroll action is applied before the end of the operation, this
  125. // 'simulates' scrolling that position into view in a cheap way, so
  126. // that the effect of intermediate scroll commands is not ignored.
  127. function resolveScrollToPos(cm) {
  128. let range = cm.curOp.scrollToPos
  129. if (range) {
  130. cm.curOp.scrollToPos = null
  131. let from = estimateCoords(cm, range.from), to = estimateCoords(cm, range.to)
  132. scrollToCoordsRange(cm, from, to, range.margin)
  133. }
  134. }
  135. export function scrollToCoordsRange(cm, from, to, margin) {
  136. let sPos = calculateScrollPos(cm, {
  137. left: Math.min(from.left, to.left),
  138. top: Math.min(from.top, to.top) - margin,
  139. right: Math.max(from.right, to.right),
  140. bottom: Math.max(from.bottom, to.bottom) + margin
  141. })
  142. scrollToCoords(cm, sPos.scrollLeft, sPos.scrollTop)
  143. }
  144. // Sync the scrollable area and scrollbars, ensure the viewport
  145. // covers the visible area.
  146. export function updateScrollTop(cm, val) {
  147. if (Math.abs(cm.doc.scrollTop - val) < 2) return
  148. if (!gecko) updateDisplaySimple(cm, {top: val})
  149. setScrollTop(cm, val, true)
  150. if (gecko) updateDisplaySimple(cm)
  151. startWorker(cm, 100)
  152. }
  153. export function setScrollTop(cm, val, forceScroll) {
  154. val = Math.max(0, Math.min(cm.display.scroller.scrollHeight - cm.display.scroller.clientHeight, val))
  155. if (cm.display.scroller.scrollTop == val && !forceScroll) return
  156. cm.doc.scrollTop = val
  157. cm.display.scrollbars.setScrollTop(val)
  158. if (cm.display.scroller.scrollTop != val) cm.display.scroller.scrollTop = val
  159. }
  160. // Sync scroller and scrollbar, ensure the gutter elements are
  161. // aligned.
  162. export function setScrollLeft(cm, val, isScroller, forceScroll) {
  163. val = Math.max(0, Math.min(val, cm.display.scroller.scrollWidth - cm.display.scroller.clientWidth))
  164. if ((isScroller ? val == cm.doc.scrollLeft : Math.abs(cm.doc.scrollLeft - val) < 2) && !forceScroll) return
  165. cm.doc.scrollLeft = val
  166. alignHorizontally(cm)
  167. if (cm.display.scroller.scrollLeft != val) cm.display.scroller.scrollLeft = val
  168. cm.display.scrollbars.setScrollLeft(val)
  169. }