Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

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

Mauriceeb56e77600eb
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`,
}}
>
- <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>
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 = (
<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,
diff --git a/client/src/components/Studio/bookRender.ts b/client/src/components/Studio/bookRender.ts
index 45858dfa7..2d38b21f5 100644
--- a/client/src/components/Studio/bookRender.ts
+++ b/client/src/components/Studio/bookRender.ts
@@ -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'}`
}
diff --git a/client/src/components/Studio/printImages.test.ts b/client/src/components/Studio/printImages.test.ts
index 7658c22e4..7250558a1 100644
--- a/client/src/components/Studio/printImages.test.ts
+++ b/client/src/components/Studio/printImages.test.ts
@@ -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 () => {
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<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;
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)
})
})