visualci-gallery 0.1.0 → 0.2.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/README.md +8 -3
- package/dist/gallery.iife.js +1157 -390
- package/dist/hydrate.d.ts +9 -2
- package/dist/hydrate.js +235 -3
- package/dist/index.d.ts +2 -2
- package/dist/model.d.ts +39 -0
- package/dist/model.js +52 -3
- package/dist/preload.d.ts +149 -0
- package/dist/preload.js +319 -0
- package/dist/render.js +21 -2
- package/package.json +1 -1
package/dist/hydrate.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type { Size, Theme } from "./model.js";
|
|
1
|
+
import type { PreloadCaps, Size, Theme } from "./model.js";
|
|
2
|
+
import { type Env } from "./preload.js";
|
|
2
3
|
export interface Gallery {
|
|
3
4
|
/** The gallery's element (the contract's `section.vg`). */
|
|
4
5
|
root: HTMLElement;
|
|
@@ -10,8 +11,14 @@ export interface Gallery {
|
|
|
10
11
|
/** Removes the listeners hydrate() added. */
|
|
11
12
|
destroy(): void;
|
|
12
13
|
}
|
|
14
|
+
export interface HydrateOptions {
|
|
15
|
+
/** Over the markup's data-vg-preload: false turns preloading off, an object sets its caps. */
|
|
16
|
+
preload?: boolean | PreloadCaps;
|
|
17
|
+
/** The loader, idle callback and timing source, for tests. */
|
|
18
|
+
env?: Env;
|
|
19
|
+
}
|
|
13
20
|
/** The gallery already hydrated on this element, if any. */
|
|
14
21
|
export declare function hydratedGallery(root: HTMLElement): Gallery | undefined;
|
|
15
22
|
/** Hydrates a gallery's markup: `el` is the contract's `section.vg`, or an element holding one. Hydrating twice
|
|
16
23
|
* returns the first. */
|
|
17
|
-
export declare function hydrate(el: HTMLElement): Gallery;
|
|
24
|
+
export declare function hydrate(el: HTMLElement, options?: HydrateOptions): Gallery;
|
package/dist/hydrate.js
CHANGED
|
@@ -4,7 +4,100 @@
|
|
|
4
4
|
// rendered. Without <dialog> (Safari before 15.4) each card stays a link to its image.
|
|
5
5
|
//
|
|
6
6
|
// Every attribute it reads or sets starts with data-vg-, so a site's own [data-theme] rules never reach the gallery.
|
|
7
|
+
import { DEFAULT_CAPS, Preloader, estimateBytes, neighbours, plan, tabItems, } from "./preload.js";
|
|
7
8
|
const hydrated = new WeakMap();
|
|
9
|
+
/** The caps from the options, else the markup (none: the defaults; "off": null). */
|
|
10
|
+
function preloadCaps(root, option) {
|
|
11
|
+
if (option === false)
|
|
12
|
+
return null;
|
|
13
|
+
if (typeof option === "object" && option !== null)
|
|
14
|
+
return sane({ ...DEFAULT_CAPS, ...definedOnly(option) });
|
|
15
|
+
if (option === true)
|
|
16
|
+
return { ...DEFAULT_CAPS };
|
|
17
|
+
const attr = root.getAttribute("data-vg-preload");
|
|
18
|
+
if (attr === null)
|
|
19
|
+
return { ...DEFAULT_CAPS };
|
|
20
|
+
if (attr === "off")
|
|
21
|
+
return null;
|
|
22
|
+
const caps = { ...DEFAULT_CAPS };
|
|
23
|
+
for (const pair of attr.split(" ")) {
|
|
24
|
+
const [k, v] = pair.split("=");
|
|
25
|
+
if (k in caps && /^\d+$/.test(v ?? ""))
|
|
26
|
+
caps[k] = Number(v);
|
|
27
|
+
}
|
|
28
|
+
return sane(caps);
|
|
29
|
+
}
|
|
30
|
+
/** At least one request at once and at most 8, as buildModel enforces, so nothing can stall the reader's own
|
|
31
|
+
* requests. */
|
|
32
|
+
function sane(caps) {
|
|
33
|
+
return { ...caps, parallel: Math.min(Math.max(Math.floor(caps.parallel) || 1, 1), 8) };
|
|
34
|
+
}
|
|
35
|
+
function definedOnly(o) {
|
|
36
|
+
const out = {};
|
|
37
|
+
for (const [k, v] of Object.entries(o))
|
|
38
|
+
if (typeof v === "number")
|
|
39
|
+
out[k] = v;
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
/** Save-Data, a 2G connection or prefers-reduced-data: only the shown tab's thumbnails. Chromium alone reports the
|
|
43
|
+
* connection, so the preloader also measures its own requests (Preloader.slow). */
|
|
44
|
+
function lowData() {
|
|
45
|
+
const c = navigator.connection;
|
|
46
|
+
if (c?.saveData || /(^|-)2g$/.test(c?.effectiveType ?? ""))
|
|
47
|
+
return true;
|
|
48
|
+
return typeof matchMedia === "function" && matchMedia("(prefers-reduced-data: reduce)").matches;
|
|
49
|
+
}
|
|
50
|
+
/** The browser side of the preloader: an Image per request at low or high fetch priority, idle callbacks, and each
|
|
51
|
+
* request's timing (Resource Timing for its bytes where the server allows, else the request's own clock). */
|
|
52
|
+
const REQUEST_TIMEOUT_MS = 30_000;
|
|
53
|
+
function browserEnv() {
|
|
54
|
+
const clock = new Map();
|
|
55
|
+
const now = () => (typeof performance !== "undefined" ? performance.now() : Date.now());
|
|
56
|
+
return {
|
|
57
|
+
load(url, high) {
|
|
58
|
+
const img = new Image();
|
|
59
|
+
img.decoding = "async";
|
|
60
|
+
img.fetchPriority = high ? "high" : "low";
|
|
61
|
+
let settle = () => { };
|
|
62
|
+
const done = new Promise((resolve) => (settle = resolve));
|
|
63
|
+
const start = now();
|
|
64
|
+
img.onload = () => {
|
|
65
|
+
clock.set(url, { start, end: now() });
|
|
66
|
+
settle(true);
|
|
67
|
+
};
|
|
68
|
+
img.onerror = () => settle(false);
|
|
69
|
+
img.src = url;
|
|
70
|
+
const cancel = () => {
|
|
71
|
+
clearTimeout(timer);
|
|
72
|
+
img.onload = img.onerror = null;
|
|
73
|
+
img.removeAttribute("src");
|
|
74
|
+
settle(false);
|
|
75
|
+
};
|
|
76
|
+
// A server that never answers doesn't hold a slot for good.
|
|
77
|
+
const timer = setTimeout(cancel, REQUEST_TIMEOUT_MS);
|
|
78
|
+
done.then(() => clearTimeout(timer));
|
|
79
|
+
return { done, cancel };
|
|
80
|
+
},
|
|
81
|
+
later: (fn, ms) => void setTimeout(fn, ms),
|
|
82
|
+
idle(fn) {
|
|
83
|
+
if (typeof requestIdleCallback === "function")
|
|
84
|
+
requestIdleCallback(() => fn(), { timeout: 2000 });
|
|
85
|
+
else
|
|
86
|
+
setTimeout(fn, 50);
|
|
87
|
+
},
|
|
88
|
+
timing(url) {
|
|
89
|
+
const own = clock.get(url);
|
|
90
|
+
clock.delete(url);
|
|
91
|
+
const entries = typeof performance !== "undefined" && performance.getEntriesByName ? performance.getEntriesByName(url) : [];
|
|
92
|
+
const e = entries[entries.length - 1];
|
|
93
|
+
// Only this request's own entry: a memory-cache hit records none, and the page's earlier entry for the same
|
|
94
|
+
// URL would date the request back to page load.
|
|
95
|
+
if (e && e.responseEnd > 0 && (!own || e.startTime >= own.start - 1))
|
|
96
|
+
return { start: e.startTime, end: e.responseEnd, bytes: e.encodedBodySize > 0 ? e.encodedBodySize : null };
|
|
97
|
+
return own ? { ...own, bytes: null } : null;
|
|
98
|
+
},
|
|
99
|
+
};
|
|
100
|
+
}
|
|
8
101
|
function pageTheme() {
|
|
9
102
|
const set = document.documentElement.getAttribute("data-theme");
|
|
10
103
|
if (set === "light" || set === "dark")
|
|
@@ -40,7 +133,7 @@ export function hydratedGallery(root) {
|
|
|
40
133
|
}
|
|
41
134
|
/** Hydrates a gallery's markup: `el` is the contract's `section.vg`, or an element holding one. Hydrating twice
|
|
42
135
|
* returns the first. */
|
|
43
|
-
export function hydrate(el) {
|
|
136
|
+
export function hydrate(el, options = {}) {
|
|
44
137
|
const found = el.matches("[data-vg]") ? el : el.querySelector("[data-vg]");
|
|
45
138
|
if (!found)
|
|
46
139
|
throw new Error("hydrate: no gallery markup ([data-vg]) in this element");
|
|
@@ -68,7 +161,8 @@ export function hydrate(el) {
|
|
|
68
161
|
function sync() {
|
|
69
162
|
// Each card links to the image it shows, so a middle click or a browser without <dialog> opens that one.
|
|
70
163
|
for (const card of all(".vg-card")) {
|
|
71
|
-
const
|
|
164
|
+
const img = shownIn(card);
|
|
165
|
+
const src = img?.getAttribute("data-src-full") ?? img?.getAttribute("src");
|
|
72
166
|
const link = card.querySelector("a.vg-thumb");
|
|
73
167
|
if (link && src)
|
|
74
168
|
link.setAttribute("href", src);
|
|
@@ -82,6 +176,8 @@ export function hydrate(el) {
|
|
|
82
176
|
b.setAttribute("aria-pressed", String(b.getAttribute(`data-vg-set-${key}`) === value));
|
|
83
177
|
}
|
|
84
178
|
sync();
|
|
179
|
+
if (!selecting)
|
|
180
|
+
replan();
|
|
85
181
|
}
|
|
86
182
|
// The size the reader chose (phone to begin with on a narrow screen). A tab without it shows the size it has,
|
|
87
183
|
// without changing the choice, so the next tab is back at the chosen size.
|
|
@@ -90,7 +186,10 @@ export function hydrate(el) {
|
|
|
90
186
|
const sizes = sizesOf(panel());
|
|
91
187
|
set("size", sizes.length === 0 || sizes.indexOf(chosen) !== -1 ? chosen : sizes[0]);
|
|
92
188
|
}
|
|
189
|
+
let selecting = false;
|
|
93
190
|
function select(tab, focus = false) {
|
|
191
|
+
const changed = tab.getAttribute("aria-selected") !== "true";
|
|
192
|
+
selecting = true;
|
|
94
193
|
for (const t of tabs) {
|
|
95
194
|
const on = t === tab;
|
|
96
195
|
t.setAttribute("aria-selected", String(on));
|
|
@@ -102,6 +201,14 @@ export function hydrate(el) {
|
|
|
102
201
|
if (focus)
|
|
103
202
|
tab.focus();
|
|
104
203
|
fit();
|
|
204
|
+
selecting = false;
|
|
205
|
+
// A tab the reader opened loads first, at high priority: its fold, then the rest. Then the queue is planned
|
|
206
|
+
// again from the new tab.
|
|
207
|
+
if (preloader && changed) {
|
|
208
|
+
const t = planTabs();
|
|
209
|
+
preloader.jump(tabItems(t.tabs[t.current], theme()), true);
|
|
210
|
+
}
|
|
211
|
+
replan();
|
|
105
212
|
}
|
|
106
213
|
/** The cards the full-size view steps through: the shown panel's, at the gallery's size. */
|
|
107
214
|
function cards() {
|
|
@@ -137,7 +244,8 @@ export function hydrate(el) {
|
|
|
137
244
|
view.replaceChildren();
|
|
138
245
|
if (shown instanceof HTMLImageElement) {
|
|
139
246
|
const big = document.createElement("img");
|
|
140
|
-
|
|
247
|
+
// The original, not the card's thumbnail.
|
|
248
|
+
const src = shown.getAttribute("data-src-full") ?? shown.getAttribute("src") ?? "";
|
|
141
249
|
big.alt = shown.alt;
|
|
142
250
|
// The image's own size from the markup (its .width is the thumbnail's rendered width).
|
|
143
251
|
big.setAttribute("width", shown.getAttribute("width") ?? "");
|
|
@@ -156,6 +264,13 @@ export function hydrate(el) {
|
|
|
156
264
|
view.append(shown.cloneNode(true));
|
|
157
265
|
}
|
|
158
266
|
view.scrollTop = 0;
|
|
267
|
+
// The screens either side, in the order the reader is likely to step.
|
|
268
|
+
if (preloader && !light()) {
|
|
269
|
+
const list = cards();
|
|
270
|
+
const i = list.indexOf(card);
|
|
271
|
+
if (i !== -1)
|
|
272
|
+
preloader.jump(neighbours(list.map(planCard), i, theme(), hiDpi()).filter((n) => n.url !== ""), false);
|
|
273
|
+
}
|
|
159
274
|
}
|
|
160
275
|
function open(card) {
|
|
161
276
|
// A screen without the gallery's size (opened by key) shows the size it has.
|
|
@@ -208,6 +323,108 @@ export function hydrate(el) {
|
|
|
208
323
|
if (next)
|
|
209
324
|
next.disabled = track.scrollLeft + track.clientWidth >= track.scrollWidth - 1;
|
|
210
325
|
}
|
|
326
|
+
// --- Preloading (preload.ts): the plan comes from the markup as it stands. ---
|
|
327
|
+
const caps = preloadCaps(root, options.preload);
|
|
328
|
+
let preloader = null;
|
|
329
|
+
const hiDpi = () => (typeof devicePixelRatio === "number" ? devicePixelRatio : 1) > 1;
|
|
330
|
+
const num = (img, name) => {
|
|
331
|
+
const v = img.getAttribute(name);
|
|
332
|
+
return v !== null && /^\d+$/.test(v) ? Number(v) : null;
|
|
333
|
+
};
|
|
334
|
+
function planImage(img) {
|
|
335
|
+
const width = Number(img.getAttribute("width")) || 1;
|
|
336
|
+
const height = Number(img.getAttribute("height")) || 1;
|
|
337
|
+
const behind = img.getAttribute("data-src-full");
|
|
338
|
+
const x2 = img.getAttribute("data-src-2x");
|
|
339
|
+
// What the full-size view loads: the 2x copy on a high-density screen, else the original behind a thumbnail,
|
|
340
|
+
// else nothing more (the card's image is the full one).
|
|
341
|
+
const use2x = hiDpi() && x2 !== null;
|
|
342
|
+
const tw = Math.min(width, 640);
|
|
343
|
+
return {
|
|
344
|
+
url: img.getAttribute("src") ?? "",
|
|
345
|
+
full: use2x ? x2 : behind,
|
|
346
|
+
width,
|
|
347
|
+
height,
|
|
348
|
+
bytes: num(img, "data-vg-bytes") ?? (behind ? estimateBytes(tw, Math.round((height * tw) / width)) : estimateBytes(width, height)),
|
|
349
|
+
fullBytes: use2x
|
|
350
|
+
? num(img, "data-vg-2x-bytes") ?? estimateBytes(width * 2, height * 2)
|
|
351
|
+
: num(img, "data-vg-full-bytes") ?? estimateBytes(width, height),
|
|
352
|
+
};
|
|
353
|
+
}
|
|
354
|
+
const planCardAt = (card, sz) => {
|
|
355
|
+
const imgs = toArray(card.querySelectorAll(`img.vg-img[data-vg-size="${sz}"]`));
|
|
356
|
+
const light = imgs.find((i) => listOf(i.getAttribute("data-vg-theme")).indexOf("light") !== -1);
|
|
357
|
+
const dark = imgs.find((i) => listOf(i.getAttribute("data-vg-theme")).indexOf("dark") !== -1);
|
|
358
|
+
return light && dark ? { light: planImage(light), dark: planImage(dark) } : null;
|
|
359
|
+
};
|
|
360
|
+
const planCard = (card) => planCardAt(card, size()) ?? { light: blank, dark: blank };
|
|
361
|
+
const blank = { url: "", full: null, width: 1, height: 1, bytes: 0, fullBytes: 0 };
|
|
362
|
+
/** The cards above the fold: in the viewport now. Other tabs can't be measured while hidden, so they're estimated
|
|
363
|
+
* from the shown one (a phone card is about half as wide as a desktop one). */
|
|
364
|
+
function planTabs() {
|
|
365
|
+
const panels = tabs.length ? tabs.map(panelOf) : all(".vg-panel");
|
|
366
|
+
const shown = panel();
|
|
367
|
+
const current = Math.max(0, panels.indexOf(shown));
|
|
368
|
+
const sizeOf = (p) => {
|
|
369
|
+
const sizes = sizesOf(p);
|
|
370
|
+
return sizes.length === 0 || sizes.indexOf(chosen) !== -1 ? chosen : sizes[0];
|
|
371
|
+
};
|
|
372
|
+
const shownSize = size();
|
|
373
|
+
let fold = 0;
|
|
374
|
+
const tabsOut = panels.map((p, i) => {
|
|
375
|
+
const sz = i === current ? shownSize : sizeOf(p);
|
|
376
|
+
const els = p ? toArray(p.querySelectorAll(".vg-card")).filter((c) => sizesOf(c).indexOf(sz) !== -1) : [];
|
|
377
|
+
// Cards whose image failed (replaced by its description) have nothing to load.
|
|
378
|
+
const pairs = els.map((el) => ({ el, card: planCardAt(el, sz) })).filter((x) => x.card !== null);
|
|
379
|
+
if (i === current) {
|
|
380
|
+
fold = pairs.filter(({ el }) => {
|
|
381
|
+
const r = el.getBoundingClientRect();
|
|
382
|
+
return r.bottom > 0 && r.top < innerHeight && r.right > 0 && r.left < innerWidth;
|
|
383
|
+
}).length;
|
|
384
|
+
}
|
|
385
|
+
return { sz, cards: pairs.map((x) => x.card) };
|
|
386
|
+
});
|
|
387
|
+
const out = tabsOut.map((t, i) => {
|
|
388
|
+
const f = i === current ? fold : t.sz === shownSize ? fold : t.sz === "phone" ? fold * 2 : Math.ceil(fold / 2);
|
|
389
|
+
return { cards: t.cards, fold: Math.min(f, t.cards.length) };
|
|
390
|
+
});
|
|
391
|
+
return { tabs: out, current };
|
|
392
|
+
}
|
|
393
|
+
/** Save-Data, 2G, reduced data or a slow measured rate: the shown tab's thumbnails, and nothing else predicted. */
|
|
394
|
+
const light = () => lowData() || !!preloader?.slow;
|
|
395
|
+
function replan() {
|
|
396
|
+
if (!preloader || !caps)
|
|
397
|
+
return;
|
|
398
|
+
const t = planTabs();
|
|
399
|
+
preloader.setPlan(plan({
|
|
400
|
+
tabs: t.tabs,
|
|
401
|
+
current: t.current,
|
|
402
|
+
theme: theme(),
|
|
403
|
+
caps,
|
|
404
|
+
mode: light() ? "light" : "full",
|
|
405
|
+
hiDpi: hiDpi(),
|
|
406
|
+
done: preloader.loaded(),
|
|
407
|
+
spent: preloader.spent,
|
|
408
|
+
}));
|
|
409
|
+
}
|
|
410
|
+
const onIntent = (e) => {
|
|
411
|
+
const tab = e.target.closest?.('[role="tab"]');
|
|
412
|
+
if (!preloader || light() || !tab || !root.contains(tab) || tab.getAttribute("aria-selected") === "true")
|
|
413
|
+
return;
|
|
414
|
+
const t = planTabs();
|
|
415
|
+
const i = tabs.indexOf(tab);
|
|
416
|
+
if (i !== -1 && t.tabs[i])
|
|
417
|
+
preloader.hint(tabItems(t.tabs[i], theme(), true));
|
|
418
|
+
};
|
|
419
|
+
let destroyed = false;
|
|
420
|
+
const env = options.env ?? browserEnv();
|
|
421
|
+
const start = () => {
|
|
422
|
+
if (destroyed || !caps || preloader)
|
|
423
|
+
return;
|
|
424
|
+
preloader = new Preloader(env, caps, () => replan());
|
|
425
|
+
replan();
|
|
426
|
+
};
|
|
427
|
+
const onLoad = () => env.idle(start);
|
|
211
428
|
const onClick = (e) => {
|
|
212
429
|
const target = e.target;
|
|
213
430
|
if (target === box)
|
|
@@ -283,6 +500,15 @@ export function hydrate(el) {
|
|
|
283
500
|
if (track && resize)
|
|
284
501
|
resize.observe(track);
|
|
285
502
|
root.addEventListener("click", onClick);
|
|
503
|
+
if (caps) {
|
|
504
|
+
root.addEventListener("pointerover", onIntent);
|
|
505
|
+
root.addEventListener("focusin", onIntent);
|
|
506
|
+
// After the page has loaded, in idle time: preloading never competes with the page itself.
|
|
507
|
+
if (document.readyState === "complete")
|
|
508
|
+
onLoad();
|
|
509
|
+
else
|
|
510
|
+
addEventListener("load", onLoad, { once: true });
|
|
511
|
+
}
|
|
286
512
|
root.addEventListener("keydown", onKey);
|
|
287
513
|
box.addEventListener("close", onClose);
|
|
288
514
|
track?.addEventListener("scroll", edges, { passive: true });
|
|
@@ -314,6 +540,12 @@ export function hydrate(el) {
|
|
|
314
540
|
destroy: () => {
|
|
315
541
|
unlock?.();
|
|
316
542
|
root.removeEventListener("click", onClick);
|
|
543
|
+
root.removeEventListener("pointerover", onIntent);
|
|
544
|
+
root.removeEventListener("focusin", onIntent);
|
|
545
|
+
removeEventListener("load", onLoad);
|
|
546
|
+
destroyed = true;
|
|
547
|
+
preloader?.stop();
|
|
548
|
+
preloader = null;
|
|
317
549
|
root.removeEventListener("keydown", onKey);
|
|
318
550
|
box.removeEventListener("close", onClose);
|
|
319
551
|
track?.removeEventListener("scroll", edges);
|
package/dist/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { type Gallery } from "./hydrate.js";
|
|
2
2
|
import { type LoadOptions } from "./load.js";
|
|
3
3
|
export { buildModel, parseShot, humanize, slugify, DEFAULT_TITLE } from "./model.js";
|
|
4
|
-
export type { Manifest, ManifestGroup, ManifestItem, Model, Group, Screen, Variant, Options, Size, Theme } from "./model.js";
|
|
4
|
+
export type { Manifest, ManifestGroup, ManifestItem, Model, Group, Screen, Variant, Thumb, Options, PreloadCaps, Size, Theme } from "./model.js";
|
|
5
5
|
export { render, escapeHtml } from "./render.js";
|
|
6
|
-
export { hydrate, type Gallery } from "./hydrate.js";
|
|
6
|
+
export { hydrate, type Gallery, type HydrateOptions } from "./hydrate.js";
|
|
7
7
|
export { load, type LoadOptions } from "./load.js";
|
|
8
8
|
/** Renders the gallery into an empty element and hydrates it. When the manifest can't be read, the element says so
|
|
9
9
|
* and the promise rejects. */
|
package/dist/model.d.ts
CHANGED
|
@@ -18,6 +18,15 @@ export interface ManifestItem {
|
|
|
18
18
|
platform?: string;
|
|
19
19
|
sha256?: string;
|
|
20
20
|
since?: string;
|
|
21
|
+
/** The image's size in bytes, when the server gives it. */
|
|
22
|
+
bytes?: number;
|
|
23
|
+
/** A small copy for the cards (VisualCI serves a 640 px WebP), when the server gives one. */
|
|
24
|
+
thumb?: {
|
|
25
|
+
url: string;
|
|
26
|
+
width: number;
|
|
27
|
+
height: number;
|
|
28
|
+
bytes?: number;
|
|
29
|
+
};
|
|
21
30
|
}
|
|
22
31
|
export interface ScreenOptions {
|
|
23
32
|
title?: string;
|
|
@@ -62,7 +71,24 @@ export interface Options {
|
|
|
62
71
|
href: string;
|
|
63
72
|
text?: string;
|
|
64
73
|
} | null;
|
|
74
|
+
/** Predictive preloading of the images a reader is likely to look at next. Default true; false turns it off; an
|
|
75
|
+
* object sets its own caps (the rest keep their defaults). */
|
|
76
|
+
preload?: boolean | PreloadCaps;
|
|
65
77
|
}
|
|
78
|
+
/** The preloader's limits: `fold` above-the-fold thumbnails always load; past them at most `count` images and
|
|
79
|
+
* `bytes` bytes; `parallel` requests at once. */
|
|
80
|
+
export interface PreloadCaps {
|
|
81
|
+
fold?: number;
|
|
82
|
+
count?: number;
|
|
83
|
+
bytes?: number;
|
|
84
|
+
parallel?: number;
|
|
85
|
+
}
|
|
86
|
+
export declare const PRELOAD_DEFAULTS: {
|
|
87
|
+
fold: number;
|
|
88
|
+
count: number;
|
|
89
|
+
bytes: number;
|
|
90
|
+
parallel: number;
|
|
91
|
+
};
|
|
66
92
|
export type Layout = "grid" | "strip";
|
|
67
93
|
export interface Variant {
|
|
68
94
|
size: Size;
|
|
@@ -71,6 +97,17 @@ export interface Variant {
|
|
|
71
97
|
url2x: string | null;
|
|
72
98
|
width: number;
|
|
73
99
|
height: number;
|
|
100
|
+
/** Bytes of `url` and `url2x`, when the manifest gives them. */
|
|
101
|
+
bytes: number | null;
|
|
102
|
+
bytes2x: number | null;
|
|
103
|
+
/** The cards' small copy, when the manifest gives one; the full-size view shows `url`. */
|
|
104
|
+
thumb: Thumb | null;
|
|
105
|
+
}
|
|
106
|
+
export interface Thumb {
|
|
107
|
+
url: string;
|
|
108
|
+
width: number;
|
|
109
|
+
height: number;
|
|
110
|
+
bytes: number | null;
|
|
74
111
|
}
|
|
75
112
|
export interface Screen {
|
|
76
113
|
key: string;
|
|
@@ -98,6 +135,8 @@ export interface Model {
|
|
|
98
135
|
headingLevel: number;
|
|
99
136
|
id: string;
|
|
100
137
|
credit: boolean;
|
|
138
|
+
/** null: preloading with the defaults. "off": none. Else the caps, every one filled in. */
|
|
139
|
+
preload: null | "off" | Required<PreloadCaps>;
|
|
101
140
|
groups: Group[];
|
|
102
141
|
}
|
|
103
142
|
export declare const DEFAULT_TITLE = "Screenshots";
|
package/dist/model.js
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
// The manifest, grouped and paired: the step every renderer shares (contract/SPEC.md, "The model"). Pure: no DOM, no
|
|
2
2
|
// network, so it runs in the browser, at build time and in tests alike.
|
|
3
|
+
export const PRELOAD_DEFAULTS = { fold: 250, count: 500, bytes: 50_000_000, parallel: 4 };
|
|
4
|
+
const PRELOAD_KEYS = ["fold", "count", "bytes", "parallel"];
|
|
5
|
+
/** The largest cap, so every language holds it exactly. */
|
|
6
|
+
const MAX_CAP = 1_000_000_000_000;
|
|
3
7
|
export const DEFAULT_TITLE = "Screenshots";
|
|
4
8
|
const SIZES = ["desktop", "phone"];
|
|
5
9
|
const THEMES = ["light", "dark"];
|
|
@@ -95,6 +99,8 @@ function opt(value, check, what) {
|
|
|
95
99
|
return value;
|
|
96
100
|
}
|
|
97
101
|
const isString = (v) => typeof v === "string";
|
|
102
|
+
/** A size in bytes: a whole number up to 10^12, exact in every language. */
|
|
103
|
+
const isBytes = (v) => typeof v === "number" && Number.isInteger(v) && v >= 0 && v <= 1_000_000_000_000;
|
|
98
104
|
const isStrings = (v) => Array.isArray(v) && v.every(isString);
|
|
99
105
|
const isTexts = (keys) => (v) => isObject(v) &&
|
|
100
106
|
Object.values(v).every((entry) => isObject(entry) && Object.entries(entry).every(([k, x]) => keys.includes(k) && (x === null || isString(x))));
|
|
@@ -117,6 +123,19 @@ function checkManifest(manifest) {
|
|
|
117
123
|
fail(`${where} (${item.name}): its url isn't an http(s) URL`);
|
|
118
124
|
if (!isSize(item.width) || !isSize(item.height))
|
|
119
125
|
fail(`${where} (${item.name}): its width and height aren't whole numbers from 1 to 1000000`);
|
|
126
|
+
if (item.bytes !== undefined && item.bytes !== null && !isBytes(item.bytes))
|
|
127
|
+
fail(`${where} (${item.name}): its bytes isn't a whole number`);
|
|
128
|
+
const t = item.thumb;
|
|
129
|
+
if (t !== undefined && t !== null) {
|
|
130
|
+
if (!isObject(t) ||
|
|
131
|
+
typeof t.url !== "string" ||
|
|
132
|
+
!ABSOLUTE.test(t.url) ||
|
|
133
|
+
!isSize(t.width) ||
|
|
134
|
+
!isSize(t.height) ||
|
|
135
|
+
(t.bytes !== undefined && t.bytes !== null && !isBytes(t.bytes))) {
|
|
136
|
+
fail(`${where} (${item.name}): its thumb needs an http(s) url, a width and a height`);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
120
139
|
});
|
|
121
140
|
});
|
|
122
141
|
}
|
|
@@ -143,6 +162,21 @@ export function buildModel(manifest, options = {}) {
|
|
|
143
162
|
const themeSwitchOpt = opt(o.themeSwitch, (v) => typeof v === "boolean", "themeSwitch") ?? true;
|
|
144
163
|
const layout = opt(o.layout, (v) => v === "grid" || v === "strip", "layout") ?? "grid";
|
|
145
164
|
const seeAllOpt = opt(o.seeAll, (v) => isObject(v) && isString(v.href) && safeHref(v.href) && (v.text === undefined || v.text === null || isString(v.text)), "seeAll");
|
|
165
|
+
const preloadOpt = opt(o.preload, (v) => typeof v === "boolean" ||
|
|
166
|
+
(isObject(v) &&
|
|
167
|
+
Object.entries(v).every(([k, x]) => PRELOAD_KEYS.includes(k) &&
|
|
168
|
+
(x === null ||
|
|
169
|
+
(typeof x === "number" && Number.isInteger(x) && x >= (k === "parallel" ? 1 : 0) && x <= (k === "parallel" ? 8 : MAX_CAP))))), "preload");
|
|
170
|
+
const preload = preloadOpt === undefined || preloadOpt === true
|
|
171
|
+
? null
|
|
172
|
+
: preloadOpt === false
|
|
173
|
+
? "off"
|
|
174
|
+
: {
|
|
175
|
+
fold: preloadOpt.fold ?? PRELOAD_DEFAULTS.fold,
|
|
176
|
+
count: preloadOpt.count ?? PRELOAD_DEFAULTS.count,
|
|
177
|
+
bytes: preloadOpt.bytes ?? PRELOAD_DEFAULTS.bytes,
|
|
178
|
+
parallel: preloadOpt.parallel ?? PRELOAD_DEFAULTS.parallel,
|
|
179
|
+
};
|
|
146
180
|
const slugs = new Set();
|
|
147
181
|
const groups = [];
|
|
148
182
|
for (const g of manifest.groups) {
|
|
@@ -154,10 +188,22 @@ export function buildModel(manifest, options = {}) {
|
|
|
154
188
|
screens.set(shot.key, variants);
|
|
155
189
|
for (const theme of shot.theme ? [shot.theme] : THEMES) {
|
|
156
190
|
const vid = `${shot.size}-${theme}`;
|
|
157
|
-
const v = variants.get(vid) ?? {
|
|
191
|
+
const v = variants.get(vid) ?? {
|
|
192
|
+
size: shot.size,
|
|
193
|
+
theme,
|
|
194
|
+
url: "",
|
|
195
|
+
url2x: null,
|
|
196
|
+
width: 0,
|
|
197
|
+
height: 0,
|
|
198
|
+
bytes: null,
|
|
199
|
+
bytes2x: null,
|
|
200
|
+
thumb: null,
|
|
201
|
+
};
|
|
158
202
|
if (shot.x2) {
|
|
159
|
-
if (v.url2x === null)
|
|
203
|
+
if (v.url2x === null) {
|
|
160
204
|
v.url2x = item.url;
|
|
205
|
+
v.bytes2x = item.bytes ?? null;
|
|
206
|
+
}
|
|
161
207
|
}
|
|
162
208
|
else if (v.url === "") {
|
|
163
209
|
// An image wider than the width its name gives was taken at a higher density: shown at the name's width,
|
|
@@ -165,7 +211,9 @@ export function buildModel(manifest, options = {}) {
|
|
|
165
211
|
const scaled = shot.cssWidth !== null && item.width > shot.cssWidth;
|
|
166
212
|
const width = scaled ? shot.cssWidth : item.width;
|
|
167
213
|
const height = scaled ? Math.floor((2 * item.height * shot.cssWidth + item.width) / (2 * item.width)) : item.height;
|
|
168
|
-
|
|
214
|
+
const t = item.thumb;
|
|
215
|
+
const thumb = t ? { url: t.url, width: t.width, height: t.height, bytes: t.bytes ?? null } : null;
|
|
216
|
+
Object.assign(v, { url: item.url, width, height, bytes: item.bytes ?? null, thumb });
|
|
169
217
|
}
|
|
170
218
|
variants.set(vid, v);
|
|
171
219
|
}
|
|
@@ -229,6 +277,7 @@ export function buildModel(manifest, options = {}) {
|
|
|
229
277
|
headingLevel,
|
|
230
278
|
id,
|
|
231
279
|
credit,
|
|
280
|
+
preload,
|
|
232
281
|
groups,
|
|
233
282
|
};
|
|
234
283
|
}
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { type Theme } from "./model.js";
|
|
2
|
+
/** One image of a card at the size its tab shows: the thumbnail's URL and its full-size (2x) copy, if any. */
|
|
3
|
+
export interface PlanImage {
|
|
4
|
+
url: string;
|
|
5
|
+
full: string | null;
|
|
6
|
+
width: number;
|
|
7
|
+
height: number;
|
|
8
|
+
/** Bytes of `url` and of `full`: the manifest's where it gives them, else estimateBytes. */
|
|
9
|
+
bytes: number;
|
|
10
|
+
fullBytes: number;
|
|
11
|
+
}
|
|
12
|
+
export interface PlanCard {
|
|
13
|
+
light: PlanImage;
|
|
14
|
+
dark: PlanImage;
|
|
15
|
+
}
|
|
16
|
+
export interface PlanTab {
|
|
17
|
+
cards: PlanCard[];
|
|
18
|
+
/** How many of its cards are above the fold (measured for the shown tab, estimated for the rest). */
|
|
19
|
+
fold: number;
|
|
20
|
+
}
|
|
21
|
+
export interface Item {
|
|
22
|
+
url: string;
|
|
23
|
+
/** An estimate when the size isn't known: see estimateBytes. */
|
|
24
|
+
bytes: number;
|
|
25
|
+
/** Loads whatever the caps say: above the fold, or something the reader did. */
|
|
26
|
+
always: boolean;
|
|
27
|
+
}
|
|
28
|
+
export interface Caps {
|
|
29
|
+
/** Above-the-fold thumbnails of the shown tab, which always load. */
|
|
30
|
+
fold: number;
|
|
31
|
+
/** Everything else: at most this many images… */
|
|
32
|
+
count: number;
|
|
33
|
+
/** …and this many bytes. */
|
|
34
|
+
bytes: number;
|
|
35
|
+
/** Requests at once. */
|
|
36
|
+
parallel: number;
|
|
37
|
+
}
|
|
38
|
+
export declare const DEFAULT_CAPS: Caps;
|
|
39
|
+
/** What a slow connection allows: the shown tab's thumbnails, two at a time. */
|
|
40
|
+
export declare const SLOW_PARALLEL = 2;
|
|
41
|
+
/** The manifest gives no file sizes. VisualCI's PNG screenshots average about 0.08 bytes a pixel (measured on two
|
|
42
|
+
* real galleries, 0.02 to 0.2); 0.1 errs on the side of preloading less. */
|
|
43
|
+
export declare function estimateBytes(width: number, height: number): number;
|
|
44
|
+
export type Mode = "full" | "light";
|
|
45
|
+
export interface PlanInput {
|
|
46
|
+
tabs: PlanTab[];
|
|
47
|
+
current: number;
|
|
48
|
+
theme: Theme;
|
|
49
|
+
caps: Caps;
|
|
50
|
+
/** "light": the shown tab's thumbnails only (Save-Data, a slow connection). */
|
|
51
|
+
mode: Mode;
|
|
52
|
+
/** Whether the full-size view uses the 2x copies (a high-density screen). */
|
|
53
|
+
hiDpi: boolean;
|
|
54
|
+
/** Already loaded: left out, so they cost nothing against the caps. */
|
|
55
|
+
done?: Set<string>;
|
|
56
|
+
/** Images and bytes the background has already loaded past the fold: the caps hold for the whole page view, not
|
|
57
|
+
* for each plan. */
|
|
58
|
+
spent?: {
|
|
59
|
+
count: number;
|
|
60
|
+
bytes: number;
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
/** The background queue: thumbnails in the shown theme, then the other theme, then the full-size images, each in the
|
|
64
|
+
* order above, within the caps. The same image twice (a screen shown the same in both themes) is queued once. */
|
|
65
|
+
export declare function plan(input: PlanInput): Item[];
|
|
66
|
+
/** A tab the reader opened: its fold, then the rest of it, in the shown theme. Uncapped: the reader asked for it. */
|
|
67
|
+
export declare function tabItems(tab: PlanTab, theme: Theme, foldOnly?: boolean): Item[];
|
|
68
|
+
/** In the full-size view at card `i`: the next one, the one after, the previous one, the one before that. */
|
|
69
|
+
export declare function neighbours(cards: PlanCard[], i: number, theme: Theme, hiDpi: boolean): Item[];
|
|
70
|
+
export interface Request {
|
|
71
|
+
/** Resolves when the image has loaded (true) or failed (false). Never rejects. */
|
|
72
|
+
done: Promise<boolean>;
|
|
73
|
+
cancel(): void;
|
|
74
|
+
}
|
|
75
|
+
/** Start and end of a finished request, in milliseconds, and its size in bytes when the browser can say (a
|
|
76
|
+
* cross-origin image without Timing-Allow-Origin can't). */
|
|
77
|
+
export interface Timing {
|
|
78
|
+
start: number;
|
|
79
|
+
end: number;
|
|
80
|
+
bytes: number | null;
|
|
81
|
+
}
|
|
82
|
+
export interface Env {
|
|
83
|
+
load(url: string, high: boolean): Request;
|
|
84
|
+
/** Runs `fn` when the browser is idle. */
|
|
85
|
+
idle(fn: () => void): void;
|
|
86
|
+
/** The finished request's timing, if the browser recorded it. */
|
|
87
|
+
timing(url: string): Timing | null;
|
|
88
|
+
/** Runs `fn` after `ms` milliseconds. */
|
|
89
|
+
later(fn: () => void, ms: number): void;
|
|
90
|
+
}
|
|
91
|
+
/** After a slow verdict with nothing left to load, the preloader tries the full plan again after this long, doubling
|
|
92
|
+
* each time the rate is still slow. */
|
|
93
|
+
export declare const RECHECK_MS = 15000;
|
|
94
|
+
/** Bytes a second below which the connection counts as slow: 125 KB/s, 1 Mbit/s. */
|
|
95
|
+
export declare const SLOW_RATE = 125000;
|
|
96
|
+
/** Finished requests a rate is measured over, the latest ones. */
|
|
97
|
+
export declare const RATE_WINDOW = 6;
|
|
98
|
+
export declare class Preloader {
|
|
99
|
+
private env;
|
|
100
|
+
private caps;
|
|
101
|
+
/** Called when the measured rate crosses SLOW_RATE, so the owner can plan again in the other mode. */
|
|
102
|
+
private onSpeed;
|
|
103
|
+
private queue;
|
|
104
|
+
private front;
|
|
105
|
+
private running;
|
|
106
|
+
private finished;
|
|
107
|
+
private samples;
|
|
108
|
+
private stopped;
|
|
109
|
+
private pumping;
|
|
110
|
+
private recheck;
|
|
111
|
+
private rechecking;
|
|
112
|
+
/** Background images loaded past the fold, against the caps. */
|
|
113
|
+
readonly spent: {
|
|
114
|
+
count: number;
|
|
115
|
+
bytes: number;
|
|
116
|
+
};
|
|
117
|
+
/** Set by a slow measured rate, cleared by a fast one. */
|
|
118
|
+
slow: boolean;
|
|
119
|
+
constructor(env: Env, caps: Caps,
|
|
120
|
+
/** Called when the measured rate crosses SLOW_RATE, so the owner can plan again in the other mode. */
|
|
121
|
+
onSpeed?: (slow: boolean) => void);
|
|
122
|
+
/** Replaces the background queue. Background requests still running that the new plan doesn't want are dropped. */
|
|
123
|
+
setPlan(items: Item[]): void;
|
|
124
|
+
/** Puts what the reader did (or is about to do) ahead of everything, replacing the last jump. To make room, the
|
|
125
|
+
* background requests running are dropped first, then the old jump's. */
|
|
126
|
+
jump(items: Item[], high: boolean): void;
|
|
127
|
+
/** Adds a likely next step (a tab the pointer is over) after the current jump, without dropping anything. */
|
|
128
|
+
hint(items: Item[]): void;
|
|
129
|
+
/** Every URL loaded so far. */
|
|
130
|
+
loaded(): Set<string>;
|
|
131
|
+
has(url: string): boolean;
|
|
132
|
+
/** What's running, for tests. */
|
|
133
|
+
inFlight(): string[];
|
|
134
|
+
stop(): void;
|
|
135
|
+
private parallel;
|
|
136
|
+
private drop;
|
|
137
|
+
/** The background waits for idle time; a jump starts at once. */
|
|
138
|
+
private schedule;
|
|
139
|
+
private next;
|
|
140
|
+
private pump;
|
|
141
|
+
/** The rate over the latest finished requests: their bytes over the time from the first start to the last end, so
|
|
142
|
+
* requests running side by side count once. Cached ones are left out. */
|
|
143
|
+
private measure;
|
|
144
|
+
/** A new verdict starts a new window, so old samples never decide the next one. */
|
|
145
|
+
private flip;
|
|
146
|
+
/** Slow, with nothing left to load: nothing new gets measured, so after a while try the full plan again. If the
|
|
147
|
+
* rate is still slow the next samples say so, and the wait doubles. */
|
|
148
|
+
private maybeRecheck;
|
|
149
|
+
}
|