@ohmagif/js 0.1.0 → 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/README.md +32 -312
- package/dist/react/Card.d.ts +3 -1
- package/dist/react/Card.js +14 -5
- package/dist/react/Grid.js +1 -1
- package/dist/react/layout.d.ts +2 -0
- package/dist/react/layout.js +7 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,31 +1,12 @@
|
|
|
1
|
-
# ohmagif
|
|
1
|
+
# @ohmagif/js
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
[](https://github.com/ohmagif/ohmagif-js/actions/workflows/ci.yml)
|
|
4
|
+
[](https://coveralls.io/github/ohmagif/ohmagif-js?branch=main)
|
|
5
|
+
[](https://www.npmjs.com/package/@ohmagif/js)
|
|
6
|
+
[](LICENSE)
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
about 40 kB gzipped in the same app.
|
|
11
|
-
- No API key needed: the public API answers pages on any site.
|
|
12
|
-
- Requests carry no cookies and no referrer, and nothing reports what a person
|
|
13
|
-
looked at or picked.
|
|
14
|
-
- Every API answer is checked before it reaches the page: GIF ids are checked
|
|
15
|
-
before they go into a request, links and file addresses must be http or
|
|
16
|
-
https, and a GIF that does not check out is dropped.
|
|
17
|
-
- No injected CSS, so a strict `Content-Security-Policy` needs no
|
|
18
|
-
`'unsafe-inline'`.
|
|
19
|
-
- Each GIF is shown in the card ohmagif's embed draws: the frame, cut from
|
|
20
|
-
the credit strip burned into the file, and a line under it, "GIF by
|
|
21
|
-
@handle on ohmagif.com", that links to the GIF's page. A file that is
|
|
22
|
-
saved or shared keeps its strip.
|
|
23
|
-
- Every GIF's size is known before a byte of it arrives (`gifSize`), so a
|
|
24
|
-
chat can keep its room and nothing moves when it loads.
|
|
25
|
-
- Files are animated AVIF, with a JPEG first frame for browsers that cannot
|
|
26
|
-
show AVIF.
|
|
27
|
-
|
|
28
|
-
## Install
|
|
8
|
+
A client for the [ohmagif](https://ohmagif.com) API and a React GIF picker.
|
|
9
|
+
No dependencies, 5.7 kB gzipped. [Example](https://ohmagif.github.io/ohmagif-js/)
|
|
29
10
|
|
|
30
11
|
```sh
|
|
31
12
|
npm install @ohmagif/js
|
|
@@ -38,312 +19,51 @@ import { OhmagifFetch } from "@ohmagif/js";
|
|
|
38
19
|
|
|
39
20
|
const gf = new OhmagifFetch();
|
|
40
21
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
22
|
+
await gf.search("side eye", { limit: 12, rating: "pg" });
|
|
23
|
+
await gf.trending({ offset: 12 });
|
|
24
|
+
await gf.tag("oops");
|
|
25
|
+
await gf.gif("eTxuLnWv7C");
|
|
26
|
+
await gf.reactions();
|
|
46
27
|
```
|
|
47
28
|
|
|
48
|
-
| Method | Answers |
|
|
49
|
-
|---|---|
|
|
50
|
-
| `search(term, options?)` | GIFs matching the words, best match first |
|
|
51
|
-
| `trending(options?)` | The GIFs shared most over the last days |
|
|
52
|
-
| `tag(slug, options?)` | GIFs with a tag or reaction |
|
|
53
|
-
| `gif(id, { signal }?)` | One GIF |
|
|
54
|
-
| `reactions({ signal }?)` | The curated reactions, each with its top GIF |
|
|
55
|
-
|
|
56
|
-
List options: `offset` (0 to 1000), `limit` (1 to 50, default 25), `rating`
|
|
57
|
-
(`g`, `pg` or `pg13`, the highest to include) and `signal`. Lists answer
|
|
58
|
-
`{ data, pagination: { total_count, count, offset, next } }`; `next` is the
|
|
59
|
-
offset of the next page, `null` after the last.
|
|
60
|
-
|
|
61
|
-
Constructor options, all optional:
|
|
62
|
-
|
|
63
|
-
| Option | |
|
|
64
|
-
|---|---|
|
|
65
|
-
| `apiKey` | Raises the limit from 60 requests a minute per address to 600 for the key. Anyone using a page can read a key sent from it, and keyed answers are never cached, so a browser app is better off without one. A string alone is taken as the key. |
|
|
66
|
-
| `baseURL` | `https://ohmagif.com` by default |
|
|
67
|
-
| `timeout` | Milliseconds, 10000 by default; 0 waits forever |
|
|
68
|
-
| `fetch` | A `fetch` to use instead of the global one |
|
|
69
|
-
|
|
70
|
-
Failures reject with an `OhmagifError` carrying `status`, `code` and
|
|
71
|
-
`retryAfter` (seconds, after a 429). `code` is the API's own (`not_found`,
|
|
72
|
-
`removed`, `search_unavailable`, ...) or one of `invalid_id` (no request was
|
|
73
|
-
sent), `network`, `timeout` and `invalid_response`. A request aborted through
|
|
74
|
-
its `signal` rejects with the signal's reason. Arguments out of range reject
|
|
75
|
-
with a `RangeError` before anything is sent.
|
|
76
|
-
|
|
77
|
-
`gf.uploadURL` is the site's upload page.
|
|
78
|
-
|
|
79
|
-
Ask for a GIF again rather than keeping it for long: when a GIF is rendered
|
|
80
|
-
again its files get new addresses and the old ones go an hour later. Store the
|
|
81
|
-
id, not the file address, and `frameSize(gif)` to hold the GIF's room before
|
|
82
|
-
it loads.
|
|
83
|
-
|
|
84
|
-
Every file has a 24 px credit strip under its frame (`CREDIT_STRIP_HEIGHT`).
|
|
85
|
-
`width` and `height` include it; `frameSize(gif)` is the frame alone, in
|
|
86
|
-
whole pixels, as the components show it.
|
|
87
|
-
|
|
88
29
|
## React
|
|
89
30
|
|
|
90
|
-
|
|
91
|
-
import { useCallback } from "react";
|
|
92
|
-
import { OhmagifFetch, type Gif as GifData } from "@ohmagif/js";
|
|
93
|
-
import { Gif, Grid } from "@ohmagif/js/react";
|
|
94
|
-
|
|
95
|
-
const gf = new OhmagifFetch();
|
|
96
|
-
|
|
97
|
-
function Picker({ term, onPick }: { term: string; onPick: (gif: GifData) => void }) {
|
|
98
|
-
const fetchGifs = useCallback(
|
|
99
|
-
(offset: number, signal: AbortSignal) => {
|
|
100
|
-
const options = { offset, limit: 12, rating: "pg" as const, signal };
|
|
101
|
-
return term ? gf.search(term, options) : gf.trending(options);
|
|
102
|
-
},
|
|
103
|
-
[term],
|
|
104
|
-
);
|
|
105
|
-
return (
|
|
106
|
-
<Grid
|
|
107
|
-
key={term}
|
|
108
|
-
fetchGifs={fetchGifs}
|
|
109
|
-
width={336}
|
|
110
|
-
columns={2}
|
|
111
|
-
noLink
|
|
112
|
-
onGifClick={(gif) => onPick(gif)}
|
|
113
|
-
uploadURL={gf.uploadURL}
|
|
114
|
-
/>
|
|
115
|
-
);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
function Message({ gif }: { gif: GifData }) {
|
|
119
|
-
return <Gif gif={gif} width={322} />;
|
|
120
|
-
}
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
### `Grid`
|
|
31
|
+
React 18 or 19.
|
|
124
32
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
| `hideAttribution` | The frame alone, without the card and its credit line |
|
|
139
|
-
| `onGifClick(gif, event)` | |
|
|
140
|
-
| `onGifsFetched(gifs)` | Every GIF the grid holds, after each page |
|
|
141
|
-
| `onGifsFetchError(error)` | |
|
|
142
|
-
| `noResultsMessage` | Shown when the first page is empty, "No GIFs found" by default |
|
|
143
|
-
| `uploadURL` | Where "Upload to ohmagif" leads, `https://ohmagif.com/upload` by default (`gf.uploadURL`); `null` offers nothing |
|
|
144
|
-
| `errorMessage` | Shown with a Retry button when a page did not load |
|
|
145
|
-
| `loader` | A component shown while a page loads |
|
|
146
|
-
| `className` | |
|
|
147
|
-
|
|
148
|
-
### `Gif`
|
|
149
|
-
|
|
150
|
-
One GIF in its card at a given width, never wider than its container. The
|
|
151
|
-
whole card links to the GIF's page. The browser picks `tiny.avif` or
|
|
152
|
-
`full.avif` by the screen's density.
|
|
153
|
-
|
|
154
|
-
| Prop | |
|
|
155
|
-
|---|---|
|
|
156
|
-
| `gif`, `width` | Required. Width of the card in CSS pixels |
|
|
157
|
-
| `noLink` | The frame does not link (a button when `onGifClick` is set); the credit line still does |
|
|
158
|
-
| `onGifClick(gif, event)` | |
|
|
159
|
-
| `hideAttribution` | The frame alone, without the card and its credit line |
|
|
160
|
-
| `lazyLoad` | Load the file only near the screen, on by default |
|
|
161
|
-
| `onLoad`, `onError` | `onError` usually means the address is out of date: ask for the GIF again |
|
|
162
|
-
| `className` | |
|
|
163
|
-
|
|
164
|
-
### Sizes
|
|
165
|
-
|
|
166
|
-
`gifSize(frame, width)` is the room a GIF takes at a width: the frame inside
|
|
167
|
-
the card's 1 px border, and the 28 px credit line. With the frame size kept in
|
|
168
|
-
a message, a chat reserves exactly that while it asks for the GIF:
|
|
169
|
-
|
|
170
|
-
```ts
|
|
171
|
-
const box = gifSize({ width: 346, height: 205 }, 348); // { width: 348, height: 235 }
|
|
33
|
+
```tsx
|
|
34
|
+
import { frameSize } from "@ohmagif/js";
|
|
35
|
+
import { Gif, gifSize, Grid } from "@ohmagif/js/react";
|
|
36
|
+
|
|
37
|
+
<Grid
|
|
38
|
+
fetchGifs={(offset, signal) => gf.trending({ offset, limit: 12, signal })}
|
|
39
|
+
width={336}
|
|
40
|
+
columns={2}
|
|
41
|
+
noLink
|
|
42
|
+
onGifClick={(gif) => send({ id: gif.id, ...frameSize(gif) })}
|
|
43
|
+
/>
|
|
44
|
+
|
|
45
|
+
<Gif gif={gif} width={322} />
|
|
172
46
|
```
|
|
173
47
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
### Styling
|
|
48
|
+
Keep a GIF's id and `frameSize(gif)`, not its file address.
|
|
49
|
+
`gifSize(frame, width)` is the room it takes before it loads. Colours follow
|
|
50
|
+
the page's `color-scheme`.
|
|
178
51
|
|
|
179
|
-
|
|
180
|
-
`ohmagif-frame`, `ohmagif-credit`, `ohmagif-end`, `ohmagif-upload`,
|
|
181
|
-
`ohmagif-loading` and `ohmagif-error`.
|
|
182
|
-
|
|
183
|
-
Custom properties: `--ohmagif-card`, `--ohmagif-border`, `--ohmagif-muted`
|
|
184
|
-
(the credit line), `--ohmagif-link` (the handle and the buttons),
|
|
185
|
-
`--ohmagif-placeholder` (a GIF that has not loaded yet) and `--ohmagif-radius`.
|
|
186
|
-
By default the colours are the embed's, light or dark as the page's
|
|
187
|
-
`color-scheme` says. A page that switches to dark with a class says so:
|
|
188
|
-
|
|
189
|
-
```css
|
|
190
|
-
html.dark { color-scheme: dark; }
|
|
191
|
-
.ohmagif-frame:focus-visible, .ohmagif-credit:focus-visible { outline: 2px solid var(--accent); }
|
|
192
|
-
```
|
|
193
|
-
|
|
194
|
-
### Images, not iframes
|
|
195
|
-
|
|
196
|
-
Both components show the file itself in an `<img>`, so a GIF in a chat costs
|
|
197
|
-
one image request and nothing else runs in the page. An iframe embed is there
|
|
198
|
-
for pages that want one: `gif.embed_url` at `gif.embed_width` x
|
|
199
|
-
`gif.embed_height`, with `frame-src https://ohmagif.com` in the policy.
|
|
200
|
-
|
|
201
|
-
### Content-Security-Policy
|
|
52
|
+
## Content-Security-Policy
|
|
202
53
|
|
|
203
54
|
```
|
|
204
55
|
img-src https://i.ohmagif.com;
|
|
205
56
|
connect-src https://ohmagif.com;
|
|
206
57
|
```
|
|
207
58
|
|
|
208
|
-
## Moving from Giphy
|
|
209
|
-
|
|
210
|
-
| Giphy | ohmagif |
|
|
211
|
-
|---|---|
|
|
212
|
-
| `import { GiphyFetch } from "@giphy/js-fetch-api"` | `import { OhmagifFetch } from "@ohmagif/js"` |
|
|
213
|
-
| `import { Grid, Gif } from "@giphy/react-components"` | `import { Grid, Gif } from "@ohmagif/js/react"` |
|
|
214
|
-
| `IGif` | `Gif` from `@ohmagif/js` |
|
|
215
|
-
| `new GiphyFetch(key)` | `new OhmagifFetch()` |
|
|
216
|
-
| `gif.images.original.width`, `.height` | `frameSize(gif).width`, `.height` |
|
|
217
|
-
| `gif.images.original_mp4.mp4` | `gif.files.full.url` (AVIF, shown by an `<img>`) |
|
|
218
|
-
| `rating: "pg-13"` | `rating: "pg13"` |
|
|
219
|
-
| `hideAttribution` | The frame alone, without the card and its credit line |
|
|
220
|
-
| "Powered by GIPHY" | Not needed: every GIF has its credit line, linked to its page |
|
|
221
|
-
| `fetchGifs(offset)` | `fetchGifs(offset, signal)`; pass `signal` on to cancel |
|
|
222
|
-
|
|
223
|
-
A grid in a picker, before and after:
|
|
224
|
-
|
|
225
|
-
```diff
|
|
226
|
-
-import { GiphyFetch } from "@giphy/js-fetch-api";
|
|
227
|
-
-import { Grid } from "@giphy/react-components";
|
|
228
|
-
-import type IGif from "@giphy/js-types/dist/gif";
|
|
229
|
-
+import { frameSize, OhmagifFetch, type Gif } from "@ohmagif/js";
|
|
230
|
-
+import { Grid } from "@ohmagif/js/react";
|
|
231
|
-
|
|
232
|
-
-const gf = new GiphyFetch(apiKey);
|
|
233
|
-
+const gf = new OhmagifFetch();
|
|
234
|
-
|
|
235
|
-
const fetchGifs = useCallback(
|
|
236
|
-
- (offset: number) => {
|
|
237
|
-
- const options = { offset, limit: 12, rating: "pg" as const };
|
|
238
|
-
+ (offset: number, signal: AbortSignal) => {
|
|
239
|
-
+ const options = { offset, limit: 12, rating: "pg" as const, signal };
|
|
240
|
-
return query ? gf.search(query, options) : gf.trending(options);
|
|
241
|
-
},
|
|
242
|
-
[query],
|
|
243
|
-
);
|
|
244
|
-
|
|
245
|
-
-const onGifClick = (gif: IGif, e: React.SyntheticEvent) => {
|
|
246
|
-
- e.preventDefault();
|
|
247
|
-
- const { width, height } = gif.images.original;
|
|
248
|
-
- onPick({ id: String(gif.id), title: gif.title, width, height });
|
|
249
|
-
-};
|
|
250
|
-
+const onGifClick = (gif: Gif) => {
|
|
251
|
-
+ onPick({ id: gif.id, title: gif.title, ...frameSize(gif) });
|
|
252
|
-
+};
|
|
253
|
-
|
|
254
|
-
-<Grid key={query} width={width} columns={2} gutter={6} fetchGifs={fetchGifs} onGifClick={onGifClick} noLink hideAttribution />
|
|
255
|
-
+<Grid key={query} width={width} columns={2} gutter={6} fetchGifs={fetchGifs} onGifClick={onGifClick} noLink uploadURL={gf.uploadURL} />
|
|
256
|
-
```
|
|
257
|
-
|
|
258
|
-
Keeping `hideAttribution` works too: the tiles then show the frame alone.
|
|
259
|
-
|
|
260
|
-
A GIF in a message, from its stored id:
|
|
261
|
-
|
|
262
|
-
```tsx
|
|
263
|
-
import { useEffect, useState } from "react";
|
|
264
|
-
import { OhmagifFetch, type Gif as GifData } from "@ohmagif/js";
|
|
265
|
-
import { Gif, gifSize } from "@ohmagif/js/react";
|
|
266
|
-
|
|
267
|
-
const gf = new OhmagifFetch();
|
|
268
|
-
|
|
269
|
-
/** `frame` is the frameSize(gif) kept with the message when it was picked. */
|
|
270
|
-
function GifMessage({ id, frame, width }: { id: string; frame: { width: number; height: number }; width: number }) {
|
|
271
|
-
const [gif, setGif] = useState<GifData | null>(null);
|
|
272
|
-
const [gone, setGone] = useState(false);
|
|
273
|
-
useEffect(() => {
|
|
274
|
-
const ctrl = new AbortController();
|
|
275
|
-
gf.gif(id, { signal: ctrl.signal }).then(
|
|
276
|
-
({ data }) => setGif(data),
|
|
277
|
-
() => !ctrl.signal.aborted && setGone(true),
|
|
278
|
-
);
|
|
279
|
-
return () => ctrl.abort();
|
|
280
|
-
}, [id]);
|
|
281
|
-
// The same room before and after the GIF arrives.
|
|
282
|
-
const box = gifSize(frame, width);
|
|
283
|
-
if (gone) return <span style={box}>GIF unavailable</span>;
|
|
284
|
-
return gif ? <Gif gif={gif} width={width} /> : <span style={{ display: "block", ...box }} />;
|
|
285
|
-
}
|
|
286
|
-
```
|
|
287
|
-
|
|
288
|
-
Also:
|
|
289
|
-
|
|
290
|
-
- Messages that hold a Giphy id cannot be shown from ohmagif. Store picks
|
|
291
|
-
under a scheme of their own (`ohmagif:<id>`), and show the old ones as
|
|
292
|
-
unavailable. `isGifId()` tells an ohmagif id apart.
|
|
293
|
-
- A key in workspace settings is no longer needed to turn GIFs on.
|
|
294
|
-
- Keep a fetched GIF for less than an hour, or ask again when its `<Gif>`
|
|
295
|
-
reports `onError`.
|
|
296
|
-
- Keep `frameSize(gif)` with the message, not `gif.width` and `gif.height`,
|
|
297
|
-
and size its placeholder with `gifSize()`.
|
|
298
|
-
- Drop `media.giphy.com`, `api.giphy.com` and their friends from the
|
|
299
|
-
Content-Security-Policy and add the two lines above.
|
|
300
|
-
|
|
301
59
|
## Development
|
|
302
60
|
|
|
303
61
|
```sh
|
|
304
62
|
make install
|
|
305
63
|
make test
|
|
64
|
+
make example
|
|
306
65
|
```
|
|
307
66
|
|
|
308
|
-
| Target | |
|
|
309
|
-
|---|---|
|
|
310
|
-
| `make install` | Install the dependencies |
|
|
311
|
-
| `make lint` | tsc |
|
|
312
|
-
| `make test` | vitest, gated at 100% coverage: every statement, branch, function and line |
|
|
313
|
-
| `make test-integration` | The client against the live API; `OHMAGIF_BASE_URL=http://localhost:8080` for a local ohmagif |
|
|
314
|
-
| `make build` | Build `dist/` |
|
|
315
|
-
| `make example` | A chat with a GIF picker against ohmagif.com on http://localhost:8082/; `?base=http://localhost:8080` for a local ohmagif |
|
|
316
|
-
| `make example-build` | Build the example into `examples/dist/` |
|
|
317
|
-
| `make e2e` | Playwright against the running example (`make example` first); `OHMAGIF_JS_E2E_URL` for another copy, a preview included |
|
|
318
|
-
| `make e2e-ci` | Build the example, serve it on :8083 and run Playwright against it |
|
|
319
|
-
| `make clean` | Remove build and test output |
|
|
320
|
-
|
|
321
|
-
`npx playwright install chromium` once before the first `make e2e`.
|
|
322
|
-
|
|
323
|
-
The pipeline runs every suite on each push and pull request, and weekly
|
|
324
|
-
against the live API. Each pull request from this repository gets its
|
|
325
|
-
example at `https://ohmagif.github.io/ohmagif-js/pr-preview/pr-<n>/`, linked
|
|
326
|
-
in a comment; main's is at https://ohmagif.github.io/ohmagif-js/. Pages
|
|
327
|
-
serves the `gh-pages` branch: Settings, Pages, Deploy from a branch,
|
|
328
|
-
`gh-pages`, `/ (root)`.
|
|
329
|
-
|
|
330
|
-
## Releasing
|
|
331
|
-
|
|
332
|
-
Set the version in `package.json`, merge, and tag the merge commit:
|
|
333
|
-
|
|
334
|
-
```sh
|
|
335
|
-
git tag v0.2.0
|
|
336
|
-
git push origin v0.2.0
|
|
337
|
-
```
|
|
338
|
-
|
|
339
|
-
The pipeline runs every check, then publishes to npm with provenance. A
|
|
340
|
-
version with a hyphen (`0.2.0-rc.1`) goes out under the dist-tag `next`. A
|
|
341
|
-
tag that does not match `package.json` stops the release.
|
|
342
|
-
|
|
343
|
-
npm takes the package from this repository's `ci.yml` through trusted
|
|
344
|
-
publishing, without a token. `npm trust list @ohmagif/js` shows the
|
|
345
|
-
configuration.
|
|
346
|
-
|
|
347
67
|
## Licence
|
|
348
68
|
|
|
349
|
-
GPL-3.0
|
|
69
|
+
GPL-3.0
|
package/dist/react/Card.d.ts
CHANGED
|
@@ -18,6 +18,8 @@ export interface CardProps {
|
|
|
18
18
|
onClick?: (e: MouseEvent<HTMLElement>) => void;
|
|
19
19
|
/** Leave out the card and its credit line: the frame alone. */
|
|
20
20
|
hideAttribution?: boolean;
|
|
21
|
+
/** The grid's shorter credit line, "by @handle". */
|
|
22
|
+
shortCredit?: boolean;
|
|
21
23
|
lazy?: boolean;
|
|
22
24
|
onLoad?: () => void;
|
|
23
25
|
onError?: () => void;
|
|
@@ -27,4 +29,4 @@ export interface CardProps {
|
|
|
27
29
|
id?: string;
|
|
28
30
|
}
|
|
29
31
|
/** 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;
|
|
32
|
+
export declare function Card({ gif, width, picture, tinyOnly, link, onClick, hideAttribution, shortCredit, lazy, onLoad, onError, className, style, id, }: CardProps): import("react").JSX.Element;
|
package/dist/react/Card.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
2
|
import { frameSize, gifLabel, tinyFrameSize } from "../gif.js";
|
|
3
|
-
import { CARD_BORDER, CREDIT_LINE_HEIGHT } from "./layout.js";
|
|
3
|
+
import { CARD_BORDER, CREDIT_LINE_HEIGHT, TILE_CREDIT_LINE_HEIGHT } from "./layout.js";
|
|
4
4
|
import { Picture } from "./Picture.js";
|
|
5
5
|
// The card is the one ohmagif's embed draws (templates/embed.html): the
|
|
6
6
|
// frame on white with a 1 px border and 6 px corners, and a 28 px credit
|
|
@@ -58,6 +58,14 @@ const creditStyle = {
|
|
|
58
58
|
cursor: "pointer",
|
|
59
59
|
outlineOffset: -2,
|
|
60
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
|
+
};
|
|
61
69
|
const handleStyle = { color: link, fontWeight: 600 };
|
|
62
70
|
/** A control at the end of a grid: Retry, or Upload to ohmagif. */
|
|
63
71
|
export const buttonStyle = {
|
|
@@ -82,7 +90,7 @@ export const buttonStyle = {
|
|
|
82
90
|
};
|
|
83
91
|
const external = { target: "_blank", rel: "noopener noreferrer", referrerPolicy: "no-referrer" };
|
|
84
92
|
/** 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, }) {
|
|
93
|
+
export function Card({ gif, width, picture = true, tinyOnly = false, link, onClick, hideAttribution = false, shortCredit = false, lazy, onLoad, onError, className, style, id, }) {
|
|
86
94
|
const frame = tinyOnly ? tinyFrameSize(gif) : frameSize(gif);
|
|
87
95
|
const frameWidth = hideAttribution ? width : width - 2 * CARD_BORDER;
|
|
88
96
|
const label = gifLabel(gif);
|
|
@@ -103,14 +111,15 @@ export function Card({ gif, width, picture = true, tinyOnly = false, link, onCli
|
|
|
103
111
|
return (_jsx("span", { ...data, role: "img", className: cls, "aria-label": label, style: s, children: pic }));
|
|
104
112
|
}
|
|
105
113
|
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"] });
|
|
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;
|
|
107
116
|
const cls = `${className} ohmagif-card`;
|
|
108
117
|
const s = { ...cardStyle, ...style };
|
|
109
118
|
// The whole card is one link to the GIF's page.
|
|
110
119
|
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:
|
|
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 })] }));
|
|
112
121
|
}
|
|
113
122
|
// The frame does what the page wants (pick it, say); the credit line
|
|
114
123
|
// 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:
|
|
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 })] }));
|
|
116
125
|
}
|
package/dist/react/Grid.js
CHANGED
|
@@ -22,7 +22,7 @@ const endStyle = { display: "flex", flexDirection: "column", alignItems: "center
|
|
|
22
22
|
const Tile = memo(function Tile({ gif, x, y, w, h, near, link, hideAttribution, onClick }) {
|
|
23
23
|
// The picture is only there while the tile is near the screen; the tile
|
|
24
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 } }));
|
|
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
26
|
});
|
|
27
27
|
const nobody = new Set();
|
|
28
28
|
/**
|
package/dist/react/layout.d.ts
CHANGED
|
@@ -3,6 +3,8 @@ import { type Gif } from "../gif.js";
|
|
|
3
3
|
export declare const CARD_BORDER = 1;
|
|
4
4
|
/** The credit line under the frame, in CSS pixels. */
|
|
5
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;
|
|
6
8
|
export interface Size {
|
|
7
9
|
width: number;
|
|
8
10
|
height: number;
|
package/dist/react/layout.js
CHANGED
|
@@ -3,6 +3,8 @@ import { tinyFrameSize } from "../gif.js";
|
|
|
3
3
|
export const CARD_BORDER = 1;
|
|
4
4
|
/** The credit line under the frame, in CSS pixels. */
|
|
5
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;
|
|
6
8
|
/**
|
|
7
9
|
* The room a GIF takes when shown `width` pixels wide: its frame (the file
|
|
8
10
|
* without its credit strip) in a card with the credit line under it, as
|
|
@@ -11,8 +13,11 @@ export const CREDIT_LINE_HEIGHT = 28;
|
|
|
11
13
|
* a message.
|
|
12
14
|
*/
|
|
13
15
|
export function gifSize(frame, width, options = {}) {
|
|
16
|
+
return cardSize(frame, width, CREDIT_LINE_HEIGHT, options);
|
|
17
|
+
}
|
|
18
|
+
function cardSize(frame, width, creditLine, options) {
|
|
14
19
|
const card = options.hideAttribution ? 0 : 2 * CARD_BORDER;
|
|
15
|
-
const line = options.hideAttribution ? 0 :
|
|
20
|
+
const line = options.hideAttribution ? 0 : creditLine;
|
|
16
21
|
const inner = Math.max(0, width - card);
|
|
17
22
|
// Not rounded: a frame a fraction of a pixel too high shows the top of
|
|
18
23
|
// the credit strip.
|
|
@@ -34,7 +39,7 @@ export function masonry(gifs, width, columns, gutter, options = {}) {
|
|
|
34
39
|
if (heights[i] < heights[c])
|
|
35
40
|
c = i;
|
|
36
41
|
// Grids show tiny.avif, so its frame is the tile's shape.
|
|
37
|
-
const { height } =
|
|
42
|
+
const { height } = cardSize(tinyFrameSize(gif), colWidth, TILE_CREDIT_LINE_HEIGHT, options);
|
|
38
43
|
const box = { x: c * (colWidth + gap), y: heights[c], width: colWidth, height };
|
|
39
44
|
heights[c] = box.y + box.height + gap;
|
|
40
45
|
return box;
|