-
-
Notifications
You must be signed in to change notification settings - Fork 14.6k
/
scrollbar.ts
283 lines (240 loc) · 8.15 KB
/
scrollbar.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
// @ts-nocheck
import {
computed,
defineComponent,
h,
onBeforeUnmount,
reactive,
ref,
unref,
watch,
withModifiers,
} from 'vue'
import { BAR_MAP } from '@element-plus/components/scrollbar'
import { cAF, rAF } from '@element-plus/utils'
import { useNamespace } from '@element-plus/hooks'
import { HORIZONTAL, SCROLLBAR_MIN_SIZE, ScrollbarDirKey } from '../defaults'
import { virtualizedScrollbarProps } from '../props'
import { renderThumbStyle } from '../utils'
import type { CSSProperties } from 'vue'
const ScrollBar = defineComponent({
name: 'ElVirtualScrollBar',
props: virtualizedScrollbarProps,
emits: ['scroll', 'start-move', 'stop-move'],
setup(props, { emit }) {
const GAP = computed(() => props.startGap + props.endGap) // top 2 + bottom 2 | left 2 + right 2
const nsVirtualScrollbar = useNamespace('virtual-scrollbar')
const nsScrollbar = useNamespace('scrollbar')
// DOM refs
const trackRef = ref<HTMLElement>()
const thumbRef = ref<HTMLElement>()
// local variables
let frameHandle: null | number = null
let onselectstartStore: null | typeof document.onselectstart = null
// data
const state = reactive({
isDragging: false,
traveled: 0,
})
const bar = computed(() => BAR_MAP[props.layout])
const trackSize = computed(() => props.clientSize! - unref(GAP))
const trackStyle = computed<CSSProperties>(() => ({
position: 'absolute',
width: `${
HORIZONTAL === props.layout ? trackSize.value : props.scrollbarSize
}px`,
height: `${
HORIZONTAL === props.layout ? props.scrollbarSize : trackSize.value
}px`,
[ScrollbarDirKey[props.layout]]: '2px',
right: '2px',
bottom: '2px',
borderRadius: '4px',
}))
const thumbSize = computed(() => {
const ratio = props.ratio!
const clientSize = props.clientSize!
if (ratio >= 100) {
return Number.POSITIVE_INFINITY
}
if (ratio >= 50) {
return (ratio * clientSize) / 100
}
const SCROLLBAR_MAX_SIZE = clientSize / 3
return Math.floor(
Math.min(
Math.max(ratio * clientSize, SCROLLBAR_MIN_SIZE),
SCROLLBAR_MAX_SIZE
)
)
})
// const sizeRange = computed(() => props.size - thumbSize.value)
const thumbStyle = computed<CSSProperties>(() => {
if (!Number.isFinite(thumbSize.value)) {
return {
display: 'none',
}
}
const thumb = `${thumbSize.value}px`
const style: CSSProperties = renderThumbStyle(
{
bar: bar.value,
size: thumb,
move: state.traveled,
},
props.layout
)
return style
})
const totalSteps = computed(() =>
Math.floor(props.clientSize! - thumbSize.value - unref(GAP))
)
const attachEvents = () => {
window.addEventListener('mousemove', onMouseMove)
window.addEventListener('mouseup', onMouseUp)
const thumbEl = unref(thumbRef)
if (!thumbEl) return
onselectstartStore = document.onselectstart
document.onselectstart = () => false
thumbEl.addEventListener('touchmove', onMouseMove)
thumbEl.addEventListener('touchend', onMouseUp)
}
const detachEvents = () => {
window.removeEventListener('mousemove', onMouseMove)
window.removeEventListener('mouseup', onMouseUp)
document.onselectstart = onselectstartStore
onselectstartStore = null
const thumbEl = unref(thumbRef)
if (!thumbEl) return
thumbEl.removeEventListener('touchmove', onMouseMove)
thumbEl.removeEventListener('touchend', onMouseUp)
}
const onThumbMouseDown = (e: Event) => {
e.stopImmediatePropagation()
if (
(e as KeyboardEvent).ctrlKey ||
[1, 2].includes((e as MouseEvent).button)
) {
return
}
state.isDragging = true
state[bar.value.axis] =
e.currentTarget![bar.value.offset] -
(e[bar.value.client] -
(e.currentTarget as HTMLElement).getBoundingClientRect()[
bar.value.direction
])
emit('start-move')
attachEvents()
}
const onMouseUp = () => {
state.isDragging = false
state[bar.value.axis] = 0
emit('stop-move')
detachEvents()
}
const onMouseMove = (e: Event) => {
const { isDragging } = state
if (!isDragging) return
if (!thumbRef.value || !trackRef.value) return
const prevPage = state[bar.value.axis]
if (!prevPage) return
cAF(frameHandle!)
// using the current track's offset top/left - the current pointer's clientY/clientX
// to get the relative position of the pointer to the track.
const offset =
(trackRef.value.getBoundingClientRect()[bar.value.direction] -
e[bar.value.client]) *
-1
// find where the thumb was clicked on.
const thumbClickPosition = thumbRef.value[bar.value.offset] - prevPage
/**
* +--------------+ +--------------+
* | - <--------- thumb.offsetTop | |
* | |+| <--+ | |
* | - | | |
* | Content | | | |
* | | | | |
* | | | | |
* | | | | -
* | | +--> | |+|
* | | | -
* +--------------+ +--------------+
*/
// using the current position - prev position to
const distance = offset - thumbClickPosition
// get how many steps in total.
// gap of 2 on top, 2 on bottom, in total 4.
// using totalSteps ÷ totalSize getting each step's size * distance to get the new
// scroll offset to scrollTo
frameHandle = rAF(() => {
state.traveled = Math.max(
props.startGap,
Math.min(
distance,
totalSteps.value // 2 is the top value
)
)
emit('scroll', distance, totalSteps.value)
})
}
const clickTrackHandler = (e: MouseEvent) => {
const offset = Math.abs(
(e.target as HTMLElement).getBoundingClientRect()[bar.value.direction] -
e[bar.value.client]
)
const thumbHalf = thumbRef.value![bar.value.offset] / 2
const distance = offset - thumbHalf
state.traveled = Math.max(0, Math.min(distance, totalSteps.value))
emit('scroll', distance, totalSteps.value)
}
watch(
() => props.scrollFrom,
(v) => {
if (state.isDragging) return
/**
* this is simply mapping the current scrollbar offset
*
* formula 1:
* v = scrollOffset / (estimatedTotalSize - clientSize)
* traveled = v * (clientSize - thumbSize - GAP) --> v * totalSteps
*
* formula 2:
* traveled = (v * clientSize) / (clientSize / totalSteps) --> (v * clientSize) * (totalSteps / clientSize) --> v * totalSteps
*/
state.traveled = Math.ceil(v! * totalSteps.value)
}
)
onBeforeUnmount(() => {
detachEvents()
})
return () => {
return h(
'div',
{
role: 'presentation',
ref: trackRef,
class: [
nsVirtualScrollbar.b(),
props.class,
(props.alwaysOn || state.isDragging) && 'always-on',
],
style: trackStyle.value,
onMousedown: withModifiers(clickTrackHandler, ['stop', 'prevent']),
onTouchstartPrevent: onThumbMouseDown,
},
h(
'div',
{
ref: thumbRef,
class: nsScrollbar.e('thumb'),
style: thumbStyle.value,
onMousedown: onThumbMouseDown,
},
[]
)
)
}
},
})
export default ScrollBar