comparison .cms/lib/codemirror/src/display/scrolling.js @ 0:78edf6b517a0 draft

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