Files
pdf.js/web/pdf_rendering_queue.js
Jonas Jenwald 9e98311293 [api-major] Remove support for using PDFPageView standalone
Currently the `PDFPageView` code, and even the CSS rules, contains a number of special-cases necessary to ensure that it works correctly when used without a surrounding viewer.
That goes back many years, and it was done since the `PDFViewer` could originally only show all pages (in a scrollable view) rather than just one page. However, since five years ago, the `PDFViewer` class also has a `PAGE` scroll-mode allowing only a single page to be shown and even before that the `PDFSinglePageViewer` class existed which allowed only one page to be displayed.

Note that using the `PDFPageView` class directly always had a number of downsides, since a lot of the PDF.js viewer-functionality assumes that a "full" viewer is available; for example it doesn't support searching, links, or adding/modifying annotations.

Furthermore, the Firefox PDF Viewer or the GENERIC PDF.js viewer doesn't need the standalone `PDFPageView` functionality which means that it does nothing except increasing the maintenance burden of the code.

Hence this patch removes all JavaScript and CSS code that only existed to support using `PDFPageView` standalone, and thus also removes the `pageviewer` viewer-component example.
However, the `PDFPageView` class is still being exported in the viewer-components to allow building a fully *custom* viewer.

*Note:* Users previously depending directly on `PDFPageView` are advised to switch to using the `PDFSinglePageViewer` class instead.
2026-11-04 14:10:16 +01:00

227 lines
6.1 KiB
JavaScript

/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** @typedef {import("./pdf_viewer").PDFViewer} PDFViewer */
// eslint-disable-next-line max-len
/** @typedef {import("./pdf_thumbnail_viewer").PDFThumbnailViewer} PDFThumbnailViewer */
/** @typedef {import("./renderable_view").RenderableView} RenderableView */
import { RenderingCancelledException } from "pdfjs-lib";
import { RenderingStates } from "./renderable_view.js";
const CLEANUP_TIMEOUT = 30000;
/**
* Controls rendering of the views for pages and thumbnails.
*/
class PDFRenderingQueue {
#highestPriorityPage = null;
#idleTimeout = null;
#pdfThumbnailViewer = null;
#pdfViewer = null;
isThumbnailViewEnabled = false;
onIdle = null;
printing = false;
/**
* @param {PDFViewer} pdfViewer
*/
setViewer(pdfViewer) {
this.#pdfViewer = pdfViewer;
}
/**
* @param {PDFThumbnailViewer} pdfThumbnailViewer
*/
setThumbnailViewer(pdfThumbnailViewer) {
this.#pdfThumbnailViewer = pdfThumbnailViewer;
}
/**
* @param {RenderableView} view
* @returns {boolean}
*/
isHighestPriority(view) {
return this.#highestPriorityPage === view.renderingId;
}
/**
* @param {object} currentlyVisiblePages
*/
renderHighestPriority(currentlyVisiblePages) {
if (this.#idleTimeout) {
clearTimeout(this.#idleTimeout);
this.#idleTimeout = null;
}
// Pages have a higher priority than thumbnails, so check them first.
if (this.#pdfViewer.forceRendering(currentlyVisiblePages)) {
return;
}
// No pages needed rendering, so check thumbnails.
if (
this.isThumbnailViewEnabled &&
this.#pdfThumbnailViewer?.forceRendering()
) {
return;
}
if (this.printing) {
// If printing is currently ongoing do not reschedule cleanup.
return;
}
if (this.onIdle) {
this.#idleTimeout = setTimeout(this.onIdle.bind(this), CLEANUP_TIMEOUT);
}
}
/**
* @param {object} visible
* @param {Array} views
* @param {boolean} scrolledDown
* @param {boolean} [preRenderExtra]
* @param {boolean} [ignoreDetailViews]
*/
getHighestPriority(
visible,
views,
scrolledDown,
preRenderExtra = false,
ignoreDetailViews = false
) {
/**
* The state has changed. Figure out which page has the highest priority to
* render next (if any).
*
* Priority:
* 1. visible pages
* 2. zoomed-in partial views of visible pages, unless `ignoreDetailViews`
* 3. if last scrolled down, the page after the visible pages, or
* if last scrolled up, the page before the visible pages
*/
const visibleViews = visible.views,
numVisible = visibleViews.length;
if (numVisible === 0) {
return null;
}
for (let i = 0; i < numVisible; i++) {
const view = visibleViews[i].view;
if (!this.isViewFinished(view)) {
return view;
}
}
if (!ignoreDetailViews) {
for (let i = 0; i < numVisible; i++) {
const { detailView } = visibleViews[i].view;
if (detailView && !this.isViewFinished(detailView)) {
return detailView;
}
}
}
const firstId = visible.first.id,
lastId = visible.last.id;
// All the visible views have rendered; try to handle any "holes" in the
// page layout (can happen e.g. with spreadModes at higher zoom levels).
if (lastId - firstId + 1 > numVisible) {
const visibleIds = visible.ids;
for (let i = 1, ii = lastId - firstId; i < ii; i++) {
const holeId = scrolledDown ? firstId + i : lastId - i;
if (visibleIds.has(holeId)) {
continue;
}
const holeView = views[holeId - 1];
if (!this.isViewFinished(holeView)) {
return holeView;
}
}
}
// All the visible views have rendered; try to render next/previous page.
// (IDs start at 1, so no need to add 1 when `scrolledDown === true`.)
let preRenderIndex = scrolledDown ? lastId : firstId - 2;
let preRenderView = views[preRenderIndex];
if (preRenderView && !this.isViewFinished(preRenderView)) {
return preRenderView;
}
if (preRenderExtra) {
preRenderIndex += scrolledDown ? 1 : -1;
preRenderView = views[preRenderIndex];
if (preRenderView && !this.isViewFinished(preRenderView)) {
return preRenderView;
}
}
// Everything that needs to be rendered has been.
return null;
}
/**
* @param {RenderableView} view
* @returns {boolean}
*/
isViewFinished(view) {
return view.renderingState === RenderingStates.FINISHED;
}
/**
* Render a page or thumbnail view. This calls the appropriate function
* based on the views state. If the view is already rendered it will return
* `false`.
* @param {RenderableView} view
*/
renderView(view) {
switch (view.renderingState) {
case RenderingStates.FINISHED:
return false;
case RenderingStates.PAUSED:
this.#highestPriorityPage = view.renderingId;
view.resume();
break;
case RenderingStates.RUNNING:
this.#highestPriorityPage = view.renderingId;
break;
case RenderingStates.INITIAL:
this.#highestPriorityPage = view.renderingId;
view
.draw()
.finally(() => {
this.renderHighestPriority();
})
.catch(reason => {
if (reason instanceof RenderingCancelledException) {
return;
}
console.error("renderView:", reason);
});
break;
}
return true;
}
}
export { PDFRenderingQueue };