Skip to content

Commit

Permalink
chore: export SanityClientOrProjectDetails type for the strict build …
Browse files Browse the repository at this point in the history
…checks.
  • Loading branch information
lorenzodejong committed Nov 13, 2023
1 parent bcc62b4 commit f13fcc8
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 8 deletions.
113 changes: 113 additions & 0 deletions dev/null
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
var __getOwnPropNames = Object.getOwnPropertyNames;
var __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};

// dist/index.cjs
var require_dist = __commonJS({
"dist/index.cjs"(exports) {
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var imageUrlBuilder = require("@sanity/image-url");
var react = require("react");
function _interopDefaultCompat(e) {
return e && typeof e === "object" && "default" in e ? e : {
default: e
};
}
var imageUrlBuilder__default = /* @__PURE__ */ _interopDefaultCompat(imageUrlBuilder);
var DEFAULT_FALLBACK_IMAGE_QUALITY = 75;
var DEFAULT_IMAGE_BUILDER = (imageUrlBuilder2, options) => {
const result = imageUrlBuilder2.quality(options.quality || DEFAULT_FALLBACK_IMAGE_QUALITY).fit("clip");
if (options.width !== null) {
return result.width(options.width);
}
return result;
};
function getSanityRefId(image) {
if (typeof image === "string") {
return image;
}
const obj = image;
const ref = image;
const img = image;
if (obj.asset) {
return obj.asset._ref || obj.asset._id;
}
return ref._ref || img._id || "";
}
function getImageDimensions(id) {
const dimensions = id.split("-")[2];
const [width, height] = dimensions.split("x").map((num) => parseInt(num, 10));
const aspectRatio = width / height;
return {
width,
height,
aspectRatio
};
}
function getCroppedDimensions(image, baseDimensions) {
const crop = image.crop;
if (!crop) {
return baseDimensions;
}
const {
width,
height
} = baseDimensions;
const croppedWidth = width * (1 - (crop.left + crop.right));
const croppedHeight = height * (1 - (crop.top + crop.bottom));
return {
width: croppedWidth,
height: croppedHeight,
aspectRatio: croppedWidth / croppedHeight
};
}
function useNextSanityImage(sanityClient, image, options) {
const imageBuilder = (options == null ? void 0 : options.imageBuilder) || DEFAULT_IMAGE_BUILDER;
return react.useMemo(() => {
if (!image) {
return null;
}
const id = image ? getSanityRefId(image) : null;
if (!id) {
return null;
}
const originalImageDimensions = getImageDimensions(id);
const croppedImageDimensions = getCroppedDimensions(image, originalImageDimensions);
const loader = (_ref) => {
let {
width: width2,
quality
} = _ref;
return imageBuilder(imageUrlBuilder__default.default(sanityClient).image(image).auto("format"), {
width: width2,
originalImageDimensions,
croppedImageDimensions,
quality: quality || null
}).url() || "";
};
const baseImgBuilderInstance = imageBuilder(imageUrlBuilder__default.default(sanityClient).image(image).auto("format"), {
width: null,
originalImageDimensions,
croppedImageDimensions,
quality: null
});
const width = baseImgBuilderInstance.options.width || (baseImgBuilderInstance.options.maxWidth ? Math.min(baseImgBuilderInstance.options.maxWidth, croppedImageDimensions.width) : croppedImageDimensions.width);
const height = baseImgBuilderInstance.options.height || (baseImgBuilderInstance.options.maxHeight ? Math.min(baseImgBuilderInstance.options.maxHeight, croppedImageDimensions.height) : Math.round(width / croppedImageDimensions.aspectRatio));
return {
loader,
src: baseImgBuilderInstance.url(),
width,
height
};
}, [imageBuilder, image, sanityClient]);
}
exports.useNextSanityImage = useNextSanityImage;
}
});

// <stdin>
require_dist();
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export type {
ImageUrlBuilder,
SanityClientOrProjectDetails,
UseNextSanityImageBuilderOptions,
UseNextSanityImageBuilder,
UseNextSanityImageDimensions,
Expand Down
10 changes: 10 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
import { ImageUrlBuilder } from '@sanity/image-url/lib/types/builder';
import {
SanityClientLike,
SanityModernClientLike,
SanityProjectDetails
} from '@sanity/image-url/lib/types/types';
import { ImageLoader } from 'next/image';

export { ImageUrlBuilder } from '@sanity/image-url/lib/types/builder';

export type SanityClientOrProjectDetails =
| SanityClientLike
| SanityProjectDetails
| SanityModernClientLike;

export interface UseNextSanityImageDimensions {
width: number;
height: number;
Expand Down
9 changes: 1 addition & 8 deletions src/useNextSanityImage.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
import imageUrlBuilder from '@sanity/image-url';
import {
SanityAsset,
SanityClientLike,
SanityImageObject,
SanityImageSource,
SanityModernClientLike,
SanityProjectDetails,
SanityReference
} from '@sanity/image-url/lib/types/types';
import { ImageLoader } from 'next/image';
import { useMemo } from 'react';

import {
SanityClientOrProjectDetails,
UseNextSanityImageBuilder,
UseNextSanityImageDimensions,
UseNextSanityImageOptions,
Expand Down Expand Up @@ -78,11 +76,6 @@ export function getCroppedDimensions(
};
}

type SanityClientOrProjectDetails =
| SanityClientLike
| SanityProjectDetails
| SanityModernClientLike;

export function useNextSanityImage(
sanityClient: SanityClientOrProjectDetails,
image: SanityImageSource,
Expand Down

0 comments on commit f13fcc8

Please sign in to comment.