From dc19965d78614c922e670ef60212eaf78cf14412 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 5 Feb 2021 16:24:39 +0100 Subject: [PATCH] Slightly re-factor how the `BaseViewer`/`PDFThumbnailViewer` handle page labels internally, to make the `null` default value clearer Currently it's not *immediately* clear from the code itself, unless you look at the definition of `this._pageLabels`, that the default value is `null`.[1] We can improve this, and also reduce the amount of code, by using modern ECMAScript features such as optional chaining and nullish coalescing. --- [1] Keep in mind that an *empty* string is actually a valid page label, according to the PDF specification. --- web/base_viewer.js | 8 +++----- web/pdf_thumbnail_viewer.js | 3 +-- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/web/base_viewer.js b/web/base_viewer.js index 2d0707e17..e0af6622a 100644 --- a/web/base_viewer.js +++ b/web/base_viewer.js @@ -290,7 +290,7 @@ class BaseViewer { this.eventBus.dispatch("pagechanging", { source: this, pageNumber: val, - pageLabel: this._pageLabels && this._pageLabels[val - 1], + pageLabel: this._pageLabels?.[val - 1] ?? null, previous, }); @@ -305,7 +305,7 @@ class BaseViewer { * labels exist. */ get currentPageLabel() { - return this._pageLabels && this._pageLabels[this._currentPageNumber - 1]; + return this._pageLabels?.[this._currentPageNumber - 1] ?? null; } /** @@ -631,9 +631,7 @@ class BaseViewer { } // Update all the `PDFPageView` instances. for (let i = 0, ii = this._pages.length; i < ii; i++) { - const pageView = this._pages[i]; - const label = this._pageLabels && this._pageLabels[i]; - pageView.setPageLabel(label); + this._pages[i].setPageLabel(this._pageLabels?.[i] ?? null); } } diff --git a/web/pdf_thumbnail_viewer.js b/web/pdf_thumbnail_viewer.js index 2da5613fe..955524b95 100644 --- a/web/pdf_thumbnail_viewer.js +++ b/web/pdf_thumbnail_viewer.js @@ -268,8 +268,7 @@ class PDFThumbnailViewer { } // Update all the `PDFThumbnailView` instances. for (let i = 0, ii = this._thumbnails.length; i < ii; i++) { - const label = this._pageLabels && this._pageLabels[i]; - this._thumbnails[i].setPageLabel(label); + this._thumbnails[i].setPageLabel(this._pageLabels?.[i] ?? null); } }