@nonphoto/sanity-image 0.1.0 → 1.0.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/dist/index.d.ts +12 -24
- package/dist/index.js +14 -19
- package/package.json +1 -1
- package/src/index.ts +45 -57
package/dist/index.d.ts
CHANGED
|
@@ -1,41 +1,29 @@
|
|
|
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
|
-
declare function imageProps({ image,
|
|
23
|
-
image: SanityImageObjectWithMetadata;
|
|
15
|
+
declare function imageProps({ client, image, metadata, widths, quality, aspectRatio, }: {
|
|
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
|
}): {
|
|
29
23
|
src: string;
|
|
30
|
-
srcset
|
|
24
|
+
srcset?: string;
|
|
31
25
|
naturalWidth?: number;
|
|
32
26
|
naturalHeight?: number;
|
|
33
27
|
};
|
|
34
|
-
declare function metaImageUrl({ image, client, width, quality, }: {
|
|
35
|
-
image: SanityImageObjectWithMetadata;
|
|
36
|
-
client: SanityClientLike | SanityProjectDetails | SanityModernClientLike;
|
|
37
|
-
width: number;
|
|
38
|
-
quality?: number;
|
|
39
|
-
}): string;
|
|
40
28
|
|
|
41
|
-
export { type
|
|
29
|
+
export { type Metadata, type Size, defaultMetaImageWidth, defaultQuality, defaultWidths, imageProps, lowResWidth };
|
package/dist/index.js
CHANGED
|
@@ -56,43 +56,38 @@ function buildAspectRatio(builder, width, aspectRatio) {
|
|
|
56
56
|
return builder.width(width);
|
|
57
57
|
}
|
|
58
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
|
+
}
|
|
59
62
|
function imageProps({
|
|
60
|
-
image,
|
|
61
63
|
client,
|
|
64
|
+
image,
|
|
65
|
+
metadata,
|
|
62
66
|
widths,
|
|
63
67
|
quality = defaultQuality,
|
|
64
68
|
aspectRatio
|
|
65
69
|
}) {
|
|
66
|
-
const sortedWidths = Array.from(widths).sort((a, b) => a - b);
|
|
67
70
|
const builder = imageUrlBuilder(client).image(image).quality(quality).auto("format");
|
|
68
|
-
const
|
|
69
|
-
const cropSize = metadata?.dimensions ?
|
|
70
|
-
width: metadata.dimensions.width -
|
|
71
|
-
height: metadata.dimensions.height -
|
|
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
|
|
72
75
|
} : metadata.dimensions : void 0;
|
|
73
76
|
const naturalSize = cropSize ? aspectRatio ? fit({ width: 1, height: aspectRatio }, cropSize, "contain") : cropSize : void 0;
|
|
77
|
+
const url = buildAspectRatio(builder, lowResWidth, aspectRatio).url();
|
|
74
78
|
return {
|
|
75
|
-
src: metadata?.lqip ??
|
|
76
|
-
srcset:
|
|
79
|
+
src: widths ? url : metadata?.lqip ?? url,
|
|
80
|
+
srcset: widths ? Array.from(widths).sort((a, b) => a - b).map(
|
|
77
81
|
(width) => `${buildAspectRatio(builder, width, aspectRatio).url()} ${width}w`
|
|
78
|
-
).join(","),
|
|
82
|
+
).join(",") : void 0,
|
|
79
83
|
naturalWidth: naturalSize?.width,
|
|
80
84
|
naturalHeight: naturalSize?.height
|
|
81
85
|
};
|
|
82
86
|
}
|
|
83
|
-
function metaImageUrl({
|
|
84
|
-
image,
|
|
85
|
-
client,
|
|
86
|
-
width = defaultMetaImageWidth,
|
|
87
|
-
quality = defaultQuality
|
|
88
|
-
}) {
|
|
89
|
-
return imageUrlBuilder(client).image(image).quality(quality).auto("format").width(width).url();
|
|
90
|
-
}
|
|
91
87
|
export {
|
|
92
88
|
defaultMetaImageWidth,
|
|
93
89
|
defaultQuality,
|
|
94
90
|
defaultWidths,
|
|
95
91
|
imageProps,
|
|
96
|
-
lowResWidth
|
|
97
|
-
metaImageUrl
|
|
92
|
+
lowResWidth
|
|
98
93
|
};
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,32 +1,23 @@
|
|
|
1
1
|
import imageUrlBuilder from "@sanity/image-url";
|
|
2
2
|
import { ImageUrlBuilder } from "@sanity/image-url/lib/types/builder";
|
|
3
3
|
import type {
|
|
4
|
-
SanityAsset,
|
|
5
4
|
SanityClientLike,
|
|
6
5
|
SanityImageCrop,
|
|
7
6
|
SanityImageDimensions,
|
|
8
|
-
|
|
7
|
+
SanityImageSource,
|
|
9
8
|
SanityModernClientLike,
|
|
10
9
|
SanityProjectDetails,
|
|
11
10
|
} from "@sanity/image-url/lib/types/types.js";
|
|
12
11
|
|
|
13
|
-
export
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
dimensions?: SanityImageDimensions;
|
|
17
|
-
};
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export interface SanityImageObjectWithMetadata {
|
|
21
|
-
asset: SanityAssetWithMetadata;
|
|
22
|
-
crop?: SanityImageCrop;
|
|
23
|
-
hotspot?: SanityImageHotspot;
|
|
12
|
+
export interface Metadata {
|
|
13
|
+
lqip?: string;
|
|
14
|
+
dimensions?: SanityImageDimensions;
|
|
24
15
|
}
|
|
25
16
|
|
|
26
|
-
export
|
|
17
|
+
export interface Size {
|
|
27
18
|
width: number;
|
|
28
19
|
height: number;
|
|
29
|
-
}
|
|
20
|
+
}
|
|
30
21
|
|
|
31
22
|
export const defaultWidths = [
|
|
32
23
|
6016, // 6K
|
|
@@ -86,39 +77,54 @@ function buildAspectRatio(
|
|
|
86
77
|
}
|
|
87
78
|
}
|
|
88
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
|
+
|
|
89
95
|
export function imageProps({
|
|
90
|
-
image,
|
|
91
96
|
client,
|
|
97
|
+
image,
|
|
98
|
+
metadata,
|
|
92
99
|
widths,
|
|
93
100
|
quality = defaultQuality,
|
|
94
101
|
aspectRatio,
|
|
95
102
|
}: {
|
|
96
|
-
image: SanityImageObjectWithMetadata;
|
|
97
103
|
client: SanityClientLike | SanityProjectDetails | SanityModernClientLike;
|
|
98
|
-
|
|
104
|
+
image: SanityImageSource;
|
|
105
|
+
metadata?: Metadata;
|
|
106
|
+
widths?: number[];
|
|
99
107
|
quality?: number;
|
|
100
108
|
aspectRatio?: number;
|
|
101
109
|
}): {
|
|
102
110
|
src: string;
|
|
103
|
-
srcset
|
|
111
|
+
srcset?: string;
|
|
104
112
|
naturalWidth?: number;
|
|
105
113
|
naturalHeight?: number;
|
|
106
114
|
} {
|
|
107
|
-
const sortedWidths = Array.from(widths).sort((a, b) => a - b);
|
|
108
|
-
|
|
109
115
|
const builder = imageUrlBuilder(client)
|
|
110
116
|
.image(image)
|
|
111
117
|
.quality(quality)
|
|
112
118
|
.auto("format");
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
119
|
+
const crop =
|
|
120
|
+
typeof image == "object" && "crop" in image && isCrop(image.crop)
|
|
121
|
+
? image.crop
|
|
122
|
+
: undefined;
|
|
116
123
|
const cropSize = metadata?.dimensions
|
|
117
|
-
?
|
|
124
|
+
? crop
|
|
118
125
|
? {
|
|
119
|
-
width: metadata.dimensions.width -
|
|
120
|
-
height:
|
|
121
|
-
metadata.dimensions.height - image.crop.top - image.crop.bottom,
|
|
126
|
+
width: metadata.dimensions.width - crop.left - crop.right,
|
|
127
|
+
height: metadata.dimensions.height - crop.top - crop.bottom,
|
|
122
128
|
}
|
|
123
129
|
: metadata.dimensions
|
|
124
130
|
: undefined;
|
|
@@ -128,37 +134,19 @@ export function imageProps({
|
|
|
128
134
|
? fit({ width: 1, height: aspectRatio }, cropSize, "contain")
|
|
129
135
|
: cropSize
|
|
130
136
|
: undefined;
|
|
131
|
-
|
|
137
|
+
const url = buildAspectRatio(builder, lowResWidth, aspectRatio).url();
|
|
132
138
|
return {
|
|
133
|
-
src:
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
139
|
+
src: widths ? url : metadata?.lqip ?? url,
|
|
140
|
+
srcset: widths
|
|
141
|
+
? Array.from(widths)
|
|
142
|
+
.sort((a, b) => a - b)
|
|
143
|
+
.map(
|
|
144
|
+
(width) =>
|
|
145
|
+
`${buildAspectRatio(builder, width, aspectRatio).url()} ${width}w`
|
|
146
|
+
)
|
|
147
|
+
.join(",")
|
|
148
|
+
: undefined,
|
|
142
149
|
naturalWidth: naturalSize?.width,
|
|
143
150
|
naturalHeight: naturalSize?.height,
|
|
144
151
|
};
|
|
145
152
|
}
|
|
146
|
-
|
|
147
|
-
export function metaImageUrl({
|
|
148
|
-
image,
|
|
149
|
-
client,
|
|
150
|
-
width = defaultMetaImageWidth,
|
|
151
|
-
quality = defaultQuality,
|
|
152
|
-
}: {
|
|
153
|
-
image: SanityImageObjectWithMetadata;
|
|
154
|
-
client: SanityClientLike | SanityProjectDetails | SanityModernClientLike;
|
|
155
|
-
width: number;
|
|
156
|
-
quality?: number;
|
|
157
|
-
}) {
|
|
158
|
-
return imageUrlBuilder(client)
|
|
159
|
-
.image(image)
|
|
160
|
-
.quality(quality)
|
|
161
|
-
.auto("format")
|
|
162
|
-
.width(width)
|
|
163
|
-
.url();
|
|
164
|
-
}
|