Debian Codegraph @

packages/interaction/src/dnd/AutoScroller.ts

raw
1 import { ScrollGeomCache } from '../ScrollGeomCache.js'
2 import { ElementScrollGeomCache } from '../ElementScrollGeomCache.js'
3 import { WindowScrollGeomCache } from '../WindowScrollGeomCache.js'
4
5 interface Edge {
6 scrollCache: ScrollGeomCache
7 name: 'top' | 'left' | 'right' | 'bottom'
8 distance: number // how many pixels the current pointer is from the edge
9 }
10
11 // If available we are using native "performance" API instead of "Date"
12 // Read more about it on MDN:
13 // https://developer.mozilla.org/en-US/docs/Web/API/Performance
14 const getTime = typeof performance === 'function' ? (performance as any).now : Date.now
15
16 /*
17 For a pointer interaction, automatically scrolls certain scroll containers when the pointer
18 approaches the edge.
19
20 The caller must call start + handleMove + stop.
21 */
22 export class AutoScroller {
23 // options that can be set by caller
24 isEnabled: boolean = true
25 scrollQuery: (Window | string)[] = [window, '.fc-scroller']
26 edgeThreshold: number = 50 // pixels
27 maxVelocity: number = 300 // pixels per second
28
29 // internal state
30 pointerScreenX: number | null = null
31 pointerScreenY: number | null = null
32 isAnimating: boolean = false
33 scrollCaches: ScrollGeomCache[] | null = null
34 msSinceRequest?: number
35
36 // protect against the initial pointerdown being too close to an edge and starting the scroll
37 everMovedUp: boolean = false
38 everMovedDown: boolean = false
39 everMovedLeft: boolean = false
40 everMovedRight: boolean = false
41
42 start(pageX: number, pageY: number, scrollStartEl: HTMLElement) {
43 if (this.isEnabled) {
44 this.scrollCaches = this.buildCaches(scrollStartEl)
45 this.pointerScreenX = null
46 this.pointerScreenY = null
47 this.everMovedUp = false
48 this.everMovedDown = false
49 this.everMovedLeft = false
50 this.everMovedRight = false
51 this.handleMove(pageX, pageY)
52 }
53 }
54
55 handleMove(pageX: number, pageY: number) {
56 if (this.isEnabled) {
57 let pointerScreenX = pageX - window.scrollX
58 let pointerScreenY = pageY - window.scrollY
59
60 let yDelta = this.pointerScreenY === null ? 0 : pointerScreenY - this.pointerScreenY
61 let xDelta = this.pointerScreenX === null ? 0 : pointerScreenX - this.pointerScreenX
62
63 if (yDelta < 0) {
64 this.everMovedUp = true
65 } else if (yDelta > 0) {
66 this.everMovedDown = true
67 }
68
69 if (xDelta < 0) {
70 this.everMovedLeft = true
71 } else if (xDelta > 0) {
72 this.everMovedRight = true
73 }
74
75 this.pointerScreenX = pointerScreenX
76 this.pointerScreenY = pointerScreenY
77
78 if (!this.isAnimating) {
79 this.isAnimating = true
80 this.requestAnimation(getTime())
81 }
82 }
83 }
84
85 stop() {
86 if (this.isEnabled) {
87 this.isAnimating = false // will stop animation
88
89 for (let scrollCache of this.scrollCaches!) {
90 scrollCache.destroy()
91 }
92
93 this.scrollCaches = null
94 }
95 }
96
97 requestAnimation(now: number) {
98 this.msSinceRequest = now
99 requestAnimationFrame(this.animate)
100 }
101
102 private animate = () => {
103 if (this.isAnimating) { // wasn't cancelled between animation calls
104 let edge = this.computeBestEdge(
105 this.pointerScreenX! + window.scrollX,
106 this.pointerScreenY! + window.scrollY,
107 )
108
109 if (edge) {
110 let now = getTime()
111 this.handleSide(edge, (now - this.msSinceRequest!) / 1000)
112 this.requestAnimation(now)
113 } else {
114 this.isAnimating = false // will stop animation
115 }
116 }
117 }
118
119 private handleSide(edge: Edge, seconds: number) {
120 let { scrollCache } = edge
121 let { edgeThreshold } = this
122 let invDistance = edgeThreshold - edge.distance
123 let velocity = // the closer to the edge, the faster we scroll
124 ((invDistance * invDistance) / (edgeThreshold * edgeThreshold)) * // quadratic
125 this.maxVelocity * seconds
126 let sign = 1
127
128 switch (edge.name) {
129 case 'left':
130 sign = -1
131 // falls through
132 case 'right':
133 scrollCache.setScrollLeft(scrollCache.getScrollLeft() + velocity * sign)
134 break
135
136 case 'top':
137 sign = -1
138 // falls through
139 case 'bottom':
140 scrollCache.setScrollTop(scrollCache.getScrollTop() + velocity * sign)
141 break
142 }
143 }
144
145 // left/top are relative to document topleft
146 private computeBestEdge(left: number, top: number): Edge | null {
147 let { edgeThreshold } = this
148 let bestSide: Edge | null = null
149 let scrollCaches = this.scrollCaches || []
150
151 for (let scrollCache of scrollCaches) {
152 let rect = scrollCache.clientRect
153 let leftDist = left - rect.left
154 let rightDist = rect.right - left
155 let topDist = top - rect.top
156 let bottomDist = rect.bottom - top
157
158 // completely within the rect?
159 if (leftDist >= 0 && rightDist >= 0 && topDist >= 0 && bottomDist >= 0) {
160 if (
161 topDist <= edgeThreshold && this.everMovedUp && scrollCache.canScrollUp() &&
162 (!bestSide || bestSide.distance > topDist)
163 ) {
164 bestSide = { scrollCache, name: 'top', distance: topDist }
165 }
166
167 if (
168 bottomDist <= edgeThreshold && this.everMovedDown && scrollCache.canScrollDown() &&
169 (!bestSide || bestSide.distance > bottomDist)
170 ) {
171 bestSide = { scrollCache, name: 'bottom', distance: bottomDist }
172 }
173
174 /*
175 TODO: fix broken RTL scrolling. canScrollLeft always returning false
176 https://github.com/fullcalendar/fullcalendar/issues/4837
177 */
178 if (
179 leftDist <= edgeThreshold && this.everMovedLeft && scrollCache.canScrollLeft() &&
180 (!bestSide || bestSide.distance > leftDist)
181 ) {
182 bestSide = { scrollCache, name: 'left', distance: leftDist }
183 }
184
185 if (
186 rightDist <= edgeThreshold && this.everMovedRight && scrollCache.canScrollRight() &&
187 (!bestSide || bestSide.distance > rightDist)
188 ) {
189 bestSide = { scrollCache, name: 'right', distance: rightDist }
190 }
191 }
192 }
193
194 return bestSide
195 }
196
197 private buildCaches(scrollStartEl: HTMLElement) {
198 return this.queryScrollEls(scrollStartEl).map((el) => {
199 if (el === window) {
200 return new WindowScrollGeomCache(false) // false = don't listen to user-generated scrolls
201 }
202 return new ElementScrollGeomCache(el, false) // false = don't listen to user-generated scrolls
203 })
204 }
205
206 private queryScrollEls(scrollStartEl: HTMLElement) {
207 let els = []
208
209 for (let query of this.scrollQuery) {
210 if (typeof query === 'object') {
211 els.push(query)
212 } else {
213 /*
214 TODO: in the future, always have auto-scroll happen on element where current Hit came from
215 Ticket: https://github.com/fullcalendar/fullcalendar/issues/4593
216 */
217 els.push(...Array.prototype.slice.call(
218 (scrollStartEl.getRootNode() as ParentNode).querySelectorAll(query),
219 ))
220 }
221 }
222
223 return els
224 }
225 }