-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: export SanityClientOrProjectDetails type for the strict build …
…checks.
- Loading branch information
1 parent
bcc62b4
commit f13fcc8
Showing
4 changed files
with
125 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters