@@ -31,6 +31,11 @@ import "./pdfViewer.css";
3131const MAX_PAGES = 2000 ;
3232const MAX_FIND_TEXT = 4 * 1024 * 1024 ;
3333
34+ const MAX_POSITIONS = 50 ;
35+ type PdfPosition = PdfViewState & { width : number ; ratios : readonly number [ ] } ;
36+ // Thread switches unmount the preview; retain only the most recent documents.
37+ const positions = new Map < string , PdfPosition > ( ) ;
38+
3439export type PdfViewState = {
3540 page : number ;
3641 offset : number ;
@@ -126,16 +131,20 @@ function PdfPage(props: {
126131export default function PdfViewer ( props : {
127132 src : string ;
128133 title : string ;
134+ positionKey ?: string ;
129135 initialView ?: PdfViewState ;
130136 onViewChange ?: ( view : PdfViewState ) => void ;
131137} ) {
138+ const [ savedPosition ] = useState ( ( ) =>
139+ props . positionKey === undefined ? undefined : positions . get ( props . positionKey ) ,
140+ ) ;
132141 const [ initialView ] = useState < PdfViewState > (
133- ( ) => props . initialView ?? { page : 0 , offset : 0 , zoom : null , left : 0 } ,
142+ ( ) => savedPosition ?? props . initialView ?? { page : 0 , offset : 0 , zoom : null , left : 0 } ,
134143 ) ;
135144 const [ pdf , setPdf ] = useState < PDFDocumentProxy | null > ( null ) ;
136145 const [ error , setError ] = useState < string | null > ( null ) ;
137146 const [ revision , setRevision ] = useState ( 0 ) ;
138- const [ ratios , setRatios ] = useState < number [ ] > ( [ ] ) ;
147+ const [ ratios , setRatios ] = useState < readonly number [ ] > ( [ ] ) ;
139148 const [ zoom , setZoom ] = useState ( initialView . zoom ) ;
140149 const [ pageWidth , setPageWidth ] = useState ( 612 ) ;
141150 const [ viewport , setViewport ] = useState ( { width : 600 , height : 800 } ) ;
@@ -181,10 +190,12 @@ export default function PdfViewer(props: {
181190 if ( controller . signal . aborted ) return ;
182191 setPageWidth ( first . width ) ;
183192 setRatios (
184- Array . from (
185- { length : document . numPages } ,
186- ( _ , index ) => sizes [ index ] ?? first . height / first . width ,
187- ) ,
193+ savedPosition ?. ratios . length === document . numPages
194+ ? savedPosition . ratios
195+ : Array . from (
196+ { length : document . numPages } ,
197+ ( _ , index ) => sizes [ index ] ?? first . height / first . width ,
198+ ) ,
188199 ) ;
189200 setPdf ( document ) ;
190201 } )
@@ -199,7 +210,7 @@ export default function PdfViewer(props: {
199210 clearTimeout ( timeout ) ;
200211 void task ?. destroy ( ) ;
201212 } ;
202- } , [ props . src , revision , initialPage ] ) ;
213+ } , [ props . src , revision , initialPage , savedPosition ] ) ;
203214 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Measure the PDF viewport before paint and observe resizes for fit-width sizing and visible-page selection.
204215 useLayoutEffect ( ( ) => {
205216 const node = scroll . current ;
@@ -222,24 +233,38 @@ export default function PdfViewer(props: {
222233 const jumpToPage = useEffectEvent ( ( index : number ) => {
223234 scroll . current ?. scrollTo ( { top : pdfPageScrollTop ( offsets , index ) } ) ;
224235 } ) ;
225- const saveView = ( node : HTMLDivElement ) => {
236+ const saveView = ( node : HTMLDivElement , notify = true ) => {
226237 const page = currentPdfPage ( offsets , ratios . length , node . scrollTop ) ;
227- props . onViewChange ?.( {
238+ if ( ! pdf ) return ;
239+ const view = {
228240 page,
229241 offset : Math . max ( 0 , node . scrollTop - ( offsets [ page ] ! - 16 ) ) ,
230242 zoom,
231243 left : node . scrollLeft ,
232- } ) ;
244+ } ;
245+ if ( notify ) props . onViewChange ?.( view ) ;
246+ if ( props . positionKey !== undefined ) {
247+ positions . delete ( props . positionKey ) ;
248+ positions . set ( props . positionKey , { ...view , width, ratios } ) ;
249+ const oldest = positions . keys ( ) . next ( ) ;
250+ if ( positions . size > MAX_POSITIONS && ! oldest . done ) positions . delete ( oldest . value ) ;
251+ }
233252 } ;
234253 const restoreView = useEffectEvent ( ( ) => {
235254 const node = scroll . current ;
236255 if ( ! pdf || ! node ) return ;
237256 const page = Math . min ( initialPage , pdf . numPages - 1 ) ;
238257 node . scrollTo ( {
239258 top :
240- offsets [ page ] ! - 16 + Math . min ( initialView . offset , Math . max ( 0 , width * ratios [ page ] ! - 1 ) ) ,
259+ offsets [ page ] ! -
260+ 16 +
261+ Math . min (
262+ initialView . offset * ( savedPosition ? width / savedPosition . width : 1 ) ,
263+ Math . max ( 0 , width * ratios [ page ] ! - 1 ) ,
264+ ) ,
241265 left : initialView . left ,
242266 } ) ;
267+ setScrollTop ( node . scrollTop ) ;
243268 saveView ( node ) ;
244269 } ) ;
245270 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Restore the saved PDF scroll position after the loaded document's page layout commits.
@@ -262,13 +287,13 @@ export default function PdfViewer(props: {
262287 const anchor = zoomAnchor . current ;
263288 zoomAnchor . current = null ;
264289 const node = scroll . current ;
265- if ( ! anchor || ! node ) return ;
266- if ( anchor . width !== width ) {
290+ if ( ! node || ! pdf ) return ;
291+ if ( anchor && anchor . width !== width ) {
267292 node . scrollTo ( {
268293 top : offsets [ anchor . page ] ! - 16 + ( anchor . offset * width ) / anchor . width ,
269294 } ) ;
270295 }
271- saveView ( node ) ;
296+ saveView ( node , anchor !== null ) ;
272297 } ) ;
273298 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- Scroll to the first asynchronous search result after its measured page offsets have committed.
274299 useLayoutEffect ( ( ) => {
0 commit comments