@@ -2334,19 +2334,19 @@ it("toggles a Mermaid attachment between diagram and source through the lazy pre
23342334 expect ( screen . container . querySelector ( '[role="alert"]' ) ) . toBeNull ( ) ;
23352335} ) ;
23362336
2337- it ( "navigates, searches, zooms, and selects mixed-height PDF pages" , async ( ) => {
2337+ function planningPdf (
2338+ titles = [ "Quarterly outlook" , "Delivery milestones" , "Budget and risks" ] ,
2339+ firstHeight = 140 ,
2340+ ) {
23382341 const objects = [
23392342 "<< /Type /Catalog /Pages 2 0 R >>" ,
2340- " << /Type /Pages /Count 3 /Kids [3 0 R 5 0 R 7 0 R] >>" ,
2343+ ` << /Type /Pages /Count ${ titles . length } /Kids [${ titles . map ( ( _ , index ) => ` ${ 3 + index * 2 } 0 R` ) . join ( " " ) } ] >>` ,
23412344 ] ;
2342- for ( const [ index , title ] of [
2343- "Quarterly outlook" ,
2344- "Delivery milestones" ,
2345- "Budget and risks" ,
2346- ] . entries ( ) ) {
2345+ const font = 3 + titles . length * 2 ;
2346+ for ( const [ index , title ] of titles . entries ( ) ) {
23472347 const text = `/GS1 gs BT /F1 24 Tf 30 ${ index === 0 ? 70 : 700 } Td ${ index === 0 ? "(Quarter) Tj /F2 24 Tf (ly outlook) Tj" : `(${ title } ) Tj` } ET` ;
23482348 objects . push (
2349- `<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 ${ index === 0 ? 140 : 792 } ] /Rotate ${ index === 2 ? 90 : 0 } /Resources << /Font << /F1 9 0 R /F2 10 0 R >> /ExtGState << /GS1 << /TR << /FunctionType 2 /Domain [0 1] /C0 [0] /C1 [1] /N 2 >> >> >> >> /Contents ${ 4 + index * 2 } 0 R >>` ,
2349+ `<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 ${ index === 0 ? firstHeight : 792 } ] /Rotate ${ index === 2 ? 90 : 0 } /Resources << /Font << /F1 ${ font } 0 R /F2 ${ font + 1 } 0 R >> /ExtGState << /GS1 << /TR << /FunctionType 2 /Domain [0 1] /C0 [0] /C1 [1] /N 2 >> >> >> >> /Contents ${ 4 + index * 2 } 0 R >>` ,
23502350 `<< /Length ${ text . length } >>\nstream\n${ text } \nendstream` ,
23512351 ) ;
23522352 }
@@ -2359,11 +2359,76 @@ it("navigates, searches, zooms, and selects mixed-height PDF pages", async () =>
23592359 source += `${ index + 1 } 0 obj\n${ object } \nendobj\n` ;
23602360 }
23612361 const xref = source . length ;
2362- source += `xref\n0 11 \n0000000000 65535 f \n${ offsets
2362+ source += `xref\n0 ${ objects . length + 1 } \n0000000000 65535 f \n${ offsets
23632363 . slice ( 1 )
23642364 . map ( ( offset ) => `${ String ( offset ) . padStart ( 10 , "0" ) } 00000 n \n` )
2365- . join ( "" ) } trailer\n<< /Size 11 /Root 1 0 R >>\nstartxref\n${ xref } \n%%EOF`;
2366- const file = new Blob ( [ source ] , { type : "application/pdf" } ) ;
2365+ . join ( "" ) } trailer\n<< /Size ${ objects . length + 1 } /Root 1 0 R >>\nstartxref\n${ xref } \n%%EOF`;
2366+ return new Blob ( [ source ] , { type : "application/pdf" } ) ;
2367+ }
2368+
2369+ it ( "keeps the PDF page after reloads and clamps it when the document gets shorter" , async ( ) => {
2370+ const { BrowserDocumentFrame } = await import ( "./BrowserDocumentFrame" ) ;
2371+ using _native = vi . spyOn ( navigator , "pdfViewerEnabled" , "get" ) . mockReturnValue ( false ) ;
2372+ using urls = ( ( ) => {
2373+ const original = URL . createObjectURL ( planningPdf ( ) ) ;
2374+ const updated = URL . createObjectURL (
2375+ planningPdf ( [ "Quarterly outlook" , "Delivery milestones" , "Updated budget" ] , 500 ) ,
2376+ ) ;
2377+ const shorter = URL . createObjectURL ( planningPdf ( [ "Quarterly outlook" , "Updated milestones" ] ) ) ;
2378+ const single = URL . createObjectURL ( planningPdf ( [ "Quarterly outlook" ] ) ) ;
2379+ return {
2380+ original,
2381+ updated,
2382+ shorter,
2383+ single,
2384+ [ Symbol . dispose ] : ( ) => [ original , updated , shorter , single ] . forEach ( URL . revokeObjectURL ) ,
2385+ } ;
2386+ } ) ( ) ;
2387+ const preview = ( src : string , revision : string , documentId = "planning" ) => (
2388+ < div style = { { display : "flex" , height : 600 , width : 320 } } >
2389+ < BrowserDocumentFrame
2390+ src = { src }
2391+ title = "Planning PDF"
2392+ pdf
2393+ documentKey = { revision }
2394+ documentId = { documentId }
2395+ />
2396+ </ div >
2397+ ) ;
2398+ await using screen = await render ( preview ( urls . original , "one" ) ) ;
2399+ await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "1 / 3 pages" ) , {
2400+ timeout : 15_000 ,
2401+ } ) ;
2402+ await screen . getByRole ( "button" , { name : "Next page" , exact : true } ) . click ( ) ;
2403+ await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "2 / 3 pages" ) ) ;
2404+ await screen . getByRole ( "button" , { name : "Next page" , exact : true } ) . click ( ) ;
2405+ await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "3 / 3 pages" ) ) ;
2406+ await screen . rerender ( preview ( urls . updated , "two" ) ) ;
2407+ await vi . waitFor ( ( ) => {
2408+ expect ( screen . container . textContent ) . toContain ( "3 / 3 pages" ) ;
2409+ expect ( screen . container . querySelector ( '[role="region"]' ) ?. textContent ) . toContain (
2410+ "Updated budget" ,
2411+ ) ;
2412+ } ) ;
2413+ await screen . rerender ( preview ( urls . shorter , "three" ) ) ;
2414+ await vi . waitFor ( ( ) => {
2415+ expect ( screen . container . textContent ) . toContain ( "2 / 2 pages" ) ;
2416+ expect ( screen . container . querySelector ( '[role="region"]' ) ?. textContent ) . toContain (
2417+ "Updated milestones" ,
2418+ ) ;
2419+ } ) ;
2420+ await screen . rerender ( preview ( urls . single , "four" ) ) ;
2421+ await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "1 / 1 pages" ) ) ;
2422+ await screen . rerender ( preview ( urls . original , "five" ) ) ;
2423+ await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "1 / 3 pages" ) ) ;
2424+ await screen . getByRole ( "button" , { name : "Next page" , exact : true } ) . click ( ) ;
2425+ await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "2 / 3 pages" ) ) ;
2426+ await screen . rerender ( preview ( urls . original , "one" , "another-document" ) ) ;
2427+ await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "1 / 3 pages" ) ) ;
2428+ } ) ;
2429+
2430+ it ( "navigates, searches, zooms, and selects mixed-height PDF pages" , async ( ) => {
2431+ const file = planningPdf ( ) ;
23672432 const { default : PdfViewer } = await import ( "./PdfViewer" ) ;
23682433 const url = URL . createObjectURL ( file ) ;
23692434 try {
0 commit comments