Skip to content

Commit

Permalink
Convert the thumbnail view(er) code to use proper private methods
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
timvandermeij committed Apr 5, 2024
1 parent 5adad89 commit 862c27e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 41 deletions.
29 changes: 10 additions & 19 deletions web/pdf_thumbnail_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class TempImageFactory {
tempCanvas.height = height;

// 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 });
ctx.save();
ctx.fillStyle = "rgb(255, 255, 255)";
Expand Down Expand Up @@ -196,10 +196,7 @@ class PDFThumbnailView {
this.resume = null;
}

/**
* @private
*/
_getPageDrawContext(upscaleFactor = 1) {
#getPageDrawContext(upscaleFactor = 1) {
// Keep the no-thumbnail outline visible, i.e. `data-loaded === false`,
// until rendering/image conversion is complete, to avoid display issues.
const canvas = document.createElement("canvas");
Expand All @@ -216,14 +213,11 @@ class PDFThumbnailView {
return { ctx, canvas, transform };
}

/**
* @private
*/
_convertCanvasToImage(canvas) {
#convertCanvasToImage(canvas) {
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");
image.className = "thumbnailImage";
Expand Down Expand Up @@ -253,7 +247,7 @@ class PDFThumbnailView {
return;
}
this.renderingState = RenderingStates.FINISHED;
this._convertCanvasToImage(canvas);
this.#convertCanvasToImage(canvas);

if (error) {
throw error;
Expand All @@ -280,7 +274,7 @@ class PDFThumbnailView {
// NOTE: To primarily avoid increasing memory usage too much, but also to
// reduce downsizing overhead, we purposely limit the up-scaling factor.
const { ctx, canvas, transform } =
this._getPageDrawContext(DRAW_UPSCALE_FACTOR);
this.#getPageDrawContext(DRAW_UPSCALE_FACTOR);
const drawViewport = this.viewport.clone({
scale: DRAW_UPSCALE_FACTOR * this.scale,
});
Expand Down Expand Up @@ -342,14 +336,11 @@ class PDFThumbnailView {
return;
}
this.renderingState = RenderingStates.FINISHED;
this._convertCanvasToImage(canvas);
this.#convertCanvasToImage(canvas);
}

/**
* @private
*/
_reduceImage(img) {
const { ctx, canvas } = this._getPageDrawContext();
#reduceImage(img) {
const { ctx, canvas } = this.#getPageDrawContext();

if (img.width <= 2 * canvas.width) {
ctx.drawImage(
Expand Down
32 changes: 10 additions & 22 deletions web/pdf_thumbnail_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,19 @@ class PDFThumbnailViewer {
this.renderingQueue = renderingQueue;
this.pageColors = pageColors || null;

this.scroll = watchScroll(this.container, this._scrollUpdated.bind(this));
this._resetView();
this.scroll = watchScroll(this.container, this.#scrollUpdated.bind(this));
this.#resetView();
}

/**
* @private
*/
_scrollUpdated() {
#scrollUpdated() {
this.renderingQueue.renderHighestPriority();
}

getThumbnail(index) {
return this._thumbnails[index];
}

/**
* @private
*/
_getVisibleThumbs() {
#getVisibleThumbs() {
return getVisibleElements({
scrollEl: this.container,
views: this._thumbnails,
Expand All @@ -107,7 +101,7 @@ class PDFThumbnailViewer {
// ... and add the highlight to the new thumbnail.
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 (views.length > 0) {
Expand Down Expand Up @@ -162,10 +156,7 @@ class PDFThumbnailViewer {
TempImageFactory.destroyCanvas();
}

/**
* @private
*/
_resetView() {
#resetView() {
this._thumbnails = [];
this._currentPageNumber = 1;
this._pageLabels = null;
Expand All @@ -180,8 +171,8 @@ class PDFThumbnailViewer {
*/
setDocument(pdfDocument) {
if (this.pdfDocument) {
this._cancelRendering();
this._resetView();
this.#cancelRendering();
this.#resetView();
}

this.pdfDocument = pdfDocument;
Expand Down Expand Up @@ -225,10 +216,7 @@ class PDFThumbnailViewer {
});
}

/**
* @private
*/
_cancelRendering() {
#cancelRendering() {
for (const thumbnail of this._thumbnails) {
thumbnail.cancelRendering();
}
Expand Down Expand Up @@ -287,7 +275,7 @@ class PDFThumbnailViewer {
}

forceRendering() {
const visibleThumbs = this._getVisibleThumbs();
const visibleThumbs = this.#getVisibleThumbs();
const scrollAhead = this.#getScrollAhead(visibleThumbs);
const thumbView = this.renderingQueue.getHighestPriority(
visibleThumbs,
Expand Down

0 comments on commit 862c27e

Please sign in to comment.