Convert the thumbnail view(er) code to use proper private methods

This allows us to get rid of the `@private` JSDoc comments, which were
used to convey intent back when proper private methods could not be used
yet in JavaScript. This improves code readability/maintenance and enables
better usage validation by tooling such as ESlint.
This commit is contained in:
Tim van der Meij 2024-04-05 14:10:25 +02:00
parent 5adad89eb3
commit 862c27ee67
No known key found for this signature in database
GPG key ID: 8C3FD2925A5F2762
2 changed files with 20 additions and 41 deletions

View file

@ -55,7 +55,7 @@ class TempImageFactory {
tempCanvas.height = height; tempCanvas.height = height;
// Since this is a temporary canvas, we need to fill it with a white // Since this is a temporary canvas, we need to fill it with a white
// background ourselves. `_getPageDrawContext` uses CSS rules for this. // background ourselves. `#getPageDrawContext` uses CSS rules for this.
const ctx = tempCanvas.getContext("2d", { alpha: false }); const ctx = tempCanvas.getContext("2d", { alpha: false });
ctx.save(); ctx.save();
ctx.fillStyle = "rgb(255, 255, 255)"; ctx.fillStyle = "rgb(255, 255, 255)";
@ -196,10 +196,7 @@ class PDFThumbnailView {
this.resume = null; this.resume = null;
} }
/** #getPageDrawContext(upscaleFactor = 1) {
* @private
*/
_getPageDrawContext(upscaleFactor = 1) {
// Keep the no-thumbnail outline visible, i.e. `data-loaded === false`, // Keep the no-thumbnail outline visible, i.e. `data-loaded === false`,
// until rendering/image conversion is complete, to avoid display issues. // until rendering/image conversion is complete, to avoid display issues.
const canvas = document.createElement("canvas"); const canvas = document.createElement("canvas");
@ -216,14 +213,11 @@ class PDFThumbnailView {
return { ctx, canvas, transform }; return { ctx, canvas, transform };
} }
/** #convertCanvasToImage(canvas) {
* @private
*/
_convertCanvasToImage(canvas) {
if (this.renderingState !== RenderingStates.FINISHED) { if (this.renderingState !== RenderingStates.FINISHED) {
throw new Error("_convertCanvasToImage: Rendering has not finished."); throw new Error("#convertCanvasToImage: Rendering has not finished.");
} }
const reducedCanvas = this._reduceImage(canvas); const reducedCanvas = this.#reduceImage(canvas);
const image = document.createElement("img"); const image = document.createElement("img");
image.className = "thumbnailImage"; image.className = "thumbnailImage";
@ -253,7 +247,7 @@ class PDFThumbnailView {
return; return;
} }
this.renderingState = RenderingStates.FINISHED; this.renderingState = RenderingStates.FINISHED;
this._convertCanvasToImage(canvas); this.#convertCanvasToImage(canvas);
if (error) { if (error) {
throw error; throw error;
@ -280,7 +274,7 @@ class PDFThumbnailView {
// NOTE: To primarily avoid increasing memory usage too much, but also to // NOTE: To primarily avoid increasing memory usage too much, but also to
// reduce downsizing overhead, we purposely limit the up-scaling factor. // reduce downsizing overhead, we purposely limit the up-scaling factor.
const { ctx, canvas, transform } = const { ctx, canvas, transform } =
this._getPageDrawContext(DRAW_UPSCALE_FACTOR); this.#getPageDrawContext(DRAW_UPSCALE_FACTOR);
const drawViewport = this.viewport.clone({ const drawViewport = this.viewport.clone({
scale: DRAW_UPSCALE_FACTOR * this.scale, scale: DRAW_UPSCALE_FACTOR * this.scale,
}); });
@ -342,14 +336,11 @@ class PDFThumbnailView {
return; return;
} }
this.renderingState = RenderingStates.FINISHED; this.renderingState = RenderingStates.FINISHED;
this._convertCanvasToImage(canvas); this.#convertCanvasToImage(canvas);
} }
/** #reduceImage(img) {
* @private const { ctx, canvas } = this.#getPageDrawContext();
*/
_reduceImage(img) {
const { ctx, canvas } = this._getPageDrawContext();
if (img.width <= 2 * canvas.width) { if (img.width <= 2 * canvas.width) {
ctx.drawImage( ctx.drawImage(

View file

@ -64,14 +64,11 @@ class PDFThumbnailViewer {
this.renderingQueue = renderingQueue; this.renderingQueue = renderingQueue;
this.pageColors = pageColors || null; this.pageColors = pageColors || null;
this.scroll = watchScroll(this.container, this._scrollUpdated.bind(this)); this.scroll = watchScroll(this.container, this.#scrollUpdated.bind(this));
this._resetView(); this.#resetView();
} }
/** #scrollUpdated() {
* @private
*/
_scrollUpdated() {
this.renderingQueue.renderHighestPriority(); this.renderingQueue.renderHighestPriority();
} }
@ -79,10 +76,7 @@ class PDFThumbnailViewer {
return this._thumbnails[index]; return this._thumbnails[index];
} }
/** #getVisibleThumbs() {
* @private
*/
_getVisibleThumbs() {
return getVisibleElements({ return getVisibleElements({
scrollEl: this.container, scrollEl: this.container,
views: this._thumbnails, views: this._thumbnails,
@ -107,7 +101,7 @@ class PDFThumbnailViewer {
// ... and add the highlight to the new thumbnail. // ... and add the highlight to the new thumbnail.
thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS); thumbnailView.div.classList.add(THUMBNAIL_SELECTED_CLASS);
} }
const { first, last, views } = this._getVisibleThumbs(); const { first, last, views } = this.#getVisibleThumbs();
// If the thumbnail isn't currently visible, scroll it into view. // If the thumbnail isn't currently visible, scroll it into view.
if (views.length > 0) { if (views.length > 0) {
@ -162,10 +156,7 @@ class PDFThumbnailViewer {
TempImageFactory.destroyCanvas(); TempImageFactory.destroyCanvas();
} }
/** #resetView() {
* @private
*/
_resetView() {
this._thumbnails = []; this._thumbnails = [];
this._currentPageNumber = 1; this._currentPageNumber = 1;
this._pageLabels = null; this._pageLabels = null;
@ -180,8 +171,8 @@ class PDFThumbnailViewer {
*/ */
setDocument(pdfDocument) { setDocument(pdfDocument) {
if (this.pdfDocument) { if (this.pdfDocument) {
this._cancelRendering(); this.#cancelRendering();
this._resetView(); this.#resetView();
} }
this.pdfDocument = pdfDocument; this.pdfDocument = pdfDocument;
@ -225,10 +216,7 @@ class PDFThumbnailViewer {
}); });
} }
/** #cancelRendering() {
* @private
*/
_cancelRendering() {
for (const thumbnail of this._thumbnails) { for (const thumbnail of this._thumbnails) {
thumbnail.cancelRendering(); thumbnail.cancelRendering();
} }
@ -287,7 +275,7 @@ class PDFThumbnailViewer {
} }
forceRendering() { forceRendering() {
const visibleThumbs = this._getVisibleThumbs(); const visibleThumbs = this.#getVisibleThumbs();
const scrollAhead = this.#getScrollAhead(visibleThumbs); const scrollAhead = this.#getScrollAhead(visibleThumbs);
const thumbView = this.renderingQueue.getHighestPriority( const thumbView = this.renderingQueue.getHighestPriority(
visibleThumbs, visibleThumbs,