From 5390d2b3773eea39c8e4851ced977bb02641df65 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 9 Apr 2022 16:30:02 +0200 Subject: [PATCH 1/2] [GENERIC viewer] Re-factor the `fileInput` initialization *This is yet another installment in a never-ending series of patches that attempt to simplify and improve old code.* The `fileInput`-element is used to support the "Open file"-button in the `GENERIC` viewer, however this is very old code. Rather than creating the element dynamically in JavaScript, we can simply define it conditionally in the HTML code thanks to the pre-processor. Furthermore, the `fileInput`-element currently has a number of unnecessary CSS rules, since the element is *purposely* never made visibly. Note that with these changes, the `fileInput`-element will now *always* have `display: none;` set. This shouldn't matter, since we can still trigger the `click`-event of the element just fine (via JavaScript) and this patch has been successfully tested in both Mozilla Firefox and Google Chrome. --- web/app.js | 24 +++++++++--------------- web/viewer.css | 14 -------------- web/viewer.html | 4 ++++ web/viewer.js | 5 ++++- 4 files changed, 17 insertions(+), 30 deletions(-) diff --git a/web/app.js b/web/app.js index bac696b45..16e8637bf 100644 --- a/web/app.js +++ b/web/app.js @@ -2142,7 +2142,7 @@ function reportPageStatsPDFBug({ pageNumber }) { } function webViewerInitialized() { - const appConfig = PDFViewerApplication.appConfig; + const { appConfig, eventBus } = PDFViewerApplication; let file; if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { const queryString = document.location.search.substring(1); @@ -2156,13 +2156,7 @@ function webViewerInitialized() { } if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { - const fileInput = document.createElement("input"); - fileInput.id = appConfig.openFileInputName; - fileInput.className = "fileInput"; - fileInput.setAttribute("type", "file"); - fileInput.oncontextmenu = noContextMenuHandler; - document.body.appendChild(fileInput); - + const fileInput = appConfig.openFileInput; if ( !window.File || !window.FileReader || @@ -2176,11 +2170,11 @@ function webViewerInitialized() { } fileInput.addEventListener("change", function (evt) { - const files = evt.target.files; + const { files } = evt.target; if (!files || files.length === 0) { return; } - PDFViewerApplication.eventBus.dispatch("fileinputchange", { + eventBus.dispatch("fileinputchange", { source: this, fileInput: evt.target, }); @@ -2195,11 +2189,11 @@ function webViewerInitialized() { appConfig.mainContainer.addEventListener("drop", function (evt) { evt.preventDefault(); - const files = evt.dataTransfer.files; + const { files } = evt.dataTransfer; if (!files || files.length === 0) { return; } - PDFViewerApplication.eventBus.dispatch("fileinputchange", { + eventBus.dispatch("fileinputchange", { source: this, fileInput: evt.dataTransfer, }); @@ -2234,7 +2228,7 @@ function webViewerInitialized() { "transitionend", function (evt) { if (evt.target === /* mainContainer */ this) { - PDFViewerApplication.eventBus.dispatch("resize", { source: this }); + eventBus.dispatch("resize", { source: this }); } }, true @@ -2460,8 +2454,8 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { }; webViewerOpenFile = function (evt) { - const openFileInputName = PDFViewerApplication.appConfig.openFileInputName; - document.getElementById(openFileInputName).click(); + const fileInput = PDFViewerApplication.appConfig.openFileInput; + fileInput.click(); }; } diff --git a/web/viewer.css b/web/viewer.css index e0c1a8a0f..97d81cf3d 100644 --- a/web/viewer.css +++ b/web/viewer.css @@ -1364,16 +1364,6 @@ dialog :link { clear: both; } -.fileInput { - background: rgba(255, 255, 255, 1); - color: rgba(0, 0, 0, 1); - margin-top: 5px; - visibility: hidden; - position: fixed; - right: 0; - top: 0; -} - #PDFBug { background: none repeat scroll 0 0 rgba(255, 255, 255, 1); border: 1px solid rgba(102, 102, 102, 1); @@ -1537,10 +1527,6 @@ dialog :link { display: block; } - .fileInput { - display: none; - } - /* Rules for browsers that support PDF.js printing */ body[data-pdfjsprinting] #outerContainer { display: none; diff --git a/web/viewer.html b/web/viewer.html index 10505b109..d33a36855 100644 --- a/web/viewer.html +++ b/web/viewer.html @@ -448,5 +448,9 @@ See https://github.com/adobe-type-tools/cmap-resources
+ + + + diff --git a/web/viewer.js b/web/viewer.js index de053070f..56f715ddc 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -189,7 +189,10 @@ function getViewerConfiguration() { }, errorWrapper, printContainer: document.getElementById("printContainer"), - openFileInputName: "fileInput", + openFileInput: + typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC") + ? document.getElementById("fileInput") + : null, debuggerScriptPath: "./debugger.js", }; } From 07ac5c337f695261c423dedb32699cfecb2586fd Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sun, 10 Apr 2022 12:50:35 +0200 Subject: [PATCH 2/2] [GENERIC viewer] Remove some, now unnecessary, checks used with `fileInput` According to the MDN compatibility data, see below, all of these features have been supported for years and years in all browsers. Looking closely at the data, the most likely reason for adding these checks in the first place was for IE 9 compatibility (since we originally "supported" that browser). - https://developer.mozilla.org/en-US/docs/Web/API/File#browser_compatibility - https://developer.mozilla.org/en-US/docs/Web/API/FileReader#browser_compatibility - https://developer.mozilla.org/en-US/docs/Web/API/FileList#browser_compatibility - https://developer.mozilla.org/en-US/docs/Web/API/Blob#browser_compatibility --- web/app.js | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/web/app.js b/web/app.js index 16e8637bf..8134c6817 100644 --- a/web/app.js +++ b/web/app.js @@ -2085,7 +2085,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { "https://mozilla.github.io", ]; validateFileURL = function (file) { - if (file === undefined) { + if (!file) { return; } try { @@ -2157,17 +2157,7 @@ function webViewerInitialized() { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { const fileInput = appConfig.openFileInput; - if ( - !window.File || - !window.FileReader || - !window.FileList || - !window.Blob - ) { - appConfig.toolbar.openFile.hidden = true; - appConfig.secondaryToolbar.openFileButton.hidden = true; - } else { - fileInput.value = null; - } + fileInput.value = null; fileInput.addEventListener("change", function (evt) { const { files } = evt.target;