Przeglądaj źródła

Only store/restore PDF scroll position when visible (#6218)

GitOrigin-RevId: c2023b5173fe4668f52da3e096c3dfc0124a7248
Alf Eaton 4 lat temu
rodzic
commit
5b560ad8ab

+ 8 - 1
services/web/frontend/js/features/pdf-preview/components/pdf-js-viewer.js

@@ -82,13 +82,16 @@ function PdfJsViewer({ url }) {
     let storePositionTimer
     let storePositionTimer
 
 
     if (initialised && pdfJsWrapper) {
     if (initialised && pdfJsWrapper) {
+      if (!pdfJsWrapper.isVisible()) {
+        return
+      }
+
       // store the scroll position in localStorage, for the synctex button
       // store the scroll position in localStorage, for the synctex button
       const storePosition = debounce(pdfViewer => {
       const storePosition = debounce(pdfViewer => {
         // set position for "sync to code" button
         // set position for "sync to code" button
         try {
         try {
           setPosition(pdfViewer.currentPosition)
           setPosition(pdfViewer.currentPosition)
         } catch (error) {
         } catch (error) {
-          // TODO: investigate handling missing offsetParent in jsdom
           // console.error(error)
           // console.error(error)
         }
         }
       }, 500)
       }, 500)
@@ -139,6 +142,10 @@ function PdfJsViewer({ url }) {
   // restore the saved scale and scroll position
   // restore the saved scale and scroll position
   useEffect(() => {
   useEffect(() => {
     if (initialised && pdfJsWrapper) {
     if (initialised && pdfJsWrapper) {
+      if (!pdfJsWrapper.isVisible()) {
+        return
+      }
+
       setScale(scale => {
       setScale(scale => {
         setPosition(position => {
         setPosition(position => {
           if (position) {
           if (position) {

+ 4 - 0
services/web/frontend/js/features/pdf-preview/util/pdf-js-wrapper.js

@@ -183,6 +183,10 @@ export default class PDFJSWrapper {
     })
     })
   }
   }
 
 
+  isVisible() {
+    return this.viewer.container.offsetParent !== null
+  }
+
   abortDocumentLoading() {
   abortDocumentLoading() {
     this.loadDocumentTask = undefined
     this.loadDocumentTask = undefined
   }
   }