@nonphoto/sanity-image 0.1.1 → 1.0.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/dist/index.d.ts +16 -26
- package/dist/index.js +24 -24
- package/package.json +32 -38
- package/src/index.ts +156 -0
- package/dist/dev.cjs +0 -100
- package/dist/dev.js +0 -93
- package/dist/index.cjs +0 -100
package/dist/index.d.ts
CHANGED
|
@@ -1,41 +1,31 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { SanityImageDimensions, SanityClientLike, SanityProjectDetails, SanityModernClientLike, SanityImageSource } from '@sanity/image-url/lib/types/types.js';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
dimensions?: SanityImageDimensions;
|
|
7
|
-
};
|
|
8
|
-
};
|
|
9
|
-
interface SanityImageObjectWithMetadata {
|
|
10
|
-
asset: SanityAssetWithMetadata;
|
|
11
|
-
crop?: SanityImageCrop;
|
|
12
|
-
hotspot?: SanityImageHotspot;
|
|
3
|
+
interface Metadata {
|
|
4
|
+
lqip?: string;
|
|
5
|
+
dimensions?: SanityImageDimensions;
|
|
13
6
|
}
|
|
14
|
-
|
|
7
|
+
interface Size {
|
|
15
8
|
width: number;
|
|
16
9
|
height: number;
|
|
17
|
-
}
|
|
10
|
+
}
|
|
18
11
|
declare const defaultWidths: number[];
|
|
19
12
|
declare const lowResWidth = 24;
|
|
20
13
|
declare const defaultMetaImageWidth = 1200;
|
|
21
14
|
declare const defaultQuality = 90;
|
|
22
|
-
|
|
23
|
-
image: SanityImageObjectWithMetadata;
|
|
15
|
+
interface ImagePropsOptions {
|
|
24
16
|
client: SanityClientLike | SanityProjectDetails | SanityModernClientLike;
|
|
25
|
-
|
|
17
|
+
image: SanityImageSource;
|
|
18
|
+
metadata?: Metadata;
|
|
19
|
+
widths?: number[];
|
|
26
20
|
quality?: number;
|
|
27
21
|
aspectRatio?: number;
|
|
28
|
-
}
|
|
22
|
+
}
|
|
23
|
+
interface ImagePropsReturn {
|
|
29
24
|
src: string;
|
|
30
|
-
srcset
|
|
25
|
+
srcset?: string;
|
|
31
26
|
naturalWidth?: number;
|
|
32
27
|
naturalHeight?: number;
|
|
33
|
-
}
|
|
34
|
-
declare function
|
|
35
|
-
image: SanityImageObjectWithMetadata;
|
|
36
|
-
client: SanityClientLike | SanityProjectDetails | SanityModernClientLike;
|
|
37
|
-
width: number;
|
|
38
|
-
quality?: number;
|
|
39
|
-
}): string;
|
|
28
|
+
}
|
|
29
|
+
declare function imageProps({ client, image, metadata, widths, quality, aspectRatio, }: ImagePropsOptions): ImagePropsReturn;
|
|
40
30
|
|
|
41
|
-
export {
|
|
31
|
+
export { type ImagePropsOptions, type ImagePropsReturn, type Metadata, type Size, defaultMetaImageWidth, defaultQuality, defaultWidths, imageProps, lowResWidth };
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import imageUrlBuilder from '@sanity/image-url';
|
|
2
|
-
|
|
3
1
|
// src/index.ts
|
|
2
|
+
import imageUrlBuilder from "@sanity/image-url";
|
|
4
3
|
var defaultWidths = [
|
|
5
4
|
6016,
|
|
6
5
|
// 6K
|
|
@@ -52,42 +51,43 @@ function fit(containee, container, mode) {
|
|
|
52
51
|
}
|
|
53
52
|
function buildAspectRatio(builder, width, aspectRatio) {
|
|
54
53
|
if (aspectRatio) {
|
|
55
|
-
return builder.width(width).height(width * aspectRatio);
|
|
54
|
+
return builder.width(width).height(Math.round(width * aspectRatio));
|
|
56
55
|
} else {
|
|
57
56
|
return builder.width(width);
|
|
58
57
|
}
|
|
59
58
|
}
|
|
59
|
+
function isCrop(x) {
|
|
60
|
+
return x && typeof x == "object" && "top" in x && typeof x.top === "number" && "right" in x && typeof x.right === "number" && "bottom" in x && typeof x.bottom === "number" && "left" in x && typeof x.left === "number";
|
|
61
|
+
}
|
|
60
62
|
function imageProps({
|
|
61
|
-
image,
|
|
62
63
|
client,
|
|
64
|
+
image,
|
|
65
|
+
metadata,
|
|
63
66
|
widths,
|
|
64
67
|
quality = defaultQuality,
|
|
65
68
|
aspectRatio
|
|
66
69
|
}) {
|
|
67
|
-
const sortedWidths = Array.from(widths).sort((a, b) => a - b);
|
|
68
70
|
const builder = imageUrlBuilder(client).image(image).quality(quality).auto("format");
|
|
69
|
-
const
|
|
70
|
-
const cropSize =
|
|
71
|
-
width: metadata.dimensions.width -
|
|
72
|
-
height: metadata.dimensions.height -
|
|
73
|
-
} : void 0;
|
|
74
|
-
const naturalSize = cropSize ? aspectRatio ? fit({ width: 1, height: aspectRatio }, cropSize, "contain") : cropSize :
|
|
71
|
+
const crop = typeof image == "object" && "crop" in image && isCrop(image.crop) ? image.crop : void 0;
|
|
72
|
+
const cropSize = metadata?.dimensions ? crop ? {
|
|
73
|
+
width: metadata.dimensions.width - crop.left - crop.right,
|
|
74
|
+
height: metadata.dimensions.height - crop.top - crop.bottom
|
|
75
|
+
} : metadata.dimensions : void 0;
|
|
76
|
+
const naturalSize = cropSize ? aspectRatio ? fit({ width: 1, height: aspectRatio }, cropSize, "contain") : cropSize : void 0;
|
|
77
|
+
const url = buildAspectRatio(builder, lowResWidth, aspectRatio).url();
|
|
75
78
|
return {
|
|
76
|
-
src: metadata?.lqip ??
|
|
77
|
-
srcset:
|
|
79
|
+
src: widths ? url : metadata?.lqip ?? url,
|
|
80
|
+
srcset: widths ? Array.from(widths).sort((a, b) => a - b).map(
|
|
78
81
|
(width) => `${buildAspectRatio(builder, width, aspectRatio).url()} ${width}w`
|
|
79
|
-
).join(","),
|
|
82
|
+
).join(",") : void 0,
|
|
80
83
|
naturalWidth: naturalSize?.width,
|
|
81
84
|
naturalHeight: naturalSize?.height
|
|
82
85
|
};
|
|
83
86
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export { defaultMetaImageWidth, defaultQuality, defaultWidths, imageProps, lowResWidth, metaImageUrl };
|
|
87
|
+
export {
|
|
88
|
+
defaultMetaImageWidth,
|
|
89
|
+
defaultQuality,
|
|
90
|
+
defaultWidths,
|
|
91
|
+
imageProps,
|
|
92
|
+
lowResWidth
|
|
93
|
+
};
|
package/package.json
CHANGED
|
@@ -1,54 +1,48 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@nonphoto/sanity-image",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "1.0.1",
|
|
4
4
|
"type": "module",
|
|
5
|
-
"files": [
|
|
6
|
-
"dist"
|
|
7
|
-
],
|
|
8
|
-
"private": false,
|
|
9
5
|
"sideEffects": false,
|
|
10
|
-
"devDependencies": {
|
|
11
|
-
"@changesets/cli": "2.26.1",
|
|
12
|
-
"@types/node": "18.14.6",
|
|
13
|
-
"prettier": "2.8.7",
|
|
14
|
-
"tsup": "6.7.0",
|
|
15
|
-
"tsup-preset-solid": "0.1.8",
|
|
16
|
-
"typescript": "^4.9.4",
|
|
17
|
-
"vite": "^4.0.4",
|
|
18
|
-
"vitest": "0.29.8"
|
|
19
|
-
},
|
|
20
|
-
"dependencies": {
|
|
21
|
-
"@sanity/image-url": "1.0.2"
|
|
22
|
-
},
|
|
23
|
-
"packageManager": "pnpm@7.29.1",
|
|
24
6
|
"publishConfig": {
|
|
25
7
|
"access": "public"
|
|
26
8
|
},
|
|
27
|
-
"browser": {},
|
|
28
9
|
"exports": {
|
|
29
|
-
"
|
|
10
|
+
".": {
|
|
30
11
|
"import": {
|
|
31
12
|
"types": "./dist/index.d.ts",
|
|
32
|
-
"default": "./dist/
|
|
33
|
-
}
|
|
34
|
-
"require": "./dist/dev.cjs"
|
|
13
|
+
"default": "./dist/index.js"
|
|
14
|
+
}
|
|
35
15
|
},
|
|
36
|
-
"
|
|
37
|
-
"types": "./dist/index.d.ts",
|
|
38
|
-
"default": "./dist/index.js"
|
|
39
|
-
},
|
|
40
|
-
"require": "./dist/index.cjs"
|
|
16
|
+
"./src/*": "./src/*"
|
|
41
17
|
},
|
|
42
|
-
"
|
|
43
|
-
|
|
18
|
+
"files": [
|
|
19
|
+
"./dist/*",
|
|
20
|
+
"./src/*"
|
|
21
|
+
],
|
|
44
22
|
"module": "./dist/index.js",
|
|
45
23
|
"types": "./dist/index.d.ts",
|
|
46
24
|
"scripts": {
|
|
47
|
-
"
|
|
48
|
-
"build": "tsup",
|
|
49
|
-
"
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
"
|
|
25
|
+
"prepare": "pnpm build",
|
|
26
|
+
"build": "tsup ./src/index.ts --dts --format esm --clean",
|
|
27
|
+
"release": "semantic-release"
|
|
28
|
+
},
|
|
29
|
+
"keywords": [
|
|
30
|
+
"spring",
|
|
31
|
+
"physics",
|
|
32
|
+
"animation"
|
|
33
|
+
],
|
|
34
|
+
"author": "Jonas Luebbers <jonas@jonasluebbers.com> (https://www.jonasluebbers.com)",
|
|
35
|
+
"license": "MIT",
|
|
36
|
+
"devDependencies": {
|
|
37
|
+
"semantic-release": "24.2.3",
|
|
38
|
+
"tsup": "8.4.0",
|
|
39
|
+
"typescript": "5.8.2"
|
|
40
|
+
},
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=20",
|
|
43
|
+
"pnpm": ">=9"
|
|
44
|
+
},
|
|
45
|
+
"dependencies": {
|
|
46
|
+
"@sanity/image-url": "1.0.2"
|
|
53
47
|
}
|
|
54
|
-
}
|
|
48
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import imageUrlBuilder from "@sanity/image-url";
|
|
2
|
+
import { ImageUrlBuilder } from "@sanity/image-url/lib/types/builder";
|
|
3
|
+
import type {
|
|
4
|
+
SanityClientLike,
|
|
5
|
+
SanityImageCrop,
|
|
6
|
+
SanityImageDimensions,
|
|
7
|
+
SanityImageSource,
|
|
8
|
+
SanityModernClientLike,
|
|
9
|
+
SanityProjectDetails,
|
|
10
|
+
} from "@sanity/image-url/lib/types/types.js";
|
|
11
|
+
|
|
12
|
+
export interface Metadata {
|
|
13
|
+
lqip?: string;
|
|
14
|
+
dimensions?: SanityImageDimensions;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface Size {
|
|
18
|
+
width: number;
|
|
19
|
+
height: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export const defaultWidths = [
|
|
23
|
+
6016, // 6K
|
|
24
|
+
5120, // 5K
|
|
25
|
+
4480, // 4.5K
|
|
26
|
+
3840, // 4K
|
|
27
|
+
3200, // QHD+
|
|
28
|
+
2560, // WQXGA
|
|
29
|
+
2048, // QXGA
|
|
30
|
+
1920, // 1080p
|
|
31
|
+
1668, // iPad
|
|
32
|
+
1280, // 720p
|
|
33
|
+
1080, // iPhone 6-8 Plus
|
|
34
|
+
960,
|
|
35
|
+
720, // iPhone 6-8
|
|
36
|
+
640, // 480p
|
|
37
|
+
480,
|
|
38
|
+
360,
|
|
39
|
+
240,
|
|
40
|
+
];
|
|
41
|
+
|
|
42
|
+
export const lowResWidth = 24;
|
|
43
|
+
|
|
44
|
+
export const defaultMetaImageWidth = 1200;
|
|
45
|
+
|
|
46
|
+
export const defaultQuality = 90;
|
|
47
|
+
|
|
48
|
+
const fitComparators = {
|
|
49
|
+
cover: Math.max,
|
|
50
|
+
contain: Math.min,
|
|
51
|
+
mean: (a: number, b: number) => (a + b) / 2,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
function fit(
|
|
55
|
+
containee: Size,
|
|
56
|
+
container: Size,
|
|
57
|
+
mode: keyof typeof fitComparators
|
|
58
|
+
) {
|
|
59
|
+
const sx = container.width / containee.width;
|
|
60
|
+
const sy = container.height / containee.height;
|
|
61
|
+
const s = fitComparators[mode](sx, sy);
|
|
62
|
+
return {
|
|
63
|
+
width: containee.width * s,
|
|
64
|
+
height: containee.height * s,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function buildAspectRatio(
|
|
69
|
+
builder: ImageUrlBuilder,
|
|
70
|
+
width: number,
|
|
71
|
+
aspectRatio?: number
|
|
72
|
+
) {
|
|
73
|
+
if (aspectRatio) {
|
|
74
|
+
return builder.width(width).height(Math.round(width * aspectRatio));
|
|
75
|
+
} else {
|
|
76
|
+
return builder.width(width);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function isCrop(x: any): x is SanityImageCrop {
|
|
81
|
+
return (
|
|
82
|
+
x &&
|
|
83
|
+
typeof x == "object" &&
|
|
84
|
+
"top" in x &&
|
|
85
|
+
typeof x.top === "number" &&
|
|
86
|
+
"right" in x &&
|
|
87
|
+
typeof x.right === "number" &&
|
|
88
|
+
"bottom" in x &&
|
|
89
|
+
typeof x.bottom === "number" &&
|
|
90
|
+
"left" in x &&
|
|
91
|
+
typeof x.left === "number"
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
export interface ImagePropsOptions {
|
|
96
|
+
client: SanityClientLike | SanityProjectDetails | SanityModernClientLike;
|
|
97
|
+
image: SanityImageSource;
|
|
98
|
+
metadata?: Metadata;
|
|
99
|
+
widths?: number[];
|
|
100
|
+
quality?: number;
|
|
101
|
+
aspectRatio?: number;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface ImagePropsReturn {
|
|
105
|
+
src: string;
|
|
106
|
+
srcset?: string;
|
|
107
|
+
naturalWidth?: number;
|
|
108
|
+
naturalHeight?: number;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
export function imageProps({
|
|
112
|
+
client,
|
|
113
|
+
image,
|
|
114
|
+
metadata,
|
|
115
|
+
widths,
|
|
116
|
+
quality = defaultQuality,
|
|
117
|
+
aspectRatio,
|
|
118
|
+
}: ImagePropsOptions): ImagePropsReturn {
|
|
119
|
+
const builder = imageUrlBuilder(client)
|
|
120
|
+
.image(image)
|
|
121
|
+
.quality(quality)
|
|
122
|
+
.auto("format");
|
|
123
|
+
const crop =
|
|
124
|
+
typeof image == "object" && "crop" in image && isCrop(image.crop)
|
|
125
|
+
? image.crop
|
|
126
|
+
: undefined;
|
|
127
|
+
const cropSize = metadata?.dimensions
|
|
128
|
+
? crop
|
|
129
|
+
? {
|
|
130
|
+
width: metadata.dimensions.width - crop.left - crop.right,
|
|
131
|
+
height: metadata.dimensions.height - crop.top - crop.bottom,
|
|
132
|
+
}
|
|
133
|
+
: metadata.dimensions
|
|
134
|
+
: undefined;
|
|
135
|
+
|
|
136
|
+
const naturalSize = cropSize
|
|
137
|
+
? aspectRatio
|
|
138
|
+
? fit({ width: 1, height: aspectRatio }, cropSize, "contain")
|
|
139
|
+
: cropSize
|
|
140
|
+
: undefined;
|
|
141
|
+
const url = buildAspectRatio(builder, lowResWidth, aspectRatio).url();
|
|
142
|
+
return {
|
|
143
|
+
src: widths ? url : metadata?.lqip ?? url,
|
|
144
|
+
srcset: widths
|
|
145
|
+
? Array.from(widths)
|
|
146
|
+
.sort((a, b) => a - b)
|
|
147
|
+
.map(
|
|
148
|
+
(width) =>
|
|
149
|
+
`${buildAspectRatio(builder, width, aspectRatio).url()} ${width}w`
|
|
150
|
+
)
|
|
151
|
+
.join(",")
|
|
152
|
+
: undefined,
|
|
153
|
+
naturalWidth: naturalSize?.width,
|
|
154
|
+
naturalHeight: naturalSize?.height,
|
|
155
|
+
};
|
|
156
|
+
}
|
package/dist/dev.cjs
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var imageUrlBuilder = require('@sanity/image-url');
|
|
4
|
-
|
|
5
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
|
-
|
|
7
|
-
var imageUrlBuilder__default = /*#__PURE__*/_interopDefault(imageUrlBuilder);
|
|
8
|
-
|
|
9
|
-
// src/index.ts
|
|
10
|
-
exports.defaultWidths = [
|
|
11
|
-
6016,
|
|
12
|
-
// 6K
|
|
13
|
-
5120,
|
|
14
|
-
// 5K
|
|
15
|
-
4480,
|
|
16
|
-
// 4.5K
|
|
17
|
-
3840,
|
|
18
|
-
// 4K
|
|
19
|
-
3200,
|
|
20
|
-
// QHD+
|
|
21
|
-
2560,
|
|
22
|
-
// WQXGA
|
|
23
|
-
2048,
|
|
24
|
-
// QXGA
|
|
25
|
-
1920,
|
|
26
|
-
// 1080p
|
|
27
|
-
1668,
|
|
28
|
-
// iPad
|
|
29
|
-
1280,
|
|
30
|
-
// 720p
|
|
31
|
-
1080,
|
|
32
|
-
// iPhone 6-8 Plus
|
|
33
|
-
960,
|
|
34
|
-
720,
|
|
35
|
-
// iPhone 6-8
|
|
36
|
-
640,
|
|
37
|
-
// 480p
|
|
38
|
-
480,
|
|
39
|
-
360,
|
|
40
|
-
240
|
|
41
|
-
];
|
|
42
|
-
exports.lowResWidth = 24;
|
|
43
|
-
exports.defaultMetaImageWidth = 1200;
|
|
44
|
-
exports.defaultQuality = 90;
|
|
45
|
-
var fitComparators = {
|
|
46
|
-
cover: Math.max,
|
|
47
|
-
contain: Math.min,
|
|
48
|
-
mean: (a, b) => (a + b) / 2
|
|
49
|
-
};
|
|
50
|
-
function fit(containee, container, mode) {
|
|
51
|
-
const sx = container.width / containee.width;
|
|
52
|
-
const sy = container.height / containee.height;
|
|
53
|
-
const s = fitComparators[mode](sx, sy);
|
|
54
|
-
return {
|
|
55
|
-
width: containee.width * s,
|
|
56
|
-
height: containee.height * s
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
function buildAspectRatio(builder, width, aspectRatio) {
|
|
60
|
-
if (aspectRatio) {
|
|
61
|
-
return builder.width(width).height(width * aspectRatio);
|
|
62
|
-
} else {
|
|
63
|
-
return builder.width(width);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
function imageProps({
|
|
67
|
-
image,
|
|
68
|
-
client,
|
|
69
|
-
widths,
|
|
70
|
-
quality = exports.defaultQuality,
|
|
71
|
-
aspectRatio
|
|
72
|
-
}) {
|
|
73
|
-
const sortedWidths = Array.from(widths).sort((a, b) => a - b);
|
|
74
|
-
const builder = imageUrlBuilder__default.default(client).image(image).quality(quality).auto("format");
|
|
75
|
-
const metadata = image.asset.metadata;
|
|
76
|
-
const cropSize = image.crop && metadata?.dimensions ? {
|
|
77
|
-
width: metadata.dimensions.width - image.crop.left - image.crop.right,
|
|
78
|
-
height: metadata.dimensions.height - image.crop.top - image.crop.bottom
|
|
79
|
-
} : void 0;
|
|
80
|
-
const naturalSize = cropSize ? aspectRatio ? fit({ width: 1, height: aspectRatio }, cropSize, "contain") : cropSize : metadata?.dimensions;
|
|
81
|
-
return {
|
|
82
|
-
src: metadata?.lqip ?? buildAspectRatio(builder, exports.lowResWidth, aspectRatio).url(),
|
|
83
|
-
srcset: sortedWidths.map(
|
|
84
|
-
(width) => `${buildAspectRatio(builder, width, aspectRatio).url()} ${width}w`
|
|
85
|
-
).join(","),
|
|
86
|
-
naturalWidth: naturalSize?.width,
|
|
87
|
-
naturalHeight: naturalSize?.height
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
function metaImageUrl({
|
|
91
|
-
image,
|
|
92
|
-
client,
|
|
93
|
-
width = exports.defaultMetaImageWidth,
|
|
94
|
-
quality = exports.defaultQuality
|
|
95
|
-
}) {
|
|
96
|
-
return imageUrlBuilder__default.default(client).image(image).quality(quality).auto("format").width(width).url();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
exports.imageProps = imageProps;
|
|
100
|
-
exports.metaImageUrl = metaImageUrl;
|
package/dist/dev.js
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import imageUrlBuilder from '@sanity/image-url';
|
|
2
|
-
|
|
3
|
-
// src/index.ts
|
|
4
|
-
var defaultWidths = [
|
|
5
|
-
6016,
|
|
6
|
-
// 6K
|
|
7
|
-
5120,
|
|
8
|
-
// 5K
|
|
9
|
-
4480,
|
|
10
|
-
// 4.5K
|
|
11
|
-
3840,
|
|
12
|
-
// 4K
|
|
13
|
-
3200,
|
|
14
|
-
// QHD+
|
|
15
|
-
2560,
|
|
16
|
-
// WQXGA
|
|
17
|
-
2048,
|
|
18
|
-
// QXGA
|
|
19
|
-
1920,
|
|
20
|
-
// 1080p
|
|
21
|
-
1668,
|
|
22
|
-
// iPad
|
|
23
|
-
1280,
|
|
24
|
-
// 720p
|
|
25
|
-
1080,
|
|
26
|
-
// iPhone 6-8 Plus
|
|
27
|
-
960,
|
|
28
|
-
720,
|
|
29
|
-
// iPhone 6-8
|
|
30
|
-
640,
|
|
31
|
-
// 480p
|
|
32
|
-
480,
|
|
33
|
-
360,
|
|
34
|
-
240
|
|
35
|
-
];
|
|
36
|
-
var lowResWidth = 24;
|
|
37
|
-
var defaultMetaImageWidth = 1200;
|
|
38
|
-
var defaultQuality = 90;
|
|
39
|
-
var fitComparators = {
|
|
40
|
-
cover: Math.max,
|
|
41
|
-
contain: Math.min,
|
|
42
|
-
mean: (a, b) => (a + b) / 2
|
|
43
|
-
};
|
|
44
|
-
function fit(containee, container, mode) {
|
|
45
|
-
const sx = container.width / containee.width;
|
|
46
|
-
const sy = container.height / containee.height;
|
|
47
|
-
const s = fitComparators[mode](sx, sy);
|
|
48
|
-
return {
|
|
49
|
-
width: containee.width * s,
|
|
50
|
-
height: containee.height * s
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
function buildAspectRatio(builder, width, aspectRatio) {
|
|
54
|
-
if (aspectRatio) {
|
|
55
|
-
return builder.width(width).height(width * aspectRatio);
|
|
56
|
-
} else {
|
|
57
|
-
return builder.width(width);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
function imageProps({
|
|
61
|
-
image,
|
|
62
|
-
client,
|
|
63
|
-
widths,
|
|
64
|
-
quality = defaultQuality,
|
|
65
|
-
aspectRatio
|
|
66
|
-
}) {
|
|
67
|
-
const sortedWidths = Array.from(widths).sort((a, b) => a - b);
|
|
68
|
-
const builder = imageUrlBuilder(client).image(image).quality(quality).auto("format");
|
|
69
|
-
const metadata = image.asset.metadata;
|
|
70
|
-
const cropSize = image.crop && metadata?.dimensions ? {
|
|
71
|
-
width: metadata.dimensions.width - image.crop.left - image.crop.right,
|
|
72
|
-
height: metadata.dimensions.height - image.crop.top - image.crop.bottom
|
|
73
|
-
} : void 0;
|
|
74
|
-
const naturalSize = cropSize ? aspectRatio ? fit({ width: 1, height: aspectRatio }, cropSize, "contain") : cropSize : metadata?.dimensions;
|
|
75
|
-
return {
|
|
76
|
-
src: metadata?.lqip ?? buildAspectRatio(builder, lowResWidth, aspectRatio).url(),
|
|
77
|
-
srcset: sortedWidths.map(
|
|
78
|
-
(width) => `${buildAspectRatio(builder, width, aspectRatio).url()} ${width}w`
|
|
79
|
-
).join(","),
|
|
80
|
-
naturalWidth: naturalSize?.width,
|
|
81
|
-
naturalHeight: naturalSize?.height
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
function metaImageUrl({
|
|
85
|
-
image,
|
|
86
|
-
client,
|
|
87
|
-
width = defaultMetaImageWidth,
|
|
88
|
-
quality = defaultQuality
|
|
89
|
-
}) {
|
|
90
|
-
return imageUrlBuilder(client).image(image).quality(quality).auto("format").width(width).url();
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
export { defaultMetaImageWidth, defaultQuality, defaultWidths, imageProps, lowResWidth, metaImageUrl };
|
package/dist/index.cjs
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var imageUrlBuilder = require('@sanity/image-url');
|
|
4
|
-
|
|
5
|
-
function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
|
|
6
|
-
|
|
7
|
-
var imageUrlBuilder__default = /*#__PURE__*/_interopDefault(imageUrlBuilder);
|
|
8
|
-
|
|
9
|
-
// src/index.ts
|
|
10
|
-
exports.defaultWidths = [
|
|
11
|
-
6016,
|
|
12
|
-
// 6K
|
|
13
|
-
5120,
|
|
14
|
-
// 5K
|
|
15
|
-
4480,
|
|
16
|
-
// 4.5K
|
|
17
|
-
3840,
|
|
18
|
-
// 4K
|
|
19
|
-
3200,
|
|
20
|
-
// QHD+
|
|
21
|
-
2560,
|
|
22
|
-
// WQXGA
|
|
23
|
-
2048,
|
|
24
|
-
// QXGA
|
|
25
|
-
1920,
|
|
26
|
-
// 1080p
|
|
27
|
-
1668,
|
|
28
|
-
// iPad
|
|
29
|
-
1280,
|
|
30
|
-
// 720p
|
|
31
|
-
1080,
|
|
32
|
-
// iPhone 6-8 Plus
|
|
33
|
-
960,
|
|
34
|
-
720,
|
|
35
|
-
// iPhone 6-8
|
|
36
|
-
640,
|
|
37
|
-
// 480p
|
|
38
|
-
480,
|
|
39
|
-
360,
|
|
40
|
-
240
|
|
41
|
-
];
|
|
42
|
-
exports.lowResWidth = 24;
|
|
43
|
-
exports.defaultMetaImageWidth = 1200;
|
|
44
|
-
exports.defaultQuality = 90;
|
|
45
|
-
var fitComparators = {
|
|
46
|
-
cover: Math.max,
|
|
47
|
-
contain: Math.min,
|
|
48
|
-
mean: (a, b) => (a + b) / 2
|
|
49
|
-
};
|
|
50
|
-
function fit(containee, container, mode) {
|
|
51
|
-
const sx = container.width / containee.width;
|
|
52
|
-
const sy = container.height / containee.height;
|
|
53
|
-
const s = fitComparators[mode](sx, sy);
|
|
54
|
-
return {
|
|
55
|
-
width: containee.width * s,
|
|
56
|
-
height: containee.height * s
|
|
57
|
-
};
|
|
58
|
-
}
|
|
59
|
-
function buildAspectRatio(builder, width, aspectRatio) {
|
|
60
|
-
if (aspectRatio) {
|
|
61
|
-
return builder.width(width).height(width * aspectRatio);
|
|
62
|
-
} else {
|
|
63
|
-
return builder.width(width);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
function imageProps({
|
|
67
|
-
image,
|
|
68
|
-
client,
|
|
69
|
-
widths,
|
|
70
|
-
quality = exports.defaultQuality,
|
|
71
|
-
aspectRatio
|
|
72
|
-
}) {
|
|
73
|
-
const sortedWidths = Array.from(widths).sort((a, b) => a - b);
|
|
74
|
-
const builder = imageUrlBuilder__default.default(client).image(image).quality(quality).auto("format");
|
|
75
|
-
const metadata = image.asset.metadata;
|
|
76
|
-
const cropSize = image.crop && metadata?.dimensions ? {
|
|
77
|
-
width: metadata.dimensions.width - image.crop.left - image.crop.right,
|
|
78
|
-
height: metadata.dimensions.height - image.crop.top - image.crop.bottom
|
|
79
|
-
} : void 0;
|
|
80
|
-
const naturalSize = cropSize ? aspectRatio ? fit({ width: 1, height: aspectRatio }, cropSize, "contain") : cropSize : metadata?.dimensions;
|
|
81
|
-
return {
|
|
82
|
-
src: metadata?.lqip ?? buildAspectRatio(builder, exports.lowResWidth, aspectRatio).url(),
|
|
83
|
-
srcset: sortedWidths.map(
|
|
84
|
-
(width) => `${buildAspectRatio(builder, width, aspectRatio).url()} ${width}w`
|
|
85
|
-
).join(","),
|
|
86
|
-
naturalWidth: naturalSize?.width,
|
|
87
|
-
naturalHeight: naturalSize?.height
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
function metaImageUrl({
|
|
91
|
-
image,
|
|
92
|
-
client,
|
|
93
|
-
width = exports.defaultMetaImageWidth,
|
|
94
|
-
quality = exports.defaultQuality
|
|
95
|
-
}) {
|
|
96
|
-
return imageUrlBuilder__default.default(client).image(image).quality(quality).auto("format").width(width).url();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
exports.imageProps = imageProps;
|
|
100
|
-
exports.metaImageUrl = metaImageUrl;
|