import { useRef, useState, type KeyboardEvent, type PointerEvent } from 'react'; import { useTranslation } from 'react-i18next'; import { Button } from '@/components/ui/button'; import { Input } from '@/components/ui/input'; import { CheckboxField } from '@/components/ui/checkbox'; import { RadioField } from '@/components/ui/radio'; import { Label } from '@/components/ui/label'; import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'; import { mediaUrl } from '../lib/media'; import { CENTRE, MAX_ZOOM, TALL, WIDE, cropFor, normalizeFocus, type Ratio } from '../lib/focusMath'; import type { FocusDto, HeroFocusRequest, HeroImageDto } from '../services/types'; type Target = 'general' | 'wide' | 'tall'; interface FocusDialogProps { /** The image to edit, with a not yet saved change of its focus already applied; null = closed. */ image: HeroImageDto | null; /** Keeps the change locally: it is sent and made live when the hero is saved, and dropped when the page is left. */ onApply: (imageId: string, request: HeroFocusRequest) => void; onClose: () => void; /** The slide has a crop of its own: offers "use the crop of the photo again". */ onReset?: () => void; } export function FocusDialog({ image, onApply, onClose, onReset }: FocusDialogProps) { const { t } = useTranslation(); return ( { if (!next) onClose(); }}> {t('hero.focus.title')} {/* Mounted only while open, so every opening starts from the stored values. */} {image && } ); } const percent = (fraction: number) => Math.round(fraction * 1000) / 10; function FocusEditor({ image, onApply, onClose, onReset }: { image: HeroImageDto; onApply: (imageId: string, request: HeroFocusRequest) => void; onClose: () => void; onReset?: () => void }) { const { t } = useTranslation(); const area = useRef(null); // The whole picture: image.src is already a crop of the wide shape and would be shown stretched and out of place. const previewUrl = image.previewSrc ? mediaUrl(image.previewSrc) : null; const [target, setTarget] = useState('general'); const [general, setGeneral] = useState(image.focus ?? CENTRE); const [wide, setWide] = useState(image.wideFocus); const [tall, setTall] = useState(image.tallFocus); const effective = { wide: wide ?? general, tall: tall ?? general }; const active: FocusDto = target === 'general' ? general : (target === 'wide' ? wide : tall) ?? general; const editable = target === 'general' || (target === 'wide' ? wide : tall) !== null; const update = (next: FocusDto) => { const clean = normalizeFocus(next); if (target === 'general') setGeneral(clean); else if (target === 'wide') setWide(clean); else setTall(clean); }; const toggleOverride = (checked: boolean) => { const value = checked ? { ...general } : null; if (target === 'wide') setWide(value); if (target === 'tall') setTall(value); }; const moveTo = (event: PointerEvent) => { const box = area.current?.getBoundingClientRect(); if (!box || box.width === 0 || box.height === 0 || !editable) return; update({ ...active, x: (event.clientX - box.left) / box.width, y: (event.clientY - box.top) / box.height }); }; const onKeyDown = (event: KeyboardEvent) => { if (!editable) return; const step = event.shiftKey ? 0.05 : 0.01; const moves: Record = { ArrowLeft: [-step, 0], ArrowRight: [step, 0], ArrowUp: [0, -step], ArrowDown: [0, step] }; const move = moves[event.key]; if (!move) return; event.preventDefault(); update({ ...active, x: active.x + move[0], y: active.y + move[1] }); }; const onSave = () => { onApply(image.id, { focus: general, wide, tall }); onClose(); }; const outline = (ratio: Ratio, focus: FocusDto) => { const c = cropFor(image.width, image.height, ratio, focus); return { left: `${(c.x / image.width) * 100}%`, top: `${(c.y / image.height) * 100}%`, width: `${(c.width / image.width) * 100}%`, height: `${(c.height / image.height) * 100}%`, }; }; return ( {t('hero.focus.intro')} {t('hero.focus.applyHint')} {t('hero.focus.targetLegend')} {(['general', 'wide', 'tall'] as const).map((value) => ( setTarget(value)} data-testid={`focus-target-${value}`} label={t(`hero.focus.target.${value}`)} /> ))} {target !== 'general' && ( toggleOverride(event.target.checked)} data-testid={`focus-override-${target}`} label={t('hero.focus.override')} /> )} { event.currentTarget.setPointerCapture?.(event.pointerId); moveTo(event); }} onPointerMove={(event) => { if (event.buttons === 1) moveTo(event); }} onKeyDown={onKeyDown} data-testid="focus-area" > {previewUrl ? ( ) : ( {t('hero.focus.previewPending')} )} {t('hero.focus.areaHint')} {t('hero.focus.x')} update({ ...active, x: Number(event.target.value) / 100 })} data-testid="focus-x" /> {t('hero.focus.y')} update({ ...active, y: Number(event.target.value) / 100 })} data-testid="focus-y" /> {t('hero.focus.zoom', { value: active.zoom.toFixed(2) })} update({ ...active, zoom: Number(event.target.value) })} className="w-full accent-primary disabled:cursor-not-allowed disabled:opacity-50" data-testid="focus-zoom" /> {([WIDE, TALL] as const).map((ratio) => { const crop = cropFor(image.width, image.height, ratio, effective[ratio.name]); return ( {t(`hero.focus.preview.${ratio.name}`)} {previewUrl && } ); })} {onReset && ( { onReset(); onClose(); }} data-testid="focus-reset" > {t('hero.focus.reset')} )} {t('common.cancel')} {t('hero.focus.apply')} ); }
{t('hero.focus.intro')}
{t('hero.focus.applyHint')}
{t('hero.focus.previewPending')}
{t('hero.focus.areaHint')}
{t(`hero.focus.preview.${ratio.name}`)}