Skip to content

Commit 143ba30

Browse files
Merge pull request #14767 from Snuffleupagus/fileInput-refactor
[GENERIC viewer] Re-factor the `fileInput` initialization
2 parents 8f26e70 + 07ac5c3 commit 143ba30

File tree

4 files changed

+19
-42
lines changed

4 files changed

+19
-42
lines changed

web/app.js

+11-27
Original file line numberDiff line numberDiff line change
@@ -2085,7 +2085,7 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
20852085
"https://mozilla.github.io",
20862086
];
20872087
validateFileURL = function (file) {
2088-
if (file === undefined) {
2088+
if (!file) {
20892089
return;
20902090
}
20912091
try {
@@ -2142,7 +2142,7 @@ function reportPageStatsPDFBug({ pageNumber }) {
21422142
}
21432143

21442144
function webViewerInitialized() {
2145-
const appConfig = PDFViewerApplication.appConfig;
2145+
const { appConfig, eventBus } = PDFViewerApplication;
21462146
let file;
21472147
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
21482148
const queryString = document.location.search.substring(1);
@@ -2156,31 +2156,15 @@ function webViewerInitialized() {
21562156
}
21572157

21582158
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
2159-
const fileInput = document.createElement("input");
2160-
fileInput.id = appConfig.openFileInputName;
2161-
fileInput.className = "fileInput";
2162-
fileInput.setAttribute("type", "file");
2163-
fileInput.oncontextmenu = noContextMenuHandler;
2164-
document.body.appendChild(fileInput);
2165-
2166-
if (
2167-
!window.File ||
2168-
!window.FileReader ||
2169-
!window.FileList ||
2170-
!window.Blob
2171-
) {
2172-
appConfig.toolbar.openFile.hidden = true;
2173-
appConfig.secondaryToolbar.openFileButton.hidden = true;
2174-
} else {
2175-
fileInput.value = null;
2176-
}
2159+
const fileInput = appConfig.openFileInput;
2160+
fileInput.value = null;
21772161

21782162
fileInput.addEventListener("change", function (evt) {
2179-
const files = evt.target.files;
2163+
const { files } = evt.target;
21802164
if (!files || files.length === 0) {
21812165
return;
21822166
}
2183-
PDFViewerApplication.eventBus.dispatch("fileinputchange", {
2167+
eventBus.dispatch("fileinputchange", {
21842168
source: this,
21852169
fileInput: evt.target,
21862170
});
@@ -2195,11 +2179,11 @@ function webViewerInitialized() {
21952179
appConfig.mainContainer.addEventListener("drop", function (evt) {
21962180
evt.preventDefault();
21972181

2198-
const files = evt.dataTransfer.files;
2182+
const { files } = evt.dataTransfer;
21992183
if (!files || files.length === 0) {
22002184
return;
22012185
}
2202-
PDFViewerApplication.eventBus.dispatch("fileinputchange", {
2186+
eventBus.dispatch("fileinputchange", {
22032187
source: this,
22042188
fileInput: evt.dataTransfer,
22052189
});
@@ -2234,7 +2218,7 @@ function webViewerInitialized() {
22342218
"transitionend",
22352219
function (evt) {
22362220
if (evt.target === /* mainContainer */ this) {
2237-
PDFViewerApplication.eventBus.dispatch("resize", { source: this });
2221+
eventBus.dispatch("resize", { source: this });
22382222
}
22392223
},
22402224
true
@@ -2460,8 +2444,8 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
24602444
};
24612445

24622446
webViewerOpenFile = function (evt) {
2463-
const openFileInputName = PDFViewerApplication.appConfig.openFileInputName;
2464-
document.getElementById(openFileInputName).click();
2447+
const fileInput = PDFViewerApplication.appConfig.openFileInput;
2448+
fileInput.click();
24652449
};
24662450
}
24672451

web/viewer.css

-14
Original file line numberDiff line numberDiff line change
@@ -1364,16 +1364,6 @@ dialog :link {
13641364
clear: both;
13651365
}
13661366

1367-
.fileInput {
1368-
background: rgba(255, 255, 255, 1);
1369-
color: rgba(0, 0, 0, 1);
1370-
margin-top: 5px;
1371-
visibility: hidden;
1372-
position: fixed;
1373-
right: 0;
1374-
top: 0;
1375-
}
1376-
13771367
#PDFBug {
13781368
background: none repeat scroll 0 0 rgba(255, 255, 255, 1);
13791369
border: 1px solid rgba(102, 102, 102, 1);
@@ -1537,10 +1527,6 @@ dialog :link {
15371527
display: block;
15381528
}
15391529

1540-
.fileInput {
1541-
display: none;
1542-
}
1543-
15441530
/* Rules for browsers that support PDF.js printing */
15451531
body[data-pdfjsprinting] #outerContainer {
15461532
display: none;

web/viewer.html

+4
Original file line numberDiff line numberDiff line change
@@ -448,5 +448,9 @@
448448

449449
</div> <!-- outerContainer -->
450450
<div id="printContainer"></div>
451+
452+
<!--#if GENERIC -->
453+
<input type="file" id="fileInput" class="hidden">
454+
<!--#endif-->
451455
</body>
452456
</html>

web/viewer.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,10 @@ function getViewerConfiguration() {
189189
},
190190
errorWrapper,
191191
printContainer: document.getElementById("printContainer"),
192-
openFileInputName: "fileInput",
192+
openFileInput:
193+
typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")
194+
? document.getElementById("fileInput")
195+
: null,
193196
debuggerScriptPath: "./debugger.js",
194197
};
195198
}

0 commit comments

Comments
 (0)