Use optional chaining, where possible, in the web/-folder

By using optional chaining, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining, it's possible to reduce unnecessary code-repetition in many cases.
This commit is contained in:
Jonas Jenwald 2021-02-05 17:36:28 +01:00
parent dc19965d78
commit 063a072742
11 changed files with 27 additions and 32 deletions

View file

@ -1326,7 +1326,7 @@ const PDFViewerApplication = {
this._initializePdfHistory({
fingerprint: pdfDocument.fingerprint,
viewOnLoad,
initialDest: openAction && openAction.dest,
initialDest: openAction?.dest,
});
const initialBookmark = this.initialBookmark;
@ -1776,11 +1776,11 @@ const PDFViewerApplication = {
);
let pdfTitle;
const infoTitle = info && info.Title;
const infoTitle = info?.Title;
if (infoTitle) {
pdfTitle = infoTitle;
}
const metadataTitle = metadata && metadata.get("dc:title");
const metadataTitle = metadata?.get("dc:title");
if (metadataTitle) {
// Ghostscript can produce invalid 'dc:title' Metadata entries:
// - The title may be "Untitled" (fixes bug 1031612).
@ -2372,11 +2372,12 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
throw new Error("file origin does not match viewer's");
}
} catch (ex) {
const message = ex && ex.message;
PDFViewerApplication.l10n
.get("loading_error", null, "An error occurred while loading the PDF.")
.then(loadingErrorMessage => {
PDFViewerApplication.error(loadingErrorMessage, { message });
PDFViewerApplication.error(loadingErrorMessage, {
message: ex?.message,
});
});
throw ex;
}
@ -2409,7 +2410,7 @@ function reportPageStatsPDFBug({ pageNumber }) {
const pageView = PDFViewerApplication.pdfViewer.getPageView(
/* index = */ pageNumber - 1
);
const pageStats = pageView && pageView.pdfPage && pageView.pdfPage.stats;
const pageStats = pageView?.pdfPage?.stats;
if (!pageStats) {
return;
}
@ -2714,8 +2715,7 @@ function webViewerUpdateViewarea(evt) {
const currentPage = PDFViewerApplication.pdfViewer.getPageView(
/* index = */ PDFViewerApplication.page - 1
);
const loading =
(currentPage && currentPage.renderingState) !== RenderingStates.FINISHED;
const loading = currentPage?.renderingState !== RenderingStates.FINISHED;
PDFViewerApplication.toolbar.updateLoadingIndicatorState(loading);
}
@ -2767,10 +2767,7 @@ function webViewerHashchange(evt) {
let webViewerFileInputChange, webViewerOpenFile;
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
webViewerFileInputChange = function (evt) {
if (
PDFViewerApplication.pdfViewer &&
PDFViewerApplication.pdfViewer.isInPresentationMode
) {
if (PDFViewerApplication.pdfViewer?.isInPresentationMode) {
return; // Opening a new PDF file isn't supported in Presentation Mode.
}
const file = evt.fileInput.files[0];
@ -3108,8 +3105,7 @@ function webViewerKeyDown(evt) {
(evt.metaKey ? 8 : 0);
const pdfViewer = PDFViewerApplication.pdfViewer;
const isViewerInPresentationMode =
pdfViewer && pdfViewer.isInPresentationMode;
const isViewerInPresentationMode = pdfViewer?.isInPresentationMode;
// First, handle the key bindings that are independent whether an input
// control is selected or not.
@ -3234,12 +3230,12 @@ function webViewerKeyDown(evt) {
// Some shortcuts should not get handled if a control/input element
// is selected.
const curElement = getActiveOrFocusedElement();
const curElementTagName = curElement && curElement.tagName.toUpperCase();
const curElementTagName = curElement?.tagName.toUpperCase();
if (
curElementTagName === "INPUT" ||
curElementTagName === "TEXTAREA" ||
curElementTagName === "SELECT" ||
(curElement && curElement.isContentEditable)
curElement?.isContentEditable
) {
// Make sure that the secondary toolbar is closed when Escape is pressed.
if (evt.keyCode !== /* Esc = */ 27) {