@@ -2337,6 +2337,7 @@ it("toggles a Mermaid attachment between diagram and source through the lazy pre
23372337function planningPdf (
23382338 titles = [ "Quarterly outlook" , "Delivery milestones" , "Budget and risks" ] ,
23392339 firstHeight = 140 ,
2340+ pageHeight = 792 ,
23402341) {
23412342 const objects = [
23422343 "<< /Type /Catalog /Pages 2 0 R >>" ,
@@ -2346,7 +2347,7 @@ function planningPdf(
23462347 for ( const [ index , title ] of titles . entries ( ) ) {
23472348 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` ;
23482349 objects . push (
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 >>` ,
2350+ `<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 ${ index === 0 ? firstHeight : pageHeight } ] /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 >>` ,
23502351 `<< /Length ${ text . length } >>\nstream\n${ text } \nendstream` ,
23512352 ) ;
23522353 }
@@ -2366,7 +2367,7 @@ function planningPdf(
23662367 return new Blob ( [ source ] , { type : "application/pdf" } ) ;
23672368}
23682369
2369- it ( "keeps the workspace PDF page after reloads and clamps it when the document gets shorter " , async ( ) => {
2370+ it ( "keeps workspace PDF scroll and zoom after reloads and clamps them when the document shrinks " , async ( ) => {
23702371 assetUrlState . mockReset ( ) ;
23712372 const { WorkspaceBrowserPreview } = await import ( "./FilePreviewPanel" ) ;
23722373 using _native = vi . spyOn ( navigator , "pdfViewerEnabled" , "get" ) . mockReturnValue ( false ) ;
@@ -2376,13 +2377,18 @@ it("keeps the workspace PDF page after reloads and clamps it when the document g
23762377 planningPdf ( [ "Quarterly outlook" , "Delivery milestones" , "Updated budget" ] , 500 ) ,
23772378 ) ;
23782379 const shorter = URL . createObjectURL ( planningPdf ( [ "Quarterly outlook" , "Updated milestones" ] ) ) ;
2380+ const shrunk = URL . createObjectURL (
2381+ planningPdf ( [ "Quarterly outlook" , "Small page" , "Budget and risks" ] , 500 , 200 ) ,
2382+ ) ;
23792383 const single = URL . createObjectURL ( planningPdf ( [ "Quarterly outlook" ] ) ) ;
23802384 return {
23812385 original,
23822386 updated,
23832387 shorter,
2388+ shrunk,
23842389 single,
2385- [ Symbol . dispose ] : ( ) => [ original , updated , shorter , single ] . forEach ( URL . revokeObjectURL ) ,
2390+ [ Symbol . dispose ] : ( ) =>
2391+ [ original , updated , shorter , shrunk , single ] . forEach ( URL . revokeObjectURL ) ,
23862392 } ;
23872393 } ) ( ) ;
23882394 const preview = ( src : string , mutation : string , path = "/repo/planning.pdf" ) => {
@@ -2413,12 +2419,53 @@ it("keeps the workspace PDF page after reloads and clamps it when the document g
24132419 await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "2 / 3 pages" ) ) ;
24142420 await screen . getByRole ( "button" , { name : "Next page" , exact : true } ) . click ( ) ;
24152421 await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "3 / 3 pages" ) ) ;
2422+ for ( let index = 0 ; index < 4 ; index ++ ) {
2423+ await screen . getByRole ( "button" , { name : "Zoom in" , exact : true } ) . click ( ) ;
2424+ }
2425+ const region = ( ) => screen . container . querySelector < HTMLElement > ( '[role="region"]' ) ! ;
2426+ const canvas = ( page : number ) =>
2427+ region ( ) . querySelector < HTMLCanvasElement > ( `canvas[aria-label="Page ${ page } "]` ) ! ;
2428+ const offset = ( page : number ) =>
2429+ region ( ) . getBoundingClientRect ( ) . top - canvas ( page ) . getBoundingClientRect ( ) . top ;
2430+ const scrollTo = ( options : ScrollToOptions ) =>
2431+ act ( async ( ) => {
2432+ const node = region ( ) ;
2433+ const scrolled = new Promise < void > ( ( resolve ) =>
2434+ node . addEventListener ( "scroll" , ( ) => resolve ( ) , { once : true } ) ,
2435+ ) ;
2436+ node . scrollTo ( options ) ;
2437+ await scrolled ;
2438+ } ) ;
2439+ const zoom = screen . container . textContent ! . match ( / \d + % / ) ! [ 0 ] ;
2440+ const top = region ( ) . scrollTop ;
2441+ await scrollTo ( { top : top + 100 , left : 80 } ) ;
2442+ await vi . waitFor ( ( ) => {
2443+ expect ( region ( ) . scrollTop ) . toBeCloseTo ( top + 100 , 0 ) ;
2444+ expect ( region ( ) . scrollLeft ) . toBe ( 80 ) ;
2445+ } ) ;
2446+ const savedOffset = offset ( 3 ) ;
24162447 await screen . rerender ( preview ( urls . updated , "two" ) ) ;
24172448 await vi . waitFor ( ( ) => {
24182449 expect ( screen . container . textContent ) . toContain ( "3 / 3 pages" ) ;
24192450 expect ( screen . container . querySelector ( '[role="region"]' ) ?. textContent ) . toContain (
24202451 "Updated budget" ,
24212452 ) ;
2453+ expect ( screen . container . textContent ) . toContain ( zoom ) ;
2454+ expect ( region ( ) . scrollLeft ) . toBe ( 80 ) ;
2455+ expect ( offset ( 3 ) ) . toBeCloseTo ( savedOffset , 0 ) ;
2456+ } ) ;
2457+ await screen . getByRole ( "button" , { name : "Previous page" , exact : true } ) . click ( ) ;
2458+ await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "2 / 3 pages" ) ) ;
2459+ const secondPageTop = region ( ) . scrollTop ;
2460+ await scrollTo ( { top : secondPageTop + 600 } ) ;
2461+ await vi . waitFor ( ( ) => expect ( region ( ) . scrollTop ) . toBeCloseTo ( secondPageTop + 600 , 0 ) ) ;
2462+ await screen . rerender ( preview ( urls . shrunk , "smaller-page" ) ) ;
2463+ await vi . waitFor ( ( ) => {
2464+ expect ( screen . container . textContent ) . toContain ( "2 / 3 pages" ) ;
2465+ expect ( screen . container . textContent ) . toContain ( zoom ) ;
2466+ expect ( region ( ) . scrollLeft ) . toBe ( 80 ) ;
2467+ expect ( offset ( 2 ) ) . toBeGreaterThan ( 0 ) ;
2468+ expect ( offset ( 2 ) ) . toBeLessThan ( canvas ( 2 ) . getBoundingClientRect ( ) . height ) ;
24222469 } ) ;
24232470 await screen . rerender ( preview ( urls . shorter , "three" ) ) ;
24242471 await vi . waitFor ( ( ) => {
@@ -2431,10 +2478,25 @@ it("keeps the workspace PDF page after reloads and clamps it when the document g
24312478 await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "1 / 1 pages" ) ) ;
24322479 await screen . rerender ( preview ( urls . original , "five" ) ) ;
24332480 await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "1 / 3 pages" ) ) ;
2481+ await screen . getByRole ( "button" , { name : "Fit width" , exact : true } ) . click ( ) ;
2482+ await screen . rerender ( preview ( urls . original , "six" ) ) ;
2483+ await vi . waitFor ( ( ) => {
2484+ expect ( screen . container . textContent ) . toContain ( "Fit width" ) ;
2485+ expect ( region ( ) . scrollLeft ) . toBe ( 0 ) ;
2486+ } ) ;
2487+ await screen . getByRole ( "button" , { name : "Zoom in" , exact : true } ) . click ( ) ;
2488+ const firstPageZoom = screen . container . textContent ! . match ( / \d + % / ) ! [ 0 ] ;
2489+ await screen . rerender ( preview ( urls . original , "seven" ) ) ;
2490+ await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( firstPageZoom ) ) ;
24342491 await screen . getByRole ( "button" , { name : "Next page" , exact : true } ) . click ( ) ;
24352492 await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "2 / 3 pages" ) ) ;
24362493 await screen . rerender ( preview ( urls . original , "one" , "/repo/another.pdf" ) ) ;
2437- await vi . waitFor ( ( ) => expect ( screen . container . textContent ) . toContain ( "1 / 3 pages" ) ) ;
2494+ await vi . waitFor ( ( ) => {
2495+ expect ( screen . container . textContent ) . toContain ( "1 / 3 pages" ) ;
2496+ expect ( screen . container . textContent ) . toContain ( "Fit width" ) ;
2497+ expect ( region ( ) . scrollTop ) . toBe ( 0 ) ;
2498+ expect ( region ( ) . scrollLeft ) . toBe ( 0 ) ;
2499+ } ) ;
24382500} ) ;
24392501
24402502it ( "navigates, searches, zooms, and selects mixed-height PDF pages" , async ( ) => {
0 commit comments