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:
parent
dc19965d78
commit
063a072742
28
web/app.js
28
web/app.js
@ -1326,7 +1326,7 @@ const PDFViewerApplication = {
|
|||||||
this._initializePdfHistory({
|
this._initializePdfHistory({
|
||||||
fingerprint: pdfDocument.fingerprint,
|
fingerprint: pdfDocument.fingerprint,
|
||||||
viewOnLoad,
|
viewOnLoad,
|
||||||
initialDest: openAction && openAction.dest,
|
initialDest: openAction?.dest,
|
||||||
});
|
});
|
||||||
const initialBookmark = this.initialBookmark;
|
const initialBookmark = this.initialBookmark;
|
||||||
|
|
||||||
@ -1776,11 +1776,11 @@ const PDFViewerApplication = {
|
|||||||
);
|
);
|
||||||
|
|
||||||
let pdfTitle;
|
let pdfTitle;
|
||||||
const infoTitle = info && info.Title;
|
const infoTitle = info?.Title;
|
||||||
if (infoTitle) {
|
if (infoTitle) {
|
||||||
pdfTitle = infoTitle;
|
pdfTitle = infoTitle;
|
||||||
}
|
}
|
||||||
const metadataTitle = metadata && metadata.get("dc:title");
|
const metadataTitle = metadata?.get("dc:title");
|
||||||
if (metadataTitle) {
|
if (metadataTitle) {
|
||||||
// Ghostscript can produce invalid 'dc:title' Metadata entries:
|
// Ghostscript can produce invalid 'dc:title' Metadata entries:
|
||||||
// - The title may be "Untitled" (fixes bug 1031612).
|
// - 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");
|
throw new Error("file origin does not match viewer's");
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
const message = ex && ex.message;
|
|
||||||
PDFViewerApplication.l10n
|
PDFViewerApplication.l10n
|
||||||
.get("loading_error", null, "An error occurred while loading the PDF.")
|
.get("loading_error", null, "An error occurred while loading the PDF.")
|
||||||
.then(loadingErrorMessage => {
|
.then(loadingErrorMessage => {
|
||||||
PDFViewerApplication.error(loadingErrorMessage, { message });
|
PDFViewerApplication.error(loadingErrorMessage, {
|
||||||
|
message: ex?.message,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
throw ex;
|
throw ex;
|
||||||
}
|
}
|
||||||
@ -2409,7 +2410,7 @@ function reportPageStatsPDFBug({ pageNumber }) {
|
|||||||
const pageView = PDFViewerApplication.pdfViewer.getPageView(
|
const pageView = PDFViewerApplication.pdfViewer.getPageView(
|
||||||
/* index = */ pageNumber - 1
|
/* index = */ pageNumber - 1
|
||||||
);
|
);
|
||||||
const pageStats = pageView && pageView.pdfPage && pageView.pdfPage.stats;
|
const pageStats = pageView?.pdfPage?.stats;
|
||||||
if (!pageStats) {
|
if (!pageStats) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -2714,8 +2715,7 @@ function webViewerUpdateViewarea(evt) {
|
|||||||
const currentPage = PDFViewerApplication.pdfViewer.getPageView(
|
const currentPage = PDFViewerApplication.pdfViewer.getPageView(
|
||||||
/* index = */ PDFViewerApplication.page - 1
|
/* index = */ PDFViewerApplication.page - 1
|
||||||
);
|
);
|
||||||
const loading =
|
const loading = currentPage?.renderingState !== RenderingStates.FINISHED;
|
||||||
(currentPage && currentPage.renderingState) !== RenderingStates.FINISHED;
|
|
||||||
PDFViewerApplication.toolbar.updateLoadingIndicatorState(loading);
|
PDFViewerApplication.toolbar.updateLoadingIndicatorState(loading);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -2767,10 +2767,7 @@ function webViewerHashchange(evt) {
|
|||||||
let webViewerFileInputChange, webViewerOpenFile;
|
let webViewerFileInputChange, webViewerOpenFile;
|
||||||
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
|
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
|
||||||
webViewerFileInputChange = function (evt) {
|
webViewerFileInputChange = function (evt) {
|
||||||
if (
|
if (PDFViewerApplication.pdfViewer?.isInPresentationMode) {
|
||||||
PDFViewerApplication.pdfViewer &&
|
|
||||||
PDFViewerApplication.pdfViewer.isInPresentationMode
|
|
||||||
) {
|
|
||||||
return; // Opening a new PDF file isn't supported in Presentation Mode.
|
return; // Opening a new PDF file isn't supported in Presentation Mode.
|
||||||
}
|
}
|
||||||
const file = evt.fileInput.files[0];
|
const file = evt.fileInput.files[0];
|
||||||
@ -3108,8 +3105,7 @@ function webViewerKeyDown(evt) {
|
|||||||
(evt.metaKey ? 8 : 0);
|
(evt.metaKey ? 8 : 0);
|
||||||
|
|
||||||
const pdfViewer = PDFViewerApplication.pdfViewer;
|
const pdfViewer = PDFViewerApplication.pdfViewer;
|
||||||
const isViewerInPresentationMode =
|
const isViewerInPresentationMode = pdfViewer?.isInPresentationMode;
|
||||||
pdfViewer && pdfViewer.isInPresentationMode;
|
|
||||||
|
|
||||||
// First, handle the key bindings that are independent whether an input
|
// First, handle the key bindings that are independent whether an input
|
||||||
// control is selected or not.
|
// control is selected or not.
|
||||||
@ -3234,12 +3230,12 @@ function webViewerKeyDown(evt) {
|
|||||||
// Some shortcuts should not get handled if a control/input element
|
// Some shortcuts should not get handled if a control/input element
|
||||||
// is selected.
|
// is selected.
|
||||||
const curElement = getActiveOrFocusedElement();
|
const curElement = getActiveOrFocusedElement();
|
||||||
const curElementTagName = curElement && curElement.tagName.toUpperCase();
|
const curElementTagName = curElement?.tagName.toUpperCase();
|
||||||
if (
|
if (
|
||||||
curElementTagName === "INPUT" ||
|
curElementTagName === "INPUT" ||
|
||||||
curElementTagName === "TEXTAREA" ||
|
curElementTagName === "TEXTAREA" ||
|
||||||
curElementTagName === "SELECT" ||
|
curElementTagName === "SELECT" ||
|
||||||
(curElement && curElement.isContentEditable)
|
curElement?.isContentEditable
|
||||||
) {
|
) {
|
||||||
// Make sure that the secondary toolbar is closed when Escape is pressed.
|
// Make sure that the secondary toolbar is closed when Escape is pressed.
|
||||||
if (evt.keyCode !== /* Esc = */ 27) {
|
if (evt.keyCode !== /* Esc = */ 27) {
|
||||||
|
@ -240,7 +240,7 @@ class BaseViewer {
|
|||||||
// Prevent printing errors when 'disableAutoFetch' is set, by ensuring
|
// Prevent printing errors when 'disableAutoFetch' is set, by ensuring
|
||||||
// that *all* pages have in fact been completely loaded.
|
// that *all* pages have in fact been completely loaded.
|
||||||
return this._pages.every(function (pageView) {
|
return this._pages.every(function (pageView) {
|
||||||
return pageView && pageView.pdfPage;
|
return pageView?.pdfPage;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -132,7 +132,7 @@ function isRuntimeAvailable() {
|
|||||||
try {
|
try {
|
||||||
// When the extension is reloaded, the extension runtime is destroyed and
|
// When the extension is reloaded, the extension runtime is destroyed and
|
||||||
// the extension APIs become unavailable.
|
// the extension APIs become unavailable.
|
||||||
if (chrome.runtime && chrome.runtime.getManifest()) {
|
if (chrome.runtime?.getManifest()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
} catch (e) {}
|
} catch (e) {}
|
||||||
@ -181,7 +181,7 @@ function requestAccessToLocalFile(fileUrl, overlayManager, callback) {
|
|||||||
// These strings are from chrome/app/resources/generated_resources_*.xtb.
|
// These strings are from chrome/app/resources/generated_resources_*.xtb.
|
||||||
const i18nFileAccessLabel = PDFJSDev.json(
|
const i18nFileAccessLabel = PDFJSDev.json(
|
||||||
"$ROOT/web/chrome-i18n-allow-access-to-file-urls.json"
|
"$ROOT/web/chrome-i18n-allow-access-to-file-urls.json"
|
||||||
)[chrome.i18n.getUILanguage && chrome.i18n.getUILanguage()];
|
)[chrome.i18n.getUILanguage?.()];
|
||||||
|
|
||||||
if (i18nFileAccessLabel) {
|
if (i18nFileAccessLabel) {
|
||||||
document.getElementById(
|
document.getElementById(
|
||||||
@ -279,7 +279,7 @@ function setReferer(url, callback) {
|
|||||||
port.onMessage.addListener(onMessage);
|
port.onMessage.addListener(onMessage);
|
||||||
// Initiate the information exchange.
|
// Initiate the information exchange.
|
||||||
port.postMessage({
|
port.postMessage({
|
||||||
referer: window.history.state && window.history.state.chromecomState,
|
referer: window.history.state?.chromecomState,
|
||||||
requestUrl: url,
|
requestUrl: url,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -111,7 +111,7 @@ class PasswordPrompt {
|
|||||||
|
|
||||||
verify() {
|
verify() {
|
||||||
const password = this.input.value;
|
const password = this.input.value;
|
||||||
if (password && password.length > 0) {
|
if (password?.length > 0) {
|
||||||
this.close();
|
this.close();
|
||||||
this.updateCallback(password);
|
this.updateCallback(password);
|
||||||
}
|
}
|
||||||
|
@ -122,7 +122,7 @@ class PDFLayerViewer extends BaseTreeViewer {
|
|||||||
this._optionalContentConfig = optionalContentConfig || null;
|
this._optionalContentConfig = optionalContentConfig || null;
|
||||||
this._pdfDocument = pdfDocument || null;
|
this._pdfDocument = pdfDocument || null;
|
||||||
|
|
||||||
const groups = optionalContentConfig && optionalContentConfig.getOrder();
|
const groups = optionalContentConfig?.getOrder();
|
||||||
if (!groups) {
|
if (!groups) {
|
||||||
this._dispatchEvent(/* layersCount = */ 0);
|
this._dispatchEvent(/* layersCount = */ 0);
|
||||||
return;
|
return;
|
||||||
|
@ -445,7 +445,7 @@ class PDFLinkService {
|
|||||||
_cachedPageNumber(pageRef) {
|
_cachedPageNumber(pageRef) {
|
||||||
const refStr =
|
const refStr =
|
||||||
pageRef.gen === 0 ? `${pageRef.num}R` : `${pageRef.num}R${pageRef.gen}`;
|
pageRef.gen === 0 ? `${pageRef.num}R` : `${pageRef.num}R${pageRef.gen}`;
|
||||||
return (this._pagesRefCache && this._pagesRefCache[refStr]) || null;
|
return this._pagesRefCache?.[refStr] || null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -199,8 +199,7 @@ class PDFPageView {
|
|||||||
const childNodes = div.childNodes;
|
const childNodes = div.childNodes;
|
||||||
const currentZoomLayerNode = (keepZoomLayer && this.zoomLayer) || null;
|
const currentZoomLayerNode = (keepZoomLayer && this.zoomLayer) || null;
|
||||||
const currentAnnotationNode =
|
const currentAnnotationNode =
|
||||||
(keepAnnotations && this.annotationLayer && this.annotationLayer.div) ||
|
(keepAnnotations && this.annotationLayer?.div) || null;
|
||||||
null;
|
|
||||||
for (let i = childNodes.length - 1; i >= 0; i--) {
|
for (let i = childNodes.length - 1; i >= 0; i--) {
|
||||||
const node = childNodes[i];
|
const node = childNodes[i];
|
||||||
if (currentZoomLayerNode === node || currentAnnotationNode === node) {
|
if (currentZoomLayerNode === node || currentAnnotationNode === node) {
|
||||||
@ -437,7 +436,7 @@ class PDFPageView {
|
|||||||
canvasWrapper.style.height = div.style.height;
|
canvasWrapper.style.height = div.style.height;
|
||||||
canvasWrapper.classList.add("canvasWrapper");
|
canvasWrapper.classList.add("canvasWrapper");
|
||||||
|
|
||||||
if (this.annotationLayer && this.annotationLayer.div) {
|
if (this.annotationLayer?.div) {
|
||||||
// The annotation layer needs to stay on top.
|
// The annotation layer needs to stay on top.
|
||||||
div.insertBefore(canvasWrapper, this.annotationLayer.div);
|
div.insertBefore(canvasWrapper, this.annotationLayer.div);
|
||||||
} else {
|
} else {
|
||||||
@ -450,7 +449,7 @@ class PDFPageView {
|
|||||||
textLayerDiv.className = "textLayer";
|
textLayerDiv.className = "textLayer";
|
||||||
textLayerDiv.style.width = canvasWrapper.style.width;
|
textLayerDiv.style.width = canvasWrapper.style.width;
|
||||||
textLayerDiv.style.height = canvasWrapper.style.height;
|
textLayerDiv.style.height = canvasWrapper.style.height;
|
||||||
if (this.annotationLayer && this.annotationLayer.div) {
|
if (this.annotationLayer?.div) {
|
||||||
// The annotation layer needs to stay on top.
|
// The annotation layer needs to stay on top.
|
||||||
div.insertBefore(textLayerDiv, this.annotationLayer.div);
|
div.insertBefore(textLayerDiv, this.annotationLayer.div);
|
||||||
} else {
|
} else {
|
||||||
|
@ -331,7 +331,7 @@ class PDFSidebar {
|
|||||||
const pagesCount = pdfViewer.pagesCount;
|
const pagesCount = pdfViewer.pagesCount;
|
||||||
for (let pageIndex = 0; pageIndex < pagesCount; pageIndex++) {
|
for (let pageIndex = 0; pageIndex < pagesCount; pageIndex++) {
|
||||||
const pageView = pdfViewer.getPageView(pageIndex);
|
const pageView = pdfViewer.getPageView(pageIndex);
|
||||||
if (pageView && pageView.renderingState === RenderingStates.FINISHED) {
|
if (pageView?.renderingState === RenderingStates.FINISHED) {
|
||||||
const thumbnailView = pdfThumbnailViewer.getThumbnail(pageIndex);
|
const thumbnailView = pdfThumbnailViewer.getThumbnail(pageIndex);
|
||||||
thumbnailView.setImage(pageView);
|
thumbnailView.setImage(pageView);
|
||||||
}
|
}
|
||||||
|
@ -131,7 +131,7 @@ class PDFSidebarResizer {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.eventBus._on("sidebarviewchanged", evt => {
|
this.eventBus._on("sidebarviewchanged", evt => {
|
||||||
this.sidebarOpen = !!(evt && evt.view);
|
this.sidebarOpen = !!evt?.view;
|
||||||
});
|
});
|
||||||
|
|
||||||
this.eventBus._on("resize", evt => {
|
this.eventBus._on("resize", evt => {
|
||||||
|
@ -338,7 +338,7 @@ class TextLayerBuilder {
|
|||||||
clearedUntilDivIdx = match.end.divIdx + 1;
|
clearedUntilDivIdx = match.end.divIdx + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!findController || !findController.highlightMatches) {
|
if (!findController?.highlightMatches) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// Convert the matches on the `findController` into the match format
|
// Convert the matches on the `findController` into the match format
|
||||||
|
@ -787,7 +787,7 @@ function dispatchDOMEvent(eventName, args = null) {
|
|||||||
throw new Error("Not implemented: dispatchDOMEvent");
|
throw new Error("Not implemented: dispatchDOMEvent");
|
||||||
}
|
}
|
||||||
const details = Object.create(null);
|
const details = Object.create(null);
|
||||||
if (args && args.length > 0) {
|
if (args?.length > 0) {
|
||||||
const obj = args[0];
|
const obj = args[0];
|
||||||
for (const key in obj) {
|
for (const key in obj) {
|
||||||
const value = obj[key];
|
const value = obj[key];
|
||||||
@ -1022,7 +1022,7 @@ function getActiveOrFocusedElement() {
|
|||||||
let curActiveOrFocused =
|
let curActiveOrFocused =
|
||||||
curRoot.activeElement || curRoot.querySelector(":focus");
|
curRoot.activeElement || curRoot.querySelector(":focus");
|
||||||
|
|
||||||
while (curActiveOrFocused && curActiveOrFocused.shadowRoot) {
|
while (curActiveOrFocused?.shadowRoot) {
|
||||||
curRoot = curActiveOrFocused.shadowRoot;
|
curRoot = curActiveOrFocused.shadowRoot;
|
||||||
curActiveOrFocused =
|
curActiveOrFocused =
|
||||||
curRoot.activeElement || curRoot.querySelector(":focus");
|
curRoot.activeElement || curRoot.querySelector(":focus");
|
||||||
|
Loading…
x
Reference in New Issue
Block a user