Mediabibliotheek fase 2/3a/3b: write-through, uitsnede per slide, leesschakelaar, hero-foto's in de bibliotheek #27

Merged
Sluijsens merged 14 commits from feature/media-library-phase2 into master 2026-11-18 08:24:41 +01:00
3 changed files with 30 additions and 29 deletions
Showing only changes of commit ed0b6ad61c - Show all commits
@@ -18,11 +18,13 @@ interface FocusDialogProps {
/** 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". */
/** "Use the crop of the photo again": applied at once, the dialog stays open. The button is always shown. */
onReset?: () => void;
/** The slide has a crop of its own; without one there is nothing to reset and the button is disabled. */
canReset?: boolean;
}
export function FocusDialog({ image, onApply, onClose, onReset }: FocusDialogProps) {
export function FocusDialog({ image, onApply, onClose, onReset, canReset = false }: FocusDialogProps) {
const { t } = useTranslation();
return (
<Dialog open={image !== null} onOpenChange={(next) => { if (!next) onClose(); }}>
@@ -31,7 +33,8 @@ export function FocusDialog({ image, onApply, onClose, onReset }: FocusDialogPro
<DialogTitle>{t('hero.focus.title')}</DialogTitle>
</DialogHeader>
{/* Mounted only while open, so every opening starts from the stored values. */}
{image && <FocusEditor image={image} onApply={onApply} onClose={onClose} onReset={onReset} />}
{/* Keyed by the crop it shows, so after a reset it shows the photo's crop. */}
{image && <FocusEditor key={JSON.stringify([image.focus, image.wideFocus, image.tallFocus])} image={image} onApply={onApply} onClose={onClose} onReset={onReset} canReset={canReset} />}
</DialogContent>
</Dialog>
);
@@ -39,7 +42,7 @@ export function FocusDialog({ image, onApply, onClose, onReset }: FocusDialogPro
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 }) {
function FocusEditor({ image, onApply, onClose, onReset, canReset }: { image: HeroImageDto; onApply: (imageId: string, request: HeroFocusRequest) => void; onClose: () => void; onReset?: () => void; canReset: boolean }) {
const { t } = useTranslation();
const area = useRef<HTMLDivElement>(null);
// The whole picture: image.src is already a crop of the wide shape and would be shown stretched and out of place.
@@ -238,16 +241,7 @@ function FocusEditor({ image, onApply, onClose, onReset }: { image: HeroImageDto
<div className="flex flex-wrap justify-end gap-2">
{onReset && (
<Button
type="button"
variant="ghost"
className="mr-auto"
onClick={() => {
onReset();
onClose();
}}
data-testid="focus-reset"
>
<Button type="button" variant="outline" className="mr-auto" disabled={!canReset} onClick={onReset} data-testid="focus-reset">
{t('hero.focus.reset')}
</Button>
)}
@@ -1315,26 +1315,36 @@ describe('Add a slide with a photo from the library or an upload', () => {
expect(screen.queryByTestId('hero-slide-0-shared-crop')).not.toBeInTheDocument();
});
it('offers to go back to the crop of the photo for a slide with a crop of its own', async () => {
it('shows the reset button always: it applies at once and keeps the dialog open for a slide with its own crop', async () => {
const one = slide(1);
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [{ ...one, ownCrop: true }, slide(2)] });
await openPage();
await userEvent.click(screen.getByTestId('hero-slide-0-focus'));
await screen.findByTestId('focus-dialog');
await userEvent.click(screen.getByTestId('focus-reset'));
const dialog = await screen.findByTestId('focus-dialog');
expect(within(dialog).getByTestId('focus-reset')).toBeEnabled();
expect(within(dialog).queryByTestId('focus-apply-hint')).not.toBeInTheDocument();
await userEvent.click(within(dialog).getByTestId('focus-reset'));
await waitFor(() => expect(getResetSlideRequests()).toEqual([one.id]));
});
expect(screen.getByTestId('focus-dialog')).toBeInTheDocument();
await waitFor(() => expect(within(screen.getByTestId('focus-dialog')).getByTestId('focus-reset')).toBeDisabled());
});
it('does not offer the reset for a slide that uses the crop of its photo', async () => {
it('shows the reset button disabled for a slide that uses the crop of its photo, and Cancel changes nothing', async () => {
setMockHero({ ...emptyHero(), id: 'h1', title: 'T', slides: [slide(1)] });
await openPage();
await userEvent.click(screen.getByTestId('hero-slide-0-focus'));
await screen.findByTestId('focus-dialog');
const dialog = await screen.findByTestId('focus-dialog');
expect(screen.queryByTestId('focus-reset')).not.toBeInTheDocument();
expect(within(dialog).getByTestId('focus-reset')).toBeVisible();
expect(within(dialog).getByTestId('focus-reset')).toBeDisabled();
fireEvent.change(within(dialog).getByTestId('focus-x'), { target: { value: '10' } });
await userEvent.click(within(dialog).getByTestId('focus-cancel'));
await waitFor(() => expect(screen.queryByTestId('focus-dialog')).not.toBeInTheDocument());
expect(getFocusRequests()).toHaveLength(0);
expect(getResetSlideRequests()).toHaveLength(0);
});
it('asks to save a new slide first before its crop can be changed', async () => {
@@ -690,14 +690,11 @@ function HeroEditor({ hero }: { hero: HeroAdminDto }) {
image={focusSlideIndex !== null ? imageOfSlide(focusSlideIndex) ?? null : null}
onApply={(_imageId, request) => focusSlideIndex !== null && applyFocus(focusSlideIndex, request)}
onClose={() => setFocusSlideIndex(null)}
onReset={
focusSlideIndex !== null && savedSlideOf(focusSlideIndex)?.ownCrop
? () => {
const slideId = watchedSlides[focusSlideIndex]?.id;
if (slideId) resetSlideFocus.mutate(slideId, { onError: (error) => setServerError(serverMessage(error, t('errors.generic'), t('errors.network'))) });
}
: undefined
}
canReset={focusSlideIndex !== null && Boolean(savedSlideOf(focusSlideIndex)?.ownCrop)}
onReset={() => {
const slideId = focusSlideIndex !== null ? watchedSlides[focusSlideIndex]?.id : undefined;
if (slideId) resetSlideFocus.mutate(slideId, { onError: (error) => setServerError(serverMessage(error, t('errors.generic'), t('errors.network'))) });
}}
/>
</form>
);