fix(studio): hold photos back only in the export's sheets

The print-mode photos held their source back for the print view, but the
assistant also draws pages in print mode and needs its photos loaded. Only
the export's sheets now ask for it, the loader reads a minimal print
document like whenReady, and the new test helpers stay within the client's
TypeScript target and lint baseline.
This commit is contained in:
Maurice
2026-11-16 10:47:50 +01:00
parent 1a50e7f0ac
commit eb56e77600
6 changed files with 32 additions and 14 deletions
@@ -1,5 +1,6 @@
import type { BookDocument } from '@trek/shared'
import { SpreadView } from './SpreadView'
import { DeferPrintPhotos } from './bookRender'
import { MARK_LENGTH, MARK_WEIGHT, sheetBox, sheetsFor, type Sheet, type SheetMode, foliosOf } from './bookSheets'
/**
@@ -104,13 +105,15 @@ function SheetView({
height: `${sheet.height}mm`,
}}
>
<SpreadView
spread={sheet.spread}
page={doc.page}
folios={foliosOf(doc.spreads, sheet.spreadIndex, doc.page.pageNumbers.startAt)}
big
print
/>
<DeferPrintPhotos.Provider value>
<SpreadView
spread={sheet.spread}
page={doc.page}
folios={foliosOf(doc.spreads, sheet.spreadIndex, doc.page.pageNumbers.startAt)}
big
print
/>
</DeferPrintPhotos.Provider>
</div>
</div>
+4 -3
View File
@@ -1,8 +1,8 @@
import { photoLookOf, type BookFrame, type BookPhotoElement, type BookShapeId, type PhotoLook } from '@trek/shared';
import type { CSSProperties, ReactNode } from 'react';
import { useContext, type CSSProperties, type ReactNode } from 'react';
import { fontStack } from './bookFonts';
import { PRINT_SRC_ATTR, photoSrc } from './bookRender';
import { DeferPrintPhotos, PRINT_SRC_ATTR, photoSrc } from './bookRender';
import { usePaintId } from './paint/usePaintId';
import { pictureFilter, silhouetteShadows, svgFilterMatrices, zoomPlacement } from './photo/photoLook';
import { photoClip } from './shapeOutline';
@@ -170,9 +170,10 @@ function Picture({
filterId: string | null;
}) {
const zoom = zoomPlacement(look.zoom, el.focalX, el.focalY);
const defer = useContext(DeferPrintPhotos) && print;
const img = (
<img
{...(print ? { [PRINT_SRC_ATTR]: photoSrc(photoId, big) } : { src: photoSrc(photoId, big) })}
{...(defer ? { [PRINT_SRC_ATTR]: photoSrc(photoId, big) } : { src: photoSrc(photoId, big) })}
alt=""
draggable={false}
// Lazy on the canvas, where a long book scrolls past; eager in print,
@@ -6,6 +6,8 @@
* exactly the kind of file you edit constantly while designing.
*/
import { createContext } from 'react'
// The typefaces live in bookFonts.ts (fontStack et al.) — a second stack map
// here is exactly how #2183 happened: the renderer kept a three-font copy
// while the picker grew to seven, and everything else fell back to Poppins.
@@ -22,6 +24,13 @@
*/
export const PRINT_SRC_ATTR = 'data-print-src'
/**
* Whether the photos drawn below hold their source back for the print view.
* Only the export's sheets say so: a print-mode page drawn anywhere else (the
* assistant's look at a page) loads its photos itself.
*/
export const DeferPrintPhotos = createContext(false)
export function photoSrc(photoId: number, big: boolean): string {
return `/api/photos/${photoId}/${big ? 'original' : 'thumbnail'}`
}
@@ -59,7 +59,7 @@ describe('loadPrintImages', () => {
expect(state.maxInFlight).toBe(3);
expect(imgs.every((img) => img.getAttribute('src')?.startsWith('/api/photos/'))).toBe(true);
expect(imgs.some((img) => img.hasAttribute(PRINT_SRC_ATTR))).toBe(false);
expect(progress.at(-1)).toBe('10/10');
expect(progress[progress.length - 1]).toBe('10/10');
});
it('FE-STUDIO-PRINTIMG-002: asks once more for a photo that failed', async () => {
+7 -2
View File
@@ -43,11 +43,16 @@ function loadOne(img: HTMLImageElement, src: string, timeoutMs: number): Promise
});
}
/** What `loadPrintImages` reads off the print document, as `whenReady` does. */
interface PrintDocument {
querySelectorAll?: (selector: string) => ArrayLike<Element>;
}
/** Resolves once every deferred photo in `doc` has loaded or been given up on. */
export async function loadPrintImages(doc: Document, options: PrintImageOptions = {}): Promise<void> {
export async function loadPrintImages(doc: PrintDocument, options: PrintImageOptions = {}): Promise<void> {
const concurrency = Math.max(1, options.concurrency ?? PRINT_IMAGES.concurrency);
const timeoutMs = options.timeoutMs ?? PRINT_IMAGES.timeoutMs;
const images = Array.from(doc.querySelectorAll<HTMLImageElement>(`img[${PRINT_SRC_ATTR}]`));
const images = Array.from(doc.querySelectorAll?.(`img[${PRINT_SRC_ATTR}]`) ?? []) as HTMLImageElement[];
const total = images.length;
let next = 0;
let done = 0;
@@ -222,6 +222,6 @@ describe('what the sheets are made of', () => {
// worth waiting for. Held back from `src` until the print view loads it a
// few at a time (#3102).
expect(img!.getAttribute('data-print-src')).toContain('/original')
expect(img!.hasAttribute('src')).toBe(false)
expect(img?.hasAttribute('src')).toBe(false)
})
})