From 211274ce308a23bc0715f061f918360a8bb5828c Mon Sep 17 00:00:00 2001 From: jenken827 Date: Sat, 12 Sep 2026 21:41:53 +0800 Subject: [PATCH] fix(reader): restore PDF reading position on open MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed-layout books (PDF/CBZ) always opened at the beginning: the open flow called view.init({}) for fixed layout, deliberately(?) discarding the lastLocation prop — so the saved position (books.current_cfi, e.g. epubcfi(/6/16) for page 8) was never applied on open, while EPUB restored fine through the sibling branch. The fake page CFI (epubcfi(/6/N)) that the save side stores resolves through the PDF backend's resolveCFI back to the recorded page, so passing lastLocation to view.init restores the exact page. Verified end-to-end on dev: read to page 5, close book, reopen -> page 5; close app mid-read -> position flushed via beforeunload -> reopen -> restored. Also drops the temporary [RelocateDiag] probe logs. --- packages/app/src/components/reader/FoliateViewer.tsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/app/src/components/reader/FoliateViewer.tsx b/packages/app/src/components/reader/FoliateViewer.tsx index fc686f352..ab1ce0dff 100644 --- a/packages/app/src/components/reader/FoliateViewer.tsx +++ b/packages/app/src/components/reader/FoliateViewer.tsx @@ -2856,9 +2856,14 @@ export const FoliateViewer = forwardRef view.addEventListener("link", linkHandler); setViewReady(true); - // Navigate to last location or start + // Navigate to last location or start. + // Fixed-layout books (PDF/CBZ) restore via their fake page CFI + // (epubcfi(/6/N)), which the PDF backend's resolveCFI maps back to + // the recorded page — same mechanism as annotation navigation. if (isFixedLayout) { - await view.init({}); + console.log("[PDFRestore] init lastLocation =", JSON.stringify(lastLocation)); + await view.init(lastLocation ? { lastLocation } : {}); + console.log("[PDFRestore] init done, current index =", view.renderer?.index); } else if (lastLocation) { try { await view.init({ lastLocation });