#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere
TypeScript dockerhacktoberfestjavapdfpdf-converterpdf-editorpdf-manipulationpdf-mergerpdf-ocrpdf-toolspdf-web-appspdfmergerself-hosted
fix(viewer): keep the reading page after adding a bookmark (#8675)
EthanHealy017f76797ab0cd
diff --git a/frontend/src/editor/core/components/viewer/DocumentRestoreBridge.test.tsx b/frontend/src/editor/core/components/viewer/DocumentRestoreBridge.test.tsxindex 0c7bd3ffdd..1b44611169 100644--- a/frontend/src/editor/core/components/viewer/DocumentRestoreBridge.test.tsx+++ b/frontend/src/editor/core/components/viewer/DocumentRestoreBridge.test.tsx@@ -114,6 +114,7 @@ const fixture = vi.hoisted(() => {const scrollRequests = perDocument(() =>emitter<{ x: number; y: number }>(),);+ const scrollTo = perDocument(() => ({ scrollToPage: vi.fn() }));return {zoom: perDocument(zoomScope),spread: perDocument(spreadScope),@@ -122,9 +123,11 @@ const fixture = vi.hoisted(() => {layoutChange,viewportResize,scrollRequests,+ scrollTo,scroll: {onLayoutReady: layoutReady.on,onLayoutChange: layoutChange.on,+ forDocument: scrollTo.forDocument,},viewport: { onViewportResize: viewportResize.on },viewportPlugin: {@@ -172,6 +175,7 @@ const request = (zoom: 1.42,spread: SpreadMode.Odd,rotation: 1,+ page: null,...overrides,});@@ -393,6 +397,49 @@ describe("DocumentRestoreBridge", () => {expect(types()).toEqual(["rotation", "spread", "zoom"]);});+ it("scrolls the replacement to the carried page once the carried values have landed", () => {+ renderBridge({ request: request({ page: 7 }), laidOut: false });+ layoutReady("second", 10);++ const { scrollToPage } = fixture.current.scrollTo.forDocument("second");+ const { requestZoom } = fixture.current.zoom.forDocument("second");+ expect(scrollToPage).toHaveBeenCalledExactlyOnceWith({+ pageNumber: 7,+ behavior: "instant",+ });+ expect(scrollToPage.mock.invocationCallOrder[0]).toBeGreaterThan(+ requestZoom.mock.invocationCallOrder[0],+ );+ });++ it("holds the carried page until a dropped zoom lands, then asks once", () => {+ const zoom = fixture.current.zoom.forDocument("second");+ zoom.drop = true;+ renderBridge({ request: request({ page: 2 }) });++ const { scrollToPage } = fixture.current.scrollTo.forDocument("second");+ expect(scrollToPage).not.toHaveBeenCalled();++ zoom.drop = false;+ act(() => fixture.current.viewportResize.emit({ documentId: "second" }));+ act(() => fixture.current.layoutChange.emit({ documentId: "second" }));++ expect(scrollToPage).toHaveBeenCalledExactlyOnceWith({+ pageNumber: 2,+ behavior: "instant",+ });+ });++ it("clamps the carried page to the replacement's length", () => {+ renderBridge({+ request: request({ zoom: null, spread: null, rotation: null, page: 9 }),+ });++ expect(+ fixture.current.scrollTo.forDocument("second").scrollToPage,+ ).toHaveBeenCalledExactlyOnceWith({ pageNumber: 3, behavior: "instant" });+ });+it("subscribes to nothing while no restore is in flight", () => {renderBridge({ request: null, restorePending: false, laidOut: false });diff --git a/frontend/src/editor/core/components/viewer/DocumentRestoreBridge.tsx b/frontend/src/editor/core/components/viewer/DocumentRestoreBridge.tsxindex 9baae5a10d..cc25a3e4ff 100644--- a/frontend/src/editor/core/components/viewer/DocumentRestoreBridge.tsx+++ b/frontend/src/editor/core/components/viewer/DocumentRestoreBridge.tsx@@ -19,6 +19,9 @@ export interface DocumentRestoreRequest {zoom: ZoomLevel | null;spread: SpreadMode | null;rotation: number | null;+ /** One-based. Brought into view once the replacement is laid out: pages away+ * from the viewport are not mounted, so the viewer cannot place it by node. */+ page: number | null;}/** `scroll-request` fires before the viewport applies that scroll, on its next frame. */@@ -48,7 +51,7 @@ const zoomMatches = (state: ZoomDocumentState, target: ZoomLevel) =>: state.zoomLevel === target;/**- * Carries zoom, spread and rotation onto a replacement document. The zoom+ * Carries zoom, spread, rotation and page onto a replacement document. The zoom* plugin silently drops a request made before the viewport is measured, so* every later plugin event re-checks; the viewport applies plugin scrolls a* frame later, so those requests are forwarded for the viewer to correct.@@ -114,6 +117,9 @@ function DocumentRestoreBridgeInner({const zoomPassSeenRef = useRef(false);// A spread changed while the state bridges mount reaches the toolbar out of order.const layoutReadyRef = useRef(false);+ const totalPagesRef = useRef(0);+ // Once only: a later pass must not pull the reader back to the carried page.+ const pageRequestedRef = useRef(false);useLayoutEffect(() => {const forDocument = (event: { documentId: string }) =>@@ -137,6 +143,7 @@ function DocumentRestoreBridgeInner({scroll?.onLayoutReady((event) => {if (!forDocument(event)) return;layoutReadyRef.current = true;+ totalPagesRef.current = event.totalPages;emit({ type: "layout-ready", totalPages: event.totalPages });bump();}),@@ -230,7 +237,24 @@ function DocumentRestoreBridgeInner({() => scope.requestZoom(target),);}- }, [request, zoom, spread, rotate, documentId, tick]);++ // After the zoom lands: its scroll request is anchored to the current top+ // and would replace one queued before it.+ if (+ request.page !== null &&+ scroll &&+ !pageRequestedRef.current &&+ landed.rotation &&+ landed.spread &&+ landed.zoom+ ) {+ pageRequestedRef.current = true;+ scroll.forDocument(documentId).scrollToPage({+ pageNumber: Math.min(request.page, totalPagesRef.current),+ behavior: "instant",+ });+ }+ }, [request, zoom, spread, rotate, scroll, documentId, tick]);return null;}diff --git a/frontend/src/editor/core/components/viewer/EmbedPdfViewer.tsx b/frontend/src/editor/core/components/viewer/EmbedPdfViewer.tsxindex b409a1b75a..4857af0e6e 100644--- a/frontend/src/editor/core/components/viewer/EmbedPdfViewer.tsx+++ b/frontend/src/editor/core/components/viewer/EmbedPdfViewer.tsx@@ -473,7 +473,14 @@ const EmbedPdfViewerContent = ({if (!pending) return false;const pageEl = resolvePendingScrollPage();- if (!pageEl) return false;+ if (!pageEl) {+ // A carried page away from the replacement's top mounts only once the+ // restore bridge scrolls to it; until then the replacement shows page 1.+ if (pending.expectSwap && documentSwappedRef.current) {+ hideSwappedDocument(pending.scroller);+ }+ return false;+ }if (!pendingScrollSwappedRef.current) {if (!pending.scroller?.isConnected) {pending.scroller = findScrollableAncestor(pageEl);@@ -1595,10 +1602,12 @@ const EmbedPdfViewerContent = ({const zoom = pendingZoomRestoreRef.current;const spread = pendingSpreadRestoreRef.current;const rotation = pendingRotationRestoreRef.current;+ const pending = pendingScrollPositionRef.current;+ const page = pending?.expectSwap ? pending.page : null;setRestoreRequest(- zoom === null && spread === null && rotation === null+ zoom === null && spread === null && rotation === null && page === null? null- : { documentId, zoom, spread, rotation },+ : { documentId, zoom, spread, rotation, page },);setRestoreTick((tick) => tick + 1);}, []);