diff --git a/client/src/components/Studio/BookSheetsView.tsx b/client/src/components/Studio/BookSheetsView.tsx index 18100cd35..6019592c5 100644 --- a/client/src/components/Studio/BookSheetsView.tsx +++ b/client/src/components/Studio/BookSheetsView.tsx @@ -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`, }} > - + + + diff --git a/client/src/components/Studio/PhotoView.tsx b/client/src/components/Studio/PhotoView.tsx index 0b45eb63f..8c9af9fe4 100644 --- a/client/src/components/Studio/PhotoView.tsx +++ b/client/src/components/Studio/PhotoView.tsx @@ -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 = ( { 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 () => { diff --git a/client/src/components/Studio/printImages.ts b/client/src/components/Studio/printImages.ts index 8cb740531..40276f16e 100644 --- a/client/src/components/Studio/printImages.ts +++ b/client/src/components/Studio/printImages.ts @@ -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; +} + /** Resolves once every deferred photo in `doc` has loaded or been given up on. */ -export async function loadPrintImages(doc: Document, options: PrintImageOptions = {}): Promise { +export async function loadPrintImages(doc: PrintDocument, options: PrintImageOptions = {}): Promise { const concurrency = Math.max(1, options.concurrency ?? PRINT_IMAGES.concurrency); const timeoutMs = options.timeoutMs ?? PRINT_IMAGES.timeoutMs; - const images = Array.from(doc.querySelectorAll(`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; diff --git a/client/tests/unit/studio/BookSheetsView.test.tsx b/client/tests/unit/studio/BookSheetsView.test.tsx index 41e914ba2..bf48fd4f2 100644 --- a/client/tests/unit/studio/BookSheetsView.test.tsx +++ b/client/tests/unit/studio/BookSheetsView.test.tsx @@ -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) }) })