@qzsy/dsh-image-viewer 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +56 -2
- package/cordis.patch.yml +3 -0
- package/dist/client.js +1394 -0
- package/dist/index.js +7 -0
- package/icon.svg +13 -0
- package/locale/en.json +6 -0
- package/locale/zh.json +6 -0
- package/package.json +73 -4
package/dist/client.js
ADDED
|
@@ -0,0 +1,1394 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Client half of `@qzsy/dsh-image-viewer`.
|
|
3
|
+
*
|
|
4
|
+
* Replaces the bare built-in image lightbox with a full-screen viewer that
|
|
5
|
+
* behaves like the iPhone Photos app:
|
|
6
|
+
*
|
|
7
|
+
* - open/close zooms from and back to the thumbnail (with cover-crop and radius)
|
|
8
|
+
* - pinch to zoom around the fingers, pan with momentum and rubber-band edges
|
|
9
|
+
* - double-tap / double-click to zoom into the tapped point and back
|
|
10
|
+
* - swipe left/right between every previewable image in the same scroll area
|
|
11
|
+
* - swipe down (or up) to dismiss, with the backdrop fading as you drag
|
|
12
|
+
* - pinch in below fit size to close
|
|
13
|
+
* - single tap toggles the chrome on touch devices
|
|
14
|
+
* - desktop: wheel / Ctrl+wheel / trackpad pinch zoom, trackpad swipe paging,
|
|
15
|
+
* drag to pan, arrow keys, + / - / 0, Esc, filmstrip and download
|
|
16
|
+
*
|
|
17
|
+
* The plugin owns no Host capability. It listens for clicks in the capture
|
|
18
|
+
* phase on `document`, so it runs before React's root listener and the
|
|
19
|
+
* built-in lightbox never opens for images it handles. Clicks with a modifier
|
|
20
|
+
* key, images inside links, and anything marked `data-qiv-ignore` pass through.
|
|
21
|
+
*/
|
|
22
|
+
window.__ModuleLoader__.load({
|
|
23
|
+
id: '@qzsy/dsh-image-viewer',
|
|
24
|
+
factory() {
|
|
25
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
/** Client dictionary namespace owned by this plugin. */
|
|
28
|
+
const NS = 'imageViewer';
|
|
29
|
+
const ROOT_ATTR = 'data-qiv-root';
|
|
30
|
+
const STYLE_ATTR = 'data-qiv-style';
|
|
31
|
+
const IGNORE_ATTR = 'data-qiv-ignore';
|
|
32
|
+
|
|
33
|
+
/** Horizontal gap between slides, in CSS px. */
|
|
34
|
+
const GAP = 24;
|
|
35
|
+
/** Movement before a press becomes a drag. */
|
|
36
|
+
const SLOP = 8;
|
|
37
|
+
const TAP_MAX_MS = 500;
|
|
38
|
+
const DOUBLE_TAP_MS = 300;
|
|
39
|
+
const DOUBLE_TAP_PX = 32;
|
|
40
|
+
/** iOS UIScrollView "normal" deceleration rate, per millisecond. */
|
|
41
|
+
const DECEL = 0.998;
|
|
42
|
+
const MAX_ITEMS = 400;
|
|
43
|
+
/** Bare <img> elements smaller than this are treated as icons. */
|
|
44
|
+
const MIN_BARE_W = 96;
|
|
45
|
+
const MIN_BARE_H = 64;
|
|
46
|
+
|
|
47
|
+
/** Spring presets: k = stiffness, c = damping (unit mass). */
|
|
48
|
+
const SPRING = {
|
|
49
|
+
snap: { k: 420, c: 36 },
|
|
50
|
+
open: { k: 300, c: 31 },
|
|
51
|
+
close: { k: 380, c: 39 },
|
|
52
|
+
page: { k: 260, c: 31 },
|
|
53
|
+
glide: { k: 170, c: 26 },
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const zh = {
|
|
57
|
+
dialog: '图片查看器',
|
|
58
|
+
close: '关闭',
|
|
59
|
+
prev: '上一张',
|
|
60
|
+
next: '下一张',
|
|
61
|
+
zoomIn: '放大',
|
|
62
|
+
zoomOut: '还原',
|
|
63
|
+
download: '下载原图',
|
|
64
|
+
counter: '{i} / {n}',
|
|
65
|
+
failed: '图片加载失败',
|
|
66
|
+
thumb: '第 {i} 张',
|
|
67
|
+
};
|
|
68
|
+
const en = {
|
|
69
|
+
dialog: 'Image viewer',
|
|
70
|
+
close: 'Close',
|
|
71
|
+
prev: 'Previous image',
|
|
72
|
+
next: 'Next image',
|
|
73
|
+
zoomIn: 'Zoom in',
|
|
74
|
+
zoomOut: 'Reset zoom',
|
|
75
|
+
download: 'Download original',
|
|
76
|
+
counter: '{i} / {n}',
|
|
77
|
+
failed: 'Image failed to load',
|
|
78
|
+
thumb: 'Image {i}',
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
//#region utilities
|
|
82
|
+
const clamp = (v, lo, hi) => Math.min(hi, Math.max(lo, v));
|
|
83
|
+
const now = () => (typeof performance !== 'undefined' ? performance.now() : Date.now());
|
|
84
|
+
|
|
85
|
+
/** UIScrollView rubber-band: resistance grows with distance past the edge. */
|
|
86
|
+
function rubber(over, dim) {
|
|
87
|
+
const d = Math.max(1, dim);
|
|
88
|
+
const sign = over < 0 ? -1 : 1;
|
|
89
|
+
const x = Math.abs(over);
|
|
90
|
+
return sign * (1 - 1 / ((x * 0.55) / d + 1)) * d;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function rubberClamp(v, lo, hi, dim) {
|
|
94
|
+
if (v < lo) return lo + rubber(v - lo, dim);
|
|
95
|
+
if (v > hi) return hi + rubber(v - hi, dim);
|
|
96
|
+
return v;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function media(query) {
|
|
100
|
+
try {
|
|
101
|
+
return typeof window.matchMedia === 'function' && window.matchMedia(query).matches === true;
|
|
102
|
+
} catch {
|
|
103
|
+
return false;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const reduceMotion = () => media('(prefers-reduced-motion: reduce)');
|
|
108
|
+
|
|
109
|
+
function format(text, params) {
|
|
110
|
+
return String(text).replace(/\{(\w+)\}/g, (match, key) =>
|
|
111
|
+
params !== undefined && params !== null && key in params ? String(params[key]) : match,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function fallbackT(key, params) {
|
|
116
|
+
const lang = (document.documentElement.getAttribute('lang') || navigator.language || 'en').toLowerCase();
|
|
117
|
+
const dict = lang.startsWith('zh') ? zh : en;
|
|
118
|
+
return format(dict[key] ?? en[key] ?? key, params);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
function h(tag, className, attrs) {
|
|
122
|
+
const node = document.createElement(tag);
|
|
123
|
+
if (className) node.className = className;
|
|
124
|
+
if (attrs) for (const [key, value] of Object.entries(attrs)) node.setAttribute(key, value);
|
|
125
|
+
return node;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function cssPx(value, basis) {
|
|
129
|
+
const text = String(value || '0').split(' ')[0];
|
|
130
|
+
const num = parseFloat(text);
|
|
131
|
+
if (!Number.isFinite(num)) return 0;
|
|
132
|
+
return text.endsWith('%') ? (num / 100) * basis : num;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
function fileName(src) {
|
|
136
|
+
try {
|
|
137
|
+
if (src.startsWith('data:')) {
|
|
138
|
+
const match = /^data:image\/([\w+.-]+)/.exec(src);
|
|
139
|
+
return 'image.' + (match ? match[1].replace('jpeg', 'jpg').replace('svg+xml', 'svg') : 'png');
|
|
140
|
+
}
|
|
141
|
+
const url = new URL(src, location.href);
|
|
142
|
+
const raw = url.searchParams.get('path') || url.pathname;
|
|
143
|
+
const base = decodeURIComponent(raw.split(/[\\/]/).pop() || '');
|
|
144
|
+
return base || 'image';
|
|
145
|
+
} catch {
|
|
146
|
+
return 'image';
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
//#endregion
|
|
150
|
+
|
|
151
|
+
//#region spring animator
|
|
152
|
+
/**
|
|
153
|
+
* Drives numeric keys of `state` toward targets with damped springs.
|
|
154
|
+
* Springs keep their velocity when re-targeted, so a gesture can hand its
|
|
155
|
+
* finger velocity to the animation that follows it.
|
|
156
|
+
*/
|
|
157
|
+
function createAnimator(state, onFrame) {
|
|
158
|
+
const springs = new Map();
|
|
159
|
+
let calls = [];
|
|
160
|
+
let raf = 0;
|
|
161
|
+
let last = 0;
|
|
162
|
+
|
|
163
|
+
const eps = (key) => (key === 'x' || key === 'y' || key === 'trackX' || key === 'radius' ? 0.2 : 0.001);
|
|
164
|
+
|
|
165
|
+
function frame(ts) {
|
|
166
|
+
raf = 0;
|
|
167
|
+
let dt = (ts - last) / 1000;
|
|
168
|
+
last = ts;
|
|
169
|
+
if (!(dt > 0)) dt = 1 / 60;
|
|
170
|
+
if (dt > 0.064) dt = 0.064;
|
|
171
|
+
const steps = Math.ceil(dt / 0.004);
|
|
172
|
+
const step = dt / steps;
|
|
173
|
+
const settled = [];
|
|
174
|
+
for (const [key, spring] of springs) {
|
|
175
|
+
let x = state[key];
|
|
176
|
+
let v = spring.v;
|
|
177
|
+
for (let i = 0; i < steps; i += 1) {
|
|
178
|
+
const a = -spring.k * (x - spring.target) - spring.c * v;
|
|
179
|
+
v += a * step;
|
|
180
|
+
x += v * step;
|
|
181
|
+
}
|
|
182
|
+
const e = eps(key);
|
|
183
|
+
if (Math.abs(x - spring.target) < e && Math.abs(v) < e * 20) {
|
|
184
|
+
x = spring.target;
|
|
185
|
+
v = 0;
|
|
186
|
+
settled.push(key);
|
|
187
|
+
}
|
|
188
|
+
state[key] = x;
|
|
189
|
+
spring.v = v;
|
|
190
|
+
}
|
|
191
|
+
for (const key of settled) {
|
|
192
|
+
springs.delete(key);
|
|
193
|
+
for (const call of calls) call.keys.delete(key);
|
|
194
|
+
}
|
|
195
|
+
const fire = [];
|
|
196
|
+
calls = calls.filter((call) => {
|
|
197
|
+
if (call.keys.size > 0) return true;
|
|
198
|
+
fire.push(call.cb);
|
|
199
|
+
return false;
|
|
200
|
+
});
|
|
201
|
+
onFrame();
|
|
202
|
+
if (springs.size > 0 && raf === 0) raf = requestAnimationFrame(frame);
|
|
203
|
+
for (const cb of fire) cb();
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function forget(keys) {
|
|
207
|
+
for (const key of keys) {
|
|
208
|
+
springs.delete(key);
|
|
209
|
+
for (const call of calls) call.keys.delete(key);
|
|
210
|
+
}
|
|
211
|
+
calls = calls.filter((call) => call.keys.size > 0);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
return {
|
|
215
|
+
to(targets, options = {}) {
|
|
216
|
+
const cfg = options.spring || SPRING.snap;
|
|
217
|
+
const velocity = options.velocity || {};
|
|
218
|
+
const keys = Object.keys(targets);
|
|
219
|
+
// A re-targeted key no longer belongs to the call that started it.
|
|
220
|
+
for (const call of calls) for (const key of keys) call.keys.delete(key);
|
|
221
|
+
calls = calls.filter((call) => call.keys.size > 0);
|
|
222
|
+
if (reduceMotion()) {
|
|
223
|
+
for (const key of keys) {
|
|
224
|
+
springs.delete(key);
|
|
225
|
+
state[key] = targets[key];
|
|
226
|
+
}
|
|
227
|
+
onFrame();
|
|
228
|
+
if (options.onDone) options.onDone();
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
for (const key of keys) {
|
|
232
|
+
const prev = springs.get(key);
|
|
233
|
+
springs.set(key, {
|
|
234
|
+
target: targets[key],
|
|
235
|
+
v: key in velocity ? velocity[key] : prev ? prev.v : 0,
|
|
236
|
+
k: cfg.k,
|
|
237
|
+
c: cfg.c,
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
if (options.onDone) calls.push({ keys: new Set(keys), cb: options.onDone });
|
|
241
|
+
if (raf === 0 && springs.size > 0) {
|
|
242
|
+
last = now();
|
|
243
|
+
raf = requestAnimationFrame(frame);
|
|
244
|
+
}
|
|
245
|
+
},
|
|
246
|
+
/** Freeze the given keys (or all) at their current values. */
|
|
247
|
+
stop(keys) {
|
|
248
|
+
forget(keys || [...springs.keys()]);
|
|
249
|
+
if (springs.size === 0 && raf !== 0) {
|
|
250
|
+
cancelAnimationFrame(raf);
|
|
251
|
+
raf = 0;
|
|
252
|
+
}
|
|
253
|
+
},
|
|
254
|
+
destroy() {
|
|
255
|
+
springs.clear();
|
|
256
|
+
calls = [];
|
|
257
|
+
if (raf !== 0) cancelAnimationFrame(raf);
|
|
258
|
+
raf = 0;
|
|
259
|
+
},
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
//#endregion
|
|
263
|
+
|
|
264
|
+
//#region icons
|
|
265
|
+
const svg = (body) =>
|
|
266
|
+
'<svg viewBox="0 0 24 24" width="20" height="20" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">' +
|
|
267
|
+
body +
|
|
268
|
+
'</svg>';
|
|
269
|
+
const ICON = {
|
|
270
|
+
close: svg('<path d="M6 6l12 12M18 6L6 18"/>'),
|
|
271
|
+
prev: svg('<path d="M15 5l-7 7 7 7"/>'),
|
|
272
|
+
next: svg('<path d="M9 5l7 7-7 7"/>'),
|
|
273
|
+
zoomIn: svg('<circle cx="11" cy="11" r="6.5"/><path d="M20 20l-4.2-4.2M8.5 11h5M11 8.5v5"/>'),
|
|
274
|
+
zoomOut: svg('<circle cx="11" cy="11" r="6.5"/><path d="M20 20l-4.2-4.2M8.5 11h5"/>'),
|
|
275
|
+
download: svg('<path d="M12 4v11M7 10.5l5 5 5-5M5 20h14"/>'),
|
|
276
|
+
};
|
|
277
|
+
//#endregion
|
|
278
|
+
|
|
279
|
+
//#region styles
|
|
280
|
+
const CSS = `
|
|
281
|
+
[${ROOT_ATTR}]{position:fixed;inset:0;z-index:2147483000;touch-action:none;user-select:none;-webkit-user-select:none;-webkit-touch-callout:none;-webkit-tap-highlight-color:transparent;overscroll-behavior:contain;outline:none;color:#fff;font:13px/1.4 system-ui,-apple-system,"Segoe UI",Roboto,sans-serif;cursor:default}
|
|
282
|
+
[${ROOT_ATTR}] *{box-sizing:border-box}
|
|
283
|
+
.qiv-bg{position:absolute;inset:0;background:#000}
|
|
284
|
+
.qiv-track{position:absolute;inset:0;overflow:hidden}
|
|
285
|
+
.qiv-slide{position:absolute;inset:0;contain:layout style}
|
|
286
|
+
.qiv-frame{position:absolute;left:50%;top:50%;transform-origin:50% 50%;cursor:zoom-in}
|
|
287
|
+
[${ROOT_ATTR}][data-gesture] .qiv-frame,[${ROOT_ATTR}][data-state=opening] .qiv-frame{will-change:transform}
|
|
288
|
+
[${ROOT_ATTR}][data-zoomed] .qiv-frame{cursor:grab}
|
|
289
|
+
[${ROOT_ATTR}][data-gesture=pan] .qiv-frame{cursor:grabbing}
|
|
290
|
+
.qiv-frame img{display:block;width:100%;height:100%;object-fit:contain;-webkit-user-drag:none;user-select:none}
|
|
291
|
+
.qiv-status{position:absolute;inset:0;display:grid;place-items:center;pointer-events:none;font-size:13px;opacity:.85}
|
|
292
|
+
.qiv-spin{width:28px;height:28px;border-radius:50%;border:2.5px solid rgba(255,255,255,.25);border-top-color:#fff;opacity:0;animation:qiv-in .2s .25s forwards,qiv-rot .8s linear infinite}
|
|
293
|
+
@keyframes qiv-in{to{opacity:1}}
|
|
294
|
+
@keyframes qiv-rot{to{transform:rotate(360deg)}}
|
|
295
|
+
.qiv-chrome{transition:opacity .22s ease}
|
|
296
|
+
[${ROOT_ATTR}][data-chrome=hidden] .qiv-chrome,[${ROOT_ATTR}][data-state=closing] .qiv-chrome,[${ROOT_ATTR}][data-state=opening] .qiv-chrome,[${ROOT_ATTR}][data-gesture=dismiss] .qiv-chrome,[${ROOT_ATTR}][data-gesture=pinch] .qiv-chrome{opacity:0;pointer-events:none}
|
|
297
|
+
.qiv-top{position:absolute;left:0;right:0;top:0;display:flex;align-items:center;justify-content:space-between;gap:12px;padding:calc(env(safe-area-inset-top,0px) + 10px) calc(env(safe-area-inset-right,0px) + 12px) 18px calc(env(safe-area-inset-left,0px) + 18px);background:linear-gradient(rgba(0,0,0,.5),rgba(0,0,0,0))}
|
|
298
|
+
.qiv-count{font-size:14px;font-weight:600;letter-spacing:.02em;font-variant-numeric:tabular-nums;text-shadow:0 1px 2px rgba(0,0,0,.5)}
|
|
299
|
+
.qiv-actions{display:flex;gap:8px;margin-left:auto}
|
|
300
|
+
.qiv-btn{appearance:none;width:40px;height:40px;padding:0;border:0;border-radius:999px;background:rgba(255,255,255,.14);color:inherit;display:grid;place-items:center;cursor:pointer;text-decoration:none;-webkit-backdrop-filter:blur(14px);backdrop-filter:blur(14px);transition:background-color .15s,opacity .15s}
|
|
301
|
+
.qiv-btn:hover{background:rgba(255,255,255,.26)}
|
|
302
|
+
.qiv-btn:active{background:rgba(255,255,255,.34)}
|
|
303
|
+
.qiv-btn:focus-visible,.qiv-thumb:focus-visible{outline:2px solid #fff;outline-offset:2px}
|
|
304
|
+
.qiv-nav{position:absolute;top:50%;width:44px;height:44px;margin-top:-22px}
|
|
305
|
+
.qiv-prev{left:calc(env(safe-area-inset-left,0px) + 16px)}
|
|
306
|
+
.qiv-next{right:calc(env(safe-area-inset-right,0px) + 16px)}
|
|
307
|
+
.qiv-nav[disabled]{opacity:0;pointer-events:none}
|
|
308
|
+
[${ROOT_ATTR}][data-coarse] .qiv-nav,[${ROOT_ATTR}][data-single] .qiv-nav{display:none}
|
|
309
|
+
.qiv-strip{position:absolute;left:0;right:0;bottom:0;display:flex;overflow-x:auto;touch-action:pan-x;scrollbar-width:none;padding:18px 16px calc(env(safe-area-inset-bottom,0px) + 12px);background:linear-gradient(rgba(0,0,0,0),rgba(0,0,0,.5))}
|
|
310
|
+
.qiv-strip::-webkit-scrollbar{display:none}
|
|
311
|
+
.qiv-strip-inner{display:flex;gap:6px;margin:0 auto}
|
|
312
|
+
.qiv-thumb{appearance:none;flex:none;width:40px;height:40px;padding:0;border:0;border-radius:6px;overflow:hidden;background:rgba(255,255,255,.1);opacity:.5;cursor:pointer;transition:opacity .15s,transform .15s}
|
|
313
|
+
.qiv-thumb:hover{opacity:.8}
|
|
314
|
+
.qiv-thumb[aria-current=true]{opacity:1;transform:scale(1.1);box-shadow:0 0 0 2px #fff}
|
|
315
|
+
.qiv-thumb img{display:block;width:100%;height:100%;object-fit:cover;pointer-events:none}
|
|
316
|
+
.qiv-sr{position:absolute;width:1px;height:1px;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap}
|
|
317
|
+
@media (prefers-reduced-motion:reduce){.qiv-chrome,.qiv-thumb{transition:none}}
|
|
318
|
+
`;
|
|
319
|
+
|
|
320
|
+
function installStyles() {
|
|
321
|
+
if (typeof document === 'undefined') return () => {};
|
|
322
|
+
if (document.querySelector(`style[${STYLE_ATTR}]`) !== null) return () => {};
|
|
323
|
+
const tag = document.createElement('style');
|
|
324
|
+
tag.setAttribute(STYLE_ATTR, '');
|
|
325
|
+
tag.textContent = CSS;
|
|
326
|
+
document.head.appendChild(tag);
|
|
327
|
+
return () => tag.remove();
|
|
328
|
+
}
|
|
329
|
+
//#endregion
|
|
330
|
+
|
|
331
|
+
//#region image discovery
|
|
332
|
+
function isZoomCursor(node) {
|
|
333
|
+
try {
|
|
334
|
+
return String(getComputedStyle(node).cursor || '').includes('zoom-in');
|
|
335
|
+
} catch {
|
|
336
|
+
return false;
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
const srcOf = (img) => img.currentSrc || img.src || '';
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Map a click target to a previewable image, or null to let the click
|
|
344
|
+
* through. Buttons qualify only when they wrap exactly one image and show
|
|
345
|
+
* a zoom-in cursor (how DSH marks its own lightbox triggers). Bare images
|
|
346
|
+
* qualify with a zoom-in cursor or when they are large enough to not be
|
|
347
|
+
* an icon.
|
|
348
|
+
*/
|
|
349
|
+
function resolveTrigger(node) {
|
|
350
|
+
if (!(node instanceof Element)) return null;
|
|
351
|
+
if (node.closest(`[${ROOT_ATTR}],[${IGNORE_ATTR}],[aria-modal="true"],[contenteditable="true"]`) !== null) return null;
|
|
352
|
+
const host = node.closest('button,[role="button"]');
|
|
353
|
+
if (host !== null) {
|
|
354
|
+
if (host.closest('a[href]') !== null) return null;
|
|
355
|
+
const imgs = host.getElementsByTagName('img');
|
|
356
|
+
if (imgs.length !== 1) return null;
|
|
357
|
+
const img = imgs[0];
|
|
358
|
+
if (!isZoomCursor(host) && !isZoomCursor(img)) return null;
|
|
359
|
+
if (srcOf(img) === '') return null;
|
|
360
|
+
return { host, img };
|
|
361
|
+
}
|
|
362
|
+
if (node.tagName !== 'IMG') return null;
|
|
363
|
+
const img = node;
|
|
364
|
+
if (srcOf(img) === '') return null;
|
|
365
|
+
if (img.closest('a[href],nav,header,[role="toolbar"],[role="menu"],[role="menubar"],[role="listbox"],[role="tablist"]') !== null) return null;
|
|
366
|
+
if (isZoomCursor(img)) return { host: img, img };
|
|
367
|
+
const rect = img.getBoundingClientRect();
|
|
368
|
+
if (rect.width >= MIN_BARE_W && rect.height >= MIN_BARE_H) return { host: img, img };
|
|
369
|
+
return null;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
/** The nearest vertically scrolling ancestor scopes the gallery (the transcript). */
|
|
373
|
+
function scrollerOf(node) {
|
|
374
|
+
for (let el = node.parentElement; el !== null && el !== document.body; el = el.parentElement) {
|
|
375
|
+
let style;
|
|
376
|
+
try {
|
|
377
|
+
style = getComputedStyle(el);
|
|
378
|
+
} catch {
|
|
379
|
+
continue;
|
|
380
|
+
}
|
|
381
|
+
const oy = style.overflowY;
|
|
382
|
+
if (oy !== 'auto' && oy !== 'scroll' && oy !== 'overlay') continue;
|
|
383
|
+
if (el.scrollHeight > el.clientHeight + 1 || el.clientHeight >= window.innerHeight * 0.5) return el;
|
|
384
|
+
}
|
|
385
|
+
return document.body;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
function makeItem(trigger, scope) {
|
|
389
|
+
const img = trigger.img;
|
|
390
|
+
const rect = img.getBoundingClientRect();
|
|
391
|
+
return {
|
|
392
|
+
el: img,
|
|
393
|
+
host: trigger.host,
|
|
394
|
+
src: srcOf(img),
|
|
395
|
+
alt: img.getAttribute('alt') || '',
|
|
396
|
+
referrerPolicy: img.referrerPolicy || '',
|
|
397
|
+
w: img.naturalWidth || 0,
|
|
398
|
+
h: img.naturalHeight || 0,
|
|
399
|
+
fw0: rect.width,
|
|
400
|
+
fh0: rect.height,
|
|
401
|
+
scope,
|
|
402
|
+
inset: { x: 0, y: 0 },
|
|
403
|
+
index: 0,
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
function collect(trigger) {
|
|
408
|
+
const scope = scrollerOf(trigger.host);
|
|
409
|
+
const hasLayout = trigger.img.getBoundingClientRect().width > 0;
|
|
410
|
+
const seen = new Set();
|
|
411
|
+
const items = [];
|
|
412
|
+
for (const img of scope.querySelectorAll('img')) {
|
|
413
|
+
if (items.length >= MAX_ITEMS && seen.has(trigger.img)) break;
|
|
414
|
+
const tr = img === trigger.img ? trigger : resolveTrigger(img);
|
|
415
|
+
if (tr === null || seen.has(tr.img)) continue;
|
|
416
|
+
if (hasLayout && img !== trigger.img) {
|
|
417
|
+
const rect = img.getBoundingClientRect();
|
|
418
|
+
if (rect.width === 0 && rect.height === 0) continue;
|
|
419
|
+
}
|
|
420
|
+
seen.add(tr.img);
|
|
421
|
+
items.push(makeItem(tr, scope));
|
|
422
|
+
}
|
|
423
|
+
if (!seen.has(trigger.img)) items.push(makeItem(trigger, scope));
|
|
424
|
+
items.forEach((item, i) => {
|
|
425
|
+
item.index = i;
|
|
426
|
+
});
|
|
427
|
+
return { items, index: items.findIndex((item) => item.el === trigger.img) };
|
|
428
|
+
}
|
|
429
|
+
//#endregion
|
|
430
|
+
|
|
431
|
+
//#region viewer
|
|
432
|
+
function openViewer(items, startIndex, t, onDestroyed) {
|
|
433
|
+
const n = items.length;
|
|
434
|
+
const coarse = media('(pointer: coarse)');
|
|
435
|
+
const restoreFocus = document.activeElement instanceof HTMLElement ? document.activeElement : null;
|
|
436
|
+
const s = { x: 0, y: 0, scale: 1, clip: 0, radius: 0, bg: 0, alpha: 1, trackX: 0 };
|
|
437
|
+
const slides = new Map();
|
|
438
|
+
const pointers = new Map();
|
|
439
|
+
let index = startIndex;
|
|
440
|
+
let vw = window.innerWidth;
|
|
441
|
+
let vh = window.innerHeight;
|
|
442
|
+
let outgoing = null;
|
|
443
|
+
let closing = false;
|
|
444
|
+
let destroyed = false;
|
|
445
|
+
let gesture = null;
|
|
446
|
+
let samples = [];
|
|
447
|
+
let tapTimer = 0;
|
|
448
|
+
let lastTap = null;
|
|
449
|
+
let lastZoomed = null;
|
|
450
|
+
let hiddenItem = null;
|
|
451
|
+
let wheelAccum = 0;
|
|
452
|
+
let wheelLast = 0;
|
|
453
|
+
let wheelLock = 0;
|
|
454
|
+
|
|
455
|
+
//#region dom
|
|
456
|
+
const root = h('div', 'qiv', { role: 'dialog', 'aria-modal': 'true', 'aria-label': t('dialog'), tabindex: '-1' });
|
|
457
|
+
root.setAttribute(ROOT_ATTR, '');
|
|
458
|
+
root.dataset.state = 'opening';
|
|
459
|
+
if (coarse) root.setAttribute('data-coarse', '');
|
|
460
|
+
if (n <= 1) root.setAttribute('data-single', '');
|
|
461
|
+
const bg = h('div', 'qiv-bg');
|
|
462
|
+
const track = h('div', 'qiv-track');
|
|
463
|
+
const top = h('div', 'qiv-top qiv-chrome');
|
|
464
|
+
const counter = h('div', 'qiv-count', { 'aria-hidden': 'true' });
|
|
465
|
+
const actions = h('div', 'qiv-actions');
|
|
466
|
+
const iconButton = (className, label, icon) => {
|
|
467
|
+
const button = h('button', className, { type: 'button', 'aria-label': label, title: label });
|
|
468
|
+
button.innerHTML = icon;
|
|
469
|
+
return button;
|
|
470
|
+
};
|
|
471
|
+
const zoomBtn = iconButton('qiv-btn', t('zoomIn'), ICON.zoomIn);
|
|
472
|
+
const download = h('a', 'qiv-btn', { 'aria-label': t('download'), title: t('download'), rel: 'noopener', target: '_blank' });
|
|
473
|
+
download.innerHTML = ICON.download;
|
|
474
|
+
const closeBtn = iconButton('qiv-btn', t('close'), ICON.close);
|
|
475
|
+
actions.append(zoomBtn, download, closeBtn);
|
|
476
|
+
top.append(counter, actions);
|
|
477
|
+
const prevBtn = iconButton('qiv-btn qiv-nav qiv-prev qiv-chrome', t('prev'), ICON.prev);
|
|
478
|
+
const nextBtn = iconButton('qiv-btn qiv-nav qiv-next qiv-chrome', t('next'), ICON.next);
|
|
479
|
+
const live = h('div', 'qiv-sr', { 'aria-live': 'polite' });
|
|
480
|
+
root.append(bg, track, top, prevBtn, nextBtn, live);
|
|
481
|
+
|
|
482
|
+
const thumbs = [];
|
|
483
|
+
let strip = null;
|
|
484
|
+
if (n > 1) {
|
|
485
|
+
strip = h('div', 'qiv-strip qiv-chrome');
|
|
486
|
+
const inner = h('div', 'qiv-strip-inner');
|
|
487
|
+
items.forEach((item, i) => {
|
|
488
|
+
const button = h('button', 'qiv-thumb', { type: 'button', 'aria-label': t('thumb', { i: i + 1 }) });
|
|
489
|
+
const img = h('img', '', { alt: '', loading: 'lazy', decoding: 'async', draggable: 'false' });
|
|
490
|
+
if (item.referrerPolicy) img.referrerPolicy = item.referrerPolicy;
|
|
491
|
+
img.src = item.src;
|
|
492
|
+
button.append(img);
|
|
493
|
+
button.addEventListener('click', () => jumpTo(i));
|
|
494
|
+
inner.append(button);
|
|
495
|
+
thumbs.push(button);
|
|
496
|
+
});
|
|
497
|
+
strip.append(inner);
|
|
498
|
+
root.append(strip);
|
|
499
|
+
}
|
|
500
|
+
//#endregion
|
|
501
|
+
|
|
502
|
+
//#region geometry
|
|
503
|
+
function pads() {
|
|
504
|
+
if (coarse) return { x: 0, y: 0 };
|
|
505
|
+
return { x: n > 1 ? 76 : 32, y: n > 1 ? 84 : 64 };
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
/** Displayed size at scale 1, and its ratio to natural pixels. */
|
|
509
|
+
function fitOf(i) {
|
|
510
|
+
const item = items[i];
|
|
511
|
+
const pad = pads();
|
|
512
|
+
const aw = Math.max(40, vw - pad.x * 2);
|
|
513
|
+
const ah = Math.max(40, vh - pad.y * 2);
|
|
514
|
+
const nw = item.w || item.fw0 || aw;
|
|
515
|
+
const nh = item.h || item.fh0 || ah;
|
|
516
|
+
let k = Math.min(aw / nw, ah / nh);
|
|
517
|
+
// Desktop never upsamples past natural size; touch fills the screen like Photos.
|
|
518
|
+
if (!coarse && item.w > 0) k = Math.min(k, 1);
|
|
519
|
+
return { w: nw * k, h: nh * k, k };
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
function maxScaleOf(i) {
|
|
523
|
+
return clamp(2 / Math.max(fitOf(i).k, 0.01), 4, 12);
|
|
524
|
+
}
|
|
525
|
+
|
|
526
|
+
function doubleTapScale() {
|
|
527
|
+
const f = fitOf(index);
|
|
528
|
+
const fill = Math.max(vw / f.w, vh / f.h);
|
|
529
|
+
// Fill the screen only when that is a meaningful zoom; otherwise 2.5x like Photos.
|
|
530
|
+
return clamp(fill >= 1.8 && fill <= 4 ? fill : 2.5, 1, maxScaleOf(index));
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
function bounds(scale) {
|
|
534
|
+
const f = fitOf(index);
|
|
535
|
+
return { x: Math.max(0, (f.w * scale - vw) / 2), y: Math.max(0, (f.h * scale - vh) / 2) };
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
/** Translation that keeps screen point (px, py) fixed while scaling to `scale`. */
|
|
539
|
+
function focal(scale, px, py, from = s) {
|
|
540
|
+
const cx = vw / 2;
|
|
541
|
+
const cy = vh / 2;
|
|
542
|
+
const lx = (px - cx - from.x) / from.scale;
|
|
543
|
+
const ly = (py - cy - from.y) / from.scale;
|
|
544
|
+
return { x: px - cx - lx * scale, y: py - cy - ly * scale };
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function hitImage(px, py) {
|
|
548
|
+
const f = fitOf(index);
|
|
549
|
+
return Math.abs(px - (vw / 2 + s.x)) <= (f.w * s.scale) / 2 && Math.abs(py - (vh / 2 + s.y)) <= (f.h * s.scale) / 2;
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
function radiusOf(item, rect) {
|
|
553
|
+
let r = 0;
|
|
554
|
+
for (const node of [item.el, item.host]) {
|
|
555
|
+
try {
|
|
556
|
+
r = Math.max(r, cssPx(getComputedStyle(node).borderTopLeftRadius, rect.width));
|
|
557
|
+
} catch {
|
|
558
|
+
// detached
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
return Math.min(r, rect.width / 2, rect.height / 2);
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
/** Where the thumbnail of `item` sits, expressed as viewer state; null if not visible. */
|
|
565
|
+
function sourceTarget(item) {
|
|
566
|
+
const el = item.el;
|
|
567
|
+
if (!el || !el.isConnected) return null;
|
|
568
|
+
const rect = el.getBoundingClientRect();
|
|
569
|
+
if (rect.width < 2 || rect.height < 2) return null;
|
|
570
|
+
if (rect.bottom < 0 || rect.top > vh || rect.right < 0 || rect.left > vw) return null;
|
|
571
|
+
if (item.scope && item.scope !== document.body) {
|
|
572
|
+
const box = item.scope.getBoundingClientRect();
|
|
573
|
+
if (rect.bottom < box.top || rect.top > box.bottom) return null;
|
|
574
|
+
}
|
|
575
|
+
const f = fitOf(item.index);
|
|
576
|
+
let cover = false;
|
|
577
|
+
try {
|
|
578
|
+
cover = getComputedStyle(el).objectFit === 'cover';
|
|
579
|
+
} catch {
|
|
580
|
+
cover = false;
|
|
581
|
+
}
|
|
582
|
+
let scale;
|
|
583
|
+
const inset = { x: 0, y: 0 };
|
|
584
|
+
if (cover) {
|
|
585
|
+
scale = Math.max(rect.width / f.w, rect.height / f.h);
|
|
586
|
+
inset.x = Math.max(0, (f.w - rect.width / scale) / 2);
|
|
587
|
+
inset.y = Math.max(0, (f.h - rect.height / scale) / 2);
|
|
588
|
+
} else {
|
|
589
|
+
scale = Math.min(rect.width / f.w, rect.height / f.h);
|
|
590
|
+
}
|
|
591
|
+
return {
|
|
592
|
+
x: rect.left + rect.width / 2 - vw / 2,
|
|
593
|
+
y: rect.top + rect.height / 2 - vh / 2,
|
|
594
|
+
scale,
|
|
595
|
+
inset,
|
|
596
|
+
radius: radiusOf(item, rect),
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
//#endregion
|
|
600
|
+
|
|
601
|
+
//#region slides + render
|
|
602
|
+
function createSlide(i) {
|
|
603
|
+
const item = items[i];
|
|
604
|
+
const slide = h('div', 'qiv-slide');
|
|
605
|
+
const frame = h('div', 'qiv-frame');
|
|
606
|
+
const img = h('img', '', { alt: item.alt, draggable: 'false', decoding: 'async' });
|
|
607
|
+
if (item.referrerPolicy) img.referrerPolicy = item.referrerPolicy;
|
|
608
|
+
const status = h('div', 'qiv-status');
|
|
609
|
+
frame.append(img);
|
|
610
|
+
slide.append(frame, status);
|
|
611
|
+
const record = { slide, frame, img, status, key: '' };
|
|
612
|
+
const ready = () => {
|
|
613
|
+
if (img.naturalWidth > 0) {
|
|
614
|
+
item.w = img.naturalWidth;
|
|
615
|
+
item.h = img.naturalHeight;
|
|
616
|
+
}
|
|
617
|
+
status.replaceChildren();
|
|
618
|
+
if (i === index && gesture === null) clampView();
|
|
619
|
+
render();
|
|
620
|
+
};
|
|
621
|
+
img.addEventListener('load', ready, { once: true });
|
|
622
|
+
img.addEventListener('error', () => {
|
|
623
|
+
status.replaceChildren(document.createTextNode(t('failed')));
|
|
624
|
+
}, { once: true });
|
|
625
|
+
img.src = item.src;
|
|
626
|
+
if (!(img.complete && img.naturalWidth > 0)) status.append(h('div', 'qiv-spin'));
|
|
627
|
+
track.append(slide);
|
|
628
|
+
return record;
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
function syncSlides() {
|
|
632
|
+
const want = new Set();
|
|
633
|
+
for (const i of [index - 1, index, index + 1]) if (i >= 0 && i < n) want.add(i);
|
|
634
|
+
if (outgoing !== null) want.add(outgoing.index);
|
|
635
|
+
for (const [i, record] of slides) {
|
|
636
|
+
if (!want.has(i)) {
|
|
637
|
+
record.slide.remove();
|
|
638
|
+
slides.delete(i);
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
for (const i of want) if (!slides.has(i)) slides.set(i, createSlide(i));
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
function render() {
|
|
645
|
+
if (destroyed) return;
|
|
646
|
+
const step = vw + GAP;
|
|
647
|
+
bg.style.opacity = String(clamp(s.bg, 0, 1));
|
|
648
|
+
for (const [i, record] of slides) {
|
|
649
|
+
const f = fitOf(i);
|
|
650
|
+
const key = f.w.toFixed(2) + 'x' + f.h.toFixed(2);
|
|
651
|
+
if (record.key !== key) {
|
|
652
|
+
record.key = key;
|
|
653
|
+
const st = record.frame.style;
|
|
654
|
+
st.width = f.w + 'px';
|
|
655
|
+
st.height = f.h + 'px';
|
|
656
|
+
st.marginLeft = -f.w / 2 + 'px';
|
|
657
|
+
st.marginTop = -f.h / 2 + 'px';
|
|
658
|
+
}
|
|
659
|
+
record.slide.style.transform = `translate3d(${(i - index) * step + s.trackX}px,0,0)`;
|
|
660
|
+
const st = record.frame.style;
|
|
661
|
+
if (i === index) {
|
|
662
|
+
st.transform = `translate3d(${s.x}px,${s.y}px,0) scale(${s.scale})`;
|
|
663
|
+
st.opacity = s.alpha >= 0.999 ? '' : String(clamp(s.alpha, 0, 1));
|
|
664
|
+
if (s.clip > 0.001 || s.radius > 0.2) {
|
|
665
|
+
const inset = items[i].inset;
|
|
666
|
+
const r = s.radius / Math.max(s.scale, 0.01);
|
|
667
|
+
st.clipPath = `inset(${inset.y * s.clip}px ${inset.x * s.clip}px round ${r}px)`;
|
|
668
|
+
} else {
|
|
669
|
+
st.clipPath = '';
|
|
670
|
+
}
|
|
671
|
+
} else if (outgoing !== null && i === outgoing.index) {
|
|
672
|
+
st.transform = `translate3d(${outgoing.x}px,${outgoing.y}px,0) scale(${outgoing.scale})`;
|
|
673
|
+
st.clipPath = '';
|
|
674
|
+
st.opacity = '';
|
|
675
|
+
} else {
|
|
676
|
+
st.transform = '';
|
|
677
|
+
st.clipPath = '';
|
|
678
|
+
st.opacity = '';
|
|
679
|
+
}
|
|
680
|
+
}
|
|
681
|
+
const zoomed = s.scale > 1.01;
|
|
682
|
+
if (zoomed !== lastZoomed) {
|
|
683
|
+
lastZoomed = zoomed;
|
|
684
|
+
root.toggleAttribute('data-zoomed', zoomed);
|
|
685
|
+
const label = zoomed ? t('zoomOut') : t('zoomIn');
|
|
686
|
+
zoomBtn.innerHTML = zoomed ? ICON.zoomOut : ICON.zoomIn;
|
|
687
|
+
zoomBtn.setAttribute('aria-label', label);
|
|
688
|
+
zoomBtn.title = label;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
|
|
692
|
+
function hideSource(item) {
|
|
693
|
+
if (hiddenItem === item) return;
|
|
694
|
+
showSource();
|
|
695
|
+
if (item && item.el.isConnected) {
|
|
696
|
+
item.prevVisibility = item.el.style.visibility;
|
|
697
|
+
item.el.style.visibility = 'hidden';
|
|
698
|
+
hiddenItem = item;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
function showSource() {
|
|
703
|
+
if (hiddenItem === null) return;
|
|
704
|
+
hiddenItem.el.style.visibility = hiddenItem.prevVisibility || '';
|
|
705
|
+
hiddenItem = null;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
function updateChrome() {
|
|
709
|
+
const item = items[index];
|
|
710
|
+
const text = n > 1 ? t('counter', { i: index + 1, n }) : '';
|
|
711
|
+
counter.textContent = text;
|
|
712
|
+
live.textContent = text;
|
|
713
|
+
prevBtn.disabled = index === 0;
|
|
714
|
+
nextBtn.disabled = index === n - 1;
|
|
715
|
+
download.href = item.src;
|
|
716
|
+
download.setAttribute('download', fileName(item.src));
|
|
717
|
+
thumbs.forEach((thumb, i) => thumb.setAttribute('aria-current', i === index ? 'true' : 'false'));
|
|
718
|
+
if (strip !== null && thumbs[index]) {
|
|
719
|
+
const thumb = thumbs[index];
|
|
720
|
+
const left = thumb.offsetLeft - strip.clientWidth / 2 + thumb.offsetWidth / 2;
|
|
721
|
+
if (typeof strip.scrollTo === 'function') strip.scrollTo({ left, behavior: reduceMotion() ? 'auto' : 'smooth' });
|
|
722
|
+
else strip.scrollLeft = left;
|
|
723
|
+
}
|
|
724
|
+
hideSource(item);
|
|
725
|
+
}
|
|
726
|
+
//#endregion
|
|
727
|
+
|
|
728
|
+
//#region animation helpers
|
|
729
|
+
const anim = createAnimator(s, render);
|
|
730
|
+
|
|
731
|
+
function clearOutgoing() {
|
|
732
|
+
if (outgoing === null) return;
|
|
733
|
+
outgoing = null;
|
|
734
|
+
syncSlides();
|
|
735
|
+
render();
|
|
736
|
+
}
|
|
737
|
+
|
|
738
|
+
function clampView() {
|
|
739
|
+
const b = bounds(s.scale);
|
|
740
|
+
s.x = clamp(s.x, -b.x, b.x);
|
|
741
|
+
s.y = clamp(s.y, -b.y, b.y);
|
|
742
|
+
}
|
|
743
|
+
|
|
744
|
+
/** Settle zoom/pan into range; velocities in px/ms become momentum. */
|
|
745
|
+
function settleView(vx = 0, vy = 0, fx = vw / 2, fy = vh / 2) {
|
|
746
|
+
const scale = clamp(s.scale, 1, maxScaleOf(index));
|
|
747
|
+
let tx = s.x;
|
|
748
|
+
let ty = s.y;
|
|
749
|
+
if (Math.abs(scale - s.scale) > 1e-4) {
|
|
750
|
+
const p = focal(scale, fx, fy);
|
|
751
|
+
tx = p.x;
|
|
752
|
+
ty = p.y;
|
|
753
|
+
}
|
|
754
|
+
tx += (vx * DECEL) / (1 - DECEL);
|
|
755
|
+
ty += (vy * DECEL) / (1 - DECEL);
|
|
756
|
+
const b = bounds(scale);
|
|
757
|
+
tx = clamp(tx, -b.x, b.x);
|
|
758
|
+
ty = clamp(ty, -b.y, b.y);
|
|
759
|
+
const moving = vx !== 0 || vy !== 0;
|
|
760
|
+
anim.to(
|
|
761
|
+
{ x: tx, y: ty, scale, bg: 1, alpha: 1 },
|
|
762
|
+
{ spring: moving ? SPRING.glide : SPRING.snap, velocity: { x: vx * 1000, y: vy * 1000 } },
|
|
763
|
+
);
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
function settleTrack(v = 0) {
|
|
767
|
+
if (Math.abs(s.trackX) < 0.5 && v === 0) {
|
|
768
|
+
s.trackX = 0;
|
|
769
|
+
clearOutgoing();
|
|
770
|
+
return;
|
|
771
|
+
}
|
|
772
|
+
anim.to({ trackX: 0 }, { spring: SPRING.page, velocity: { trackX: v }, onDone: clearOutgoing });
|
|
773
|
+
}
|
|
774
|
+
|
|
775
|
+
function commitPage(dir, v = 0) {
|
|
776
|
+
const step = vw + GAP;
|
|
777
|
+
anim.stop(['x', 'y', 'scale', 'clip', 'radius', 'trackX']);
|
|
778
|
+
outgoing = { index, x: s.x, y: s.y, scale: s.scale };
|
|
779
|
+
index += dir;
|
|
780
|
+
s.trackX += dir * step;
|
|
781
|
+
s.x = 0;
|
|
782
|
+
s.y = 0;
|
|
783
|
+
s.scale = 1;
|
|
784
|
+
s.clip = 0;
|
|
785
|
+
s.radius = 0;
|
|
786
|
+
s.bg = 1;
|
|
787
|
+
s.alpha = 1;
|
|
788
|
+
syncSlides();
|
|
789
|
+
updateChrome();
|
|
790
|
+
render();
|
|
791
|
+
anim.to({ trackX: 0 }, { spring: SPRING.page, velocity: { trackX: v }, onDone: clearOutgoing });
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
function go(dir) {
|
|
795
|
+
const target = index + dir;
|
|
796
|
+
if (target < 0 || target >= n || closing) return;
|
|
797
|
+
commitPage(dir, 0);
|
|
798
|
+
}
|
|
799
|
+
|
|
800
|
+
function jumpTo(target) {
|
|
801
|
+
if (target === index || target < 0 || target >= n || closing) return;
|
|
802
|
+
if (Math.abs(target - index) === 1) {
|
|
803
|
+
commitPage(target - index, 0);
|
|
804
|
+
return;
|
|
805
|
+
}
|
|
806
|
+
const dir = target > index ? 1 : -1;
|
|
807
|
+
anim.stop();
|
|
808
|
+
outgoing = null;
|
|
809
|
+
index = target;
|
|
810
|
+
Object.assign(s, { x: 0, y: 0, scale: 1, clip: 0, radius: 0, bg: 1, alpha: 1, trackX: dir * (vw + GAP) * 0.35 });
|
|
811
|
+
syncSlides();
|
|
812
|
+
updateChrome();
|
|
813
|
+
render();
|
|
814
|
+
anim.to({ trackX: 0 }, { spring: SPRING.page });
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
function toggleZoom(px, py) {
|
|
818
|
+
if (s.scale > 1.01) {
|
|
819
|
+
anim.to({ scale: 1, x: 0, y: 0 }, { spring: SPRING.snap });
|
|
820
|
+
return;
|
|
821
|
+
}
|
|
822
|
+
const scale = doubleTapScale();
|
|
823
|
+
const p = focal(scale, px, py);
|
|
824
|
+
const b = bounds(scale);
|
|
825
|
+
anim.to({ scale, x: clamp(p.x, -b.x, b.x), y: clamp(p.y, -b.y, b.y) }, { spring: SPRING.snap });
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
function zoomBy(factor, px, py, animate) {
|
|
829
|
+
const scale = clamp(s.scale * factor, 1, maxScaleOf(index));
|
|
830
|
+
const p = focal(scale, px, py);
|
|
831
|
+
const b = bounds(scale);
|
|
832
|
+
const target = { scale, x: clamp(p.x, -b.x, b.x), y: clamp(p.y, -b.y, b.y) };
|
|
833
|
+
if (animate) {
|
|
834
|
+
anim.to(target, { spring: SPRING.snap });
|
|
835
|
+
} else {
|
|
836
|
+
anim.stop(['x', 'y', 'scale']);
|
|
837
|
+
Object.assign(s, target);
|
|
838
|
+
render();
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
|
|
842
|
+
function toggleChrome() {
|
|
843
|
+
root.dataset.chrome = root.dataset.chrome === 'hidden' ? 'shown' : 'hidden';
|
|
844
|
+
}
|
|
845
|
+
//#endregion
|
|
846
|
+
|
|
847
|
+
//#region pointer gestures
|
|
848
|
+
function clearTap() {
|
|
849
|
+
if (tapTimer !== 0) clearTimeout(tapTimer);
|
|
850
|
+
tapTimer = 0;
|
|
851
|
+
lastTap = null;
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
function velocity() {
|
|
855
|
+
const tNow = now();
|
|
856
|
+
const recent = samples.filter((p) => tNow - p.t <= 100);
|
|
857
|
+
if (recent.length < 2) return { x: 0, y: 0 };
|
|
858
|
+
const a = recent[0];
|
|
859
|
+
const b = recent[recent.length - 1];
|
|
860
|
+
const dt = b.t - a.t;
|
|
861
|
+
if (dt <= 0 || tNow - b.t > 60) return { x: 0, y: 0 };
|
|
862
|
+
return { x: (b.x - a.x) / dt, y: (b.y - a.y) / dt };
|
|
863
|
+
}
|
|
864
|
+
|
|
865
|
+
function setGesture(mode) {
|
|
866
|
+
if (mode === null) delete root.dataset.gesture;
|
|
867
|
+
else root.dataset.gesture = mode;
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
function pointPair() {
|
|
871
|
+
const list = [...pointers.values()];
|
|
872
|
+
return [list[0], list[1]];
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
function startPinch() {
|
|
876
|
+
clearTap();
|
|
877
|
+
const [a, b] = pointPair();
|
|
878
|
+
gesture = {
|
|
879
|
+
mode: 'pinch',
|
|
880
|
+
d0: Math.max(1, Math.hypot(a.x - b.x, a.y - b.y)),
|
|
881
|
+
s0: s.scale,
|
|
882
|
+
x0: s.x,
|
|
883
|
+
y0: s.y,
|
|
884
|
+
mx: (a.x + b.x) / 2,
|
|
885
|
+
my: (a.y + b.y) / 2,
|
|
886
|
+
lastMx: (a.x + b.x) / 2,
|
|
887
|
+
lastMy: (a.y + b.y) / 2,
|
|
888
|
+
};
|
|
889
|
+
setGesture('pinch');
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
function startSingle(id, p) {
|
|
893
|
+
gesture = {
|
|
894
|
+
mode: 'pending',
|
|
895
|
+
id,
|
|
896
|
+
type: p.type,
|
|
897
|
+
sx: p.x,
|
|
898
|
+
sy: p.y,
|
|
899
|
+
t0: now(),
|
|
900
|
+
x0: s.x,
|
|
901
|
+
y0: s.y,
|
|
902
|
+
track0: s.trackX,
|
|
903
|
+
atMin: false,
|
|
904
|
+
atMax: false,
|
|
905
|
+
};
|
|
906
|
+
samples = [{ t: now(), x: p.x, y: p.y }];
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
function classify(dx, dy) {
|
|
910
|
+
if (Math.abs(s.trackX) > 1) return 'page';
|
|
911
|
+
if (s.scale > 1.01) return 'pan';
|
|
912
|
+
return Math.abs(dx) > Math.abs(dy) ? 'page' : 'dismiss';
|
|
913
|
+
}
|
|
914
|
+
|
|
915
|
+
function onDown(e) {
|
|
916
|
+
if (closing) return;
|
|
917
|
+
if (e.pointerType === 'mouse' && e.button !== 0) return;
|
|
918
|
+
if (e.target instanceof Element && e.target.closest('.qiv-chrome') !== null) return;
|
|
919
|
+
e.preventDefault();
|
|
920
|
+
try {
|
|
921
|
+
root.setPointerCapture(e.pointerId);
|
|
922
|
+
} catch {
|
|
923
|
+
// capture is best-effort
|
|
924
|
+
}
|
|
925
|
+
const p = { x: e.clientX, y: e.clientY, type: e.pointerType || 'mouse' };
|
|
926
|
+
pointers.set(e.pointerId, p);
|
|
927
|
+
anim.stop(['x', 'y', 'scale', 'bg', 'alpha', 'trackX']);
|
|
928
|
+
if (pointers.size === 1) startSingle(e.pointerId, p);
|
|
929
|
+
else if (pointers.size === 2) startPinch();
|
|
930
|
+
}
|
|
931
|
+
|
|
932
|
+
function movePinch() {
|
|
933
|
+
const [a, b] = pointPair();
|
|
934
|
+
const g = gesture;
|
|
935
|
+
const d = Math.hypot(a.x - b.x, a.y - b.y);
|
|
936
|
+
const mx = (a.x + b.x) / 2;
|
|
937
|
+
const my = (a.y + b.y) / 2;
|
|
938
|
+
const max = maxScaleOf(index);
|
|
939
|
+
const raw = (g.s0 * d) / g.d0;
|
|
940
|
+
let scale = raw;
|
|
941
|
+
if (raw > max) scale = max * Math.pow(raw / max, 0.35);
|
|
942
|
+
else if (raw < 1) scale = Math.pow(raw, 0.7);
|
|
943
|
+
scale = Math.max(0.25, scale);
|
|
944
|
+
const cx = vw / 2;
|
|
945
|
+
const cy = vh / 2;
|
|
946
|
+
const lx = (g.mx - cx - g.x0) / g.s0;
|
|
947
|
+
const ly = (g.my - cy - g.y0) / g.s0;
|
|
948
|
+
s.scale = scale;
|
|
949
|
+
s.x = mx - cx - lx * scale;
|
|
950
|
+
s.y = my - cy - ly * scale;
|
|
951
|
+
s.bg = scale < 1 ? clamp((scale - 0.4) / 0.6, 0, 1) : 1;
|
|
952
|
+
g.lastMx = mx;
|
|
953
|
+
g.lastMy = my;
|
|
954
|
+
render();
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
function onMove(e) {
|
|
958
|
+
const p = pointers.get(e.pointerId);
|
|
959
|
+
if (p === undefined) return;
|
|
960
|
+
p.x = e.clientX;
|
|
961
|
+
p.y = e.clientY;
|
|
962
|
+
const g = gesture;
|
|
963
|
+
if (g === null) return;
|
|
964
|
+
if (g.mode === 'pinch') {
|
|
965
|
+
if (pointers.size >= 2) movePinch();
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
if (e.pointerId !== g.id) return;
|
|
969
|
+
samples.push({ t: now(), x: p.x, y: p.y });
|
|
970
|
+
if (samples.length > 24) samples.splice(0, samples.length - 24);
|
|
971
|
+
let dx = p.x - g.sx;
|
|
972
|
+
let dy = p.y - g.sy;
|
|
973
|
+
if (g.mode === 'pending') {
|
|
974
|
+
if (Math.hypot(dx, dy) < SLOP) return;
|
|
975
|
+
clearTap();
|
|
976
|
+
g.mode = classify(dx, dy);
|
|
977
|
+
// Rebase so the drag starts from here instead of jumping by SLOP.
|
|
978
|
+
g.sx = p.x;
|
|
979
|
+
g.sy = p.y;
|
|
980
|
+
g.x0 = s.x;
|
|
981
|
+
g.y0 = s.y;
|
|
982
|
+
g.track0 = s.trackX;
|
|
983
|
+
if (g.mode === 'pan') {
|
|
984
|
+
const b = bounds(s.scale);
|
|
985
|
+
g.atMin = s.x <= -b.x + 1;
|
|
986
|
+
g.atMax = s.x >= b.x - 1;
|
|
987
|
+
}
|
|
988
|
+
setGesture(g.mode);
|
|
989
|
+
return;
|
|
990
|
+
}
|
|
991
|
+
if (g.mode === 'pan') {
|
|
992
|
+
const b = bounds(s.scale);
|
|
993
|
+
const nx = g.x0 + dx;
|
|
994
|
+
const ny = g.y0 + dy;
|
|
995
|
+
const horizontal = Math.abs(dx) > Math.abs(dy);
|
|
996
|
+
// Zoomed photo already at its edge: keep dragging to reach the neighbour.
|
|
997
|
+
if (horizontal && nx > b.x && g.atMax && index > 0) {
|
|
998
|
+
g.mode = 'page';
|
|
999
|
+
g.fromPan = true;
|
|
1000
|
+
// trackX = track0 + dx must equal the overflow nx - b.x.
|
|
1001
|
+
g.track0 = s.trackX + g.x0 - b.x;
|
|
1002
|
+
s.x = b.x;
|
|
1003
|
+
setGesture('page');
|
|
1004
|
+
} else if (horizontal && nx < -b.x && g.atMin && index < n - 1) {
|
|
1005
|
+
g.mode = 'page';
|
|
1006
|
+
g.fromPan = true;
|
|
1007
|
+
g.track0 = s.trackX + g.x0 + b.x;
|
|
1008
|
+
s.x = -b.x;
|
|
1009
|
+
setGesture('page');
|
|
1010
|
+
} else {
|
|
1011
|
+
s.x = rubberClamp(nx, -b.x, b.x, vw);
|
|
1012
|
+
s.y = rubberClamp(ny, -b.y, b.y, vh);
|
|
1013
|
+
render();
|
|
1014
|
+
return;
|
|
1015
|
+
}
|
|
1016
|
+
}
|
|
1017
|
+
if (g.mode === 'page') {
|
|
1018
|
+
let tx = g.track0 + dx;
|
|
1019
|
+
if ((tx > 0 && index === 0) || (tx < 0 && index === n - 1)) tx = rubber(tx, vw);
|
|
1020
|
+
s.trackX = tx;
|
|
1021
|
+
} else if (g.mode === 'dismiss') {
|
|
1022
|
+
s.x = g.x0 + dx;
|
|
1023
|
+
s.y = g.y0 + dy;
|
|
1024
|
+
const progress = clamp(Math.abs(s.y) / (vh * 0.5), 0, 1);
|
|
1025
|
+
s.scale = 1 - 0.3 * progress;
|
|
1026
|
+
s.bg = 1 - Math.min(1, Math.abs(s.y) / (vh * 0.35));
|
|
1027
|
+
}
|
|
1028
|
+
render();
|
|
1029
|
+
}
|
|
1030
|
+
|
|
1031
|
+
function onTap(e, g) {
|
|
1032
|
+
const tNow = now();
|
|
1033
|
+
const x = e.clientX;
|
|
1034
|
+
const y = e.clientY;
|
|
1035
|
+
const mouse = g.type === 'mouse';
|
|
1036
|
+
if (lastTap !== null && tNow - lastTap.t < DOUBLE_TAP_MS && Math.hypot(x - lastTap.x, y - lastTap.y) < DOUBLE_TAP_PX) {
|
|
1037
|
+
clearTap();
|
|
1038
|
+
toggleZoom(x, y);
|
|
1039
|
+
return;
|
|
1040
|
+
}
|
|
1041
|
+
clearTap();
|
|
1042
|
+
if (mouse && !hitImage(x, y)) {
|
|
1043
|
+
close();
|
|
1044
|
+
return;
|
|
1045
|
+
}
|
|
1046
|
+
lastTap = { t: tNow, x, y };
|
|
1047
|
+
tapTimer = setTimeout(() => {
|
|
1048
|
+
tapTimer = 0;
|
|
1049
|
+
lastTap = null;
|
|
1050
|
+
if (!mouse) toggleChrome();
|
|
1051
|
+
}, DOUBLE_TAP_MS);
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
function releasePage(vx) {
|
|
1055
|
+
const step = vw + GAP;
|
|
1056
|
+
let dir = 0;
|
|
1057
|
+
if ((s.trackX < -step * 0.5 || (vx < -0.3 && s.trackX < -10)) && index < n - 1) dir = 1;
|
|
1058
|
+
else if ((s.trackX > step * 0.5 || (vx > 0.3 && s.trackX > 10)) && index > 0) dir = -1;
|
|
1059
|
+
if (dir !== 0) commitPage(dir, vx * 1000);
|
|
1060
|
+
else settleTrack(vx * 1000);
|
|
1061
|
+
}
|
|
1062
|
+
|
|
1063
|
+
function releaseDismiss(v) {
|
|
1064
|
+
const dy = s.y;
|
|
1065
|
+
const against = Math.abs(v.y) > 0.3 && Math.sign(v.y) !== Math.sign(dy);
|
|
1066
|
+
const fling = Math.abs(v.y) > 0.45 && Math.sign(v.y) === Math.sign(dy);
|
|
1067
|
+
if (!against && (fling || Math.abs(dy) > Math.min(140, vh * 0.14))) {
|
|
1068
|
+
close({ x: v.x * 1000, y: v.y * 1000 });
|
|
1069
|
+
return;
|
|
1070
|
+
}
|
|
1071
|
+
anim.to({ x: 0, y: 0, scale: 1, bg: 1 }, { spring: SPRING.snap, velocity: { x: v.x * 1000, y: v.y * 1000 } });
|
|
1072
|
+
}
|
|
1073
|
+
|
|
1074
|
+
function onUp(e) {
|
|
1075
|
+
if (!pointers.has(e.pointerId)) return;
|
|
1076
|
+
pointers.delete(e.pointerId);
|
|
1077
|
+
try {
|
|
1078
|
+
root.releasePointerCapture(e.pointerId);
|
|
1079
|
+
} catch {
|
|
1080
|
+
// already released
|
|
1081
|
+
}
|
|
1082
|
+
const g = gesture;
|
|
1083
|
+
if (g === null) return;
|
|
1084
|
+
const cancelled = e.type === 'pointercancel';
|
|
1085
|
+
if (g.mode === 'pinch') {
|
|
1086
|
+
if (pointers.size === 1) {
|
|
1087
|
+
// One finger left: keep going as a pan from where it is.
|
|
1088
|
+
const [id, p] = [...pointers.entries()][0];
|
|
1089
|
+
startSingle(id, p);
|
|
1090
|
+
gesture.mode = 'pan';
|
|
1091
|
+
const b = bounds(s.scale);
|
|
1092
|
+
gesture.atMin = s.x <= -b.x + 1;
|
|
1093
|
+
gesture.atMax = s.x >= b.x - 1;
|
|
1094
|
+
setGesture('pan');
|
|
1095
|
+
return;
|
|
1096
|
+
}
|
|
1097
|
+
if (pointers.size === 0) {
|
|
1098
|
+
gesture = null;
|
|
1099
|
+
setGesture(null);
|
|
1100
|
+
if (s.scale < 0.8 && g.s0 <= 1.05) close();
|
|
1101
|
+
else settleView(0, 0, g.lastMx, g.lastMy);
|
|
1102
|
+
}
|
|
1103
|
+
return;
|
|
1104
|
+
}
|
|
1105
|
+
if (e.pointerId !== g.id) return;
|
|
1106
|
+
const v = cancelled ? { x: 0, y: 0 } : velocity();
|
|
1107
|
+
gesture = null;
|
|
1108
|
+
setGesture(null);
|
|
1109
|
+
if (g.mode === 'pending') {
|
|
1110
|
+
settleTrack();
|
|
1111
|
+
settleView();
|
|
1112
|
+
if (!cancelled && now() - g.t0 < TAP_MAX_MS) onTap(e, g);
|
|
1113
|
+
return;
|
|
1114
|
+
}
|
|
1115
|
+
if (g.mode === 'page') {
|
|
1116
|
+
releasePage(v.x);
|
|
1117
|
+
settleView();
|
|
1118
|
+
} else if (g.mode === 'pan') {
|
|
1119
|
+
settleTrack();
|
|
1120
|
+
settleView(v.x, v.y);
|
|
1121
|
+
} else if (g.mode === 'dismiss') {
|
|
1122
|
+
releaseDismiss(v);
|
|
1123
|
+
}
|
|
1124
|
+
}
|
|
1125
|
+
//#endregion
|
|
1126
|
+
|
|
1127
|
+
//#region wheel + keyboard + resize
|
|
1128
|
+
function onWheel(e) {
|
|
1129
|
+
e.preventDefault();
|
|
1130
|
+
if (closing) return;
|
|
1131
|
+
const unit = e.deltaMode === 1 ? 16 : e.deltaMode === 2 ? vh : 1;
|
|
1132
|
+
const dx = e.deltaX * unit;
|
|
1133
|
+
const dy = e.deltaY * unit;
|
|
1134
|
+
if (e.ctrlKey || e.metaKey) {
|
|
1135
|
+
// Trackpad pinch arrives as ctrl+wheel with small deltas.
|
|
1136
|
+
zoomBy(Math.exp(-dy * 0.01), e.clientX, e.clientY, false);
|
|
1137
|
+
return;
|
|
1138
|
+
}
|
|
1139
|
+
const mouseWheel = e.deltaMode !== 0 || (dx === 0 && Math.abs(dy) >= 40 && Number.isInteger(dy));
|
|
1140
|
+
if (mouseWheel) {
|
|
1141
|
+
zoomBy(Math.exp(-dy * 0.0025), e.clientX, e.clientY, true);
|
|
1142
|
+
return;
|
|
1143
|
+
}
|
|
1144
|
+
if (s.scale > 1.01) {
|
|
1145
|
+
const b = bounds(s.scale);
|
|
1146
|
+
anim.stop(['x', 'y']);
|
|
1147
|
+
s.x = clamp(s.x - dx, -b.x, b.x);
|
|
1148
|
+
s.y = clamp(s.y - dy, -b.y, b.y);
|
|
1149
|
+
render();
|
|
1150
|
+
return;
|
|
1151
|
+
}
|
|
1152
|
+
const tNow = now();
|
|
1153
|
+
if (tNow < wheelLock) {
|
|
1154
|
+
// Swallow the inertial tail of a trackpad swipe that already paged.
|
|
1155
|
+
wheelLock = tNow + 200;
|
|
1156
|
+
return;
|
|
1157
|
+
}
|
|
1158
|
+
if (tNow - wheelLast > 200) wheelAccum = 0;
|
|
1159
|
+
wheelLast = tNow;
|
|
1160
|
+
if (Math.abs(dx) <= Math.abs(dy)) return;
|
|
1161
|
+
wheelAccum += dx;
|
|
1162
|
+
if (Math.abs(wheelAccum) > 60) {
|
|
1163
|
+
go(wheelAccum > 0 ? 1 : -1);
|
|
1164
|
+
wheelAccum = 0;
|
|
1165
|
+
wheelLock = tNow + 450;
|
|
1166
|
+
}
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
function trapFocus(e) {
|
|
1170
|
+
const focusables = [...root.querySelectorAll('button:not([disabled]),a[href]')].filter(
|
|
1171
|
+
(node) => getComputedStyle(node).display !== 'none',
|
|
1172
|
+
);
|
|
1173
|
+
if (focusables.length === 0) {
|
|
1174
|
+
root.focus();
|
|
1175
|
+
return;
|
|
1176
|
+
}
|
|
1177
|
+
const at = focusables.indexOf(document.activeElement);
|
|
1178
|
+
const next = e.shiftKey ? (at <= 0 ? focusables.length - 1 : at - 1) : at === -1 || at === focusables.length - 1 ? 0 : at + 1;
|
|
1179
|
+
focusables[next].focus();
|
|
1180
|
+
}
|
|
1181
|
+
|
|
1182
|
+
function onKey(e) {
|
|
1183
|
+
if (closing || destroyed) return;
|
|
1184
|
+
if (e.ctrlKey || e.metaKey || e.altKey) return;
|
|
1185
|
+
let handled = true;
|
|
1186
|
+
switch (e.key) {
|
|
1187
|
+
case 'Escape':
|
|
1188
|
+
close();
|
|
1189
|
+
break;
|
|
1190
|
+
case 'ArrowLeft':
|
|
1191
|
+
go(-1);
|
|
1192
|
+
break;
|
|
1193
|
+
case 'ArrowRight':
|
|
1194
|
+
go(1);
|
|
1195
|
+
break;
|
|
1196
|
+
case '+':
|
|
1197
|
+
case '=':
|
|
1198
|
+
zoomBy(1.5, vw / 2, vh / 2, true);
|
|
1199
|
+
break;
|
|
1200
|
+
case '-':
|
|
1201
|
+
case '_':
|
|
1202
|
+
zoomBy(1 / 1.5, vw / 2, vh / 2, true);
|
|
1203
|
+
break;
|
|
1204
|
+
case '0':
|
|
1205
|
+
anim.to({ scale: 1, x: 0, y: 0 }, { spring: SPRING.snap });
|
|
1206
|
+
break;
|
|
1207
|
+
case 'Tab':
|
|
1208
|
+
trapFocus(e);
|
|
1209
|
+
break;
|
|
1210
|
+
default:
|
|
1211
|
+
handled = false;
|
|
1212
|
+
}
|
|
1213
|
+
if (handled) e.preventDefault();
|
|
1214
|
+
// Keep app shortcuts from firing behind the viewer.
|
|
1215
|
+
e.stopPropagation();
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
function onResize() {
|
|
1219
|
+
vw = root.clientWidth || window.innerWidth;
|
|
1220
|
+
vh = root.clientHeight || window.innerHeight;
|
|
1221
|
+
anim.stop(['x', 'y', 'scale', 'trackX']);
|
|
1222
|
+
s.trackX = 0;
|
|
1223
|
+
clearOutgoing();
|
|
1224
|
+
clampView();
|
|
1225
|
+
render();
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
const preventDefault = (e) => e.preventDefault();
|
|
1229
|
+
//#endregion
|
|
1230
|
+
|
|
1231
|
+
//#region open / close
|
|
1232
|
+
function close(velocity = {}) {
|
|
1233
|
+
if (closing || destroyed) return;
|
|
1234
|
+
closing = true;
|
|
1235
|
+
clearTap();
|
|
1236
|
+
root.dataset.state = 'closing';
|
|
1237
|
+
root.style.pointerEvents = 'none';
|
|
1238
|
+
pointers.clear();
|
|
1239
|
+
gesture = null;
|
|
1240
|
+
setGesture(null);
|
|
1241
|
+
anim.stop();
|
|
1242
|
+
outgoing = null;
|
|
1243
|
+
syncSlides();
|
|
1244
|
+
const item = items[index];
|
|
1245
|
+
const target = sourceTarget(item);
|
|
1246
|
+
const fallback = setTimeout(finish, 1200);
|
|
1247
|
+
const done = () => {
|
|
1248
|
+
clearTimeout(fallback);
|
|
1249
|
+
finish();
|
|
1250
|
+
};
|
|
1251
|
+
if (target !== null) {
|
|
1252
|
+
item.inset = target.inset;
|
|
1253
|
+
anim.to(
|
|
1254
|
+
{ x: target.x, y: target.y, scale: target.scale, clip: 1, radius: target.radius, bg: 0, trackX: 0, alpha: 1 },
|
|
1255
|
+
{ spring: SPRING.close, velocity, onDone: done },
|
|
1256
|
+
);
|
|
1257
|
+
} else {
|
|
1258
|
+
anim.to({ bg: 0, alpha: 0, scale: s.scale * 0.9, trackX: 0 }, { spring: SPRING.close, velocity, onDone: done });
|
|
1259
|
+
}
|
|
1260
|
+
}
|
|
1261
|
+
|
|
1262
|
+
function finish() {
|
|
1263
|
+
if (destroyed) return;
|
|
1264
|
+
destroyed = true;
|
|
1265
|
+
anim.destroy();
|
|
1266
|
+
clearTap();
|
|
1267
|
+
showSource();
|
|
1268
|
+
window.removeEventListener('keydown', onKey, true);
|
|
1269
|
+
window.removeEventListener('resize', onResize);
|
|
1270
|
+
root.remove();
|
|
1271
|
+
if (restoreFocus !== null && restoreFocus.isConnected) {
|
|
1272
|
+
try {
|
|
1273
|
+
restoreFocus.focus({ preventScroll: true });
|
|
1274
|
+
} catch {
|
|
1275
|
+
// focus is best-effort
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
onDestroyed();
|
|
1279
|
+
}
|
|
1280
|
+
|
|
1281
|
+
root.addEventListener('pointerdown', onDown);
|
|
1282
|
+
root.addEventListener('pointermove', onMove);
|
|
1283
|
+
root.addEventListener('pointerup', onUp);
|
|
1284
|
+
root.addEventListener('pointercancel', onUp);
|
|
1285
|
+
root.addEventListener('wheel', onWheel, { passive: false });
|
|
1286
|
+
root.addEventListener('dragstart', preventDefault);
|
|
1287
|
+
root.addEventListener('gesturestart', preventDefault);
|
|
1288
|
+
root.addEventListener('gesturechange', preventDefault);
|
|
1289
|
+
window.addEventListener('keydown', onKey, true);
|
|
1290
|
+
window.addEventListener('resize', onResize);
|
|
1291
|
+
zoomBtn.addEventListener('click', () => {
|
|
1292
|
+
if (s.scale > 1.01) anim.to({ scale: 1, x: 0, y: 0 }, { spring: SPRING.snap });
|
|
1293
|
+
else zoomBy(doubleTapScale(), vw / 2, vh / 2, true);
|
|
1294
|
+
});
|
|
1295
|
+
closeBtn.addEventListener('click', () => close());
|
|
1296
|
+
prevBtn.addEventListener('click', () => go(-1));
|
|
1297
|
+
nextBtn.addEventListener('click', () => go(1));
|
|
1298
|
+
|
|
1299
|
+
document.body.appendChild(root);
|
|
1300
|
+
vw = root.clientWidth || window.innerWidth;
|
|
1301
|
+
vh = root.clientHeight || window.innerHeight;
|
|
1302
|
+
|
|
1303
|
+
const first = items[index];
|
|
1304
|
+
const from = sourceTarget(first);
|
|
1305
|
+
if (from !== null) {
|
|
1306
|
+
first.inset = from.inset;
|
|
1307
|
+
Object.assign(s, { x: from.x, y: from.y, scale: from.scale, clip: 1, radius: from.radius, bg: 0, alpha: 1 });
|
|
1308
|
+
} else {
|
|
1309
|
+
Object.assign(s, { scale: 0.92, bg: 0, alpha: 0 });
|
|
1310
|
+
}
|
|
1311
|
+
syncSlides();
|
|
1312
|
+
updateChrome();
|
|
1313
|
+
render();
|
|
1314
|
+
try {
|
|
1315
|
+
closeBtn.focus({ preventScroll: true });
|
|
1316
|
+
} catch {
|
|
1317
|
+
root.focus();
|
|
1318
|
+
}
|
|
1319
|
+
anim.to({ x: 0, y: 0, scale: 1, clip: 0, radius: 0, bg: 1, alpha: 1 }, {
|
|
1320
|
+
spring: SPRING.open,
|
|
1321
|
+
onDone: () => {
|
|
1322
|
+
if (!closing) root.dataset.state = 'open';
|
|
1323
|
+
},
|
|
1324
|
+
});
|
|
1325
|
+
// Interrupted opens still need to leave the "opening" state.
|
|
1326
|
+
setTimeout(() => {
|
|
1327
|
+
if (!closing && !destroyed) root.dataset.state = 'open';
|
|
1328
|
+
}, 600);
|
|
1329
|
+
//#endregion
|
|
1330
|
+
|
|
1331
|
+
return { close, destroy: finish };
|
|
1332
|
+
}
|
|
1333
|
+
//#endregion
|
|
1334
|
+
|
|
1335
|
+
//#region click interception
|
|
1336
|
+
function installInterceptor(t) {
|
|
1337
|
+
let viewer = null;
|
|
1338
|
+
const onClick = (e) => {
|
|
1339
|
+
if (e.defaultPrevented || e.button !== 0 || e.ctrlKey || e.metaKey || e.shiftKey || e.altKey) return;
|
|
1340
|
+
const trigger = resolveTrigger(e.target);
|
|
1341
|
+
if (trigger === null) return;
|
|
1342
|
+
const { items, index } = collect(trigger);
|
|
1343
|
+
if (index < 0) return;
|
|
1344
|
+
e.preventDefault();
|
|
1345
|
+
e.stopPropagation();
|
|
1346
|
+
e.stopImmediatePropagation();
|
|
1347
|
+
if (viewer !== null) viewer.destroy();
|
|
1348
|
+
viewer = openViewer(items, index, t, () => {
|
|
1349
|
+
viewer = null;
|
|
1350
|
+
});
|
|
1351
|
+
};
|
|
1352
|
+
document.addEventListener('click', onClick, true);
|
|
1353
|
+
return () => {
|
|
1354
|
+
document.removeEventListener('click', onClick, true);
|
|
1355
|
+
if (viewer !== null) viewer.destroy();
|
|
1356
|
+
};
|
|
1357
|
+
}
|
|
1358
|
+
//#endregion
|
|
1359
|
+
|
|
1360
|
+
//#region apply
|
|
1361
|
+
const inject = ['locale'];
|
|
1362
|
+
|
|
1363
|
+
/**
|
|
1364
|
+
* Install styles and the capture-phase click interceptor.
|
|
1365
|
+
* @param ctx - client root context.
|
|
1366
|
+
*/
|
|
1367
|
+
function apply(ctx) {
|
|
1368
|
+
let t = fallbackT;
|
|
1369
|
+
const locale = ctx.locale;
|
|
1370
|
+
if (locale && typeof locale.register === 'function') {
|
|
1371
|
+
ctx.effect(() => locale.register(NS, { zh, en }), 'image-viewer: dictionaries');
|
|
1372
|
+
if (typeof locale.bind === 'function') {
|
|
1373
|
+
const bound = locale.bind(NS);
|
|
1374
|
+
t = (key, params) => {
|
|
1375
|
+
let value;
|
|
1376
|
+
try {
|
|
1377
|
+
value = bound(key, params);
|
|
1378
|
+
} catch {
|
|
1379
|
+
value = undefined;
|
|
1380
|
+
}
|
|
1381
|
+
return typeof value === 'string' && value !== '' && value !== key && value !== NS + '.' + key
|
|
1382
|
+
? value
|
|
1383
|
+
: fallbackT(key, params);
|
|
1384
|
+
};
|
|
1385
|
+
}
|
|
1386
|
+
}
|
|
1387
|
+
ctx.effect(() => installStyles(), 'image-viewer: styles');
|
|
1388
|
+
ctx.effect(() => installInterceptor(t), 'image-viewer: click interceptor');
|
|
1389
|
+
}
|
|
1390
|
+
//#endregion
|
|
1391
|
+
|
|
1392
|
+
return { inject, apply };
|
|
1393
|
+
},
|
|
1394
|
+
});
|