From 614ab4ef2c53088b1c3e51d7331bc8cc045db52e Mon Sep 17 00:00:00 2001
From: Jonas Jenwald <jonas.jenwald@gmail.com>
Date: Sat, 11 Nov 2017 22:57:59 +0100
Subject: [PATCH] Fix incorrect behaviour in
 `PDFThumbnailViewer.scrollThumbnailIntoView` for multiple columns of
 thumbnails

If the sidebar is resized such that the thumbnails are displayed in multiple columns, then scrolling the currently active thumbnail into view doesn't work correctly in some cases.
The reason is that the code in `PDFThumbnailViewer.scrollThumbnailIntoView` implicitly assumes that the thumbnails will be present in just *one* column. Since that may no longer be the case, it's not sufficient to simply check if the thumbnail is visible. Instead we must explicitly check that *all*, i.e. 100 percent, of the thumbnail is already visible, and otherwise scroll it into view.
---
 web/pdf_thumbnail_viewer.js | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/web/pdf_thumbnail_viewer.js b/web/pdf_thumbnail_viewer.js
index ce23d6556..a84c72a48 100644
--- a/web/pdf_thumbnail_viewer.js
+++ b/web/pdf_thumbnail_viewer.js
@@ -84,7 +84,20 @@ class PDFThumbnailViewer {
       let first = visibleThumbs.first.id;
       // Account for only one thumbnail being visible.
       let last = (numVisibleThumbs > 1 ? visibleThumbs.last.id : first);
+
+      let shouldScroll = false;
       if (page <= first || page >= last) {
+        shouldScroll = true;
+      } else {
+        visibleThumbs.views.some(function(view) {
+          if (view.id !== page) {
+            return false;
+          }
+          shouldScroll = view.percent < 100;
+          return true;
+        });
+      }
+      if (shouldScroll) {
         scrollIntoView(thumbnail, { top: THUMBNAIL_SCROLL_MARGIN, });
       }
     }