| 1 | import type React from "react"; |
| 2 | |
| 3 | import { type RootImage as RootImageType } from "../../utils/parser"; |
| 4 | import { type ImageCropSettings } from "../../utils/types"; |
| 5 | |
| 6 | export const BASE_WIDTH_PERCENTAGE = "45%"; |
| 7 | export const BASE_HEIGHT = 384; |
| 8 | export const MIN_WIDTH_PERCENTAGE = 20; |
| 9 | export const MAX_WIDTH_PERCENTAGE = 80; |
| 10 | export const MIN_HEIGHT = 200; |
| 11 | export const MAX_HEIGHT = 800; |
| 12 | |
| 13 | export function getRootImageCropSettings( |
| 14 | image: RootImageType, |
| 15 | ): ImageCropSettings { |
| 16 | return ( |
| 17 | image.cropSettings ?? { |
| 18 | objectFit: "cover", |
| 19 | objectPosition: { x: 50, y: 50 }, |
| 20 | zoom: 1, |
| 21 | } |
| 22 | ); |
| 23 | } |
| 24 | |
| 25 | export function getRootImageObjectStyles( |
| 26 | image: RootImageType, |
| 27 | ): React.CSSProperties { |
| 28 | const cropSettings = getRootImageCropSettings(image); |
| 29 | |
| 30 | return { |
| 31 | objectFit: cropSettings.objectFit, |
| 32 | objectPosition: `${cropSettings.objectPosition.x}% ${cropSettings.objectPosition.y}%`, |
| 33 | transform: `scale(${cropSettings.zoom ?? 1})`, |
| 34 | transformOrigin: `${cropSettings.objectPosition.x}% ${cropSettings.objectPosition.y}%`, |
| 35 | height: "100%", |
| 36 | width: "100%", |
| 37 | display: "block", |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | export function getRootImageSizeStyle( |
| 42 | image: RootImageType, |
| 43 | layoutType?: string, |
| 44 | ): React.CSSProperties { |
| 45 | const hasExplicitHeight = Boolean(image.size?.h); |
| 46 | const hasExplicitWidth = Boolean(image.size?.w); |
| 47 | |
| 48 | if (!hasExplicitHeight && !hasExplicitWidth) { |
| 49 | if (layoutType === "vertical") { |
| 50 | return { height: BASE_HEIGHT, width: "100%" }; |
| 51 | } |
| 52 | return { width: BASE_WIDTH_PERCENTAGE, height: "auto" }; |
| 53 | } |
| 54 | |
| 55 | if (layoutType === "vertical") { |
| 56 | return { height: image.size?.h ?? BASE_HEIGHT, width: "100%" }; |
| 57 | } |
| 58 | |
| 59 | return { width: image.size?.w ?? BASE_WIDTH_PERCENTAGE, height: "auto" }; |
| 60 | } |
| 61 |