/* eslint-disable jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions, jsx-a11y/img-redundant-alt */ import { FileBrowser } from '@components/admin/FileBrowser.js'; import { InputField } from '@components/common/form/InputField.js'; import { Button } from '@components/common/ui/Button.js'; import React from 'react'; import { useFormContext } from 'react-hook-form'; type AlignmentType = 'left' | 'center' | 'right'; interface BannerSettingProps { bannerWidget: { src: string; alignment: AlignmentType; width: number; height: number; alt: string; link?: string; }; } export default function BannerSetting({ bannerWidget: { src, alignment = 'left', width, height, alt, link } }: BannerSettingProps) { const { setValue, watch } = useFormContext(); const image = watch('settings.src', src); const currentAlignment = watch('settings.alignment', alignment); const [openFileBrowser, setOpenFileBrowser] = React.useState(false); const [imageDimensions, setImageDimensions] = React.useState({ width: width || 0, height: height || 0 }); // Function to get image dimensions const getImageDimensions = (imageUrl: string) => { if (!imageUrl) return; const img = new Image(); img.onload = () => { const newWidth = img.naturalWidth; const newHeight = img.naturalHeight; setImageDimensions({ width: newWidth, height: newHeight }); // Update form values setValue('settings.width', newWidth); setValue('settings.height', newHeight); }; img.src = imageUrl; }; // Get dimensions when image changes React.useEffect(() => { if (image) { getImageDimensions(image); } }, [image]); return (
{openFileBrowser && ( { setValue('settings.src', file); setOpenFileBrowser(false); }} close={() => setOpenFileBrowser(false)} /> )}
{/* Add a semi-transparent grid background to better visualize alignment */}
{/* Apply alignment to a container that only takes necessary width */}
{image && (
{ // This is a backup in case the useEffect doesn't trigger const img = e.target as HTMLImageElement; if (img.naturalWidth > 0 && img.naturalHeight > 0) { if ( imageDimensions.width !== img.naturalWidth || imageDimensions.height !== img.naturalHeight ) { setImageDimensions({ width: img.naturalWidth, height: img.naturalHeight }); setValue('settings.width', img.naturalWidth); setValue('settings.height', img.naturalHeight); } } }} alt="Banner Image" />
)}
{ setValue('settings.alignment', 'left'); }} className={`border p-3 flex justify-center items-center cursor-pointer ${ currentAlignment === 'left' ? 'border-blue-500 bg-blue-100' : 'border-gray-300' }`} >
{ setValue('settings.alignment', 'center'); }} className={`border p-3 flex justify-center items-center cursor-pointer ${ currentAlignment === 'center' ? 'border-blue-500 bg-blue-100' : 'border-gray-300' }`} >
{ setValue('settings.alignment', 'right'); }} className={`border p-3 flex justify-center items-center cursor-pointer ${ currentAlignment === 'right' ? 'border-blue-500 bg-blue-100' : 'border-gray-300' }`} >
{/* Hidden width and height fields - automatically populated */} {/* Display image dimensions as information */}
Image dimensions: {imageDimensions.width} × {imageDimensions.height}{' '} pixels
); } export const query = ` query Query($src: String, $alignment: String, $width: Float, $height: Float, $alt: String) { bannerWidget(src: $src, alignment: $alignment, width: $width, height: $height, alt: $alt) { src alignment width height alt } } `; export const variables = `{ src: getWidgetSetting("src"), alignment: getWidgetSetting("alignment"), width: getWidgetSetting("width"), height: getWidgetSetting("height"), alt: getWidgetSetting("alt") }`;