Merge pull request #14926 from Snuffleupagus/rm-sourceEventType

Remove the `sourceEventType` from the viewer (bug 1757771 follow-up)
This commit is contained in:
Tim van der Meij 2022-05-21 12:34:54 +02:00 committed by GitHub
commit 42a6217427
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 40 deletions

View File

@ -811,7 +811,7 @@ const PDFViewerApplication = {
) { ) {
try { try {
// Trigger saving, to prevent data loss in forms; see issue 12257. // Trigger saving, to prevent data loss in forms; see issue 12257.
await this.save({ sourceEventType: "save" }); await this.save();
} catch (reason) { } catch (reason) {
// Ignoring errors, to ensure that document closing won't break. // Ignoring errors, to ensure that document closing won't break.
} }
@ -964,7 +964,7 @@ const PDFViewerApplication = {
throw new Error("PDF document not downloaded."); throw new Error("PDF document not downloaded.");
}, },
async download({ sourceEventType = "download" } = {}) { async download() {
const url = this._downloadUrl, const url = this._downloadUrl,
filename = this._docFilename; filename = this._docFilename;
try { try {
@ -973,7 +973,7 @@ const PDFViewerApplication = {
const data = await this.pdfDocument.getData(); const data = await this.pdfDocument.getData();
const blob = new Blob([data], { type: "application/pdf" }); const blob = new Blob([data], { type: "application/pdf" });
await this.downloadManager.download(blob, url, filename, sourceEventType); await this.downloadManager.download(blob, url, filename);
} catch (reason) { } catch (reason) {
// When the PDF document isn't ready, or the PDF file is still // When the PDF document isn't ready, or the PDF file is still
// downloading, simply download using the URL. // downloading, simply download using the URL.
@ -981,7 +981,7 @@ const PDFViewerApplication = {
} }
}, },
async save({ sourceEventType = "download" } = {}) { async save() {
if (this._saveInProgress) { if (this._saveInProgress) {
return; return;
} }
@ -996,23 +996,23 @@ const PDFViewerApplication = {
const data = await this.pdfDocument.saveDocument(); const data = await this.pdfDocument.saveDocument();
const blob = new Blob([data], { type: "application/pdf" }); const blob = new Blob([data], { type: "application/pdf" });
await this.downloadManager.download(blob, url, filename, sourceEventType); await this.downloadManager.download(blob, url, filename);
} catch (reason) { } catch (reason) {
// When the PDF document isn't ready, or the PDF file is still // When the PDF document isn't ready, or the PDF file is still
// downloading, simply fallback to a "regular" download. // downloading, simply fallback to a "regular" download.
console.error(`Error when saving the document: ${reason.message}`); console.error(`Error when saving the document: ${reason.message}`);
await this.download({ sourceEventType }); await this.download();
} finally { } finally {
await this.pdfScriptingManager.dispatchDidSave(); await this.pdfScriptingManager.dispatchDidSave();
this._saveInProgress = false; this._saveInProgress = false;
} }
}, },
downloadOrSave(options) { downloadOrSave() {
if (this.pdfDocument?.annotationStorage.size > 0) { if (this.pdfDocument?.annotationStorage.size > 0) {
this.save(options); this.save();
} else { } else {
this.download(options); this.download();
} }
}, },
@ -1875,7 +1875,6 @@ const PDFViewerApplication = {
eventBus._on("presentationmode", webViewerPresentationMode); eventBus._on("presentationmode", webViewerPresentationMode);
eventBus._on("print", webViewerPrint); eventBus._on("print", webViewerPrint);
eventBus._on("download", webViewerDownload); eventBus._on("download", webViewerDownload);
eventBus._on("save", webViewerSave);
eventBus._on("firstpage", webViewerFirstPage); eventBus._on("firstpage", webViewerFirstPage);
eventBus._on("lastpage", webViewerLastPage); eventBus._on("lastpage", webViewerLastPage);
eventBus._on("nextpage", webViewerNextPage); eventBus._on("nextpage", webViewerNextPage);
@ -1973,7 +1972,6 @@ const PDFViewerApplication = {
eventBus._off("presentationmode", webViewerPresentationMode); eventBus._off("presentationmode", webViewerPresentationMode);
eventBus._off("print", webViewerPrint); eventBus._off("print", webViewerPrint);
eventBus._off("download", webViewerDownload); eventBus._off("download", webViewerDownload);
eventBus._off("save", webViewerSave);
eventBus._off("firstpage", webViewerFirstPage); eventBus._off("firstpage", webViewerFirstPage);
eventBus._off("lastpage", webViewerLastPage); eventBus._off("lastpage", webViewerLastPage);
eventBus._off("nextpage", webViewerNextPage); eventBus._off("nextpage", webViewerNextPage);
@ -2330,7 +2328,7 @@ function webViewerNamedAction(evt) {
break; break;
case "SaveAs": case "SaveAs":
webViewerSave(); PDFViewerApplication.downloadOrSave();
break; break;
} }
} }
@ -2461,10 +2459,7 @@ function webViewerPrint() {
PDFViewerApplication.triggerPrinting(); PDFViewerApplication.triggerPrinting();
} }
function webViewerDownload() { function webViewerDownload() {
PDFViewerApplication.downloadOrSave({ sourceEventType: "download" }); PDFViewerApplication.downloadOrSave();
}
function webViewerSave() {
PDFViewerApplication.downloadOrSave({ sourceEventType: "save" });
} }
function webViewerFirstPage() { function webViewerFirstPage() {
if (PDFViewerApplication.pdfDocument) { if (PDFViewerApplication.pdfDocument) {

View File

@ -109,13 +109,7 @@ class DownloadManager {
return false; return false;
} }
/** download(blob, url, filename) {
* @param sourceEventType {string} Used to signal what triggered the download.
* The version of PDF.js integrated with Firefox uses this to to determine
* which dialog to show. "save" triggers "save as" and "download" triggers
* the "open with" dialog.
*/
download(blob, url, filename, sourceEventType = "download") {
const blobUrl = URL.createObjectURL(blob); const blobUrl = URL.createObjectURL(blob);
download(blobUrl, filename); download(blobUrl, filename);
} }

View File

@ -116,13 +116,11 @@ class DownloadManager {
new Blob([data], { type: contentType }) new Blob([data], { type: contentType })
); );
FirefoxCom.requestAsync("download", { FirefoxCom.request("download", {
blobUrl, blobUrl,
originalUrl: blobUrl, originalUrl: blobUrl,
filename, filename,
isAttachment: true, isAttachment: true,
}).then(error => {
URL.revokeObjectURL(blobUrl);
}); });
} }
@ -158,21 +156,13 @@ class DownloadManager {
return false; return false;
} }
download(blob, url, filename, sourceEventType = "download") { download(blob, url, filename) {
const blobUrl = URL.createObjectURL(blob); const blobUrl = URL.createObjectURL(blob);
FirefoxCom.requestAsync("download", { FirefoxCom.request("download", {
blobUrl, blobUrl,
originalUrl: url, originalUrl: url,
filename, filename,
sourceEventType,
}).then(error => {
if (error) {
// If downloading failed in `PdfStreamConverter.jsm` it's very unlikely
// that attempting to fallback and re-download would be helpful here.
console.error("`ChromeActions.download` failed.");
}
URL.revokeObjectURL(blobUrl);
}); });
} }
} }
@ -275,7 +265,7 @@ class MozL10n {
if (!PDFViewerApplication.initialized) { if (!PDFViewerApplication.initialized) {
return; return;
} }
PDFViewerApplication.eventBus.dispatch(type, { source: window }); PDFViewerApplication.eventBus.dispatch("download", { source: window });
}; };
window.addEventListener("save", handleEvent); window.addEventListener("save", handleEvent);

View File

@ -267,9 +267,8 @@ class IDownloadManager {
* @param {Blob} blob * @param {Blob} blob
* @param {string} url * @param {string} url
* @param {string} filename * @param {string} filename
* @param {string} [sourceEventType]
*/ */
download(blob, url, filename, sourceEventType = "download") {} download(blob, url, filename) {}
} }
/** /**

View File

@ -311,7 +311,7 @@ class PDFScriptingManager {
this._pdfViewer.currentScaleValue = value; this._pdfViewer.currentScaleValue = value;
break; break;
case "SaveAs": case "SaveAs":
this._eventBus.dispatch("save", { source: this }); this._eventBus.dispatch("download", { source: this });
break; break;
case "FirstPage": case "FirstPage":
this._pdfViewer.currentPageNumber = 1; this._pdfViewer.currentPageNumber = 1;