@nonphoto/sanity-image 0.1.1 → 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 CHANGED
@@ -1,41 +1,29 @@
1
- import { SanityAsset, SanityImageDimensions, SanityImageCrop, SanityImageHotspot, SanityClientLike, SanityProjectDetails, SanityModernClientLike } from '@sanity/image-url/lib/types/types.js';
1
+ import { SanityImageDimensions, SanityClientLike, SanityProjectDetails, SanityModernClientLike, SanityImageSource } from '@sanity/image-url/lib/types/types.js';
2
2
 
3
- type SanityAssetWithMetadata = SanityAsset & {
4
- metadata?: {
5
- lqip?: string;
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
- type Size = {
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, client, widths, quality, aspectRatio, }: {
23
- image: SanityImageObjectWithMetadata;
15
+ declare function imageProps({ client, image, metadata, widths, quality, aspectRatio, }: {
24
16
  client: SanityClientLike | SanityProjectDetails | SanityModernClientLike;
25
- widths: number[];
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: string;
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 { SanityAssetWithMetadata, SanityImageObjectWithMetadata, Size, defaultMetaImageWidth, defaultQuality, defaultWidths, imageProps, lowResWidth, metaImageUrl };
29
+ export { 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 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;
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 ?? buildAspectRatio(builder, lowResWidth, aspectRatio).url(),
77
- srcset: sortedWidths.map(
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
- 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 };
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.1",
3
+ "version": "1.0.0",
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
- "development": {
10
+ ".": {
30
11
  "import": {
31
12
  "types": "./dist/index.d.ts",
32
- "default": "./dist/dev.js"
33
- },
34
- "require": "./dist/dev.cjs"
13
+ "default": "./dist/index.js"
14
+ }
35
15
  },
36
- "import": {
37
- "types": "./dist/index.d.ts",
38
- "default": "./dist/index.js"
39
- },
40
- "require": "./dist/index.cjs"
16
+ "./src/*": "./src/*"
41
17
  },
42
- "typesVersions": {},
43
- "main": "./dist/index.cjs",
18
+ "files": [
19
+ "./dist/*",
20
+ "./src/*"
21
+ ],
44
22
  "module": "./dist/index.js",
45
23
  "types": "./dist/index.d.ts",
46
24
  "scripts": {
47
- "dev": "vite serve dev",
48
- "build": "tsup",
49
- "test": "vitest",
50
- "format": "prettier --ignore-path .gitignore -w \"src/**/*.{js,ts,json,css,tsx,jsx}\" \"dev/**/*.{js,ts,json,css,tsx,jsx}\"",
51
- "update-deps": "pnpm up -Li",
52
- "typecheck": "tsc --noEmit"
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,152 @@
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 function imageProps({
96
+ client,
97
+ image,
98
+ metadata,
99
+ widths,
100
+ quality = defaultQuality,
101
+ aspectRatio,
102
+ }: {
103
+ client: SanityClientLike | SanityProjectDetails | SanityModernClientLike;
104
+ image: SanityImageSource;
105
+ metadata?: Metadata;
106
+ widths?: number[];
107
+ quality?: number;
108
+ aspectRatio?: number;
109
+ }): {
110
+ src: string;
111
+ srcset?: string;
112
+ naturalWidth?: number;
113
+ naturalHeight?: number;
114
+ } {
115
+ const builder = imageUrlBuilder(client)
116
+ .image(image)
117
+ .quality(quality)
118
+ .auto("format");
119
+ const crop =
120
+ typeof image == "object" && "crop" in image && isCrop(image.crop)
121
+ ? image.crop
122
+ : undefined;
123
+ const cropSize = metadata?.dimensions
124
+ ? crop
125
+ ? {
126
+ width: metadata.dimensions.width - crop.left - crop.right,
127
+ height: metadata.dimensions.height - crop.top - crop.bottom,
128
+ }
129
+ : metadata.dimensions
130
+ : undefined;
131
+
132
+ const naturalSize = cropSize
133
+ ? aspectRatio
134
+ ? fit({ width: 1, height: aspectRatio }, cropSize, "contain")
135
+ : cropSize
136
+ : undefined;
137
+ const url = buildAspectRatio(builder, lowResWidth, aspectRatio).url();
138
+ return {
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,
149
+ naturalWidth: naturalSize?.width,
150
+ naturalHeight: naturalSize?.height,
151
+ };
152
+ }
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;