Repository navigation
Expand file tree
/
Copy pathscrub-input.mjs
More file actions
284 lines (272 loc) · 10.2 KB
/
Copy pathscrub-input.mjs
File metadata and controls
284 lines (272 loc) · 10.2 KB
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
const DEFAULT_MIN = -Infinity;
const DEFAULT_MAX = Infinity;
const DEFAULT_WHEEL_DETENT = 40;
const DEFAULT_WHEEL_THROTTLE = 32;
const DEFAULT_MAX_DETENTS = 8;
const finite = (value, fallback) => (Number.isFinite(Number(value)) ? Number(value) : fallback);
export function clampScrubValue(value, min = DEFAULT_MIN, max = DEFAULT_MAX) {
const lower = finite(min, DEFAULT_MIN),
upper = finite(max, DEFAULT_MAX);
return Math.min(upper, Math.max(lower, finite(value, lower)));
}
/** Parse the percent field consistently: 125 and 125% both mean 125 percent. */
export function parsePercentValue(value, { min = 0.1, max = 4, fallback = 1 } = {}) {
const raw = String(value ?? '').trim();
if (!raw) return clampScrubValue(fallback, min, max);
const percent = raw.endsWith('%');
const number = Number(percent ? raw.slice(0, -1).trim() : raw);
if (!Number.isFinite(number)) return clampScrubValue(fallback, min, max);
const ratio = number / 100;
return clampScrubValue(ratio, min, max);
}
export function formatPercentValue(value, digits = 0) {
const ratio = finite(value, 1);
return `${(ratio * 100).toFixed(Math.max(0, Math.min(3, Math.trunc(digits))))}%`;
}
export function normalizeWheelPixels(event, viewportHeight = 800) {
if (!event) return 0;
const mode = Number(event.deltaMode) || 0;
const multiplier = mode === 1 ? 16 : mode === 2 ? Math.max(1, finite(viewportHeight, 800)) : 1;
const vertical = finite(event.deltaY, 0),
horizontal = finite(event.deltaX, 0);
return (Math.abs(vertical) >= Math.abs(horizontal) ? vertical : horizontal) * multiplier;
}
/** Consume whole detents while retaining the sub-detent remainder. */
export function consumeDetents(
accumulator,
detent = DEFAULT_WHEEL_DETENT,
maxDetents = DEFAULT_MAX_DETENTS,
) {
const size = Math.max(1, Math.abs(finite(detent, DEFAULT_WHEEL_DETENT)));
const limit = Math.max(1, Math.trunc(finite(maxDetents, DEFAULT_MAX_DETENTS)));
const raw = Number(accumulator) || 0;
const whole = raw >= 0 ? Math.floor(raw / size) : Math.ceil(raw / size);
const detents = Math.max(-limit, Math.min(limit, whole));
return { detents, remainder: raw - detents * size };
}
function defaultRead(input) {
const number = Number(input?.value);
return Number.isFinite(number) ? number : 0;
}
/**
* Attach an Alt-drag / Alt-wheel scrub interaction to a native input.
* Normal pointer, wheel, and keyboard input are left untouched.
*/
export function createScrubInput(input, options = {}) {
if (!input?.addEventListener) throw new TypeError('createScrubInput requires an input element');
const ownerDocument = input.ownerDocument || (typeof document !== 'undefined' ? document : null);
const ownerWindow =
ownerDocument?.defaultView || (typeof window !== 'undefined' ? window : globalThis);
const read = options.getValue || (() => defaultRead(input));
const write =
options.setValue ||
((value) => {
input.value = String(value);
});
const min = options.min ?? DEFAULT_MIN;
const max = options.max ?? DEFAULT_MAX;
const step = Math.max(Number.EPSILON, Math.abs(finite(options.step, 1)));
const pixelsPerStep = Math.max(1, finite(options.pixelsPerStep, 16));
const wheelDetent = Math.max(1, finite(options.wheelDetent, DEFAULT_WHEEL_DETENT));
const wheelThrottle = Math.max(0, finite(options.wheelThrottle, DEFAULT_WHEEL_THROTTLE));
const maxDetents = Math.max(1, Math.trunc(finite(options.maxDetents, DEFAULT_MAX_DETENTS)));
const deltaToValue = options.deltaToValue || ((value, delta) => value + delta * step);
const tick = options.tick || (() => ownerWindow?.previewHaptics?.tick?.());
const onCommit = options.onCommit || (() => {});
const className = options.className || 'alt-scrub';
let hovering = false,
altDown = false,
active = false,
pointerId = null,
startX = 0,
startValue = 0;
let wheelRemainder = 0,
wheelTimer = 0,
pointerFrame = 0,
pendingPointerX = null;
const wasScrubInput = input.classList.contains('scrub-input');
input.classList.add('scrub-input');
function updateClass() {
input.classList.toggle(className, (hovering && altDown) || active);
}
function currentValue() {
return clampScrubValue(Number(read()), min, max);
}
function setValue(value, source) {
const next = clampScrubValue(value, min, max);
write(next, { source, commit: false });
return next;
}
function commit(source) {
onCommit({ source, value: currentValue() });
}
function finishPointer(source = 'pointer') {
if (!active) return;
if (pointerFrame) {
(ownerWindow.cancelAnimationFrame || clearTimeout).call(ownerWindow, pointerFrame);
pointerFrame = 0;
flushPointer();
}
active = false;
const id = pointerId;
pointerId = null;
pendingPointerX = null;
ownerDocument?.removeEventListener('pointermove', documentPointerMove, true);
ownerDocument?.removeEventListener('pointerup', documentPointerUp, true);
ownerDocument?.removeEventListener('pointercancel', documentPointerCancel, true);
if (id !== null && input.hasPointerCapture?.(id)) input.releasePointerCapture(id);
updateClass();
commit(source);
}
function flushPointer() {
pointerFrame = 0;
if (!active || pendingPointerX === null) return;
const delta = pendingPointerX - startX;
pendingPointerX = null;
setValue(deltaToValue(startValue, delta / pixelsPerStep), 'pointer');
}
function schedulePointer(x) {
pendingPointerX = x;
if (pointerFrame) return;
const frame = ownerWindow.requestAnimationFrame;
pointerFrame = frame ? frame(flushPointer) : setTimeout(flushPointer, 16);
}
function pointerMove(event) {
if (!active || event.pointerId !== pointerId) return;
event.preventDefault();
schedulePointer(event.clientX);
}
function documentPointerMove(event) {
pointerMove(event);
}
function documentPointerUp(event) {
if (event.pointerId === pointerId) finishPointer();
}
function documentPointerCancel(event) {
if (event.pointerId === pointerId) finishPointer('pointercancel');
}
function pointerDown(event) {
if (event.button !== 0 || !event.altKey) return;
event.preventDefault();
altDown = true;
active = true;
pointerId = event.pointerId;
startX = event.clientX;
startValue = currentValue();
pendingPointerX = null;
try {
input.focus({ preventScroll: true });
} catch {
input.focus?.();
}
input.setPointerCapture?.(event.pointerId);
ownerDocument?.addEventListener('pointermove', documentPointerMove, true);
ownerDocument?.addEventListener('pointerup', documentPointerUp, true);
ownerDocument?.addEventListener('pointercancel', documentPointerCancel, true);
updateClass();
}
function pointerMoveOnInput(event) {
pointerMove(event);
}
function pointerEnd(event) {
if (active && event.pointerId === pointerId)
finishPointer(event.type === 'pointercancel' ? 'pointercancel' : 'pointer');
}
function flushWheel() {
wheelTimer = 0;
const result = consumeDetents(wheelRemainder, wheelDetent, maxDetents);
wheelRemainder = result.remainder;
if (result.detents) {
const next = setValue(deltaToValue(currentValue(), result.detents), 'wheel');
for (let index = 0; index < Math.abs(result.detents); index++) tick();
onCommit({ source: 'wheel', value: next });
}
if (Math.abs(wheelRemainder) >= wheelDetent) wheelTimer = setTimeout(flushWheel, wheelThrottle);
}
function wheel(event) {
if (!event.altKey) return;
event.preventDefault();
altDown = true;
updateClass();
wheelRemainder += -normalizeWheelPixels(
event,
ownerDocument?.documentElement?.clientHeight || 800,
);
if (!wheelTimer) wheelTimer = setTimeout(flushWheel, wheelThrottle);
}
function pointerEnter(event) {
hovering = true;
if (event.altKey) altDown = true;
updateClass();
}
function pointerLeave() {
hovering = false;
updateClass();
}
function keyDown(event) {
if (event.key === 'Alt' || event.altKey) {
altDown = true;
updateClass();
}
}
function keyUp(event) {
if (event.key === 'Alt' || !event.altKey) {
altDown = false;
if (active) finishPointer('alt-release');
updateClass();
}
}
function windowBlur() {
altDown = false;
if (active) finishPointer('blur');
updateClass();
}
input.addEventListener('pointerenter', pointerEnter);
input.addEventListener('pointerleave', pointerLeave);
input.addEventListener('pointerdown', pointerDown);
input.addEventListener('pointermove', pointerMoveOnInput);
input.addEventListener('pointerup', pointerEnd);
input.addEventListener('pointercancel', pointerEnd);
input.addEventListener('lostpointercapture', pointerEnd);
input.addEventListener('wheel', wheel, { passive: false });
ownerWindow.addEventListener?.('keydown', keyDown);
ownerWindow.addEventListener?.('keyup', keyUp);
ownerWindow.addEventListener?.('blur', windowBlur);
return {
destroy() {
if (wheelTimer) {
clearTimeout(wheelTimer);
wheelTimer = 0;
}
finishPointer('destroy');
input.removeEventListener('pointerenter', pointerEnter);
input.removeEventListener('pointerleave', pointerLeave);
input.removeEventListener('pointerdown', pointerDown);
input.removeEventListener('pointermove', pointerMoveOnInput);
input.removeEventListener('pointerup', pointerEnd);
input.removeEventListener('pointercancel', pointerEnd);
input.removeEventListener('lostpointercapture', pointerEnd);
input.removeEventListener('wheel', wheel);
ownerWindow.removeEventListener?.('keydown', keyDown);
ownerWindow.removeEventListener?.('keyup', keyUp);
ownerWindow.removeEventListener?.('blur', windowBlur);
input.classList.remove(className);
if (!wasScrubInput) input.classList.remove('scrub-input');
},
cancel() {
if (active) finishPointer('cancel');
wheelRemainder = 0;
if (wheelTimer) {
clearTimeout(wheelTimer);
wheelTimer = 0;
}
},
flushWheel,
isScrubbing: () => active,
};
}
export const SCRUB_DEFAULTS = Object.freeze({
wheelDetent: DEFAULT_WHEEL_DETENT,
wheelThrottle: DEFAULT_WHEEL_THROTTLE,
maxDetents: DEFAULT_MAX_DETENTS,
});