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