fix(studio): finish preparing the Journey PDF export

A sandboxed print frame cannot run the callback that bounds the wait for print assets. Run the timer in the app so an already settled SVG image cannot keep the Save as PDF button disabled.

Closes #3309
This commit is contained in:
Maurice
2026-12-03 11:01:25 +01:00
parent 75020dd2df
commit 8ae919c650
2 changed files with 48 additions and 5 deletions
+9 -5
View File
@@ -98,18 +98,22 @@ function settle(el: Element): Promise<void> {
* worse than a marker that prints blank.
*/
export async function whenReady(iframe: HTMLIFrameElement): Promise<void> {
const win = iframe.contentWindow;
const doc = iframe.contentDocument as PrintDocument | null;
if (!win || !doc) return;
if (!doc) return;
const imgs = Array.from(doc.images);
const svg = svgImagesToWait(doc);
const images = imgs.map((img) => (img.complete ? Promise.resolve() : settle(img)));
const wait = win.setTimeout?.bind(win) ?? setTimeout;
// The sandbox blocks the frame's timer callbacks. Keep the deadline in the app.
let timeout: ReturnType<typeof setTimeout> | undefined;
const ceiling = new Promise<void>((resolve) => {
wait(resolve, printReadyCeilingMs(imgs.length + svg.length));
timeout = setTimeout(resolve, printReadyCeilingMs(imgs.length + svg.length));
});
await Promise.race([Promise.all([...images, ...svg.map(settle), doc.fonts?.ready ?? Promise.resolve()]), ceiling]);
try {
await Promise.race([Promise.all([...images, ...svg.map(settle), doc.fonts?.ready ?? Promise.resolve()]), ceiling]);
} finally {
clearTimeout(timeout);
}
}
@@ -1,4 +1,5 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'
import { PRINT_SRC_ATTR } from '../../../src/components/Studio/bookRender'
import { printSheets } from '../../../src/components/Studio/printSheets'
/**
@@ -269,6 +270,44 @@ describe('the save button', () => {
expect(saveButton().disabled).toBe(false)
})
it('enables saving after the last photo even when the sandbox blocks frame timers (#3309)', async () => {
vi.useFakeTimers()
const close = open()
try {
const iframe = frame()
const doc = document.implementation.createHTMLDocument('print')
doc.body.innerHTML = `<img ${PRINT_SRC_ATTR}="/photo.jpg"><svg><image href="/marker.jpg"/></svg>`
const photo = doc.images[0]
Object.defineProperties(photo, {
complete: { get: () => photo.hasAttribute('src') },
src: {
set: (src: string) => {
photo.setAttribute('src', src)
photo.dispatchEvent(new Event('load'))
},
},
})
Object.defineProperty(iframe, 'contentDocument', { value: doc })
// Chromium drops callbacks scheduled on the sandboxed window.
Object.defineProperty(iframe, 'contentWindow', { value: { setTimeout: () => 1 } })
// The eagerly loaded SVG image settles before the frame's load event.
doc.querySelector('image')?.dispatchEvent(new Event('load'))
iframe.dispatchEvent(new Event('load'))
await vi.advanceTimersByTimeAsync(0)
expect(saveButton().textContent).toBe('Preparing 1/1')
expect(saveButton().disabled).toBe(true)
await vi.advanceTimersByTimeAsync(8_000)
expect(saveButton().disabled).toBe(false)
expect(saveButton().textContent).toBe('Save as PDF')
expect(iframe.getAttribute('sandbox')).toBe('allow-same-origin allow-modals')
} finally {
close()
vi.useRealTimers()
}
})
it('prints the frame, not the page around it', async () => {
open()
const iframe = frame()