@ohmagif/js 0.0.0-stage → 0.1.1
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 +674 -0
- package/README.md +68 -2
- package/dist/fetch.d.ts +89 -0
- package/dist/fetch.js +239 -0
- package/dist/gif.d.ts +96 -0
- package/dist/gif.js +138 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/react/Card.d.ts +32 -0
- package/dist/react/Card.js +125 -0
- package/dist/react/Gif.d.ts +28 -0
- package/dist/react/Gif.js +12 -0
- package/dist/react/Grid.d.ts +55 -0
- package/dist/react/Grid.js +178 -0
- package/dist/react/Picture.d.ts +19 -0
- package/dist/react/Picture.js +22 -0
- package/dist/react/index.d.ts +3 -0
- package/dist/react/index.js +3 -0
- package/dist/react/layout.d.ts +36 -0
- package/dist/react/layout.js +48 -0
- package/package.json +69 -4
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { frameSize, gifLabel, tinyFrameSize } from "../gif.js";
|
|
3
|
+
import { CARD_BORDER, CREDIT_LINE_HEIGHT, TILE_CREDIT_LINE_HEIGHT } from "./layout.js";
|
|
4
|
+
import { Picture } from "./Picture.js";
|
|
5
|
+
// The card is the one ohmagif's embed draws (templates/embed.html): the
|
|
6
|
+
// frame on white with a 1 px border and 6 px corners, and a 28 px credit
|
|
7
|
+
// line under it, "GIF by @handle on ohmagif.com". Here the whole line links
|
|
8
|
+
// to the GIF's page. Each colour is a custom property a page can set; by
|
|
9
|
+
// default it follows the page's color-scheme, as the embed follows the
|
|
10
|
+
// system's.
|
|
11
|
+
const card = "var(--ohmagif-card, light-dark(#FFFFFF, #1D1B18))";
|
|
12
|
+
const border = "var(--ohmagif-border, light-dark(#E9E5DE, #2F2C27))";
|
|
13
|
+
const muted = "var(--ohmagif-muted, light-dark(#6F6A62, #A7A196))";
|
|
14
|
+
const link = "var(--ohmagif-link, light-dark(#2F6B12, #B8E07A))";
|
|
15
|
+
const placeholder = "var(--ohmagif-placeholder, rgba(128, 128, 128, 0.16))";
|
|
16
|
+
const radius = "var(--ohmagif-radius, 6px)";
|
|
17
|
+
/** Narrower than this, the credit line starts at the handle. */
|
|
18
|
+
export const FULL_CREDIT_WIDTH = 280;
|
|
19
|
+
const reset = { boxSizing: "border-box", margin: 0, padding: 0, border: 0, font: "inherit", textAlign: "left" };
|
|
20
|
+
const cardStyle = {
|
|
21
|
+
...reset,
|
|
22
|
+
display: "block",
|
|
23
|
+
overflow: "hidden",
|
|
24
|
+
background: card,
|
|
25
|
+
borderWidth: CARD_BORDER,
|
|
26
|
+
borderStyle: "solid",
|
|
27
|
+
borderColor: border,
|
|
28
|
+
borderRadius: radius,
|
|
29
|
+
color: "inherit",
|
|
30
|
+
textDecoration: "none",
|
|
31
|
+
};
|
|
32
|
+
const frameBase = {
|
|
33
|
+
...reset,
|
|
34
|
+
display: "block",
|
|
35
|
+
width: "100%",
|
|
36
|
+
// The frame's shape alone decides its height, whatever a page's own
|
|
37
|
+
// rules say about buttons.
|
|
38
|
+
height: "auto",
|
|
39
|
+
// The frame's box: the credit strip under it in the file is cut off.
|
|
40
|
+
overflow: "hidden",
|
|
41
|
+
background: placeholder,
|
|
42
|
+
// A focus ring drawn inside is never cut off by a scrolling parent or
|
|
43
|
+
// covered by the next GIF.
|
|
44
|
+
outlineOffset: -2,
|
|
45
|
+
};
|
|
46
|
+
const creditStyle = {
|
|
47
|
+
...reset,
|
|
48
|
+
display: "block",
|
|
49
|
+
height: CREDIT_LINE_HEIGHT,
|
|
50
|
+
padding: "6px 10px",
|
|
51
|
+
fontSize: 14,
|
|
52
|
+
lineHeight: "16px",
|
|
53
|
+
color: muted,
|
|
54
|
+
whiteSpace: "nowrap",
|
|
55
|
+
overflow: "hidden",
|
|
56
|
+
textOverflow: "ellipsis",
|
|
57
|
+
textDecoration: "none",
|
|
58
|
+
cursor: "pointer",
|
|
59
|
+
outlineOffset: -2,
|
|
60
|
+
};
|
|
61
|
+
/** A grid tile's credit line: "by @handle", small. */
|
|
62
|
+
const shortCreditStyle = {
|
|
63
|
+
...creditStyle,
|
|
64
|
+
height: TILE_CREDIT_LINE_HEIGHT,
|
|
65
|
+
padding: "3px 6px",
|
|
66
|
+
fontSize: 10,
|
|
67
|
+
lineHeight: "12px",
|
|
68
|
+
};
|
|
69
|
+
const handleStyle = { color: link, fontWeight: 600 };
|
|
70
|
+
/** A control at the end of a grid: Retry, or Upload to ohmagif. */
|
|
71
|
+
export const buttonStyle = {
|
|
72
|
+
...reset,
|
|
73
|
+
display: "inline-flex",
|
|
74
|
+
alignItems: "center",
|
|
75
|
+
justifyContent: "center",
|
|
76
|
+
height: 36,
|
|
77
|
+
padding: "0 14px",
|
|
78
|
+
borderWidth: 1,
|
|
79
|
+
borderStyle: "solid",
|
|
80
|
+
borderColor: border,
|
|
81
|
+
borderRadius: 4,
|
|
82
|
+
background: card,
|
|
83
|
+
color: link,
|
|
84
|
+
fontSize: 14,
|
|
85
|
+
fontWeight: 600,
|
|
86
|
+
lineHeight: 1,
|
|
87
|
+
textDecoration: "none",
|
|
88
|
+
whiteSpace: "nowrap",
|
|
89
|
+
cursor: "pointer",
|
|
90
|
+
};
|
|
91
|
+
const external = { target: "_blank", rel: "noopener noreferrer", referrerPolicy: "no-referrer" };
|
|
92
|
+
/** A GIF cut to its frame, in the embed's card with the credit line under it. */
|
|
93
|
+
export function Card({ gif, width, picture = true, tinyOnly = false, link, onClick, hideAttribution = false, shortCredit = false, lazy, onLoad, onError, className, style, id, }) {
|
|
94
|
+
const frame = tinyOnly ? tinyFrameSize(gif) : frameSize(gif);
|
|
95
|
+
const frameWidth = hideAttribution ? width : width - 2 * CARD_BORDER;
|
|
96
|
+
const label = gifLabel(gif);
|
|
97
|
+
const pic = picture ? (_jsx(Picture, { gif: gif, width: frameWidth, tinyOnly: tinyOnly, alt: "", lazy: lazy, onLoad: onLoad, onError: onError })) : null;
|
|
98
|
+
const shape = { ...frameBase, aspectRatio: `${frame.width} / ${frame.height}` };
|
|
99
|
+
const pointer = link || onClick ? { cursor: "pointer" } : null;
|
|
100
|
+
const data = id ? { "data-ohmagif-id": id } : {};
|
|
101
|
+
// The frame alone: one element, named for those who cannot see it.
|
|
102
|
+
if (hideAttribution) {
|
|
103
|
+
const s = { ...shape, ...pointer, borderRadius: radius, ...style };
|
|
104
|
+
const cls = `${className} ohmagif-frame`;
|
|
105
|
+
if (link) {
|
|
106
|
+
return (_jsx("a", { ...data, ...external, className: cls, href: gif.url, "aria-label": label, style: s, onClick: onClick, children: pic }));
|
|
107
|
+
}
|
|
108
|
+
if (onClick) {
|
|
109
|
+
return (_jsx("button", { ...data, type: "button", className: cls, "aria-label": label, style: s, onClick: onClick, children: pic }));
|
|
110
|
+
}
|
|
111
|
+
return (_jsx("span", { ...data, role: "img", className: cls, "aria-label": label, style: s, children: pic }));
|
|
112
|
+
}
|
|
113
|
+
const handle = _jsxs("span", { style: handleStyle, children: ["@", gif.creator.handle] });
|
|
114
|
+
const words = shortCredit ? (_jsxs(_Fragment, { children: ["by ", handle] })) : width >= FULL_CREDIT_WIDTH ? (_jsxs(_Fragment, { children: ["GIF by ", handle, " on ohmagif.com"] })) : (_jsxs(_Fragment, { children: [handle, " on ohmagif.com"] }));
|
|
115
|
+
const credit = shortCredit ? shortCreditStyle : creditStyle;
|
|
116
|
+
const cls = `${className} ohmagif-card`;
|
|
117
|
+
const s = { ...cardStyle, ...style };
|
|
118
|
+
// The whole card is one link to the GIF's page.
|
|
119
|
+
if (link) {
|
|
120
|
+
return (_jsxs("a", { ...data, ...external, className: cls, href: gif.url, style: { ...s, ...pointer }, onClick: onClick, children: [_jsx("span", { role: "img", "aria-label": label, className: "ohmagif-frame", style: shape, children: pic }), _jsx("span", { className: "ohmagif-credit", style: credit, children: words })] }));
|
|
121
|
+
}
|
|
122
|
+
// The frame does what the page wants (pick it, say); the credit line
|
|
123
|
+
// links to the GIF's page.
|
|
124
|
+
return (_jsxs("span", { ...data, className: cls, style: s, children: [onClick ? (_jsx("button", { type: "button", "aria-label": label, className: "ohmagif-frame", style: { ...shape, ...pointer }, onClick: onClick, children: pic })) : (_jsx("span", { role: "img", "aria-label": label, className: "ohmagif-frame", style: shape, children: pic })), _jsx("a", { ...external, className: "ohmagif-credit", href: gif.url, style: credit, children: words })] }));
|
|
125
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { MouseEvent } from "react";
|
|
2
|
+
import type { Gif as GifData } from "../gif.js";
|
|
3
|
+
export interface GifProps {
|
|
4
|
+
gif: GifData;
|
|
5
|
+
/**
|
|
6
|
+
* Width in CSS pixels, card included. gifSize(frameSize(gif), width) is
|
|
7
|
+
* the room it takes. It never grows past its container.
|
|
8
|
+
*/
|
|
9
|
+
width: number;
|
|
10
|
+
/** The frame does not link to the GIF's page; the credit line still does. */
|
|
11
|
+
noLink?: boolean;
|
|
12
|
+
/** Called on a click on the frame. With noLink, the frame is a button. */
|
|
13
|
+
onGifClick?: (gif: GifData, e: MouseEvent<HTMLElement>) => void;
|
|
14
|
+
/** The frame alone, without the card and its credit line. */
|
|
15
|
+
hideAttribution?: boolean;
|
|
16
|
+
/** Load the file only once the GIF is near the screen. On by default. */
|
|
17
|
+
lazyLoad?: boolean;
|
|
18
|
+
className?: string;
|
|
19
|
+
onLoad?: () => void;
|
|
20
|
+
/** The file did not load: its address may be out of date, ask for the GIF again. */
|
|
21
|
+
onError?: () => void;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* One GIF at a given width: its frame, cut from the credit strip in the
|
|
25
|
+
* file, in a card with the credit line under it, as ohmagif's embed shows
|
|
26
|
+
* it.
|
|
27
|
+
*/
|
|
28
|
+
export declare function Gif({ gif, width, noLink, onGifClick, hideAttribution, lazyLoad, className, onLoad, onError }: GifProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
3
|
+
import { Card } from "./Card.js";
|
|
4
|
+
/**
|
|
5
|
+
* One GIF at a given width: its frame, cut from the credit strip in the
|
|
6
|
+
* file, in a card with the credit line under it, as ohmagif's embed shows
|
|
7
|
+
* it.
|
|
8
|
+
*/
|
|
9
|
+
export function Gif({ gif, width, noLink = false, onGifClick, hideAttribution = false, lazyLoad = true, className, onLoad, onError }) {
|
|
10
|
+
const click = onGifClick && ((e) => onGifClick(gif, e));
|
|
11
|
+
return (_jsx(Card, { gif: gif, width: width, link: !noLink, onClick: click, hideAttribution: hideAttribution, lazy: lazyLoad, onLoad: onLoad, onError: onError, className: className ? `ohmagif-gif ${className}` : "ohmagif-gif", style: { width, maxWidth: "100%" } }));
|
|
12
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type ElementType, type MouseEvent, type ReactNode } from "react";
|
|
2
|
+
import { type Gif as GifData } from "../gif.js";
|
|
3
|
+
/**
|
|
4
|
+
* One page of GIFs, as OhmagifFetch's search, trending and tag answer it.
|
|
5
|
+
* `next` is the offset of the following page (null after the last one);
|
|
6
|
+
* without it the next page starts at `offset + count`.
|
|
7
|
+
*/
|
|
8
|
+
export interface GridPage {
|
|
9
|
+
data: readonly GifData[];
|
|
10
|
+
pagination: {
|
|
11
|
+
offset: number;
|
|
12
|
+
count: number;
|
|
13
|
+
total_count: number;
|
|
14
|
+
next?: number | null;
|
|
15
|
+
};
|
|
16
|
+
}
|
|
17
|
+
export interface GridProps {
|
|
18
|
+
/**
|
|
19
|
+
* Fetches the page at an offset. The grid aborts the signal when it
|
|
20
|
+
* unmounts. Give the grid a new `key` to start again from the first
|
|
21
|
+
* page (a new search, say).
|
|
22
|
+
*/
|
|
23
|
+
fetchGifs: (offset: number, signal: AbortSignal) => Promise<GridPage>;
|
|
24
|
+
/** Width in CSS pixels. */
|
|
25
|
+
width: number;
|
|
26
|
+
columns: number;
|
|
27
|
+
/** Space between GIFs in CSS pixels, 6 by default. */
|
|
28
|
+
gutter?: number;
|
|
29
|
+
/** Tiles are buttons rather than links to the GIF's page. */
|
|
30
|
+
noLink?: boolean;
|
|
31
|
+
/** Tiles are the frame alone, without the card and its credit line. */
|
|
32
|
+
hideAttribution?: boolean;
|
|
33
|
+
onGifClick?: (gif: GifData, e: MouseEvent<HTMLElement>) => void;
|
|
34
|
+
/** Every GIF the grid holds, after each page. */
|
|
35
|
+
onGifsFetched?: (gifs: GifData[]) => void;
|
|
36
|
+
onGifsFetchError?: (error: unknown) => void;
|
|
37
|
+
/** Shown when the first page is empty. */
|
|
38
|
+
noResultsMessage?: ReactNode;
|
|
39
|
+
/**
|
|
40
|
+
* The upload page, offered when nothing matched and after the last page;
|
|
41
|
+
* OhmagifFetch's uploadURL. null offers nothing.
|
|
42
|
+
*/
|
|
43
|
+
uploadURL?: string | null;
|
|
44
|
+
/** Shown above the Retry button when a page did not load. */
|
|
45
|
+
errorMessage?: ReactNode;
|
|
46
|
+
/** Shown while a page loads. */
|
|
47
|
+
loader?: ElementType;
|
|
48
|
+
className?: string;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* GIFs in columns, loading the next page as the end comes near. Only the
|
|
52
|
+
* tiles within a screen of the visible area hold a picture, so a long
|
|
53
|
+
* scroll costs no more to show than the first page.
|
|
54
|
+
*/
|
|
55
|
+
export declare function Grid({ fetchGifs, width, columns, gutter, noLink, hideAttribution, onGifClick, onGifsFetched, onGifsFetchError, noResultsMessage, uploadURL, errorMessage, loader: Loader, className, }: GridProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use client";
|
|
2
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
3
|
+
import { memo, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState, } from "react";
|
|
4
|
+
import { DEFAULT_BASE_URL, httpURL } from "../gif.js";
|
|
5
|
+
import { buttonStyle, Card } from "./Card.js";
|
|
6
|
+
import { masonry } from "./layout.js";
|
|
7
|
+
// useLayoutEffect has nothing to do on a server and React 18 warns there.
|
|
8
|
+
const useIsoLayoutEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
|
|
9
|
+
/** The element the grid scrolls in, or null for the page itself. */
|
|
10
|
+
function scrollParent(el) {
|
|
11
|
+
for (let p = el.parentElement; p && p !== document.body && p !== document.documentElement; p = p.parentElement) {
|
|
12
|
+
const { overflowY } = getComputedStyle(p);
|
|
13
|
+
if (overflowY === "auto" || overflowY === "scroll" || overflowY === "overlay")
|
|
14
|
+
return p;
|
|
15
|
+
}
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
function DefaultLoader() {
|
|
19
|
+
return (_jsx("div", { className: "ohmagif-loading", role: "status", children: "Loading" }));
|
|
20
|
+
}
|
|
21
|
+
const endStyle = { display: "flex", flexDirection: "column", alignItems: "center", gap: 8, padding: "12px 0" };
|
|
22
|
+
const Tile = memo(function Tile({ gif, x, y, w, h, near, link, hideAttribution, onClick }) {
|
|
23
|
+
// The picture is only there while the tile is near the screen; the tile
|
|
24
|
+
// keeps its size and its name without it.
|
|
25
|
+
return (_jsx(Card, { gif: gif, id: gif.id, width: w, picture: near, tinyOnly: true, link: link, onClick: (e) => onClick(gif, e), hideAttribution: hideAttribution, shortCredit: true, className: "ohmagif-tile", style: { position: "absolute", left: x, top: y, width: w, height: h } }));
|
|
26
|
+
});
|
|
27
|
+
const nobody = new Set();
|
|
28
|
+
/**
|
|
29
|
+
* GIFs in columns, loading the next page as the end comes near. Only the
|
|
30
|
+
* tiles within a screen of the visible area hold a picture, so a long
|
|
31
|
+
* scroll costs no more to show than the first page.
|
|
32
|
+
*/
|
|
33
|
+
export function Grid({ fetchGifs, width, columns, gutter = 6, noLink = false, hideAttribution = false, onGifClick, onGifsFetched, onGifsFetchError, noResultsMessage = "No GIFs found", uploadURL = `${DEFAULT_BASE_URL}/upload`, errorMessage = "GIFs did not load", loader: Loader = DefaultLoader, className, }) {
|
|
34
|
+
const [gifs, setGifs] = useState([]);
|
|
35
|
+
const [status, setStatus] = useState("idle");
|
|
36
|
+
const [near, setNear] = useState(nobody);
|
|
37
|
+
// Without IntersectionObserver every tile holds its picture.
|
|
38
|
+
const [everyPicture, setEveryPicture] = useState(false);
|
|
39
|
+
const boxRef = useRef(null);
|
|
40
|
+
const endRef = useRef(null);
|
|
41
|
+
const observer = useRef(null);
|
|
42
|
+
const callbacks = useRef({ fetchGifs, onGifClick, onGifsFetched, onGifsFetchError });
|
|
43
|
+
useIsoLayoutEffect(() => {
|
|
44
|
+
callbacks.current = { fetchGifs, onGifClick, onGifsFetched, onGifsFetchError };
|
|
45
|
+
});
|
|
46
|
+
// What loading decides on, read and written outside of rendering.
|
|
47
|
+
const [s] = useState(() => ({
|
|
48
|
+
status: "idle",
|
|
49
|
+
next: 0,
|
|
50
|
+
all: [],
|
|
51
|
+
seen: new Set(),
|
|
52
|
+
endNear: false,
|
|
53
|
+
ctrl: null,
|
|
54
|
+
}));
|
|
55
|
+
const load = useCallback(() => {
|
|
56
|
+
const ctrl = s.ctrl;
|
|
57
|
+
if (s.status !== "idle" || !s.endNear || !ctrl)
|
|
58
|
+
return;
|
|
59
|
+
const offset = s.next;
|
|
60
|
+
s.status = "loading";
|
|
61
|
+
setStatus("loading");
|
|
62
|
+
const settle = (status) => {
|
|
63
|
+
s.status = status;
|
|
64
|
+
setStatus(status);
|
|
65
|
+
};
|
|
66
|
+
new Promise((resolve) => resolve(callbacks.current.fetchGifs(offset, ctrl.signal))).then((page) => {
|
|
67
|
+
if (ctrl.signal.aborted)
|
|
68
|
+
return;
|
|
69
|
+
const data = Array.isArray(page?.data) ? page.data : [];
|
|
70
|
+
const fresh = data.filter((g) => !s.seen.has(g.id));
|
|
71
|
+
for (const g of fresh)
|
|
72
|
+
s.seen.add(g.id);
|
|
73
|
+
if (fresh.length)
|
|
74
|
+
s.all = [...s.all, ...fresh];
|
|
75
|
+
const p = page?.pagination;
|
|
76
|
+
const next = p?.next !== undefined ? p.next : data.length && p ? p.offset + (p.count || data.length) : null;
|
|
77
|
+
const done = next === null || next <= offset || (p !== undefined && next >= p.total_count);
|
|
78
|
+
if (next !== null)
|
|
79
|
+
s.next = next;
|
|
80
|
+
setGifs(s.all);
|
|
81
|
+
settle(done ? "done" : "idle");
|
|
82
|
+
callbacks.current.onGifsFetched?.(s.all);
|
|
83
|
+
}, (err) => {
|
|
84
|
+
if (ctrl.signal.aborted)
|
|
85
|
+
return;
|
|
86
|
+
settle("error");
|
|
87
|
+
callbacks.current.onGifsFetchError?.(err);
|
|
88
|
+
});
|
|
89
|
+
}, [s]);
|
|
90
|
+
useIsoLayoutEffect(() => {
|
|
91
|
+
const ctrl = new AbortController();
|
|
92
|
+
s.ctrl = ctrl;
|
|
93
|
+
const box = boxRef.current;
|
|
94
|
+
const end = endRef.current;
|
|
95
|
+
if (typeof IntersectionObserver === "undefined") {
|
|
96
|
+
// Every picture at once, and the first page only.
|
|
97
|
+
setEveryPicture(true);
|
|
98
|
+
s.endNear = true;
|
|
99
|
+
load();
|
|
100
|
+
return () => {
|
|
101
|
+
ctrl.abort();
|
|
102
|
+
s.ctrl = null;
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
const io = new IntersectionObserver((entries) => {
|
|
106
|
+
const add = [];
|
|
107
|
+
const drop = [];
|
|
108
|
+
for (const e of entries) {
|
|
109
|
+
if (e.target === end) {
|
|
110
|
+
s.endNear = e.isIntersecting;
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
// Every other target is a tile, which carries its GIF's id.
|
|
114
|
+
(e.isIntersecting ? add : drop).push(e.target.dataset.ohmagifId);
|
|
115
|
+
}
|
|
116
|
+
if (add.length || drop.length) {
|
|
117
|
+
setNear((prev) => {
|
|
118
|
+
const next = new Set(prev);
|
|
119
|
+
for (const id of add)
|
|
120
|
+
next.add(id);
|
|
121
|
+
for (const id of drop)
|
|
122
|
+
next.delete(id);
|
|
123
|
+
return next;
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
load();
|
|
127
|
+
},
|
|
128
|
+
// A screen's height ahead: pictures and the next page are there
|
|
129
|
+
// before they are scrolled to.
|
|
130
|
+
{ root: scrollParent(box), rootMargin: "100% 0px" });
|
|
131
|
+
observer.current = io;
|
|
132
|
+
io.observe(end);
|
|
133
|
+
// Tiles are already there when a hidden grid shows again (in an
|
|
134
|
+
// <Activity>, say): its effects start over, its elements stay.
|
|
135
|
+
for (const el of Array.from(box.children))
|
|
136
|
+
io.observe(el);
|
|
137
|
+
return () => {
|
|
138
|
+
io.disconnect();
|
|
139
|
+
observer.current = null;
|
|
140
|
+
ctrl.abort();
|
|
141
|
+
s.ctrl = null;
|
|
142
|
+
// The aborted request never settles; a remount starts it again.
|
|
143
|
+
if (s.status === "loading")
|
|
144
|
+
s.status = "idle";
|
|
145
|
+
};
|
|
146
|
+
}, [s, load]);
|
|
147
|
+
// New tiles are watched as they appear (observing one twice does nothing).
|
|
148
|
+
useIsoLayoutEffect(() => {
|
|
149
|
+
const io = observer.current;
|
|
150
|
+
if (io && boxRef.current)
|
|
151
|
+
for (const el of Array.from(boxRef.current.children))
|
|
152
|
+
io.observe(el);
|
|
153
|
+
}, [gifs]);
|
|
154
|
+
// After a page, or a retry, look at the end again: watching it anew makes
|
|
155
|
+
// the observer report where it is now, so a page that did not fill the
|
|
156
|
+
// screen is followed by the next one.
|
|
157
|
+
useIsoLayoutEffect(() => {
|
|
158
|
+
const io = observer.current;
|
|
159
|
+
if (status !== "idle" || !io || !endRef.current)
|
|
160
|
+
return;
|
|
161
|
+
io.unobserve(endRef.current);
|
|
162
|
+
io.observe(endRef.current);
|
|
163
|
+
}, [status]);
|
|
164
|
+
// Only the error state shows the button.
|
|
165
|
+
const retry = useCallback(() => {
|
|
166
|
+
s.status = "idle";
|
|
167
|
+
setStatus("idle");
|
|
168
|
+
if (!observer.current)
|
|
169
|
+
load();
|
|
170
|
+
}, [s, load]);
|
|
171
|
+
const click = useCallback((gif, e) => callbacks.current.onGifClick?.(gif, e), []);
|
|
172
|
+
const layout = useMemo(() => masonry(gifs, width, columns, gutter, { hideAttribution }), [gifs, width, columns, gutter, hideAttribution]);
|
|
173
|
+
const upload = httpURL(uploadURL);
|
|
174
|
+
return (_jsxs("div", { className: className ? `ohmagif-grid ${className}` : "ohmagif-grid", style: { width, maxWidth: "100%" }, children: [_jsx("div", { ref: boxRef, style: { position: "relative", width, height: layout.height }, children: gifs.map((gif, i) => {
|
|
175
|
+
const b = layout.boxes[i];
|
|
176
|
+
return (_jsx(Tile, { gif: gif, x: b.x, y: b.y, w: b.width, h: b.height, near: everyPicture || near.has(gif.id), link: !noLink, hideAttribution: hideAttribution, onClick: click }, gif.id));
|
|
177
|
+
}) }), _jsx("div", { ref: endRef, "aria-hidden": "true", style: { height: 1 } }), status === "loading" && _jsx(Loader, {}), status === "done" && (gifs.length === 0 || upload) && (_jsxs("div", { className: "ohmagif-end", style: endStyle, children: [gifs.length === 0 && noResultsMessage, upload && (_jsx("a", { className: "ohmagif-upload", href: upload, target: "_blank", rel: "noopener noreferrer", referrerPolicy: "no-referrer", style: buttonStyle, children: "Upload to ohmagif" }))] })), status === "error" && (_jsxs("div", { className: "ohmagif-error", role: "alert", style: endStyle, children: [errorMessage, _jsx("button", { type: "button", style: buttonStyle, onClick: retry, children: "Retry" })] }))] }));
|
|
178
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { Gif } from "../gif.js";
|
|
2
|
+
export interface PictureProps {
|
|
3
|
+
gif: Gif;
|
|
4
|
+
/** The width the frame is shown at, in CSS pixels. */
|
|
5
|
+
width: number;
|
|
6
|
+
/** tiny.avif whatever the width and density, as a grid shows it. */
|
|
7
|
+
tinyOnly?: boolean;
|
|
8
|
+
alt: string;
|
|
9
|
+
lazy?: boolean;
|
|
10
|
+
onLoad?: () => void;
|
|
11
|
+
onError?: () => void;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The animated AVIF, with the poster for a browser that cannot show AVIF.
|
|
15
|
+
* The browser picks tiny or full by the screen's density unless told
|
|
16
|
+
* tinyOnly. The width and height attributes are the size the file is shown
|
|
17
|
+
* at, so the page knows it before a byte arrives.
|
|
18
|
+
*/
|
|
19
|
+
export declare function Picture({ gif, width, tinyOnly, alt, lazy, onLoad, onError }: PictureProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
const pictureStyle = { display: "contents" };
|
|
3
|
+
// As wide as the frame and as high as the file: the frame's box is cut to
|
|
4
|
+
// the frame, so the credit strip at the bottom of the file falls outside
|
|
5
|
+
// it. Whichever file the browser picks, its own shape decides where the
|
|
6
|
+
// strip starts.
|
|
7
|
+
const imgStyle = { display: "block", width: "100%", height: "auto" };
|
|
8
|
+
/**
|
|
9
|
+
* The animated AVIF, with the poster for a browser that cannot show AVIF.
|
|
10
|
+
* The browser picks tiny or full by the screen's density unless told
|
|
11
|
+
* tinyOnly. The width and height attributes are the size the file is shown
|
|
12
|
+
* at, so the page knows it before a byte arrives.
|
|
13
|
+
*/
|
|
14
|
+
export function Picture({ gif, width, tinyOnly = false, alt, lazy = false, onLoad, onError }) {
|
|
15
|
+
const { full, tiny, poster } = gif.files;
|
|
16
|
+
const both = !tinyOnly && full.width > tiny.width;
|
|
17
|
+
const srcSet = both ? `${tiny.url} ${tiny.width}w, ${full.url} ${full.width}w` : tinyOnly ? tiny.url : full.url;
|
|
18
|
+
const file = tinyOnly ? tiny : full;
|
|
19
|
+
const w = Math.max(1, Math.round(width));
|
|
20
|
+
const h = Math.max(1, Math.round((width * file.height) / file.width));
|
|
21
|
+
return (_jsxs("picture", { style: pictureStyle, children: [_jsx("source", { type: "image/avif", srcSet: srcSet, sizes: both ? `${Math.ceil(width)}px` : undefined }), _jsx("img", { src: poster.url, alt: alt, width: w, height: h, decoding: "async", loading: lazy ? "lazy" : undefined, referrerPolicy: "no-referrer", style: imgStyle, onLoad: onLoad, onError: onError })] }));
|
|
22
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type Gif } from "../gif.js";
|
|
2
|
+
/** The card's border, all round, in CSS pixels. */
|
|
3
|
+
export declare const CARD_BORDER = 1;
|
|
4
|
+
/** The credit line under the frame, in CSS pixels. */
|
|
5
|
+
export declare const CREDIT_LINE_HEIGHT = 28;
|
|
6
|
+
/** The shorter credit line under a grid's tiles, in CSS pixels. */
|
|
7
|
+
export declare const TILE_CREDIT_LINE_HEIGHT = 18;
|
|
8
|
+
export interface Size {
|
|
9
|
+
width: number;
|
|
10
|
+
height: number;
|
|
11
|
+
}
|
|
12
|
+
export interface SizeOptions {
|
|
13
|
+
/** The frame alone, without the card and its credit line. */
|
|
14
|
+
hideAttribution?: boolean;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The room a GIF takes when shown `width` pixels wide: its frame (the file
|
|
18
|
+
* without its credit strip) in a card with the credit line under it, as
|
|
19
|
+
* Grid and Gif draw it. Reserve this while the GIF is fetched and nothing
|
|
20
|
+
* moves when it arrives. `frame` is frameSize(gif), or the size kept with
|
|
21
|
+
* a message.
|
|
22
|
+
*/
|
|
23
|
+
export declare function gifSize(frame: Size, width: number, options?: SizeOptions): Size;
|
|
24
|
+
export interface Box extends Size {
|
|
25
|
+
x: number;
|
|
26
|
+
y: number;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Places GIFs in columns, each into the shortest column so far, in the
|
|
30
|
+
* order given. A GIF already placed keeps its place when more are added,
|
|
31
|
+
* so a new page never moves what is on screen.
|
|
32
|
+
*/
|
|
33
|
+
export declare function masonry(gifs: readonly Gif[], width: number, columns: number, gutter: number, options?: SizeOptions): {
|
|
34
|
+
boxes: Box[];
|
|
35
|
+
height: number;
|
|
36
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { tinyFrameSize } from "../gif.js";
|
|
2
|
+
/** The card's border, all round, in CSS pixels. */
|
|
3
|
+
export const CARD_BORDER = 1;
|
|
4
|
+
/** The credit line under the frame, in CSS pixels. */
|
|
5
|
+
export const CREDIT_LINE_HEIGHT = 28;
|
|
6
|
+
/** The shorter credit line under a grid's tiles, in CSS pixels. */
|
|
7
|
+
export const TILE_CREDIT_LINE_HEIGHT = 18;
|
|
8
|
+
/**
|
|
9
|
+
* The room a GIF takes when shown `width` pixels wide: its frame (the file
|
|
10
|
+
* without its credit strip) in a card with the credit line under it, as
|
|
11
|
+
* Grid and Gif draw it. Reserve this while the GIF is fetched and nothing
|
|
12
|
+
* moves when it arrives. `frame` is frameSize(gif), or the size kept with
|
|
13
|
+
* a message.
|
|
14
|
+
*/
|
|
15
|
+
export function gifSize(frame, width, options = {}) {
|
|
16
|
+
return cardSize(frame, width, CREDIT_LINE_HEIGHT, options);
|
|
17
|
+
}
|
|
18
|
+
function cardSize(frame, width, creditLine, options) {
|
|
19
|
+
const card = options.hideAttribution ? 0 : 2 * CARD_BORDER;
|
|
20
|
+
const line = options.hideAttribution ? 0 : creditLine;
|
|
21
|
+
const inner = Math.max(0, width - card);
|
|
22
|
+
// Not rounded: a frame a fraction of a pixel too high shows the top of
|
|
23
|
+
// the credit strip.
|
|
24
|
+
return { width, height: (inner * frame.height) / frame.width + card + line };
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Places GIFs in columns, each into the shortest column so far, in the
|
|
28
|
+
* order given. A GIF already placed keeps its place when more are added,
|
|
29
|
+
* so a new page never moves what is on screen.
|
|
30
|
+
*/
|
|
31
|
+
export function masonry(gifs, width, columns, gutter, options = {}) {
|
|
32
|
+
const cols = Math.max(1, Math.floor(columns) || 1);
|
|
33
|
+
const gap = Math.max(0, gutter);
|
|
34
|
+
const colWidth = Math.max(0, (width - gap * (cols - 1)) / cols);
|
|
35
|
+
const heights = new Array(cols).fill(0);
|
|
36
|
+
const boxes = gifs.map((gif) => {
|
|
37
|
+
let c = 0;
|
|
38
|
+
for (let i = 1; i < cols; i++)
|
|
39
|
+
if (heights[i] < heights[c])
|
|
40
|
+
c = i;
|
|
41
|
+
// Grids show tiny.avif, so its frame is the tile's shape.
|
|
42
|
+
const { height } = cardSize(tinyFrameSize(gif), colWidth, TILE_CREDIT_LINE_HEIGHT, options);
|
|
43
|
+
const box = { x: c * (colWidth + gap), y: heights[c], width: colWidth, height };
|
|
44
|
+
heights[c] = box.y + box.height + gap;
|
|
45
|
+
return box;
|
|
46
|
+
});
|
|
47
|
+
return { boxes, height: boxes.length ? Math.max(...heights) - gap : 0 };
|
|
48
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,71 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ohmagif/js",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Client for the ohmagif.com API and a React GIF grid, without dependencies",
|
|
5
|
+
"license": "GPL-3.0-only",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/ohmagif/ohmagif-js.git"
|
|
9
|
+
},
|
|
10
|
+
"homepage": "https://github.com/ohmagif/ohmagif-js#readme",
|
|
11
|
+
"keywords": [
|
|
12
|
+
"gif",
|
|
13
|
+
"ohmagif",
|
|
14
|
+
"react",
|
|
15
|
+
"gif-picker",
|
|
16
|
+
"avif"
|
|
17
|
+
],
|
|
18
|
+
"type": "module",
|
|
19
|
+
"sideEffects": false,
|
|
20
|
+
"exports": {
|
|
21
|
+
".": {
|
|
22
|
+
"types": "./dist/index.d.ts",
|
|
23
|
+
"default": "./dist/index.js"
|
|
24
|
+
},
|
|
25
|
+
"./react": {
|
|
26
|
+
"types": "./dist/react/index.d.ts",
|
|
27
|
+
"default": "./dist/react/index.js"
|
|
28
|
+
},
|
|
29
|
+
"./package.json": "./package.json"
|
|
30
|
+
},
|
|
31
|
+
"files": [
|
|
32
|
+
"dist"
|
|
33
|
+
],
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"scripts": {
|
|
38
|
+
"build": "rm -rf dist && tsc -p tsconfig.build.json",
|
|
39
|
+
"typecheck": "tsc --noEmit",
|
|
40
|
+
"test": "vitest run",
|
|
41
|
+
"test:coverage": "vitest run --coverage",
|
|
42
|
+
"test:integration": "vitest run --config vitest.integration.config.ts",
|
|
43
|
+
"example": "vite examples --port 8082 --strictPort",
|
|
44
|
+
"e2e": "playwright test",
|
|
45
|
+
"prepare": "npm run build",
|
|
46
|
+
"prepublishOnly": "npm run typecheck && npm test"
|
|
47
|
+
},
|
|
48
|
+
"peerDependencies": {
|
|
49
|
+
"react": "^18.2.0 || ^19.0.0"
|
|
50
|
+
},
|
|
51
|
+
"peerDependenciesMeta": {
|
|
52
|
+
"react": {
|
|
53
|
+
"optional": true
|
|
54
|
+
}
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@playwright/test": "1.65.1",
|
|
58
|
+
"@testing-library/dom": "10.4.2",
|
|
59
|
+
"@testing-library/react": "16.3.3",
|
|
60
|
+
"@types/node": "26.6.11",
|
|
61
|
+
"@types/react": "19.3.3",
|
|
62
|
+
"@types/react-dom": "19.3.0",
|
|
63
|
+
"@vitest/coverage-v8": "5.1.2",
|
|
64
|
+
"jsdom": "31.0.1",
|
|
65
|
+
"react": "19.3.1",
|
|
66
|
+
"react-dom": "19.3.1",
|
|
67
|
+
"typescript": "7.0.2",
|
|
68
|
+
"vite": "8.4.0",
|
|
69
|
+
"vitest": "5.1.2"
|
|
70
|
+
}
|
|
71
|
+
}
|