-
Notifications
You must be signed in to change notification settings - Fork 369
/
Copy pathHorizontallyScrollableArea.tsx
323 lines (269 loc) · 9.62 KB
/
HorizontallyScrollableArea.tsx
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
import type {SequenceEditorPanelLayout} from '@theatre/studio/panels/SequenceEditorPanel/layout/layout'
import useDrag from '@theatre/studio/uiComponents/useDrag'
import useRefAndState from '@theatre/studio/utils/useRefAndState'
import {usePrism} from '@theatre/react'
import type {Pointer} from '@theatre/dataverse'
import {prism, val} from '@theatre/dataverse'
import {clamp} from 'lodash-es'
import React, {useLayoutEffect, useMemo} from 'react'
import styled from 'styled-components'
import {useReceiveVerticalWheelEvent} from '@theatre/studio/panels/SequenceEditorPanel/VerticalScrollContainer'
import {pointerEventsAutoInNormalMode} from '@theatre/studio/css'
import {useCssCursorLock} from '@theatre/studio/uiComponents/PointerEventsHandler'
import type {IRange} from '@theatre/core/types/public'
import DopeSnap from '@theatre/studio/panels/SequenceEditorPanel/RightOverlay/DopeSnap'
import {snapToAll, snapToNone} from './KeyframeSnapTarget'
const Container = styled.div`
position: absolute;
right: 0;
overflow-x: scroll;
overflow-y: hidden;
${pointerEventsAutoInNormalMode};
// hide the scrollbar on Gecko
scrollbar-width: none;
// hide the scrollbar on Webkit/Blink
&::-webkit-scrollbar {
display: none;
}
`
const HorizontallyScrollableArea: React.FC<{
layoutP: Pointer<SequenceEditorPanelLayout>
height: number
children: React.ReactNode
}> = React.memo(({layoutP, children, height}) => {
const {width, unitSpaceToScaledSpaceMultiplier} = usePrism(
() => ({
width: val(layoutP.rightDims.width),
unitSpaceToScaledSpaceMultiplier: val(layoutP.scaledSpace.fromUnitSpace)(
1,
),
}),
[layoutP],
)
const [containerRef, containerNode] = useRefAndState<HTMLDivElement | null>(
null,
)
useHandlePanAndZoom(layoutP, containerNode)
useDragPlayheadHandlers(layoutP, containerNode)
useUpdateScrollFromClippedSpaceRange(layoutP, containerNode)
return (
<Container
ref={containerRef}
style={{
width: width + 'px',
height: height + 'px',
// @ts-expect-error
'--unitSpaceToScaledSpaceMultiplier': unitSpaceToScaledSpaceMultiplier,
}}
>
{children}
</Container>
)
})
export default HorizontallyScrollableArea
function useDragPlayheadHandlers(
layoutP: Pointer<SequenceEditorPanelLayout>,
containerEl: HTMLDivElement | null,
) {
const handlers = useMemo((): Parameters<typeof useDrag>[1] => {
return {
debugName: 'HorizontallyScrollableArea',
onDragStart(event) {
if (event.target instanceof HTMLInputElement) {
// editing some value
return false
}
if (event.shiftKey || event.altKey || event.ctrlKey || event.metaKey) {
// e.g. marquee selection has shiftKey
return false
}
if (
event
.composedPath()
.some((el) => el instanceof HTMLElement && el.draggable === true)
) {
// Question: I think to check if we want another descendent element
// to be able to take control of this drag event.
// Question: e.g. for `useDragKeyframe`?
return false
}
const initialPositionInClippedSpace =
event.clientX - containerEl!.getBoundingClientRect().left
const initialPositionInUnitSpace = clamp(
val(layoutP.clippedSpace.toUnitSpace)(initialPositionInClippedSpace),
0,
Infinity,
)
const setIsSeeking = val(layoutP.seeker.setIsSeeking)
const sequence = val(layoutP.sheet).getSequence()
sequence.position = initialPositionInUnitSpace
const posBeforeSeek = initialPositionInUnitSpace
const scaledSpaceToUnitSpace = val(layoutP.scaledSpace.toUnitSpace)
setIsSeeking(true)
snapToAll()
return {
onDrag(dx: number, _, event) {
const deltaPos = scaledSpaceToUnitSpace(dx)
const unsnappedPos = clamp(
posBeforeSeek + deltaPos,
0,
sequence.length,
)
let newPosition = unsnappedPos
const snapPos = DopeSnap.checkIfMouseEventSnapToPos(event, {})
if (snapPos != null) {
newPosition = snapPos
}
sequence.position = newPosition
},
onDragEnd() {
setIsSeeking(false)
snapToNone()
},
}
},
}
}, [layoutP, containerEl])
const [isDragging] = useDrag(containerEl, handlers)
useCssCursorLock(isDragging, 'draggingPositionInSequenceEditor', 'ew-resize')
}
function useHandlePanAndZoom(
layoutP: Pointer<SequenceEditorPanelLayout>,
node: HTMLDivElement | null,
) {
const receiveVerticalWheelEvent = useReceiveVerticalWheelEvent()
useLayoutEffect(() => {
if (!node) return
const receiveWheelEvent = (event: WheelEvent) => {
// pinch
if (event.ctrlKey) {
event.preventDefault()
event.stopPropagation()
const pivotPointInClippedSpace =
event.clientX - node.getBoundingClientRect().left
const pivotPointInUnitSpace = val(layoutP.clippedSpace.toUnitSpace)(
pivotPointInClippedSpace,
)
const oldRange = val(layoutP.clippedSpace.range)
const delta = normalize(event.deltaY, [-50, 50])
const scaleFactor = 1 + delta * 0.03
const newRange = oldRange.map((originalPos) => {
return (
(originalPos - pivotPointInUnitSpace) * scaleFactor +
pivotPointInUnitSpace
)
}) as IRange
// Set maximum scroll points based on the sequence length.
// This is to avoid zooming out to infinity.
const sequenceLength = val(layoutP.sheet).getSequence().length
const maxEnd = sequenceLength + sequenceLength * 0.25
val(layoutP.clippedSpace.setRange)(
normalizeRange(newRange, [0, maxEnd]),
)
return
}
// panning
else if (event.shiftKey) {
event.preventDefault()
event.stopPropagation()
const sequenceLength = val(layoutP.sheet).getSequence().length
const oldRange = val(layoutP.clippedSpace.range)
const windowSize = oldRange[1] - oldRange[0]
const speed = windowSize / sequenceLength
// if there's no deltaY, the browser is probably assigning to deltaX because of the shiftKey
// it appeared that Safari + Chrome continue to use deltaY with shiftKey, while FF on macOS
// updates the deltaX with deltaY unchanged.
// this is a little awkward with track pads + shift on macOS FF, but that's not a big deal
// since scrolling horizontally with macOS track pads is not necessary to hold shift.
const delta = normalize(event.deltaY || event.deltaX, [-50, 50])
const scaleFactor = delta * 0.05 * speed
const newRange = oldRange.map(
(originalPos) => originalPos + scaleFactor,
) as IRange
val(layoutP.clippedSpace.setRange)(newRange)
return
} else {
receiveVerticalWheelEvent(event)
event.preventDefault()
event.stopPropagation()
const scaledSpaceToUnitSpace = val(layoutP.scaledSpace.toUnitSpace)
const deltaPos = scaledSpaceToUnitSpace(event.deltaX * 1)
const oldRange = val(layoutP.clippedSpace.range)
const newRange = oldRange.map((p) => p + deltaPos) as IRange
const setRange = val(layoutP.clippedSpace.setRange)
setRange(newRange)
return
}
}
const listenerOptions = {
capture: true,
passive: false,
}
node.addEventListener('wheel', receiveWheelEvent, listenerOptions)
return () => {
node.removeEventListener('wheel', receiveWheelEvent, listenerOptions)
}
}, [node, layoutP])
useDrag(
node,
useMemo<Parameters<typeof useDrag>[1]>(() => {
return {
onDragStart(e) {
const oldRange = val(layoutP.clippedSpace.range)
const setRange = val(layoutP.clippedSpace.setRange)
const scaledSpaceToUnitSpace = val(layoutP.scaledSpace.toUnitSpace)
e.preventDefault()
e.stopPropagation()
return {
onDrag(dx, dy, _, __, deltaYFromLastEvent) {
receiveVerticalWheelEvent({deltaY: -deltaYFromLastEvent})
const delta = -scaledSpaceToUnitSpace(dx)
const newRange = oldRange.map(
(originalPos) => originalPos + delta,
) as IRange
setRange(newRange)
},
}
},
debugName: 'HorizontallyScrollableArea Middle Button Drag',
buttons: [1],
lockCSSCursorTo: 'grabbing',
}
}, [layoutP]),
)
}
function normalize(value: number, [min, max]: [min: number, max: number]) {
return Math.max(Math.min(value, max), min)
}
function normalizeRange(
range: IRange,
minMax: [min: number, max: number],
): IRange {
return [normalize(range[0], minMax), normalize(range[1], minMax)]
}
function useUpdateScrollFromClippedSpaceRange(
layoutP: Pointer<SequenceEditorPanelLayout>,
node: HTMLDivElement | null,
) {
useLayoutEffect(() => {
if (!node) return
const d = prism(() => {
const range = val(layoutP.clippedSpace.range)
const rangeStartInScaledSpace = val(layoutP.scaledSpace.fromUnitSpace)(
range[0],
)
return rangeStartInScaledSpace
})
const update = () => {
const rangeStartInScaledSpace = d.getValue()
node.scrollLeft = rangeStartInScaledSpace
}
const untap = d.onStale(update)
update()
const timeout = setTimeout(update, 100)
return () => {
clearTimeout(timeout)
untap()
}
}, [layoutP, node])
}