Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loosen hotspot & crop types to match sanity typegen #54

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ export type ImageWithPreviewProps<T extends React.ElementType> = {
React.ComponentPropsWithRef<T>

export type CropData = {
bottom: number
left: number
right: number
top: number
top?: number
bottom?: number
left?: number
right?: number
}

export type HotspotData = {
x: number
y: number
x?: number
y?: number
}

export type Asset = {
Expand Down Expand Up @@ -165,7 +165,7 @@ export type ImageQueryInputs = {
* The hotspot coordinates to use for the image. Note: hotspot `width` and
* `height` are not used.
*/
hotspot?: { x: number; y: number }
hotspot?: HotspotData

/** The crop coordinates to use for the image. */
crop?: CropData
Expand Down
27 changes: 18 additions & 9 deletions src/urlBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,13 @@ export const buildQueryParams = ({
// Hotspot is relative to post-`rect` dimensions; if `crop` is present,
// the hotspot inputs need to be adjusted accordingly
const x = crop
? (hotspot.x - crop.left) / (1 - crop.left - crop.right)
: hotspot.x
? ((hotspot.x ?? 0) - (crop.left ?? 0)) /
(1 - (crop.left ?? 0) - (crop.right ?? 0))
: hotspot.x ?? 0
const y = crop
? (hotspot.y - crop.top) / (1 - crop.top - crop.bottom)
: hotspot.y
? ((hotspot.y ?? 0) - (crop.top ?? 0)) /
(1 - (crop.top ?? 0) - (crop.bottom ?? 0))
: hotspot.y ?? 0

params["fp-x"] = roundWithPrecision(clamp(x, 0, 1), 3)
params["fp-y"] = roundWithPrecision(clamp(y, 0, 1), 3)
Expand Down Expand Up @@ -252,14 +254,21 @@ export const croppedImageSize = (
dimensions: { width: number; height: number },
crop: CropData
): ImageIdParts["dimensions"] => {
if (crop.left + crop.right >= 1 || crop.top + crop.bottom >= 1) {
if (
(crop.left ?? 0) + (crop.right ?? 0) >= 1 ||
(crop.top ?? 0) + (crop.bottom ?? 0) >= 1
) {
throw new Error(
`Invalid crop: ${JSON.stringify(crop)}; crop values must be less than 1`
)
}

const width = Math.round(dimensions.width * (1 - crop.left - crop.right))
const height = Math.round(dimensions.height * (1 - crop.top - crop.bottom))
const width = Math.round(
dimensions.width * (1 - (crop.left ?? 0) - (crop.right ?? 0))
)
const height = Math.round(
dimensions.height * (1 - (crop.top ?? 0) - (crop.bottom ?? 0))
)
const aspectRatio = width / height

return { width, height, aspectRatio }
Expand All @@ -276,8 +285,8 @@ export const buildRect = (
const { width, height } = croppedImageSize(dimensions, crop)

return [
Math.round(crop.left * dimensions.width),
Math.round(crop.top * dimensions.height),
Math.round((crop.left ?? 0) * dimensions.width),
Math.round((crop.top ?? 0) * dimensions.height),
width,
height,
].join(",")
Expand Down