Merge pull request #12758 from Snuffleupagus/AnnotationStorage-rm-event

Run `AnnotationStorage.resetModified` when destroying the `PDFDocumentLoadingTask`/`PDFDocumentProxy`
This commit is contained in:
Tim van der Meij 2020-12-19 21:13:28 +01:00 committed by GitHub
commit 1c8ead133a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 20 deletions

View File

@ -2127,6 +2127,10 @@ class WorkerTransport {
this.setupMessageHandler(); this.setupMessageHandler();
} }
get loadingTaskSettled() {
return this.loadingTask._capability.settled;
}
destroy() { destroy() {
if (this.destroyCapability) { if (this.destroyCapability) {
return this.destroyCapability.promise; return this.destroyCapability.promise;
@ -2154,6 +2158,18 @@ class WorkerTransport {
// We also need to wait for the worker to finish its long running tasks. // We also need to wait for the worker to finish its long running tasks.
const terminated = this.messageHandler.sendWithPromise("Terminate", null); const terminated = this.messageHandler.sendWithPromise("Terminate", null);
waitOn.push(terminated); waitOn.push(terminated);
// Allow `AnnotationStorage`-related clean-up when destroying the document.
if (this.loadingTaskSettled) {
const annotationStorageResetModified = this.loadingTask.promise
.then(pdfDocument => {
// Avoid initializing the `annotationStorage` if it doesn't exist.
if (pdfDocument.hasOwnProperty("annotationStorage")) {
pdfDocument.annotationStorage.resetModified();
}
})
.catch(() => {});
waitOn.push(annotationStorageResetModified);
}
Promise.all(waitOn).then(() => { Promise.all(waitOn).then(() => {
this.commonObjs.clear(); this.commonObjs.clear();
this.fontLoader.clear(); this.fontLoader.clear();

View File

@ -1061,6 +1061,14 @@ const PDFViewerApplication = {
}); });
}, },
downloadOrSave(options) {
if (this.pdfDocument?.annotationStorage.size > 0) {
this.save(options);
} else {
this.download(options);
}
},
/** /**
* For PDF documents that contain e.g. forms and javaScript, we should only * For PDF documents that contain e.g. forms and javaScript, we should only
* trigger the fallback bar once the user has interacted with the page. * trigger the fallback bar once the user has interacted with the page.
@ -1280,14 +1288,6 @@ const PDFViewerApplication = {
this.pdfLinkService.setDocument(pdfDocument, baseDocumentUrl); this.pdfLinkService.setDocument(pdfDocument, baseDocumentUrl);
this.pdfDocumentProperties.setDocument(pdfDocument, this.url); this.pdfDocumentProperties.setDocument(pdfDocument, this.url);
const annotationStorage = pdfDocument.annotationStorage;
annotationStorage.onSetModified = function () {
window.addEventListener("beforeunload", beforeUnload);
};
annotationStorage.onResetModified = function () {
window.removeEventListener("beforeunload", beforeUnload);
};
const pdfViewer = this.pdfViewer; const pdfViewer = this.pdfViewer;
pdfViewer.setDocument(pdfDocument); pdfViewer.setDocument(pdfDocument);
const { firstPagePromise, onePageRendered, pagesPromise } = pdfViewer; const { firstPagePromise, onePageRendered, pagesPromise } = pdfViewer;
@ -1315,6 +1315,7 @@ const PDFViewerApplication = {
firstPagePromise.then(pdfPage => { firstPagePromise.then(pdfPage => {
this.loadingBar.setWidth(this.appConfig.viewerContainer); this.loadingBar.setWidth(this.appConfig.viewerContainer);
this._initializeAnnotationStorageCallbacks(pdfDocument);
Promise.all([ Promise.all([
animationStarted, animationStarted,
@ -1882,6 +1883,23 @@ const PDFViewerApplication = {
} }
}, },
/**
* @private
*/
_initializeAnnotationStorageCallbacks(pdfDocument) {
if (pdfDocument !== this.pdfDocument) {
return;
}
const { annotationStorage } = pdfDocument;
annotationStorage.onSetModified = function () {
window.addEventListener("beforeunload", beforeUnload);
};
annotationStorage.onResetModified = function () {
window.removeEventListener("beforeunload", beforeUnload);
};
},
setInitialView( setInitialView(
storedHash, storedHash,
{ rotation, sidebarView, scrollMode, spreadMode } = {} { rotation, sidebarView, scrollMode, spreadMode } = {}
@ -2743,21 +2761,11 @@ function webViewerPresentationMode() {
function webViewerPrint() { function webViewerPrint() {
PDFViewerApplication.triggerPrinting(); PDFViewerApplication.triggerPrinting();
} }
function webViewerDownloadOrSave(sourceEventType) {
if (
PDFViewerApplication.pdfDocument &&
PDFViewerApplication.pdfDocument.annotationStorage.size > 0
) {
PDFViewerApplication.save({ sourceEventType });
} else {
PDFViewerApplication.download({ sourceEventType });
}
}
function webViewerDownload() { function webViewerDownload() {
webViewerDownloadOrSave("download"); PDFViewerApplication.downloadOrSave({ sourceEventType: "download" });
} }
function webViewerSave() { function webViewerSave() {
webViewerDownloadOrSave("save"); PDFViewerApplication.downloadOrSave({ sourceEventType: "save" });
} }
function webViewerFirstPage() { function webViewerFirstPage() {
if (PDFViewerApplication.pdfDocument) { if (PDFViewerApplication.pdfDocument) {