@alulore/kumiko 0.15.1 → 0.17.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/media-viewer/icons.d.ts +4 -0
- package/dist/media-viewer/icons.js +18 -0
- package/dist/media-viewer/index.d.ts +4 -0
- package/dist/media-viewer/index.js +3 -0
- package/dist/media-viewer/media-slide.d.ts +14 -0
- package/dist/media-viewer/media-slide.js +67 -0
- package/dist/media-viewer/media-viewer.css +611 -0
- package/dist/media-viewer/media-viewer.d.ts +17 -0
- package/dist/media-viewer/media-viewer.js +241 -0
- package/dist/media-viewer/pager-drag.d.ts +10 -0
- package/dist/media-viewer/pager-drag.js +53 -0
- package/dist/media-viewer/types.d.ts +58 -0
- package/dist/media-viewer/types.js +17 -0
- package/dist/media-viewer/video-controls.d.ts +3 -0
- package/dist/media-viewer/video-controls.js +62 -0
- package/dist/media-viewer/view-gesture.d.ts +74 -0
- package/dist/media-viewer/view-gesture.js +465 -0
- package/dist/media-viewer/viewer-chrome.d.ts +13 -0
- package/dist/media-viewer/viewer-chrome.js +35 -0
- package/dist/media-viewer/viewer-motion.d.ts +9 -0
- package/dist/media-viewer/viewer-motion.js +126 -0
- package/dist/media-viewer/zoom-pan.d.ts +35 -0
- package/dist/media-viewer/zoom-pan.js +102 -0
- package/dist/shortcuts/bindings.d.ts +44 -0
- package/dist/shortcuts/bindings.js +71 -0
- package/dist/shortcuts/capture.d.ts +7 -0
- package/dist/shortcuts/capture.js +92 -0
- package/dist/shortcuts/guard.d.ts +9 -0
- package/dist/shortcuts/guard.js +48 -0
- package/dist/shortcuts/help.d.ts +25 -0
- package/dist/shortcuts/help.js +26 -0
- package/dist/shortcuts/hints.d.ts +19 -0
- package/dist/shortcuts/hints.js +33 -0
- package/dist/shortcuts/index.d.ts +11 -0
- package/dist/shortcuts/index.js +11 -0
- package/dist/shortcuts/judge.d.ts +42 -0
- package/dist/shortcuts/judge.js +34 -0
- package/dist/shortcuts/keys.d.ts +11 -0
- package/dist/shortcuts/keys.js +89 -0
- package/dist/shortcuts/list.d.ts +16 -0
- package/dist/shortcuts/list.js +43 -0
- package/dist/shortcuts/notes.d.ts +28 -0
- package/dist/shortcuts/notes.js +27 -0
- package/dist/shortcuts/provider.d.ts +98 -0
- package/dist/shortcuts/provider.js +116 -0
- package/dist/shortcuts/rebinder.d.ts +23 -0
- package/dist/shortcuts/rebinder.js +84 -0
- package/dist/shortcuts/registration.d.ts +19 -0
- package/dist/shortcuts/registration.js +84 -0
- package/dist/shortcuts/shortcuts.css +171 -0
- package/dist/stored/index.d.ts +4 -0
- package/dist/stored/index.js +4 -0
- package/dist/stored/storage.d.ts +22 -0
- package/dist/stored/storage.js +10 -0
- package/dist/stored/stored.d.ts +93 -0
- package/dist/stored/stored.js +209 -0
- package/dist/stored/use-stored.d.ts +6 -0
- package/dist/stored/use-stored.js +12 -0
- package/dist/stored/validators.d.ts +6 -0
- package/dist/stored/validators.js +13 -0
- package/dist/titlebar/index.d.ts +2 -0
- package/dist/titlebar/index.js +2 -0
- package/dist/titlebar/titlebar-looks.d.ts +53 -0
- package/dist/titlebar/titlebar-looks.js +26 -0
- package/dist/titlebar/titlebar.css +561 -0
- package/dist/titlebar/titlebar.d.ts +54 -0
- package/dist/titlebar/titlebar.js +68 -0
- package/dist/undo/history.d.ts +62 -0
- package/dist/undo/history.js +151 -0
- package/dist/undo/index.d.ts +10 -0
- package/dist/undo/index.js +10 -0
- package/dist/undo/undo-adapter.d.ts +35 -0
- package/dist/undo/undo-adapter.js +35 -0
- package/dist/undo/undo-feedback.d.ts +28 -0
- package/dist/undo/undo-feedback.js +69 -0
- package/dist/undo/undo-keys.d.ts +32 -0
- package/dist/undo/undo-keys.js +76 -0
- package/dist/undo/undo-options.d.ts +51 -0
- package/dist/undo/undo-options.js +6 -0
- package/dist/undo/undo-panel.d.ts +23 -0
- package/dist/undo/undo-panel.js +56 -0
- package/dist/undo/undo-provider.d.ts +88 -0
- package/dist/undo/undo-provider.js +104 -0
- package/dist/undo/undo-reveal.d.ts +9 -0
- package/dist/undo/undo-reveal.js +45 -0
- package/dist/undo/undo-store.d.ts +89 -0
- package/dist/undo/undo-store.js +243 -0
- package/dist/undo/undo.css +135 -0
- package/dist/undo/use-undo-value.d.ts +43 -0
- package/dist/undo/use-undo-value.js +85 -0
- package/package.json +42 -1
|
@@ -0,0 +1,465 @@
|
|
|
1
|
+
import { createVelocityTracker } from "@ui-sdk/react/gesture";
|
|
2
|
+
import { readMotion } from "../motion/flip";
|
|
3
|
+
import { FIT, actualScale, clamp, clampPan, coast, doubleTapScale, fitSize, maxScale, overshoot, panLimits, pinchView, shouldPage, softPan, wheelFactor, zoomTo } from "./zoom-pan";
|
|
4
|
+
const SLOP = 8;
|
|
5
|
+
const DOUBLE_MS = 300;
|
|
6
|
+
const PAGE_COOLDOWN = 420;
|
|
7
|
+
const EDGE_PAGE_PX = 70;
|
|
8
|
+
const FRICTION = 0.995;
|
|
9
|
+
const ORIGIN = { x: 0, y: 0 };
|
|
10
|
+
function centreOf(rect, x, y) {
|
|
11
|
+
return { x: x - rect.left - rect.width / 2, y: y - rect.top - rect.height / 2 };
|
|
12
|
+
}
|
|
13
|
+
function middle(a, b) {
|
|
14
|
+
return { x: (a.x + b.x) / 2, y: (a.y + b.y) / 2 };
|
|
15
|
+
}
|
|
16
|
+
/*
|
|
17
|
+
* Drives one slide's stage with pointers, wheel and taps: pinch, drag, inertia, double tap,
|
|
18
|
+
* swipe down to close and (without a carousel) swipe to page. It writes the view as CSS
|
|
19
|
+
* variables, so a drag never re-renders React.
|
|
20
|
+
*/
|
|
21
|
+
export class ViewGesture {
|
|
22
|
+
view = FIT;
|
|
23
|
+
mode = "idle";
|
|
24
|
+
down = null;
|
|
25
|
+
pointers = new Map();
|
|
26
|
+
vx = createVelocityTracker();
|
|
27
|
+
vy = createVelocityTracker();
|
|
28
|
+
pinchStart = null;
|
|
29
|
+
frame = 0;
|
|
30
|
+
lastTap = null;
|
|
31
|
+
tapTimer = 0;
|
|
32
|
+
lastPage = 0;
|
|
33
|
+
settleTimer = 0;
|
|
34
|
+
dragX = 0;
|
|
35
|
+
stage;
|
|
36
|
+
options;
|
|
37
|
+
hooks;
|
|
38
|
+
constructor(stage, options, hooks) {
|
|
39
|
+
this.stage = stage;
|
|
40
|
+
this.options = options;
|
|
41
|
+
this.hooks = hooks;
|
|
42
|
+
stage.addEventListener("pointerdown", this.onDown);
|
|
43
|
+
stage.addEventListener("pointermove", this.onMove);
|
|
44
|
+
stage.addEventListener("pointerup", this.onUp);
|
|
45
|
+
stage.addEventListener("pointercancel", this.onCancel);
|
|
46
|
+
stage.addEventListener("wheel", this.onWheel, { passive: false });
|
|
47
|
+
this.write(FIT);
|
|
48
|
+
}
|
|
49
|
+
destroy() {
|
|
50
|
+
this.stage.removeEventListener("pointerdown", this.onDown);
|
|
51
|
+
this.stage.removeEventListener("pointermove", this.onMove);
|
|
52
|
+
this.stage.removeEventListener("pointerup", this.onUp);
|
|
53
|
+
this.stage.removeEventListener("pointercancel", this.onCancel);
|
|
54
|
+
this.stage.removeEventListener("wheel", this.onWheel);
|
|
55
|
+
cancelAnimationFrame(this.frame);
|
|
56
|
+
window.clearTimeout(this.tapTimer);
|
|
57
|
+
window.clearTimeout(this.settleTimer);
|
|
58
|
+
}
|
|
59
|
+
/* A zoom step from a button or a key, around the centre of the frame. */
|
|
60
|
+
zoomBy(factor) {
|
|
61
|
+
const { fit, max } = this.measure();
|
|
62
|
+
const scale = clamp(this.view.scale * factor, 1, max);
|
|
63
|
+
const grown = zoomTo(this.view, scale, ORIGIN, "center");
|
|
64
|
+
this.settleTo(scale <= 1.01 ? FIT : clampPan(grown, panLimits(fit, this.size(), scale)));
|
|
65
|
+
}
|
|
66
|
+
reset() {
|
|
67
|
+
this.settleTo(FIT);
|
|
68
|
+
}
|
|
69
|
+
size() {
|
|
70
|
+
const rect = this.stage.getBoundingClientRect();
|
|
71
|
+
return { w: rect.width, h: rect.height };
|
|
72
|
+
}
|
|
73
|
+
measure() {
|
|
74
|
+
const natural = this.hooks().natural();
|
|
75
|
+
const fit = fitSize(natural, this.size());
|
|
76
|
+
return {
|
|
77
|
+
fit,
|
|
78
|
+
max: maxScale(natural, fit),
|
|
79
|
+
limits: panLimits(fit, this.size(), this.view.scale)
|
|
80
|
+
};
|
|
81
|
+
}
|
|
82
|
+
write(view) {
|
|
83
|
+
this.view = view;
|
|
84
|
+
const style = this.stage.style;
|
|
85
|
+
style.setProperty("--mv-x", `${view.x}px`);
|
|
86
|
+
style.setProperty("--mv-y", `${view.y}px`);
|
|
87
|
+
style.setProperty("--mv-scale", String(view.scale));
|
|
88
|
+
if (view.scale > 1.001) {
|
|
89
|
+
this.stage.dataset.zoomed = "";
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
delete this.stage.dataset.zoomed;
|
|
93
|
+
}
|
|
94
|
+
this.hooks().onView(view);
|
|
95
|
+
}
|
|
96
|
+
writeDrag(x, y, scale) {
|
|
97
|
+
const style = this.stage.style;
|
|
98
|
+
style.setProperty("--mv-drag-x", `${x}px`);
|
|
99
|
+
style.setProperty("--mv-drag-y", `${y}px`);
|
|
100
|
+
style.setProperty("--mv-drag-scale", String(scale));
|
|
101
|
+
}
|
|
102
|
+
/* Moves to a view with the settle transition on, and drops any drag offset. */
|
|
103
|
+
settleTo(view) {
|
|
104
|
+
const { duration } = readMotion(this.stage, "settle");
|
|
105
|
+
this.stage.dataset.settling = "";
|
|
106
|
+
window.clearTimeout(this.settleTimer);
|
|
107
|
+
this.settleTimer = window.setTimeout(() => this.endSettling(), duration + 60);
|
|
108
|
+
this.writeDrag(0, 0, 1);
|
|
109
|
+
this.hooks().onDismissProgress(0);
|
|
110
|
+
this.write(view);
|
|
111
|
+
}
|
|
112
|
+
endSettling() {
|
|
113
|
+
window.clearTimeout(this.settleTimer);
|
|
114
|
+
delete this.stage.dataset.settling;
|
|
115
|
+
}
|
|
116
|
+
rtl() {
|
|
117
|
+
return getComputedStyle(this.stage).direction === "rtl";
|
|
118
|
+
}
|
|
119
|
+
ignores(event) {
|
|
120
|
+
const target = event.target;
|
|
121
|
+
if (target instanceof Element && target.closest("button, a, input, [data-mv-control]")) {
|
|
122
|
+
return true;
|
|
123
|
+
}
|
|
124
|
+
if (target instanceof HTMLVideoElement && target.controls) {
|
|
125
|
+
/* The browser's own bar sits along the bottom of the video, and keeps its touches. */
|
|
126
|
+
return event.clientY > target.getBoundingClientRect().bottom - 56;
|
|
127
|
+
}
|
|
128
|
+
return event.pointerType === "mouse" && event.button !== 0;
|
|
129
|
+
}
|
|
130
|
+
onDown = (event) => {
|
|
131
|
+
if (this.ignores(event)) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
cancelAnimationFrame(this.frame);
|
|
135
|
+
this.endSettling();
|
|
136
|
+
const rect = this.stage.getBoundingClientRect();
|
|
137
|
+
this.pointers.set(event.pointerId, centreOf(rect, event.clientX, event.clientY));
|
|
138
|
+
if (this.pointers.size === 2 && this.options().zoomable) {
|
|
139
|
+
if (this.mode === "page") {
|
|
140
|
+
this.dragX = 0;
|
|
141
|
+
this.cancelPageDrag();
|
|
142
|
+
}
|
|
143
|
+
this.startPinch();
|
|
144
|
+
}
|
|
145
|
+
else if (this.pointers.size > 1) {
|
|
146
|
+
this.mode = "ignored";
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
149
|
+
this.startSingle(event);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
startSingle(event) {
|
|
153
|
+
const point = { x: event.clientX, y: event.clientY };
|
|
154
|
+
this.down = { point, time: event.timeStamp, type: event.pointerType };
|
|
155
|
+
this.vx.reset();
|
|
156
|
+
this.vy.reset();
|
|
157
|
+
this.vx.add(event.timeStamp, point.x);
|
|
158
|
+
this.vy.add(event.timeStamp, point.y);
|
|
159
|
+
const zoomed = this.view.scale > 1.001 && this.options().zoomable;
|
|
160
|
+
this.mode = zoomed ? "pan" : "pending";
|
|
161
|
+
if (zoomed && event.pointerType === "mouse") {
|
|
162
|
+
this.stage.setPointerCapture(event.pointerId);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
startPinch() {
|
|
166
|
+
const [a, b] = [...this.pointers.values()];
|
|
167
|
+
if (!a || !b) {
|
|
168
|
+
return;
|
|
169
|
+
}
|
|
170
|
+
this.pinchStart = {
|
|
171
|
+
view: this.view,
|
|
172
|
+
centre: middle(a, b),
|
|
173
|
+
gap: Math.hypot(a.x - b.x, a.y - b.y)
|
|
174
|
+
};
|
|
175
|
+
this.writeDrag(0, 0, 1);
|
|
176
|
+
this.hooks().onDismissProgress(0);
|
|
177
|
+
this.mode = "pinch";
|
|
178
|
+
}
|
|
179
|
+
onMove = (event) => {
|
|
180
|
+
const previous = this.pointers.get(event.pointerId);
|
|
181
|
+
if (!previous || this.mode === "idle") {
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
const now = centreOf(this.stage.getBoundingClientRect(), event.clientX, event.clientY);
|
|
185
|
+
this.pointers.set(event.pointerId, now);
|
|
186
|
+
this.vx.add(event.timeStamp, event.clientX);
|
|
187
|
+
this.vy.add(event.timeStamp, event.clientY);
|
|
188
|
+
this.route(event, now, previous);
|
|
189
|
+
};
|
|
190
|
+
route(event, now, previous) {
|
|
191
|
+
switch (this.mode) {
|
|
192
|
+
case "pinch":
|
|
193
|
+
this.movePinch();
|
|
194
|
+
break;
|
|
195
|
+
case "pan":
|
|
196
|
+
this.movePan(now.x - previous.x, now.y - previous.y);
|
|
197
|
+
break;
|
|
198
|
+
case "pending":
|
|
199
|
+
this.decide(event);
|
|
200
|
+
/* The move that settles what the gesture is also moves it, since moves can arrive in few events. */
|
|
201
|
+
if (this.isDragging()) {
|
|
202
|
+
this.moveDrag(event);
|
|
203
|
+
}
|
|
204
|
+
break;
|
|
205
|
+
case "dismiss":
|
|
206
|
+
case "page":
|
|
207
|
+
this.moveDrag(event);
|
|
208
|
+
break;
|
|
209
|
+
case "idle":
|
|
210
|
+
case "ignored":
|
|
211
|
+
break;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
isDragging() {
|
|
215
|
+
return this.mode === "page" || this.mode === "dismiss";
|
|
216
|
+
}
|
|
217
|
+
movePinch() {
|
|
218
|
+
const start = this.pinchStart;
|
|
219
|
+
const [a, b] = [...this.pointers.values()];
|
|
220
|
+
if (!start || !a || !b) {
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
223
|
+
const { max } = this.measure();
|
|
224
|
+
const gap = Math.hypot(a.x - b.x, a.y - b.y);
|
|
225
|
+
const next = pinchView(start.view, start.centre, start.gap, middle(a, b), gap, this.options().anchor, max);
|
|
226
|
+
this.write(next);
|
|
227
|
+
}
|
|
228
|
+
movePan(dx, dy) {
|
|
229
|
+
const { limits } = this.measure();
|
|
230
|
+
const moved = { scale: this.view.scale, x: this.view.x + dx, y: this.view.y + dy };
|
|
231
|
+
this.write(softPan(moved, limits, this.size()));
|
|
232
|
+
}
|
|
233
|
+
/* The first move past the slop picks what the gesture is, once, so it never flips mid-drag. */
|
|
234
|
+
decide(event) {
|
|
235
|
+
const down = this.down;
|
|
236
|
+
if (!down) {
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
const dx = event.clientX - down.point.x;
|
|
240
|
+
const dy = event.clientY - down.point.y;
|
|
241
|
+
if (Math.hypot(dx, dy) < SLOP) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const options = this.options();
|
|
245
|
+
const vertical = Math.abs(dy) > Math.abs(dx);
|
|
246
|
+
if (vertical && options.swipeDownClose && down.type !== "mouse" && dy > 0) {
|
|
247
|
+
this.mode = "dismiss";
|
|
248
|
+
}
|
|
249
|
+
else if (!vertical && (options.pagingFollows || down.type !== "mouse")) {
|
|
250
|
+
this.mode = "page";
|
|
251
|
+
this.stage.setPointerCapture(event.pointerId);
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
this.mode = "ignored";
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
moveDrag(event) {
|
|
258
|
+
const down = this.down;
|
|
259
|
+
if (!down) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
const dx = event.clientX - down.point.x;
|
|
263
|
+
const dy = event.clientY - down.point.y;
|
|
264
|
+
if (this.mode === "page") {
|
|
265
|
+
this.dragX = dx;
|
|
266
|
+
if (this.options().pagingFollows) {
|
|
267
|
+
this.writeDrag(dx, 0, 1);
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
this.hooks().dragPager?.(dx);
|
|
271
|
+
}
|
|
272
|
+
return;
|
|
273
|
+
}
|
|
274
|
+
const progress = clamp(Math.max(0, dy) / (this.size().h * 0.45), 0, 1);
|
|
275
|
+
this.writeDrag(dx * 0.4, Math.max(0, dy), 1 - progress * 0.25);
|
|
276
|
+
this.hooks().onDismissProgress(progress);
|
|
277
|
+
}
|
|
278
|
+
onUp = (event) => {
|
|
279
|
+
if (!this.pointers.has(event.pointerId)) {
|
|
280
|
+
return;
|
|
281
|
+
}
|
|
282
|
+
this.pointers.delete(event.pointerId);
|
|
283
|
+
if (this.mode === "pinch") {
|
|
284
|
+
this.endPinch();
|
|
285
|
+
}
|
|
286
|
+
else if (this.pointers.size === 0) {
|
|
287
|
+
this.finish(event, this.mode);
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
onCancel = (event) => {
|
|
291
|
+
this.pointers.delete(event.pointerId);
|
|
292
|
+
if (this.pointers.size === 0) {
|
|
293
|
+
const paging = this.mode === "page";
|
|
294
|
+
this.mode = "idle";
|
|
295
|
+
this.dragX = 0;
|
|
296
|
+
if (paging) {
|
|
297
|
+
this.cancelPageDrag();
|
|
298
|
+
}
|
|
299
|
+
this.settleTo(this.view);
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
endPinch() {
|
|
303
|
+
const { fit, max } = this.measure();
|
|
304
|
+
const scale = clamp(this.view.scale, 1, max);
|
|
305
|
+
const grown = zoomTo(this.view, scale, ORIGIN, "center");
|
|
306
|
+
const limits = panLimits(fit, this.size(), scale);
|
|
307
|
+
this.settleTo(scale <= 1.02 ? FIT : clampPan(grown, limits));
|
|
308
|
+
this.pinchStart = null;
|
|
309
|
+
this.mode = this.pointers.size === 0 ? "idle" : "ignored";
|
|
310
|
+
}
|
|
311
|
+
finish(event, mode) {
|
|
312
|
+
this.mode = "idle";
|
|
313
|
+
const velocity = this.vx.velocity(event.timeStamp);
|
|
314
|
+
if (mode === "pending") {
|
|
315
|
+
this.tap(event);
|
|
316
|
+
}
|
|
317
|
+
else if (mode === "pan") {
|
|
318
|
+
this.releasePan(event, velocity);
|
|
319
|
+
}
|
|
320
|
+
else if (mode === "dismiss") {
|
|
321
|
+
this.releaseDismiss(event);
|
|
322
|
+
}
|
|
323
|
+
else if (mode === "page") {
|
|
324
|
+
this.releasePage(velocity);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
releaseDismiss(event) {
|
|
328
|
+
const dy = this.down ? event.clientY - this.down.point.y : 0;
|
|
329
|
+
if (dy > this.size().h * 0.2 || this.vy.velocity(event.timeStamp) > 0.7) {
|
|
330
|
+
this.hooks().onDismiss();
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
this.settleTo(this.view);
|
|
334
|
+
}
|
|
335
|
+
releasePage(velocity) {
|
|
336
|
+
const dx = this.dragX;
|
|
337
|
+
this.dragX = 0;
|
|
338
|
+
const quick = Math.abs(velocity) > 0.4 && Math.abs(dx) > 16;
|
|
339
|
+
if (Math.abs(dx) > this.size().w * 0.25 || quick) {
|
|
340
|
+
const forward = dx < 0 !== this.rtl();
|
|
341
|
+
this.writeDrag(0, 0, 1);
|
|
342
|
+
const direction = forward ? 1 : -1;
|
|
343
|
+
if (this.options().pagingFollows) {
|
|
344
|
+
this.hooks().onPage(direction);
|
|
345
|
+
}
|
|
346
|
+
else {
|
|
347
|
+
this.hooks().releasePager?.(direction);
|
|
348
|
+
}
|
|
349
|
+
return;
|
|
350
|
+
}
|
|
351
|
+
this.cancelPageDrag();
|
|
352
|
+
}
|
|
353
|
+
/* A drag that did not page puts the stage or the carousel back. */
|
|
354
|
+
cancelPageDrag() {
|
|
355
|
+
if (this.options().pagingFollows) {
|
|
356
|
+
this.settleTo(this.view);
|
|
357
|
+
}
|
|
358
|
+
else {
|
|
359
|
+
this.hooks().releasePager?.(0);
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
releasePan(event, velocity) {
|
|
363
|
+
if (this.down &&
|
|
364
|
+
Math.hypot(event.clientX - this.down.point.x, event.clientY - this.down.point.y) < SLOP) {
|
|
365
|
+
/* A press that went nowhere on a zoomed picture is a tap, so a double tap can fit it again. */
|
|
366
|
+
this.tap(event);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
const { limits } = this.measure();
|
|
370
|
+
const over = overshoot(this.view.x, limits.x);
|
|
371
|
+
if (this.options().zoomedDrag === "edge-pages" &&
|
|
372
|
+
shouldPage(over, velocity, EDGE_PAGE_PX)) {
|
|
373
|
+
const forward = over < 0 !== this.rtl();
|
|
374
|
+
this.settleTo(clampPan(this.view, limits));
|
|
375
|
+
this.hooks().onPage(forward ? 1 : -1);
|
|
376
|
+
return;
|
|
377
|
+
}
|
|
378
|
+
const speedY = this.vy.velocity(event.timeStamp);
|
|
379
|
+
const outside = over !== 0 || overshoot(this.view.y, limits.y) !== 0;
|
|
380
|
+
const still = readMotion(this.stage, "settle").duration === 0;
|
|
381
|
+
if (outside || still || Math.hypot(velocity, speedY) < 0.05) {
|
|
382
|
+
this.settleTo(clampPan(this.view, limits));
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
this.inertia(velocity, speedY, limits);
|
|
386
|
+
}
|
|
387
|
+
inertia(vx, vy, limits) {
|
|
388
|
+
let last = performance.now();
|
|
389
|
+
let speedX = vx;
|
|
390
|
+
let speedY = vy;
|
|
391
|
+
const step = (now) => {
|
|
392
|
+
const dt = Math.min(48, now - last);
|
|
393
|
+
last = now;
|
|
394
|
+
const x = coast(this.view.x, speedX, dt, FRICTION);
|
|
395
|
+
const y = coast(this.view.y, speedY, dt, FRICTION);
|
|
396
|
+
const next = clampPan({ scale: this.view.scale, x: x.position, y: y.position }, limits);
|
|
397
|
+
speedX = next.x === x.position ? x.velocity : 0;
|
|
398
|
+
speedY = next.y === y.position ? y.velocity : 0;
|
|
399
|
+
this.write(next);
|
|
400
|
+
if (Math.hypot(speedX, speedY) > 0.02) {
|
|
401
|
+
this.frame = requestAnimationFrame(step);
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
this.frame = requestAnimationFrame(step);
|
|
405
|
+
}
|
|
406
|
+
tap(event) {
|
|
407
|
+
if (!this.down || event.timeStamp - this.down.time > 400) {
|
|
408
|
+
return;
|
|
409
|
+
}
|
|
410
|
+
const point = { x: event.clientX, y: event.clientY };
|
|
411
|
+
const last = this.lastTap;
|
|
412
|
+
const near = last && Math.hypot(point.x - last.point.x, point.y - last.point.y) < 40;
|
|
413
|
+
window.clearTimeout(this.tapTimer);
|
|
414
|
+
if (near && event.timeStamp - last.time < DOUBLE_MS && this.options().zoomable) {
|
|
415
|
+
this.lastTap = null;
|
|
416
|
+
this.doubleTap(centreOf(this.stage.getBoundingClientRect(), point.x, point.y));
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
this.lastTap = { time: event.timeStamp, point };
|
|
420
|
+
const delay = this.options().zoomable ? DOUBLE_MS - 40 : 0;
|
|
421
|
+
this.tapTimer = window.setTimeout(() => this.hooks().onTap(), delay);
|
|
422
|
+
}
|
|
423
|
+
doubleTap(at) {
|
|
424
|
+
const { fit, max } = this.measure();
|
|
425
|
+
const actual = Math.max(2, actualScale(this.hooks().natural(), fit));
|
|
426
|
+
const target = this.options().doubleTap === "actual" ? Math.min(max, actual) : 2;
|
|
427
|
+
const scale = doubleTapScale(this.view.scale, target);
|
|
428
|
+
const grown = zoomTo(this.view, scale, at, "point");
|
|
429
|
+
this.settleTo(scale === 1 ? FIT : clampPan(grown, panLimits(fit, this.size(), scale)));
|
|
430
|
+
}
|
|
431
|
+
onWheel = (event) => {
|
|
432
|
+
const options = this.options();
|
|
433
|
+
const horizontal = Math.abs(event.deltaX) > Math.abs(event.deltaY);
|
|
434
|
+
if (!event.ctrlKey && horizontal && this.view.scale <= 1.001) {
|
|
435
|
+
return;
|
|
436
|
+
}
|
|
437
|
+
event.preventDefault();
|
|
438
|
+
if (event.ctrlKey || options.wheel === "zoom") {
|
|
439
|
+
if (options.zoomable) {
|
|
440
|
+
this.wheelZoom(event);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
else if (this.view.scale > 1.001) {
|
|
444
|
+
this.movePan(-event.deltaX, -event.deltaY);
|
|
445
|
+
}
|
|
446
|
+
else {
|
|
447
|
+
this.wheelPage(event);
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
wheelZoom(event) {
|
|
451
|
+
this.endSettling();
|
|
452
|
+
const { fit, max } = this.measure();
|
|
453
|
+
const scale = clamp(this.view.scale * wheelFactor(event.deltaY, event.ctrlKey), 1, max);
|
|
454
|
+
const at = centreOf(this.stage.getBoundingClientRect(), event.clientX, event.clientY);
|
|
455
|
+
const grown = zoomTo(this.view, scale, at, this.options().anchor);
|
|
456
|
+
this.write(scale <= 1.001 ? FIT : clampPan(grown, panLimits(fit, this.size(), scale)));
|
|
457
|
+
}
|
|
458
|
+
wheelPage(event) {
|
|
459
|
+
if (event.timeStamp - this.lastPage < PAGE_COOLDOWN || Math.abs(event.deltaY) < 4) {
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
this.lastPage = event.timeStamp;
|
|
463
|
+
this.hooks().onPage(event.deltaY > 0 ? 1 : -1);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { MediaViewerItem } from "./types";
|
|
2
|
+
export interface ChromeProps {
|
|
3
|
+
items: MediaViewerItem[];
|
|
4
|
+
index: number;
|
|
5
|
+
zoom: number;
|
|
6
|
+
zoomable: boolean;
|
|
7
|
+
onGo: (index: number) => void;
|
|
8
|
+
onStep: (direction: 1 | -1) => void;
|
|
9
|
+
onZoom: (factor: number) => void;
|
|
10
|
+
onFit: () => void;
|
|
11
|
+
onFullscreen: () => void;
|
|
12
|
+
}
|
|
13
|
+
export declare function ViewerChrome(props: ChromeProps): React.JSX.Element | null;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { DialogClose, DialogTitle } from "../dialog";
|
|
3
|
+
import { Icon } from "./icons";
|
|
4
|
+
function Button({ label, icon, onClick, disabled }) {
|
|
5
|
+
return (_jsx("button", { type: "button", "data-mv-button": "", "aria-label": label, disabled: disabled, onClick: onClick, children: _jsx(Icon, { name: icon }) }));
|
|
6
|
+
}
|
|
7
|
+
function Dots({ items, index, onGo }) {
|
|
8
|
+
return (_jsx("div", { "data-mv-indicator": "dots", role: "group", "aria-label": "Pick an item", children: items.map((item, at) => (_jsx("button", { type: "button", "data-mv-dot": "", "aria-label": item.name, "aria-current": at === index ? "true" : undefined, onClick: () => onGo(at) }, item.id))) }));
|
|
9
|
+
}
|
|
10
|
+
function Strip({ items, index, onGo }) {
|
|
11
|
+
return (_jsx("div", { "data-mv-indicator": "strip", role: "group", "aria-label": "Pick an item", children: items.map((item, at) => (_jsx("button", { type: "button", "data-mv-thumb": "", "aria-label": item.name, "aria-current": at === index ? "true" : undefined, onClick: () => onGo(at), children: _jsx("img", { src: item.thumb ?? item.poster ?? item.src, alt: "", draggable: false }) }, item.id))) }));
|
|
12
|
+
}
|
|
13
|
+
function Zoom({ zoom, zoomable, onZoom, onFit }) {
|
|
14
|
+
return (_jsxs("div", { "data-mv-zoom": "", role: "group", "aria-label": "Zoom", children: [_jsx(Button, { label: "Zoom out", icon: "minus", disabled: !zoomable, onClick: () => onZoom(1 / 1.25) }), _jsxs("span", { "data-mv-percent": "", "aria-live": "off", children: [Math.round(zoom * 100), "%"] }), _jsx(Button, { label: "Zoom in", icon: "plus", disabled: !zoomable, onClick: () => onZoom(1.25) }), _jsx("button", { type: "button", "data-mv-button": "", "data-mv-fit": "", disabled: !zoomable, onClick: onFit, children: "Fit" })] }));
|
|
15
|
+
}
|
|
16
|
+
function Heading({ item, index, count }) {
|
|
17
|
+
const size = item.width && item.height ? `${item.width} × ${item.height}` : "";
|
|
18
|
+
return (_jsxs("div", { "data-mv-heading": "", children: [_jsx(DialogTitle, { children: item.name }), _jsxs("div", { "data-mv-meta": "", children: [count > 1 ? _jsx("span", { "data-mv-counter": "", children: `${index + 1} / ${count}` }) : null, size === "" ? null : _jsx("span", { "data-mv-size": "", children: size })] })] }));
|
|
19
|
+
}
|
|
20
|
+
function TopBar({ item, index, count, onFullscreen }) {
|
|
21
|
+
return (_jsxs("div", { "data-mv-chrome": "", "data-mv-top": "", children: [_jsx(Heading, { item: item, index: index, count: count }), _jsxs("div", { "data-mv-actions": "", children: [_jsx("span", { "data-mv-optional": "", children: _jsx(Button, { label: "Toggle fullscreen", icon: "fullscreen", onClick: onFullscreen }) }), _jsx("a", { "data-mv-button": "", "data-mv-optional": "", href: item.src, download: item.name, "aria-label": "Download", children: _jsx(Icon, { name: "download" }) }), _jsx(DialogClose, { "data-mv-button": "", "aria-label": "Close viewer", children: _jsx(Icon, { name: "close" }) })] })] }));
|
|
22
|
+
}
|
|
23
|
+
function Sides({ onStep }) {
|
|
24
|
+
return (_jsxs(_Fragment, { children: [_jsx("span", { "data-mv-chrome": "", "data-mv-optional": "", "data-mv-side": "start", children: _jsx(Button, { label: "Previous", icon: "previous", onClick: () => onStep(-1) }) }), _jsx("span", { "data-mv-chrome": "", "data-mv-optional": "", "data-mv-side": "end", children: _jsx(Button, { label: "Next", icon: "next", onClick: () => onStep(1) }) })] }));
|
|
25
|
+
}
|
|
26
|
+
/* The buttons, bars and indicators. Their place is the layout word's business, not this markup's. */
|
|
27
|
+
export function ViewerChrome(props) {
|
|
28
|
+
const { items, index, onStep, onFullscreen } = props;
|
|
29
|
+
const item = items[index];
|
|
30
|
+
if (!item) {
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
33
|
+
const several = items.length > 1;
|
|
34
|
+
return (_jsxs(_Fragment, { children: [_jsx(TopBar, { item: item, index: index, count: items.length, onFullscreen: onFullscreen }), several ? _jsx(Sides, { onStep: onStep }) : null, _jsxs("div", { "data-mv-chrome": "", "data-mv-bottom": "", children: [several ? _jsx(Dots, { ...props }) : null, several ? _jsx(Strip, { ...props }) : null, _jsx("span", { "data-mv-optional": "", children: _jsx(Zoom, { ...props }) })] })] }));
|
|
35
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { Chrome, OpenMotion } from "./types";
|
|
2
|
+
export type OriginOf = (index: number) => HTMLElement | null | undefined;
|
|
3
|
+
export declare function useOpenFromThumbnail(content: HTMLElement | null, index: number, motion: OpenMotion, originOf: OriginOf | undefined): void;
|
|
4
|
+
export declare function closeToThumbnail(content: HTMLElement | null, index: number, originOf: OriginOf | undefined): Promise<void>;
|
|
5
|
+
export declare function useChrome(mode: Chrome, content: HTMLElement | null): {
|
|
6
|
+
hidden: boolean;
|
|
7
|
+
poke: () => void;
|
|
8
|
+
toggle: () => void;
|
|
9
|
+
};
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
2
|
+
import { readMotion } from "../motion/flip";
|
|
3
|
+
function partsOf(content, index, originOf) {
|
|
4
|
+
const origin = originOf?.(index);
|
|
5
|
+
const media = content.querySelector("[data-mv-stage][data-active] [data-mv-media]");
|
|
6
|
+
const src = media instanceof HTMLVideoElement ? media.poster : media?.getAttribute("src");
|
|
7
|
+
if (!origin?.isConnected || !media || !src) {
|
|
8
|
+
return null;
|
|
9
|
+
}
|
|
10
|
+
return { media, origin, src };
|
|
11
|
+
}
|
|
12
|
+
/*
|
|
13
|
+
* A copy of the picture that flies between the thumbnail and its place. It is a fixed child of the
|
|
14
|
+
* panel, so no scroller or bar clips it, and the real picture stays hidden while it flies.
|
|
15
|
+
*/
|
|
16
|
+
function flyingCopy(content, src, box) {
|
|
17
|
+
const copy = document.createElement("img");
|
|
18
|
+
copy.src = src;
|
|
19
|
+
copy.alt = "";
|
|
20
|
+
copy.dataset.mvFlight = "";
|
|
21
|
+
Object.assign(copy.style, {
|
|
22
|
+
position: "fixed",
|
|
23
|
+
zIndex: "2",
|
|
24
|
+
margin: "0",
|
|
25
|
+
pointerEvents: "none",
|
|
26
|
+
objectFit: "cover",
|
|
27
|
+
left: `${box.left}px`,
|
|
28
|
+
top: `${box.top}px`,
|
|
29
|
+
width: `${box.width}px`,
|
|
30
|
+
height: `${box.height}px`
|
|
31
|
+
});
|
|
32
|
+
content.append(copy);
|
|
33
|
+
content.dataset.flying = "";
|
|
34
|
+
return copy;
|
|
35
|
+
}
|
|
36
|
+
function boxOf(rect) {
|
|
37
|
+
return {
|
|
38
|
+
left: `${rect.left}px`,
|
|
39
|
+
top: `${rect.top}px`,
|
|
40
|
+
width: `${rect.width}px`,
|
|
41
|
+
height: `${rect.height}px`
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
/* Opens from the thumbnail: a copy of the picture grows from it into place. */
|
|
45
|
+
export function useOpenFromThumbnail(content, index, motion, originOf) {
|
|
46
|
+
const latest = useRef({ index, originOf });
|
|
47
|
+
useEffect(() => {
|
|
48
|
+
latest.current = { index, originOf };
|
|
49
|
+
});
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
if (!content || motion !== "thumbnail") {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
const frame = requestAnimationFrame(() => {
|
|
55
|
+
const parts = partsOf(content, latest.current.index, latest.current.originOf);
|
|
56
|
+
const { duration, easing } = readMotion(content, "settle");
|
|
57
|
+
if (!parts || duration === 0) {
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const to = parts.media.getBoundingClientRect();
|
|
61
|
+
const copy = flyingCopy(content, parts.src, to);
|
|
62
|
+
const from = boxOf(parts.origin.getBoundingClientRect());
|
|
63
|
+
copy.animate([from, boxOf(to)], { duration, easing })
|
|
64
|
+
.finished.then(() => {
|
|
65
|
+
copy.remove();
|
|
66
|
+
delete content.dataset.flying;
|
|
67
|
+
})
|
|
68
|
+
.catch(() => undefined);
|
|
69
|
+
});
|
|
70
|
+
return () => cancelAnimationFrame(frame);
|
|
71
|
+
}, [content, motion]);
|
|
72
|
+
}
|
|
73
|
+
/* Closes toward the thumbnail, resolving once the picture has arrived (or at once, with no thumbnail). */
|
|
74
|
+
export function closeToThumbnail(content, index, originOf) {
|
|
75
|
+
const parts = content ? partsOf(content, index, originOf) : null;
|
|
76
|
+
const { duration, easing } = content
|
|
77
|
+
? readMotion(content, "settle")
|
|
78
|
+
: { duration: 0, easing: "" };
|
|
79
|
+
if (!content || !parts || duration === 0) {
|
|
80
|
+
return Promise.resolve();
|
|
81
|
+
}
|
|
82
|
+
const held = parts.media.getBoundingClientRect();
|
|
83
|
+
const copy = flyingCopy(content, parts.src, held);
|
|
84
|
+
const target = boxOf(parts.origin.getBoundingClientRect());
|
|
85
|
+
const animation = copy.animate([boxOf(held), target], { duration, easing, fill: "forwards" });
|
|
86
|
+
return animation.finished.then(() => undefined, () => undefined);
|
|
87
|
+
}
|
|
88
|
+
const IDLE_MS = 2800;
|
|
89
|
+
/*
|
|
90
|
+
* Whether the bars and buttons show. Always and minimal never hide. Autohide fades them after
|
|
91
|
+
* a quiet spell, shows them on a move or a key, and a tap toggles them. They stay while focus is inside.
|
|
92
|
+
*/
|
|
93
|
+
export function useChrome(mode, content) {
|
|
94
|
+
const [hidden, setHidden] = useState(false);
|
|
95
|
+
const timer = useRef(0);
|
|
96
|
+
const hide = useCallback(() => {
|
|
97
|
+
if (content?.querySelector("[data-mv-chrome]:focus-within")) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
setHidden(true);
|
|
101
|
+
}, [content]);
|
|
102
|
+
const poke = useCallback(() => {
|
|
103
|
+
window.clearTimeout(timer.current);
|
|
104
|
+
setHidden(false);
|
|
105
|
+
if (mode === "autohide") {
|
|
106
|
+
timer.current = window.setTimeout(hide, IDLE_MS);
|
|
107
|
+
}
|
|
108
|
+
}, [mode, hide]);
|
|
109
|
+
const toggle = useCallback(() => {
|
|
110
|
+
if (mode !== "autohide") {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
window.clearTimeout(timer.current);
|
|
114
|
+
if (hidden) {
|
|
115
|
+
poke();
|
|
116
|
+
}
|
|
117
|
+
else {
|
|
118
|
+
setHidden(true);
|
|
119
|
+
}
|
|
120
|
+
}, [mode, hidden, poke]);
|
|
121
|
+
useEffect(() => {
|
|
122
|
+
poke();
|
|
123
|
+
return () => window.clearTimeout(timer.current);
|
|
124
|
+
}, [poke]);
|
|
125
|
+
return { hidden, poke, toggle };
|
|
126
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export interface Size {
|
|
2
|
+
w: number;
|
|
3
|
+
h: number;
|
|
4
|
+
}
|
|
5
|
+
export interface Point {
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
}
|
|
9
|
+
export interface View {
|
|
10
|
+
scale: number;
|
|
11
|
+
x: number;
|
|
12
|
+
y: number;
|
|
13
|
+
}
|
|
14
|
+
export type ZoomAnchor = "point" | "center";
|
|
15
|
+
export declare const FIT: View;
|
|
16
|
+
export declare const MIN_SCALE = 1;
|
|
17
|
+
export declare function clamp(value: number, low: number, high: number): number;
|
|
18
|
+
export declare function fitSize(natural: Size, frame: Size): Size;
|
|
19
|
+
export declare function actualScale(natural: Size, fit: Size): number;
|
|
20
|
+
export declare function maxScale(natural: Size, fit: Size): number;
|
|
21
|
+
export declare function panLimits(fit: Size, frame: Size, scale: number): Point;
|
|
22
|
+
export declare function clampPan(view: View, limits: Point): View;
|
|
23
|
+
export declare function softLimit(value: number, limit: number, dimension: number): number;
|
|
24
|
+
export declare function softPan(view: View, limits: Point, frame: Size): View;
|
|
25
|
+
export declare function overshoot(value: number, limit: number): number;
|
|
26
|
+
export declare function zoomTo(view: View, scale: number, at: Point, anchor: ZoomAnchor): View;
|
|
27
|
+
export declare function factorScale(scale: number, factor: number, max: number): number;
|
|
28
|
+
export declare function wheelFactor(deltaY: number, pinch: boolean): number;
|
|
29
|
+
export declare function doubleTapScale(scale: number, target: number): number;
|
|
30
|
+
export declare function pinchView(start: View, startCentre: Point, startGap: number, centre: Point, gap: number, anchor: ZoomAnchor, max: number): View;
|
|
31
|
+
export declare function coast(position: number, velocity: number, dt: number, rate: number): {
|
|
32
|
+
position: number;
|
|
33
|
+
velocity: number;
|
|
34
|
+
};
|
|
35
|
+
export declare function shouldPage(overshootPx: number, velocity: number, threshold: number): boolean;
|