@reuse-sh/lightbox 0.0.6 → 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 +65 -16
- package/dist/audio.d.ts +2 -0
- package/dist/audio.js +17 -0
- package/dist/base.css +602 -0
- package/dist/browser/audio.js +3 -0
- package/dist/browser/audio.js.map +1 -0
- package/dist/browser/carousel-link.js +3 -0
- package/dist/browser/carousel-link.js.map +1 -0
- package/dist/browser/embed.js +3 -0
- package/dist/browser/embed.js.map +1 -0
- package/dist/browser/html.js +3 -0
- package/dist/browser/html.js.map +1 -0
- package/dist/browser/iframe.js +3 -0
- package/dist/browser/iframe.js.map +1 -0
- package/dist/browser/lightbox.js +3 -0
- package/dist/browser/lightbox.js.map +1 -0
- package/dist/browser/video.js +3 -0
- package/dist/browser/video.js.map +1 -0
- package/dist/carousel.d.ts +6 -0
- package/dist/carousel.js +17 -0
- package/dist/chrome.d.ts +35 -0
- package/dist/chrome.js +53 -0
- package/dist/embed.d.ts +14 -0
- package/dist/embed.js +173 -0
- package/dist/geometry.d.ts +37 -1
- package/dist/geometry.js +75 -5
- package/dist/html.d.ts +2 -0
- package/dist/html.js +15 -0
- package/dist/icons.d.ts +15 -0
- package/dist/icons.js +40 -0
- package/dist/iframe.d.ts +8 -0
- package/dist/iframe.js +43 -0
- package/dist/index.d.ts +180 -47
- package/dist/index.js +3817 -398
- package/dist/react.d.ts +18 -9
- package/dist/react.js +71 -46
- package/dist/sources.d.ts +28 -0
- package/dist/sources.js +179 -0
- package/dist/theme.css +236 -0
- package/dist/themes/dark.css +12 -0
- package/dist/themes/light.css +12 -0
- package/dist/themes/page.css +28 -0
- package/dist/themes/photos.css +17 -0
- package/dist/unlayered/base.css +599 -0
- package/dist/unlayered/theme.css +233 -0
- package/dist/unlayered/themes/dark.css +9 -0
- package/dist/unlayered/themes/light.css +9 -0
- package/dist/unlayered/themes/page.css +25 -0
- package/dist/unlayered/themes/photos.css +14 -0
- package/dist/video.d.ts +3 -0
- package/dist/video.js +55 -0
- package/package.json +33 -4
- package/dist/styles.css +0 -218
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/*! @reuse-sh/lightbox 0.2.0 | MIT License */
|
|
2
|
+
function e(e,t,n){let r=t;return e.addEventListener(`play`,()=>r=!1,{signal:n}),{interactive:!0,ready:new Promise(t=>{e.addEventListener(`loadedmetadata`,t,{once:!0,signal:n}),e.addEventListener(`error`,t,{once:!0,signal:n})}),pause:()=>e.pause(),resume(){r&&(r=!1,e.play().catch(t=>{if(t.name===`NotAllowedError`)return e.muted=!0,e.play()}).catch(()=>{}))},unmount(){e.pause(),e.removeAttribute(`src`),e.load(),e.remove()}}}function t(){return{type:`video`,mount(t,{item:n,signal:r,autoplay:i}){let a=t.ownerDocument.createElement(`video`);return a.className=`re-lightbox-video`,a.controls=!0,a.playsInline=!0,a.preload=`metadata`,n.previewSrc&&(a.poster=n.previewSrc),a.src=n.src??``,n.alt&&a.setAttribute(`aria-label`,n.alt),t.append(a),e(a,i,r)}}}export{e as playerOf,t as video};
|
|
3
|
+
//# sourceMappingURL=video.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"video.js","names":["playerOf","pending","autoplay","media","resolve","error","video","element","container","item","signal"],"sources":["../video.js"],"sourcesContent":["// A player starts by itself only the first time it is current, so a return leaves it as it was.\nexport function playerOf(media, autoplay, signal) {\n let pending = autoplay;\n media.addEventListener(\"play\", () => (pending = false), { signal });\n return {\n interactive: true,\n ready: new Promise((resolve) => {\n // Safari may stop at metadata under preload=\"metadata\"; the element shows its poster till then.\n media.addEventListener(\"loadedmetadata\", resolve, { once: true, signal });\n media.addEventListener(\"error\", resolve, { once: true, signal });\n }),\n pause: () => media.pause(),\n resume() {\n if (!pending)\n return;\n pending = false;\n // A browser that refuses sound may still play muted.\n media\n .play()\n .catch((error) => {\n // A pause before the first frame aborts the play; only a refusal retries muted.\n if (error.name !== \"NotAllowedError\")\n return;\n media.muted = true;\n return media.play();\n })\n .catch(() => { });\n },\n unmount() {\n media.pause();\n media.removeAttribute(\"src\");\n media.load();\n media.remove();\n },\n };\n}\nexport function video() {\n return {\n type: \"video\",\n mount(container, { item, signal, autoplay }) {\n const element = container.ownerDocument.createElement(\"video\");\n element.className = \"re-lightbox-video\";\n element.controls = true;\n element.playsInline = true;\n element.preload = \"metadata\";\n if (item.previewSrc)\n element.poster = item.previewSrc;\n element.src = item.src ?? \"\";\n if (item.alt)\n element.setAttribute(\"aria-label\", item.alt);\n container.append(element);\n return playerOf(element, autoplay, signal);\n },\n };\n}\n"],"mappings":";AACA,SAAgBA,EAAS,EAAO,EAAU,EAAQ,CAC9C,IAAIC,EAAUC,EAEd,OADAC,EAAM,iBAAiB,WAAe,EAAU,GAAQ,CAAE,QAAO,CAAC,EAC3D,CACH,YAAa,GACb,MAAO,IAAI,QAAS,GAAY,CAE5BA,EAAM,iBAAiB,iBAAkBC,EAAS,CAAE,KAAM,GAAM,QAAO,CAAC,EACxED,EAAM,iBAAiB,QAASC,EAAS,CAAE,KAAM,GAAM,QAAO,CAAC,CACnE,CAAC,EACD,UAAaD,EAAM,MAAM,EACzB,QAAS,CACAF,IAEL,EAAU,GAEVE,EACK,KAAK,CAAC,CACN,MAAO,GAAU,CAElB,GAAIE,EAAM,OAAS,kBAGnB,MADA,GAAM,MAAQ,GACPF,EAAM,KAAK,CACtB,CAAC,CAAC,CACG,UAAY,CAAE,CAAC,EACxB,EACA,SAAU,CACNA,EAAM,MAAM,EACZA,EAAM,gBAAgB,KAAK,EAC3BA,EAAM,KAAK,EACXA,EAAM,OAAO,CACjB,CACJ,CACJ,CACA,SAAgBG,GAAQ,CACpB,MAAO,CACH,KAAM,QACN,MAAM,EAAW,CAAE,OAAM,SAAQ,YAAY,CACzC,IAAMC,EAAUC,EAAU,cAAc,cAAc,OAAO,EAW7D,MAVA,GAAQ,UAAY,oBACpB,EAAQ,SAAW,GACnB,EAAQ,YAAc,GACtB,EAAQ,QAAU,WACdC,EAAK,aACL,EAAQ,OAASA,EAAK,YAC1B,EAAQ,IAAMA,EAAK,KAAO,GACtBA,EAAK,KACLF,EAAQ,aAAa,aAAcE,EAAK,GAAG,EAC/CD,EAAU,OAAOD,CAAO,EACjBP,EAASO,EAASL,EAAUQ,CAAM,CAC7C,CACJ,CACJ,C"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Carousel } from "@reuse-sh/carousel";
|
|
2
|
+
import type { Lightbox } from "./index.js";
|
|
3
|
+
export interface ConnectCarouselOptions {
|
|
4
|
+
slideOf?: (index: number) => number;
|
|
5
|
+
}
|
|
6
|
+
export declare function connectCarousel(lightbox: Lightbox, carousel: Carousel, { slideOf }?: ConnectCarouselOptions): () => void;
|
package/dist/carousel.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export function connectCarousel(lightbox, carousel, { slideOf = (index) => index } = {}) {
|
|
2
|
+
const hold = (isHeld) => carousel.hold("lightbox", isHeld);
|
|
3
|
+
const follow = (event) => carousel.goTo(slideOf(event.index), "instant");
|
|
4
|
+
const stops = [
|
|
5
|
+
lightbox.on("open", () => hold(true)),
|
|
6
|
+
lightbox.on("settle", follow),
|
|
7
|
+
lightbox.on("beforeclose", follow),
|
|
8
|
+
lightbox.on("closed", () => hold(false)),
|
|
9
|
+
];
|
|
10
|
+
if (lightbox.getState().isOpen)
|
|
11
|
+
hold(true);
|
|
12
|
+
return () => {
|
|
13
|
+
for (const stop of stops)
|
|
14
|
+
stop();
|
|
15
|
+
hold(false);
|
|
16
|
+
};
|
|
17
|
+
}
|
package/dist/chrome.d.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Point } from "./geometry.js";
|
|
2
|
+
export type ChromeInput = {
|
|
3
|
+
type: "move";
|
|
4
|
+
point: Point;
|
|
5
|
+
movement: Point;
|
|
6
|
+
} | {
|
|
7
|
+
type: "idle";
|
|
8
|
+
} | {
|
|
9
|
+
type: "key";
|
|
10
|
+
} | {
|
|
11
|
+
type: "focus";
|
|
12
|
+
} | {
|
|
13
|
+
type: "tap";
|
|
14
|
+
} | {
|
|
15
|
+
type: "zoom";
|
|
16
|
+
} | {
|
|
17
|
+
type: "unzoom";
|
|
18
|
+
} | {
|
|
19
|
+
type: "pinch";
|
|
20
|
+
} | {
|
|
21
|
+
type: "touch";
|
|
22
|
+
} | {
|
|
23
|
+
type: "close";
|
|
24
|
+
};
|
|
25
|
+
export interface ChromeModel {
|
|
26
|
+
readonly chrome: "shown" | "hidden";
|
|
27
|
+
readonly hiddenAt: Point | null;
|
|
28
|
+
readonly pointer: Point | null;
|
|
29
|
+
readonly lastPointerIsMouse: boolean;
|
|
30
|
+
readonly timer: boolean;
|
|
31
|
+
readonly closed: boolean;
|
|
32
|
+
}
|
|
33
|
+
export declare const SHOW_DISTANCE = 24;
|
|
34
|
+
export declare function initialChrome(lastPointerIsMouse: boolean): ChromeModel;
|
|
35
|
+
export declare function stepChrome(model: ChromeModel, input: ChromeInput, controlInUse: boolean): ChromeModel;
|
package/dist/chrome.js
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export const SHOW_DISTANCE = 24;
|
|
2
|
+
export function initialChrome(lastPointerIsMouse) {
|
|
3
|
+
return {
|
|
4
|
+
chrome: "shown",
|
|
5
|
+
hiddenAt: null,
|
|
6
|
+
pointer: null,
|
|
7
|
+
lastPointerIsMouse,
|
|
8
|
+
timer: lastPointerIsMouse,
|
|
9
|
+
closed: false,
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export function stepChrome(model, input, controlInUse) {
|
|
13
|
+
if (model.closed)
|
|
14
|
+
return model;
|
|
15
|
+
const show = (from) => ({
|
|
16
|
+
...from,
|
|
17
|
+
chrome: "shown",
|
|
18
|
+
hiddenAt: null,
|
|
19
|
+
timer: from.lastPointerIsMouse,
|
|
20
|
+
});
|
|
21
|
+
const hide = (from) => controlInUse
|
|
22
|
+
? { ...from, timer: from.lastPointerIsMouse }
|
|
23
|
+
: { ...from, chrome: "hidden", hiddenAt: from.pointer, timer: false };
|
|
24
|
+
switch (input.type) {
|
|
25
|
+
case "move": {
|
|
26
|
+
const next = { ...model, pointer: input.point, lastPointerIsMouse: true };
|
|
27
|
+
if (model.chrome === "shown")
|
|
28
|
+
return { ...next, timer: true };
|
|
29
|
+
const hiddenAt = model.hiddenAt ?? {
|
|
30
|
+
x: input.point.x - input.movement.x,
|
|
31
|
+
y: input.point.y - input.movement.y,
|
|
32
|
+
};
|
|
33
|
+
return Math.hypot(input.point.x - hiddenAt.x, input.point.y - hiddenAt.y) >= SHOW_DISTANCE
|
|
34
|
+
? show(next)
|
|
35
|
+
: { ...next, hiddenAt };
|
|
36
|
+
}
|
|
37
|
+
case "idle":
|
|
38
|
+
return model.chrome === "shown" ? hide(model) : model;
|
|
39
|
+
case "key":
|
|
40
|
+
case "focus":
|
|
41
|
+
case "unzoom":
|
|
42
|
+
return show(model);
|
|
43
|
+
case "tap":
|
|
44
|
+
return model.chrome === "shown" ? hide(model) : show(model);
|
|
45
|
+
case "zoom":
|
|
46
|
+
case "pinch":
|
|
47
|
+
return model.chrome === "shown" ? hide(model) : model;
|
|
48
|
+
case "touch":
|
|
49
|
+
return { ...model, lastPointerIsMouse: false, timer: false };
|
|
50
|
+
case "close":
|
|
51
|
+
return { ...model, timer: false, closed: true };
|
|
52
|
+
}
|
|
53
|
+
}
|
package/dist/embed.d.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type FrameOptions } from "./iframe.js";
|
|
2
|
+
import type { LightboxContent, LightboxItem } from "./index.js";
|
|
3
|
+
export interface EmbedProvider {
|
|
4
|
+
name: string;
|
|
5
|
+
pattern: RegExp;
|
|
6
|
+
url(match: RegExpExecArray, item: LightboxItem): string;
|
|
7
|
+
/** Width over height, 16 / 9 when missing. */
|
|
8
|
+
aspect?: number;
|
|
9
|
+
}
|
|
10
|
+
export interface EmbedOptions extends FrameOptions {
|
|
11
|
+
/** Tried in order before the built-in YouTube and Vimeo providers. */
|
|
12
|
+
providers?: readonly EmbedProvider[];
|
|
13
|
+
}
|
|
14
|
+
export declare function embed(options?: EmbedOptions): LightboxContent;
|
package/dist/embed.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { frameOf, loaded } from "./iframe.js";
|
|
2
|
+
const parse = (data) => {
|
|
3
|
+
try {
|
|
4
|
+
const value = typeof data === "string" ? JSON.parse(data) : data;
|
|
5
|
+
return typeof value === "object" && value !== null
|
|
6
|
+
? value
|
|
7
|
+
: undefined;
|
|
8
|
+
}
|
|
9
|
+
catch {
|
|
10
|
+
return undefined;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const youtubeRemote = {
|
|
14
|
+
greet: (send) => send({ event: "listening", id: 1, channel: "widget" }),
|
|
15
|
+
command: (command) => ({
|
|
16
|
+
event: "command",
|
|
17
|
+
func: command === "mute" ? "mute" : `${command}Video`,
|
|
18
|
+
args: [],
|
|
19
|
+
}),
|
|
20
|
+
state(data) {
|
|
21
|
+
const info = data.info;
|
|
22
|
+
const value = info?.playerState;
|
|
23
|
+
return value === 1
|
|
24
|
+
? "playing"
|
|
25
|
+
: value === 3
|
|
26
|
+
? "waiting"
|
|
27
|
+
: value === 2 || value === 0
|
|
28
|
+
? "paused"
|
|
29
|
+
: value === -1 || value === 5
|
|
30
|
+
? "unstarted"
|
|
31
|
+
: undefined;
|
|
32
|
+
},
|
|
33
|
+
};
|
|
34
|
+
const vimeoRemote = {
|
|
35
|
+
greet(send) {
|
|
36
|
+
for (const value of ["play", "pause", "ended"])
|
|
37
|
+
send({ method: "addEventListener", value });
|
|
38
|
+
},
|
|
39
|
+
command: (command) => command === "mute"
|
|
40
|
+
? { method: "setVolume", value: 0 }
|
|
41
|
+
: { method: command },
|
|
42
|
+
state: (data) => data.event === "play"
|
|
43
|
+
? "playing"
|
|
44
|
+
: data.event === "pause" || data.event === "ended"
|
|
45
|
+
? "paused"
|
|
46
|
+
: undefined,
|
|
47
|
+
};
|
|
48
|
+
const remotes = new WeakMap();
|
|
49
|
+
const youtube = (id) => `https://www.youtube-nocookie.com/embed/${id}?enablejsapi=1&playsinline=1`;
|
|
50
|
+
const builtIn = [
|
|
51
|
+
{
|
|
52
|
+
name: "youtube-shorts",
|
|
53
|
+
pattern: /^https?:\/\/(?:www\.|m\.)?youtube\.com\/shorts\/([\w-]+)/,
|
|
54
|
+
url: (match) => youtube(match[1]),
|
|
55
|
+
aspect: 9 / 16,
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: "youtube",
|
|
59
|
+
pattern: /^https?:\/\/(?:(?:www\.|m\.)?youtube(?:-nocookie)?\.com\/(?:watch\?(?:.*&)?v=|embed\/|live\/)|youtu\.be\/)([\w-]+)/,
|
|
60
|
+
url: (match) => youtube(match[1]),
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: "vimeo",
|
|
64
|
+
pattern: /^https?:\/\/(?:player\.)?vimeo\.com\/(?:video\/)?(\d+)(?:\/([\da-f]+))?/,
|
|
65
|
+
url: (match) => `https://player.vimeo.com/video/${match[1]}${match[2] ? `?h=${match[2]}` : ""}`,
|
|
66
|
+
},
|
|
67
|
+
];
|
|
68
|
+
remotes.set(builtIn[0], youtubeRemote);
|
|
69
|
+
remotes.set(builtIn[1], youtubeRemote);
|
|
70
|
+
remotes.set(builtIn[2], vimeoRemote);
|
|
71
|
+
// The source URL's own parameters, such as a start time, carry over to the player.
|
|
72
|
+
function withParameters(url, source) {
|
|
73
|
+
try {
|
|
74
|
+
const target = new URL(url);
|
|
75
|
+
for (const [key, value] of new URL(source).searchParams) {
|
|
76
|
+
// YouTube links say t=30s; its player reads start=30.
|
|
77
|
+
if (key === "v" || target.searchParams.has(key === "t" ? "start" : key))
|
|
78
|
+
continue;
|
|
79
|
+
if (key !== "t")
|
|
80
|
+
target.searchParams.set(key, value);
|
|
81
|
+
else {
|
|
82
|
+
const [, h = 0, m = 0, sec = 0] = /^(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s?)?$/.exec(value) ?? [];
|
|
83
|
+
const seconds = Number(h) * 3600 + Number(m) * 60 + Number(sec);
|
|
84
|
+
if (seconds)
|
|
85
|
+
target.searchParams.set("start", String(seconds));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return target.href;
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return url;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
const MUTED_RETRY = 1000;
|
|
95
|
+
export function embed(options = {}) {
|
|
96
|
+
const providers = [...(options.providers ?? []), ...builtIn];
|
|
97
|
+
return {
|
|
98
|
+
type: "embed",
|
|
99
|
+
mount(container, { item, signal, autoplay }) {
|
|
100
|
+
const source = item.src ?? "";
|
|
101
|
+
let provider;
|
|
102
|
+
let match = null;
|
|
103
|
+
for (const candidate of providers) {
|
|
104
|
+
match = candidate.pattern.exec(source);
|
|
105
|
+
if (match) {
|
|
106
|
+
provider = candidate;
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
// An unknown URL stays blank: markup can name any page as an embed.
|
|
111
|
+
const url = provider && match
|
|
112
|
+
? withParameters(provider.url(match, item), source)
|
|
113
|
+
: "";
|
|
114
|
+
const frame = frameOf(container, url, item.alt ?? provider?.name ?? "", options.sandbox);
|
|
115
|
+
const ready = loaded(frame, signal);
|
|
116
|
+
const base = {
|
|
117
|
+
interactive: true,
|
|
118
|
+
aspect: provider?.aspect,
|
|
119
|
+
ready,
|
|
120
|
+
unmount: () => frame.remove(),
|
|
121
|
+
};
|
|
122
|
+
const remote = provider && remotes.get(provider);
|
|
123
|
+
if (!remote)
|
|
124
|
+
return base;
|
|
125
|
+
const origin = new URL(url).origin;
|
|
126
|
+
const send = (message) => frame.contentWindow?.postMessage(JSON.stringify(message), origin);
|
|
127
|
+
let pending = autoplay;
|
|
128
|
+
let current = false;
|
|
129
|
+
let state;
|
|
130
|
+
let retry;
|
|
131
|
+
container.ownerDocument.defaultView?.addEventListener("message", (event) => {
|
|
132
|
+
if (event.source !== frame.contentWindow || event.origin !== origin)
|
|
133
|
+
return;
|
|
134
|
+
const data = parse(event.data);
|
|
135
|
+
state = (data && remote.state(data)) ?? state;
|
|
136
|
+
if (state === "playing") {
|
|
137
|
+
pending = false;
|
|
138
|
+
clearTimeout(retry);
|
|
139
|
+
}
|
|
140
|
+
}, { signal });
|
|
141
|
+
signal.addEventListener("abort", () => clearTimeout(retry));
|
|
142
|
+
void ready.then(() => remote.greet(send));
|
|
143
|
+
return {
|
|
144
|
+
...base,
|
|
145
|
+
pause() {
|
|
146
|
+
current = false;
|
|
147
|
+
clearTimeout(retry);
|
|
148
|
+
send(remote.command("pause"));
|
|
149
|
+
},
|
|
150
|
+
resume() {
|
|
151
|
+
current = true;
|
|
152
|
+
if (!pending)
|
|
153
|
+
return;
|
|
154
|
+
pending = false;
|
|
155
|
+
void ready.then(() => {
|
|
156
|
+
// A slide left before its player loaded stays quiet.
|
|
157
|
+
if (signal.aborted || !current)
|
|
158
|
+
return;
|
|
159
|
+
send(remote.command("play"));
|
|
160
|
+
// A player the browser keeps from starting with sound gets one muted try.
|
|
161
|
+
retry = setTimeout(() => {
|
|
162
|
+
// Buffering on a slow line is a start, not a refusal.
|
|
163
|
+
if (state === "playing" || state === "waiting")
|
|
164
|
+
return;
|
|
165
|
+
send(remote.command("mute"));
|
|
166
|
+
send(remote.command("play"));
|
|
167
|
+
}, MUTED_RETRY);
|
|
168
|
+
});
|
|
169
|
+
},
|
|
170
|
+
};
|
|
171
|
+
},
|
|
172
|
+
};
|
|
173
|
+
}
|
package/dist/geometry.d.ts
CHANGED
|
@@ -16,6 +16,42 @@ export interface Rect extends Size {
|
|
|
16
16
|
export declare const identity: Zoom;
|
|
17
17
|
export declare function clamp(value: number, min: number, max: number): number;
|
|
18
18
|
export declare function contain(image: Size, viewport: Size, padding?: number): Size;
|
|
19
|
+
export declare function widthFit(image: Size, viewport: Size, padding?: number): Size;
|
|
20
|
+
export type Shape = "tall" | "wide" | "regular";
|
|
21
|
+
export declare function shapeOf(image: Size, viewport: Size, padding?: number): Shape;
|
|
19
22
|
export declare function zoomAround(from: Zoom, scale: number, focus: Point): Zoom;
|
|
20
|
-
|
|
23
|
+
/** Where the photo pans: the area's size and how far its center sits from the photo's resting center. */
|
|
24
|
+
export interface Area extends Size {
|
|
25
|
+
x?: number;
|
|
26
|
+
y?: number;
|
|
27
|
+
}
|
|
28
|
+
export interface Range {
|
|
29
|
+
min: number;
|
|
30
|
+
max: number;
|
|
31
|
+
}
|
|
32
|
+
export interface PanRange {
|
|
33
|
+
x: Range;
|
|
34
|
+
y: Range;
|
|
35
|
+
}
|
|
36
|
+
export declare function panRange(scale: number, content: Size, area: Area): PanRange;
|
|
37
|
+
export declare function boundZoom(zoom: Zoom, content: Size, area: Area): Zoom;
|
|
38
|
+
export declare const RUBBER = 0.35;
|
|
39
|
+
export declare function rubber(value: number, range: Range): number;
|
|
40
|
+
export declare function unrubber(value: number, range: Range): number;
|
|
41
|
+
export declare function elasticZoom(zoom: Zoom, content: Size, area: Area): Zoom;
|
|
42
|
+
export interface ZoomLimits {
|
|
43
|
+
min: number;
|
|
44
|
+
max: number;
|
|
45
|
+
tap: number;
|
|
46
|
+
}
|
|
47
|
+
export declare function zoomLimits({ fileWidth, image, viewport, zoomed, padding, svg, maxZoom, tapZoom, }: {
|
|
48
|
+
fileWidth: number | undefined;
|
|
49
|
+
image: Size;
|
|
50
|
+
viewport: Size;
|
|
51
|
+
zoomed?: Size;
|
|
52
|
+
padding: number;
|
|
53
|
+
svg: boolean;
|
|
54
|
+
maxZoom: number | undefined;
|
|
55
|
+
tapZoom: number;
|
|
56
|
+
}): ZoomLimits;
|
|
21
57
|
export declare function cover(image: Size, frame: Size, position?: Point): Rect;
|
package/dist/geometry.js
CHANGED
|
@@ -8,6 +8,17 @@ export function contain(image, viewport, padding = 32) {
|
|
|
8
8
|
const scale = Math.min(width / image.width, height / image.height, 1);
|
|
9
9
|
return { width: image.width * scale, height: image.height * scale };
|
|
10
10
|
}
|
|
11
|
+
export function widthFit(image, viewport, padding = 32) {
|
|
12
|
+
const width = Math.max(1, viewport.width - padding * 2);
|
|
13
|
+
const scale = Math.min(width / image.width, 1);
|
|
14
|
+
return { width: image.width * scale, height: image.height * scale };
|
|
15
|
+
}
|
|
16
|
+
export function shapeOf(image, viewport, padding = 32) {
|
|
17
|
+
if (image.height > 2 * image.width &&
|
|
18
|
+
widthFit(image, viewport, padding).height > 2 * viewport.height)
|
|
19
|
+
return "tall";
|
|
20
|
+
return image.width > 3 * image.height ? "wide" : "regular";
|
|
21
|
+
}
|
|
11
22
|
export function zoomAround(from, scale, focus) {
|
|
12
23
|
const ratio = scale / from.scale;
|
|
13
24
|
return {
|
|
@@ -16,15 +27,74 @@ export function zoomAround(from, scale, focus) {
|
|
|
16
27
|
y: focus.y - ratio * (focus.y - from.y),
|
|
17
28
|
};
|
|
18
29
|
}
|
|
19
|
-
|
|
20
|
-
const
|
|
21
|
-
|
|
30
|
+
function axisRange(content, area, offset = 0) {
|
|
31
|
+
const over = (content - area) / 2;
|
|
32
|
+
if (over >= 0)
|
|
33
|
+
return { min: offset - over, max: offset + over };
|
|
34
|
+
const at = clamp(0, offset + over, offset - over);
|
|
35
|
+
return { min: at, max: at };
|
|
36
|
+
}
|
|
37
|
+
export function panRange(scale, content, area) {
|
|
38
|
+
return {
|
|
39
|
+
x: axisRange(content.width * scale, area.width, area.x),
|
|
40
|
+
y: axisRange(content.height * scale, area.height, area.y),
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export function boundZoom(zoom, content, area) {
|
|
44
|
+
const range = panRange(zoom.scale, content, area);
|
|
22
45
|
return {
|
|
23
46
|
scale: zoom.scale,
|
|
24
|
-
x: clamp(zoom.x,
|
|
25
|
-
y: clamp(zoom.y,
|
|
47
|
+
x: clamp(zoom.x, range.x.min, range.x.max) || 0,
|
|
48
|
+
y: clamp(zoom.y, range.y.min, range.y.max) || 0,
|
|
26
49
|
};
|
|
27
50
|
}
|
|
51
|
+
export const RUBBER = 0.35;
|
|
52
|
+
export function rubber(value, range) {
|
|
53
|
+
if (value > range.max)
|
|
54
|
+
return range.max + (value - range.max) * RUBBER;
|
|
55
|
+
if (value < range.min)
|
|
56
|
+
return range.min - (range.min - value) * RUBBER;
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
export function unrubber(value, range) {
|
|
60
|
+
if (value > range.max)
|
|
61
|
+
return range.max + (value - range.max) / RUBBER;
|
|
62
|
+
if (value < range.min)
|
|
63
|
+
return range.min - (range.min - value) / RUBBER;
|
|
64
|
+
return value;
|
|
65
|
+
}
|
|
66
|
+
export function elasticZoom(zoom, content, area) {
|
|
67
|
+
const range = panRange(zoom.scale, content, area);
|
|
68
|
+
return {
|
|
69
|
+
scale: zoom.scale,
|
|
70
|
+
x: rubber(zoom.x, range.x),
|
|
71
|
+
y: rubber(zoom.y, range.y),
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
export function zoomLimits({ fileWidth, image, viewport, zoomed = viewport, padding, svg, maxZoom, tapZoom, }) {
|
|
75
|
+
const factor = (fallback) => maxZoom !== undefined && Number.isFinite(maxZoom) && maxZoom > 0
|
|
76
|
+
? maxZoom
|
|
77
|
+
: fallback;
|
|
78
|
+
const shape = shapeOf(image, viewport, padding);
|
|
79
|
+
const whole = contain(image, viewport, padding);
|
|
80
|
+
const fitted = shape === "tall" ? widthFit(image, viewport, padding) : whole;
|
|
81
|
+
const min = shape === "tall" ? whole.width / fitted.width : 1;
|
|
82
|
+
const preferred = (max, fallback) => shape === "tall"
|
|
83
|
+
? min
|
|
84
|
+
: shape === "wide"
|
|
85
|
+
? clamp(zoomed.height / fitted.height, 1, max)
|
|
86
|
+
: clamp(fallback, 1, max);
|
|
87
|
+
const tap = Number.isFinite(tapZoom) ? tapZoom : 1;
|
|
88
|
+
if (svg) {
|
|
89
|
+
const max = Math.max(1, factor(4));
|
|
90
|
+
return { min, max, tap: preferred(max, tap) };
|
|
91
|
+
}
|
|
92
|
+
const natural = fileWidth ? fileWidth / fitted.width : 0;
|
|
93
|
+
if (natural <= 1 + 1e-3)
|
|
94
|
+
return { min, max: 1, tap: preferred(1, 1) };
|
|
95
|
+
const max = Math.max(1, natural * factor(1));
|
|
96
|
+
return { min, max, tap: preferred(max, Math.max(tap, natural)) };
|
|
97
|
+
}
|
|
28
98
|
export function cover(image, frame, position = { x: 0.5, y: 0.5 }) {
|
|
29
99
|
const scale = Math.max(frame.width / image.width, frame.height / image.height);
|
|
30
100
|
const width = image.width * scale;
|
package/dist/html.d.ts
ADDED
package/dist/html.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// The slide takes a Node from item.html(); a string is never parsed as markup.
|
|
2
|
+
export function html() {
|
|
3
|
+
return {
|
|
4
|
+
type: "html",
|
|
5
|
+
mount(container, { item }) {
|
|
6
|
+
const box = container.ownerDocument.createElement("div");
|
|
7
|
+
box.className = "re-lightbox-html";
|
|
8
|
+
const node = item.html?.();
|
|
9
|
+
if (node)
|
|
10
|
+
box.append(node);
|
|
11
|
+
container.append(box);
|
|
12
|
+
return { interactive: true, unmount: () => box.remove() };
|
|
13
|
+
},
|
|
14
|
+
};
|
|
15
|
+
}
|
package/dist/icons.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare const paths: {
|
|
2
|
+
close: string;
|
|
3
|
+
previous: string;
|
|
4
|
+
next: string;
|
|
5
|
+
zoomIn: string;
|
|
6
|
+
zoomOut: string;
|
|
7
|
+
fullscreenEnter: string;
|
|
8
|
+
fullscreenExit: string;
|
|
9
|
+
download: string;
|
|
10
|
+
play: string;
|
|
11
|
+
pause: string;
|
|
12
|
+
};
|
|
13
|
+
export type BuiltInIcon = keyof typeof paths;
|
|
14
|
+
export declare function showIcon(button: Element, name: BuiltInIcon): void;
|
|
15
|
+
export {};
|
package/dist/icons.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
const lens = "M18 11a7 7 0 1 1-14 0 7 7 0 0 1 14 0m-2 5 4 4M8 11h6";
|
|
2
|
+
const paths = {
|
|
3
|
+
close: "M6 6l12 12M18 6 6 18",
|
|
4
|
+
previous: "m15 6-6 6 6 6",
|
|
5
|
+
next: "m9 6 6 6-6 6",
|
|
6
|
+
zoomIn: `${lens}M11 8v6`,
|
|
7
|
+
zoomOut: lens,
|
|
8
|
+
fullscreenEnter: "M4 9V4h5m6 0h5v5m0 6v5h-5m-6 0H4v-5",
|
|
9
|
+
fullscreenExit: "M9 4v5H4m11-5v5h5m0 6h-5v5m-6 0v-5H4",
|
|
10
|
+
download: "M12 4v11m-5-5 5 5 5-5M5 20h14",
|
|
11
|
+
play: "M8 5v14l11-7z",
|
|
12
|
+
pause: "M9 5v14m6-14v14",
|
|
13
|
+
};
|
|
14
|
+
const svgNamespace = "http://www.w3.org/2000/svg";
|
|
15
|
+
export function showIcon(button, name) {
|
|
16
|
+
const d = paths[name];
|
|
17
|
+
if (button.querySelector("path")?.getAttribute("d") === d)
|
|
18
|
+
return;
|
|
19
|
+
const doc = button.ownerDocument;
|
|
20
|
+
const svg = doc.createElementNS(svgNamespace, "svg");
|
|
21
|
+
const attributes = {
|
|
22
|
+
width: "24",
|
|
23
|
+
height: "24",
|
|
24
|
+
viewBox: "0 0 24 24",
|
|
25
|
+
fill: "none",
|
|
26
|
+
stroke: "currentColor",
|
|
27
|
+
"stroke-width": "2",
|
|
28
|
+
"stroke-linecap": "round",
|
|
29
|
+
"stroke-linejoin": "round",
|
|
30
|
+
"aria-hidden": "true",
|
|
31
|
+
};
|
|
32
|
+
for (const [key, value] of Object.entries(attributes))
|
|
33
|
+
svg.setAttribute(key, value);
|
|
34
|
+
if (name === "previous" || name === "next")
|
|
35
|
+
svg.setAttribute("class", "re-lightbox-arrow-icon");
|
|
36
|
+
const path = doc.createElementNS(svgNamespace, "path");
|
|
37
|
+
path.setAttribute("d", d);
|
|
38
|
+
svg.append(path);
|
|
39
|
+
button.replaceChildren(svg);
|
|
40
|
+
}
|
package/dist/iframe.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { LightboxContent } from "./index.js";
|
|
2
|
+
export interface FrameOptions {
|
|
3
|
+
/** Added as the iframe's sandbox attribute. */
|
|
4
|
+
sandbox?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function frameOf(container: HTMLElement, src: string, title: string, sandbox: string | undefined): HTMLIFrameElement;
|
|
7
|
+
export declare function loaded(frame: HTMLIFrameElement, signal: AbortSignal): Promise<unknown>;
|
|
8
|
+
export declare function iframe(options?: FrameOptions): LightboxContent;
|
package/dist/iframe.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// No top navigation for a page from another origin or for the popups it opens.
|
|
2
|
+
const CROSS_ORIGIN_SANDBOX = "allow-scripts allow-same-origin allow-forms allow-presentation allow-popups";
|
|
3
|
+
export function frameOf(container, src, title, sandbox) {
|
|
4
|
+
const document = container.ownerDocument;
|
|
5
|
+
const frame = document.createElement("iframe");
|
|
6
|
+
frame.className = "re-lightbox-iframe";
|
|
7
|
+
frame.allow = "autoplay; fullscreen; picture-in-picture";
|
|
8
|
+
frame.referrerPolicy = "strict-origin-when-cross-origin";
|
|
9
|
+
frame.loading = "lazy";
|
|
10
|
+
frame.title = title;
|
|
11
|
+
// Only web pages load: a javascript: URL would run in the host page.
|
|
12
|
+
frame.src = "about:blank";
|
|
13
|
+
if (src.trim())
|
|
14
|
+
try {
|
|
15
|
+
const parsed = new URL(src, document.baseURI);
|
|
16
|
+
if (/^https?:$/.test(parsed.protocol))
|
|
17
|
+
frame.src = parsed.href;
|
|
18
|
+
if (parsed.origin !== document.location.origin)
|
|
19
|
+
sandbox ??= CROSS_ORIGIN_SANDBOX;
|
|
20
|
+
}
|
|
21
|
+
catch { }
|
|
22
|
+
if (sandbox != null)
|
|
23
|
+
frame.sandbox = sandbox;
|
|
24
|
+
container.append(frame);
|
|
25
|
+
return frame;
|
|
26
|
+
}
|
|
27
|
+
export function loaded(frame, signal) {
|
|
28
|
+
return new Promise((resolve) => frame.addEventListener("load", resolve, { once: true, signal }));
|
|
29
|
+
}
|
|
30
|
+
// A page in a frame has no pause, so the viewer unmounts it when another slide is current.
|
|
31
|
+
export function iframe(options = {}) {
|
|
32
|
+
return {
|
|
33
|
+
type: "iframe",
|
|
34
|
+
mount(container, { item, signal }) {
|
|
35
|
+
const frame = frameOf(container, item.src ?? "", item.alt ?? "", options.sandbox);
|
|
36
|
+
return {
|
|
37
|
+
interactive: true,
|
|
38
|
+
ready: loaded(frame, signal),
|
|
39
|
+
unmount: () => frame.remove(),
|
|
40
|
+
};
|
|
41
|
+
},
|
|
42
|
+
};
|
|
43
|
+
}
|