@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
|
@@ -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,34 @@
|
|
|
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
|
+
export interface Size {
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
}
|
|
10
|
+
export interface SizeOptions {
|
|
11
|
+
/** The frame alone, without the card and its credit line. */
|
|
12
|
+
hideAttribution?: boolean;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The room a GIF takes when shown `width` pixels wide: its frame (the file
|
|
16
|
+
* without its credit strip) in a card with the credit line under it, as
|
|
17
|
+
* Grid and Gif draw it. Reserve this while the GIF is fetched and nothing
|
|
18
|
+
* moves when it arrives. `frame` is frameSize(gif), or the size kept with
|
|
19
|
+
* a message.
|
|
20
|
+
*/
|
|
21
|
+
export declare function gifSize(frame: Size, width: number, options?: SizeOptions): Size;
|
|
22
|
+
export interface Box extends Size {
|
|
23
|
+
x: number;
|
|
24
|
+
y: number;
|
|
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 declare function masonry(gifs: readonly Gif[], width: number, columns: number, gutter: number, options?: SizeOptions): {
|
|
32
|
+
boxes: Box[];
|
|
33
|
+
height: number;
|
|
34
|
+
};
|
|
@@ -0,0 +1,43 @@
|
|
|
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
|
+
/**
|
|
7
|
+
* The room a GIF takes when shown `width` pixels wide: its frame (the file
|
|
8
|
+
* without its credit strip) in a card with the credit line under it, as
|
|
9
|
+
* Grid and Gif draw it. Reserve this while the GIF is fetched and nothing
|
|
10
|
+
* moves when it arrives. `frame` is frameSize(gif), or the size kept with
|
|
11
|
+
* a message.
|
|
12
|
+
*/
|
|
13
|
+
export function gifSize(frame, width, options = {}) {
|
|
14
|
+
const card = options.hideAttribution ? 0 : 2 * CARD_BORDER;
|
|
15
|
+
const line = options.hideAttribution ? 0 : CREDIT_LINE_HEIGHT;
|
|
16
|
+
const inner = Math.max(0, width - card);
|
|
17
|
+
// Not rounded: a frame a fraction of a pixel too high shows the top of
|
|
18
|
+
// the credit strip.
|
|
19
|
+
return { width, height: (inner * frame.height) / frame.width + card + line };
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Places GIFs in columns, each into the shortest column so far, in the
|
|
23
|
+
* order given. A GIF already placed keeps its place when more are added,
|
|
24
|
+
* so a new page never moves what is on screen.
|
|
25
|
+
*/
|
|
26
|
+
export function masonry(gifs, width, columns, gutter, options = {}) {
|
|
27
|
+
const cols = Math.max(1, Math.floor(columns) || 1);
|
|
28
|
+
const gap = Math.max(0, gutter);
|
|
29
|
+
const colWidth = Math.max(0, (width - gap * (cols - 1)) / cols);
|
|
30
|
+
const heights = new Array(cols).fill(0);
|
|
31
|
+
const boxes = gifs.map((gif) => {
|
|
32
|
+
let c = 0;
|
|
33
|
+
for (let i = 1; i < cols; i++)
|
|
34
|
+
if (heights[i] < heights[c])
|
|
35
|
+
c = i;
|
|
36
|
+
// Grids show tiny.avif, so its frame is the tile's shape.
|
|
37
|
+
const { height } = gifSize(tinyFrameSize(gif), colWidth, options);
|
|
38
|
+
const box = { x: c * (colWidth + gap), y: heights[c], width: colWidth, height };
|
|
39
|
+
heights[c] = box.y + box.height + gap;
|
|
40
|
+
return box;
|
|
41
|
+
});
|
|
42
|
+
return { boxes, height: boxes.length ? Math.max(...heights) - gap : 0 };
|
|
43
|
+
}
|
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.0",
|
|
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
|
+
}
|