From e78e4e72bf21e5536cac44f7024fc05dd2898b49 Mon Sep 17 00:00:00 2001
From: Jonas Jenwald <jonas.jenwald@gmail.com>
Date: Mon, 1 Nov 2021 13:47:38 +0100
Subject: [PATCH] Further modernize
 `PDFThumbnailViewer.scrollThumbnailIntoView`

The way that we're currently handling the last-`id` is very old, and there's no longer any good reason to special-case things when only one thumbnail is visible.
Furthermore, we can also modernize the loop slightly by using `for...of` instead of `Array.prototype.some()` when checking for fully visible thumbnails.
---
 web/pdf_thumbnail_viewer.js | 23 +++++++++--------------
 1 file changed, 9 insertions(+), 14 deletions(-)

diff --git a/web/pdf_thumbnail_viewer.js b/web/pdf_thumbnail_viewer.js
index d25e8e60d..0d83ca25d 100644
--- a/web/pdf_thumbnail_viewer.js
+++ b/web/pdf_thumbnail_viewer.js
@@ -99,26 +99,21 @@ class PDFThumbnailViewer {
       // ... and add the highlight to the new thumbnail.
       thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
     }
-    const visibleThumbs = this._getVisibleThumbs();
-    const numVisibleThumbs = visibleThumbs.views.length;
+    const { first, last, views } = this._getVisibleThumbs();
 
     // If the thumbnail isn't currently visible, scroll it into view.
-    if (numVisibleThumbs > 0) {
-      const first = visibleThumbs.first.id;
-      // Account for only one thumbnail being visible.
-      const last = numVisibleThumbs > 1 ? visibleThumbs.last.id : first;
-
+    if (views.length > 0) {
       let shouldScroll = false;
-      if (pageNumber <= first || pageNumber >= last) {
+      if (pageNumber <= first.id || pageNumber >= last.id) {
         shouldScroll = true;
       } else {
-        visibleThumbs.views.some(function (view) {
-          if (view.id !== pageNumber) {
-            return false;
+        for (const { id, percent } of views) {
+          if (id !== pageNumber) {
+            continue;
           }
-          shouldScroll = view.percent < 100;
-          return true;
-        });
+          shouldScroll = percent < 100;
+          break;
+        }
       }
       if (shouldScroll) {
         scrollIntoView(thumbnailView.div, { top: THUMBNAIL_SCROLL_MARGIN });