@ohmagif/js 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +348 -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 +30 -0
- package/dist/react/Card.js +116 -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 +34 -0
- package/dist/react/layout.js +43 -0
- package/package.json +69 -4
package/dist/gif.d.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/** The site the API and its pages are on. */
|
|
2
|
+
export declare const DEFAULT_BASE_URL = "https://ohmagif.com";
|
|
3
|
+
/** How suitable a GIF is: g, pg or pg13. ohmagif hosts nothing above pg13. */
|
|
4
|
+
export type Rating = "g" | "pg" | "pg13";
|
|
5
|
+
export declare const RATINGS: readonly Rating[];
|
|
6
|
+
/** One file of a GIF. Every file carries its creator's credit. */
|
|
7
|
+
export interface GifFile {
|
|
8
|
+
url: string;
|
|
9
|
+
type: string;
|
|
10
|
+
width: number;
|
|
11
|
+
height: number;
|
|
12
|
+
bytes: number;
|
|
13
|
+
}
|
|
14
|
+
export interface Creator {
|
|
15
|
+
handle: string;
|
|
16
|
+
name: string;
|
|
17
|
+
url: string;
|
|
18
|
+
}
|
|
19
|
+
/** A GIF as the ohmagif API describes it. */
|
|
20
|
+
export interface Gif {
|
|
21
|
+
/** 10 characters from [0-9A-Za-z]. */
|
|
22
|
+
id: string;
|
|
23
|
+
/** A short caption. */
|
|
24
|
+
title: string;
|
|
25
|
+
/** What happens in the GIF, for alt text. */
|
|
26
|
+
description: string;
|
|
27
|
+
/** The GIF's page on ohmagif.com. */
|
|
28
|
+
url: string;
|
|
29
|
+
embed_url: string;
|
|
30
|
+
embed_width: number;
|
|
31
|
+
embed_height: number;
|
|
32
|
+
/** RFC 3339, UTC. */
|
|
33
|
+
created: string;
|
|
34
|
+
/** Size of `files.full`, credit strip included. */
|
|
35
|
+
width: number;
|
|
36
|
+
height: number;
|
|
37
|
+
/** Seconds. */
|
|
38
|
+
duration: number;
|
|
39
|
+
rating: Rating;
|
|
40
|
+
tags: string[];
|
|
41
|
+
reaction: string | null;
|
|
42
|
+
creator: Creator;
|
|
43
|
+
files: {
|
|
44
|
+
/** Animated AVIF at the GIF's own size. */
|
|
45
|
+
full: GifFile;
|
|
46
|
+
/** Animated AVIF, 320 px wide. */
|
|
47
|
+
tiny: GifFile;
|
|
48
|
+
/** The first frame as JPEG, for clients without AVIF. */
|
|
49
|
+
poster: GifFile;
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/** Whether a value is an ohmagif GIF id. */
|
|
53
|
+
export declare function isGifId(value: unknown): value is string;
|
|
54
|
+
type Obj = Record<string, unknown>;
|
|
55
|
+
export declare function asObject(value: unknown): Obj | null;
|
|
56
|
+
/**
|
|
57
|
+
* An absolute http or https address, or null. Anything else (javascript:,
|
|
58
|
+
* data:, a relative path) never reaches an href or a src.
|
|
59
|
+
*/
|
|
60
|
+
export declare function httpURL(value: unknown): string | null;
|
|
61
|
+
/**
|
|
62
|
+
* Checks one GIF of an answer and copies the fields this package knows,
|
|
63
|
+
* nothing else. Answers null for anything that is not a complete GIF, so a
|
|
64
|
+
* bad entry drops out of a list rather than breaking the page.
|
|
65
|
+
*/
|
|
66
|
+
export declare function parseGif(value: unknown): Gif | null;
|
|
67
|
+
/** The text that names a GIF to someone who cannot see it. */
|
|
68
|
+
export declare function gifLabel(gif: Gif): string;
|
|
69
|
+
/**
|
|
70
|
+
* Height in pixels of the credit strip under the frame of every file, full,
|
|
71
|
+
* tiny and poster alike. A saved or shared file keeps it; a page showing
|
|
72
|
+
* the GIF cuts it off and lays the credit over the frame instead, as
|
|
73
|
+
* ohmagif.com does.
|
|
74
|
+
*/
|
|
75
|
+
export declare const CREDIT_STRIP_HEIGHT = 24;
|
|
76
|
+
/** A file's height without its credit strip. */
|
|
77
|
+
export declare function frameHeight(file: GifFile): number;
|
|
78
|
+
/**
|
|
79
|
+
* The size of the GIF's frame at full size, without the credit strip: the
|
|
80
|
+
* shape the components show it in. Keep this with a message to reserve
|
|
81
|
+
* the GIF's room before it loads.
|
|
82
|
+
*
|
|
83
|
+
* tiny.avif and full.avif round their frames apart by a fraction of a
|
|
84
|
+
* pixel, and a browser shows either. This is the shorter of the two shapes
|
|
85
|
+
* in whole pixels, so whichever it shows, the strip stays out of sight.
|
|
86
|
+
*/
|
|
87
|
+
export declare function frameSize(gif: Gif): {
|
|
88
|
+
width: number;
|
|
89
|
+
height: number;
|
|
90
|
+
};
|
|
91
|
+
/** The frame of tiny.avif, the shape of a GIF in a grid. */
|
|
92
|
+
export declare function tinyFrameSize(gif: Gif): {
|
|
93
|
+
width: number;
|
|
94
|
+
height: number;
|
|
95
|
+
};
|
|
96
|
+
export {};
|
package/dist/gif.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
// The GIF object of the ohmagif API, and the checks every answer passes
|
|
2
|
+
// before any of it reaches a page.
|
|
3
|
+
/** The site the API and its pages are on. */
|
|
4
|
+
export const DEFAULT_BASE_URL = "https://ohmagif.com";
|
|
5
|
+
export const RATINGS = ["g", "pg", "pg13"];
|
|
6
|
+
const GIF_ID = /^[0-9A-Za-z]{10}$/;
|
|
7
|
+
/** Whether a value is an ohmagif GIF id. */
|
|
8
|
+
export function isGifId(value) {
|
|
9
|
+
return typeof value === "string" && GIF_ID.test(value);
|
|
10
|
+
}
|
|
11
|
+
export function asObject(value) {
|
|
12
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
13
|
+
}
|
|
14
|
+
function str(value) {
|
|
15
|
+
return typeof value === "string" ? value : null;
|
|
16
|
+
}
|
|
17
|
+
/** A non-negative finite number. */
|
|
18
|
+
function num(value) {
|
|
19
|
+
return typeof value === "number" && Number.isFinite(value) && value >= 0 ? value : null;
|
|
20
|
+
}
|
|
21
|
+
/** A positive integer, as every pixel size is. */
|
|
22
|
+
function size(value) {
|
|
23
|
+
return Number.isInteger(value) && value > 0 ? value : null;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* An absolute http or https address, or null. Anything else (javascript:,
|
|
27
|
+
* data:, a relative path) never reaches an href or a src.
|
|
28
|
+
*/
|
|
29
|
+
export function httpURL(value) {
|
|
30
|
+
if (typeof value !== "string")
|
|
31
|
+
return null;
|
|
32
|
+
let url;
|
|
33
|
+
try {
|
|
34
|
+
url = new URL(value);
|
|
35
|
+
}
|
|
36
|
+
catch {
|
|
37
|
+
return null;
|
|
38
|
+
}
|
|
39
|
+
return url.protocol === "https:" || url.protocol === "http:" ? url.href : null;
|
|
40
|
+
}
|
|
41
|
+
function file(value) {
|
|
42
|
+
const o = asObject(value);
|
|
43
|
+
if (!o)
|
|
44
|
+
return null;
|
|
45
|
+
const url = httpURL(o.url);
|
|
46
|
+
const type = str(o.type);
|
|
47
|
+
const width = size(o.width);
|
|
48
|
+
const height = size(o.height);
|
|
49
|
+
if (!url || type === null || !width || !height)
|
|
50
|
+
return null;
|
|
51
|
+
return { url, type, width, height, bytes: num(o.bytes) ?? 0 };
|
|
52
|
+
}
|
|
53
|
+
function creator(value) {
|
|
54
|
+
const o = asObject(value);
|
|
55
|
+
if (!o)
|
|
56
|
+
return null;
|
|
57
|
+
const handle = str(o.handle);
|
|
58
|
+
const url = httpURL(o.url);
|
|
59
|
+
if (!handle || !url)
|
|
60
|
+
return null;
|
|
61
|
+
return { handle, name: str(o.name) ?? handle, url };
|
|
62
|
+
}
|
|
63
|
+
function isRating(value) {
|
|
64
|
+
return RATINGS.includes(value);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Checks one GIF of an answer and copies the fields this package knows,
|
|
68
|
+
* nothing else. Answers null for anything that is not a complete GIF, so a
|
|
69
|
+
* bad entry drops out of a list rather than breaking the page.
|
|
70
|
+
*/
|
|
71
|
+
export function parseGif(value) {
|
|
72
|
+
const o = asObject(value);
|
|
73
|
+
if (!o || !isGifId(o.id))
|
|
74
|
+
return null;
|
|
75
|
+
const url = httpURL(o.url);
|
|
76
|
+
const embedURL = httpURL(o.embed_url);
|
|
77
|
+
const width = size(o.width);
|
|
78
|
+
const height = size(o.height);
|
|
79
|
+
const by = creator(o.creator);
|
|
80
|
+
const files = asObject(o.files);
|
|
81
|
+
const full = file(files?.full);
|
|
82
|
+
const tiny = file(files?.tiny);
|
|
83
|
+
const poster = file(files?.poster);
|
|
84
|
+
if (!url || !embedURL || !width || !height || !by || !full || !tiny || !poster || !isRating(o.rating)) {
|
|
85
|
+
return null;
|
|
86
|
+
}
|
|
87
|
+
return {
|
|
88
|
+
id: o.id,
|
|
89
|
+
title: str(o.title) ?? "",
|
|
90
|
+
description: str(o.description) ?? "",
|
|
91
|
+
url,
|
|
92
|
+
embed_url: embedURL,
|
|
93
|
+
embed_width: size(o.embed_width) ?? width,
|
|
94
|
+
embed_height: size(o.embed_height) ?? height,
|
|
95
|
+
created: str(o.created) ?? "",
|
|
96
|
+
width,
|
|
97
|
+
height,
|
|
98
|
+
duration: num(o.duration) ?? 0,
|
|
99
|
+
rating: o.rating,
|
|
100
|
+
tags: Array.isArray(o.tags) ? o.tags.filter((t) => typeof t === "string") : [],
|
|
101
|
+
reaction: str(o.reaction),
|
|
102
|
+
creator: by,
|
|
103
|
+
files: { full, tiny, poster },
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/** The text that names a GIF to someone who cannot see it. */
|
|
107
|
+
export function gifLabel(gif) {
|
|
108
|
+
return gif.description || gif.title || "GIF";
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* Height in pixels of the credit strip under the frame of every file, full,
|
|
112
|
+
* tiny and poster alike. A saved or shared file keeps it; a page showing
|
|
113
|
+
* the GIF cuts it off and lays the credit over the frame instead, as
|
|
114
|
+
* ohmagif.com does.
|
|
115
|
+
*/
|
|
116
|
+
export const CREDIT_STRIP_HEIGHT = 24;
|
|
117
|
+
/** A file's height without its credit strip. */
|
|
118
|
+
export function frameHeight(file) {
|
|
119
|
+
return file.height > CREDIT_STRIP_HEIGHT ? file.height - CREDIT_STRIP_HEIGHT : file.height;
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The size of the GIF's frame at full size, without the credit strip: the
|
|
123
|
+
* shape the components show it in. Keep this with a message to reserve
|
|
124
|
+
* the GIF's room before it loads.
|
|
125
|
+
*
|
|
126
|
+
* tiny.avif and full.avif round their frames apart by a fraction of a
|
|
127
|
+
* pixel, and a browser shows either. This is the shorter of the two shapes
|
|
128
|
+
* in whole pixels, so whichever it shows, the strip stays out of sight.
|
|
129
|
+
*/
|
|
130
|
+
export function frameSize(gif) {
|
|
131
|
+
const { full, tiny } = gif.files;
|
|
132
|
+
const fromTiny = Math.floor((full.width * frameHeight(tiny)) / tiny.width);
|
|
133
|
+
return { width: full.width, height: Math.max(1, Math.min(frameHeight(full), fromTiny)) };
|
|
134
|
+
}
|
|
135
|
+
/** The frame of tiny.avif, the shape of a GIF in a grid. */
|
|
136
|
+
export function tinyFrameSize(gif) {
|
|
137
|
+
return { width: gif.files.tiny.width, height: frameHeight(gif.files.tiny) };
|
|
138
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { OhmagifError, OhmagifFetch, type GifResult, type GifsResult, type OhmagifFetchOptions, type PageOptions, type Pagination, type Reaction, type ReactionsResult, type RequestOptions, } from "./fetch.js";
|
|
2
|
+
export { CREDIT_STRIP_HEIGHT, DEFAULT_BASE_URL, frameHeight, frameSize, tinyFrameSize, gifLabel, isGifId, parseGif, RATINGS, type Creator, type Gif, type GifFile, type Rating, } from "./gif.js";
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { CSSProperties, MouseEvent } from "react";
|
|
2
|
+
import { type Gif } from "../gif.js";
|
|
3
|
+
/** Narrower than this, the credit line starts at the handle. */
|
|
4
|
+
export declare const FULL_CREDIT_WIDTH = 280;
|
|
5
|
+
/** A control at the end of a grid: Retry, or Upload to ohmagif. */
|
|
6
|
+
export declare const buttonStyle: CSSProperties;
|
|
7
|
+
export interface CardProps {
|
|
8
|
+
gif: Gif;
|
|
9
|
+
/** The width the card is shown at, in CSS pixels. */
|
|
10
|
+
width: number;
|
|
11
|
+
/** Whether the picture is there: a grid leaves it out far from the screen. */
|
|
12
|
+
picture?: boolean;
|
|
13
|
+
/** tiny.avif whatever the width, as a grid shows it. */
|
|
14
|
+
tinyOnly?: boolean;
|
|
15
|
+
/** The card links to the GIF's page. */
|
|
16
|
+
link: boolean;
|
|
17
|
+
/** Called on a click on the frame. Without link, it makes the frame a button. */
|
|
18
|
+
onClick?: (e: MouseEvent<HTMLElement>) => void;
|
|
19
|
+
/** Leave out the card and its credit line: the frame alone. */
|
|
20
|
+
hideAttribution?: boolean;
|
|
21
|
+
lazy?: boolean;
|
|
22
|
+
onLoad?: () => void;
|
|
23
|
+
onError?: () => void;
|
|
24
|
+
className: string;
|
|
25
|
+
/** Size and place, from the caller. */
|
|
26
|
+
style: CSSProperties;
|
|
27
|
+
id?: string;
|
|
28
|
+
}
|
|
29
|
+
/** A GIF cut to its frame, in the embed's card with the credit line under it. */
|
|
30
|
+
export declare function Card({ gif, width, picture, tinyOnly, link, onClick, hideAttribution, lazy, onLoad, onError, className, style, id, }: CardProps): import("react").JSX.Element;
|
|
@@ -0,0 +1,116 @@
|
|
|
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 } 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
|
+
const handleStyle = { color: link, fontWeight: 600 };
|
|
62
|
+
/** A control at the end of a grid: Retry, or Upload to ohmagif. */
|
|
63
|
+
export const buttonStyle = {
|
|
64
|
+
...reset,
|
|
65
|
+
display: "inline-flex",
|
|
66
|
+
alignItems: "center",
|
|
67
|
+
justifyContent: "center",
|
|
68
|
+
height: 36,
|
|
69
|
+
padding: "0 14px",
|
|
70
|
+
borderWidth: 1,
|
|
71
|
+
borderStyle: "solid",
|
|
72
|
+
borderColor: border,
|
|
73
|
+
borderRadius: 4,
|
|
74
|
+
background: card,
|
|
75
|
+
color: link,
|
|
76
|
+
fontSize: 14,
|
|
77
|
+
fontWeight: 600,
|
|
78
|
+
lineHeight: 1,
|
|
79
|
+
textDecoration: "none",
|
|
80
|
+
whiteSpace: "nowrap",
|
|
81
|
+
cursor: "pointer",
|
|
82
|
+
};
|
|
83
|
+
const external = { target: "_blank", rel: "noopener noreferrer", referrerPolicy: "no-referrer" };
|
|
84
|
+
/** A GIF cut to its frame, in the embed's card with the credit line under it. */
|
|
85
|
+
export function Card({ gif, width, picture = true, tinyOnly = false, link, onClick, hideAttribution = false, lazy, onLoad, onError, className, style, id, }) {
|
|
86
|
+
const frame = tinyOnly ? tinyFrameSize(gif) : frameSize(gif);
|
|
87
|
+
const frameWidth = hideAttribution ? width : width - 2 * CARD_BORDER;
|
|
88
|
+
const label = gifLabel(gif);
|
|
89
|
+
const pic = picture ? (_jsx(Picture, { gif: gif, width: frameWidth, tinyOnly: tinyOnly, alt: "", lazy: lazy, onLoad: onLoad, onError: onError })) : null;
|
|
90
|
+
const shape = { ...frameBase, aspectRatio: `${frame.width} / ${frame.height}` };
|
|
91
|
+
const pointer = link || onClick ? { cursor: "pointer" } : null;
|
|
92
|
+
const data = id ? { "data-ohmagif-id": id } : {};
|
|
93
|
+
// The frame alone: one element, named for those who cannot see it.
|
|
94
|
+
if (hideAttribution) {
|
|
95
|
+
const s = { ...shape, ...pointer, borderRadius: radius, ...style };
|
|
96
|
+
const cls = `${className} ohmagif-frame`;
|
|
97
|
+
if (link) {
|
|
98
|
+
return (_jsx("a", { ...data, ...external, className: cls, href: gif.url, "aria-label": label, style: s, onClick: onClick, children: pic }));
|
|
99
|
+
}
|
|
100
|
+
if (onClick) {
|
|
101
|
+
return (_jsx("button", { ...data, type: "button", className: cls, "aria-label": label, style: s, onClick: onClick, children: pic }));
|
|
102
|
+
}
|
|
103
|
+
return (_jsx("span", { ...data, role: "img", className: cls, "aria-label": label, style: s, children: pic }));
|
|
104
|
+
}
|
|
105
|
+
const handle = _jsxs("span", { style: handleStyle, children: ["@", gif.creator.handle] });
|
|
106
|
+
const words = width >= FULL_CREDIT_WIDTH ? _jsxs(_Fragment, { children: ["GIF by ", handle, " on ohmagif.com"] }) : _jsxs(_Fragment, { children: [handle, " on ohmagif.com"] });
|
|
107
|
+
const cls = `${className} ohmagif-card`;
|
|
108
|
+
const s = { ...cardStyle, ...style };
|
|
109
|
+
// The whole card is one link to the GIF's page.
|
|
110
|
+
if (link) {
|
|
111
|
+
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: creditStyle, children: words })] }));
|
|
112
|
+
}
|
|
113
|
+
// The frame does what the page wants (pick it, say); the credit line
|
|
114
|
+
// links to the GIF's page.
|
|
115
|
+
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: creditStyle, children: words })] }));
|
|
116
|
+
}
|
|
@@ -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, 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
|
+
}
|