mirror of
https://github.com/mozilla/pdf.js.git
synced 2025-04-20 15:18:08 +02:00
Merge pull request #15215 from Snuffleupagus/optional-content-initial
[api-minor] Improve how we disable `PDFThumbnailView.setImage` for documents with Optional Content
This commit is contained in:
commit
c7b71a3376
5 changed files with 140 additions and 62 deletions
|
@ -1856,6 +1856,7 @@ class BaseViewer {
|
|||
return Promise.resolve(null);
|
||||
}
|
||||
if (!this._optionalContentConfigPromise) {
|
||||
console.error("optionalContentConfigPromise: Not initialized yet.");
|
||||
// Prevent issues if the getter is accessed *before* the `onePageRendered`
|
||||
// promise has resolved; won't (normally) happen in the default viewer.
|
||||
return this.pdfDocument.getOptionalContentConfig();
|
||||
|
|
|
@ -99,6 +99,8 @@ const MAX_CANVAS_PIXELS = compatibilityParams.maxCanvasPixels || 16777216;
|
|||
class PDFPageView {
|
||||
#annotationMode = AnnotationMode.ENABLE_FORMS;
|
||||
|
||||
#useThumbnailCanvas = true;
|
||||
|
||||
/**
|
||||
* @param {PDFPageViewOptions} options
|
||||
*/
|
||||
|
@ -151,7 +153,12 @@ class PDFPageView {
|
|||
this.renderingState = RenderingStates.INITIAL;
|
||||
this.resume = null;
|
||||
this._renderError = null;
|
||||
this._isStandalone = !this.renderingQueue?.hasViewer();
|
||||
if (
|
||||
typeof PDFJSDev === "undefined" ||
|
||||
PDFJSDev.test("!PRODUCTION || GENERIC")
|
||||
) {
|
||||
this._isStandalone = !this.renderingQueue?.hasViewer();
|
||||
}
|
||||
|
||||
this._annotationCanvasMap = null;
|
||||
|
||||
|
@ -174,6 +181,26 @@ class PDFPageView {
|
|||
this.div = div;
|
||||
|
||||
container?.append(div);
|
||||
|
||||
if (
|
||||
(typeof PDFJSDev === "undefined" ||
|
||||
PDFJSDev.test("!PRODUCTION || GENERIC")) &&
|
||||
this._isStandalone
|
||||
) {
|
||||
const { optionalContentConfigPromise } = options;
|
||||
if (optionalContentConfigPromise) {
|
||||
// Ensure that the thumbnails always display the *initial* document
|
||||
// state.
|
||||
optionalContentConfigPromise.then(optionalContentConfig => {
|
||||
if (
|
||||
optionalContentConfigPromise !== this._optionalContentConfigPromise
|
||||
) {
|
||||
return;
|
||||
}
|
||||
this.#useThumbnailCanvas = optionalContentConfig.hasInitialVisibility;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
setPdfPage(pdfPage) {
|
||||
|
@ -359,7 +386,11 @@ class PDFPageView {
|
|||
|
||||
this.loadingIconDiv = document.createElement("div");
|
||||
this.loadingIconDiv.className = "loadingIcon notVisible";
|
||||
if (this._isStandalone) {
|
||||
if (
|
||||
(typeof PDFJSDev === "undefined" ||
|
||||
PDFJSDev.test("!PRODUCTION || GENERIC")) &&
|
||||
this._isStandalone
|
||||
) {
|
||||
this.toggleLoadingIconSpinner(/* viewVisible = */ true);
|
||||
}
|
||||
this.loadingIconDiv.setAttribute("role", "img");
|
||||
|
@ -376,6 +407,16 @@ class PDFPageView {
|
|||
}
|
||||
if (optionalContentConfigPromise instanceof Promise) {
|
||||
this._optionalContentConfigPromise = optionalContentConfigPromise;
|
||||
|
||||
// Ensure that the thumbnails always display the *initial* document state.
|
||||
optionalContentConfigPromise.then(optionalContentConfig => {
|
||||
if (
|
||||
optionalContentConfigPromise !== this._optionalContentConfigPromise
|
||||
) {
|
||||
return;
|
||||
}
|
||||
this.#useThumbnailCanvas = optionalContentConfig.hasInitialVisibility;
|
||||
});
|
||||
}
|
||||
|
||||
const totalRotation = (this.rotation + this.pdfPageRotate) % 360;
|
||||
|
@ -384,7 +425,11 @@ class PDFPageView {
|
|||
rotation: totalRotation,
|
||||
});
|
||||
|
||||
if (this._isStandalone) {
|
||||
if (
|
||||
(typeof PDFJSDev === "undefined" ||
|
||||
PDFJSDev.test("!PRODUCTION || GENERIC")) &&
|
||||
this._isStandalone
|
||||
) {
|
||||
docStyle.setProperty("--scale-factor", this.viewport.scale);
|
||||
}
|
||||
|
||||
|
@ -999,6 +1044,14 @@ class PDFPageView {
|
|||
this.div.removeAttribute("data-page-label");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* For use by the `PDFThumbnailView.setImage`-method.
|
||||
* @ignore
|
||||
*/
|
||||
get thumbnailCanvas() {
|
||||
return this.#useThumbnailCanvas ? this.canvas : null;
|
||||
}
|
||||
}
|
||||
|
||||
export { PDFPageView };
|
||||
|
|
|
@ -37,7 +37,6 @@ const THUMBNAIL_WIDTH = 98; // px
|
|||
* The default value is `null`.
|
||||
* @property {IPDFLinkService} linkService - The navigation/linking service.
|
||||
* @property {PDFRenderingQueue} renderingQueue - The rendering queue object.
|
||||
* @property {function} checkSetImageDisabled
|
||||
* @property {IL10n} l10n - Localization service.
|
||||
* @property {Object} [pageColors] - Overwrites background and foreground colors
|
||||
* with user defined ones in order to improve readability in high contrast
|
||||
|
@ -88,7 +87,6 @@ class PDFThumbnailView {
|
|||
optionalContentConfigPromise,
|
||||
linkService,
|
||||
renderingQueue,
|
||||
checkSetImageDisabled,
|
||||
l10n,
|
||||
pageColors,
|
||||
}) {
|
||||
|
@ -109,11 +107,6 @@ class PDFThumbnailView {
|
|||
this.renderTask = null;
|
||||
this.renderingState = RenderingStates.INITIAL;
|
||||
this.resume = null;
|
||||
this._checkSetImageDisabled =
|
||||
checkSetImageDisabled ||
|
||||
function () {
|
||||
return false;
|
||||
};
|
||||
|
||||
const pageWidth = this.viewport.width,
|
||||
pageHeight = this.viewport.height,
|
||||
|
@ -356,13 +349,10 @@ class PDFThumbnailView {
|
|||
}
|
||||
|
||||
setImage(pageView) {
|
||||
if (this._checkSetImageDisabled()) {
|
||||
return;
|
||||
}
|
||||
if (this.renderingState !== RenderingStates.INITIAL) {
|
||||
return;
|
||||
}
|
||||
const { canvas, pdfPage } = pageView;
|
||||
const { thumbnailCanvas: canvas, pdfPage } = pageView;
|
||||
if (!canvas) {
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -85,12 +85,6 @@ class PDFThumbnailViewer {
|
|||
|
||||
this.scroll = watchScroll(this.container, this._scrollUpdated.bind(this));
|
||||
this._resetView();
|
||||
|
||||
eventBus._on("optionalcontentconfigchanged", () => {
|
||||
// Ensure that the thumbnails always render with the *default* optional
|
||||
// content configuration.
|
||||
this._setImageDisabled = true;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -195,8 +189,6 @@ class PDFThumbnailViewer {
|
|||
this._currentPageNumber = 1;
|
||||
this._pageLabels = null;
|
||||
this._pagesRotation = 0;
|
||||
this._optionalContentConfigPromise = null;
|
||||
this._setImageDisabled = false;
|
||||
|
||||
// Remove the thumbnails from the DOM.
|
||||
this.container.textContent = "";
|
||||
|
@ -220,13 +212,8 @@ class PDFThumbnailViewer {
|
|||
|
||||
firstPagePromise
|
||||
.then(firstPdfPage => {
|
||||
this._optionalContentConfigPromise = optionalContentConfigPromise;
|
||||
|
||||
const pagesCount = pdfDocument.numPages;
|
||||
const viewport = firstPdfPage.getViewport({ scale: 1 });
|
||||
const checkSetImageDisabled = () => {
|
||||
return this._setImageDisabled;
|
||||
};
|
||||
|
||||
for (let pageNum = 1; pageNum <= pagesCount; ++pageNum) {
|
||||
const thumbnail = new PDFThumbnailView({
|
||||
|
@ -236,7 +223,6 @@ class PDFThumbnailViewer {
|
|||
optionalContentConfigPromise,
|
||||
linkService: this.linkService,
|
||||
renderingQueue: this.renderingQueue,
|
||||
checkSetImageDisabled,
|
||||
l10n: this.l10n,
|
||||
pageColors: this.pageColors,
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue