Improve the larger event listeners in the web/app.js file

- Shorten the names of the event listeners.

 - Use `bind` to pass in the `PDFViewerApplication`-scope to the event handlers.
   This makes the event handler code (a lot) less verbose, and this change is possible now that we're removing event listeners with `AbortSignal`.

 - Move the GENERIC-only event listeners into the same pre-processor block.

*Note:* This patch reduces the size of the `gulp mozcentral` output by `~4.3` kilo-bytes, which isn't a lot but still cannot hurt.
This commit is contained in:
Jonas Jenwald 2024-08-01 13:56:18 +02:00
parent b80e552760
commit 22ec252193

View file

@ -1909,19 +1909,21 @@ const PDFViewerApplication = {
_eventBusAbortController: { signal }, _eventBusAbortController: { signal },
} = this; } = this;
eventBus._on("resize", webViewerResize, { signal }); eventBus._on("resize", onResize.bind(this), { signal });
eventBus._on("hashchange", webViewerHashchange, { signal }); eventBus._on("hashchange", onHashchange.bind(this), { signal });
eventBus._on("beforeprint", this.beforePrint.bind(this), { signal }); eventBus._on("beforeprint", this.beforePrint.bind(this), { signal });
eventBus._on("afterprint", this.afterPrint.bind(this), { signal }); eventBus._on("afterprint", this.afterPrint.bind(this), { signal });
eventBus._on("pagerender", webViewerPageRender, { signal }); eventBus._on("pagerender", onPageRender.bind(this), { signal });
eventBus._on("pagerendered", webViewerPageRendered, { signal }); eventBus._on("pagerendered", onPageRendered.bind(this), { signal });
eventBus._on("updateviewarea", webViewerUpdateViewarea, { signal }); eventBus._on("updateviewarea", onUpdateViewarea.bind(this), { signal });
eventBus._on("pagechanging", webViewerPageChanging, { signal }); eventBus._on("pagechanging", onPageChanging.bind(this), { signal });
eventBus._on("scalechanging", webViewerScaleChanging, { signal }); eventBus._on("scalechanging", onScaleChanging.bind(this), { signal });
eventBus._on("rotationchanging", webViewerRotationChanging, { signal }); eventBus._on("rotationchanging", onRotationChanging.bind(this), { signal });
eventBus._on("sidebarviewchanged", webViewerSidebarViewChanged, { signal }); eventBus._on("sidebarviewchanged", onSidebarViewChanged.bind(this), {
eventBus._on("pagemode", webViewerPageMode, { signal }); signal,
eventBus._on("namedaction", webViewerNamedAction, { signal }); });
eventBus._on("pagemode", onPageMode.bind(this), { signal });
eventBus._on("namedaction", onNamedAction.bind(this), { signal });
eventBus._on( eventBus._on(
"presentationmodechanged", "presentationmodechanged",
evt => (pdfViewer.presentationModeState = evt.state), evt => (pdfViewer.presentationModeState = evt.state),
@ -1949,7 +1951,9 @@ const PDFViewerApplication = {
eventBus._on("zoomin", this.zoomIn.bind(this), { signal }); eventBus._on("zoomin", this.zoomIn.bind(this), { signal });
eventBus._on("zoomout", this.zoomOut.bind(this), { signal }); eventBus._on("zoomout", this.zoomOut.bind(this), { signal });
eventBus._on("zoomreset", this.zoomReset.bind(this), { signal }); eventBus._on("zoomreset", this.zoomReset.bind(this), { signal });
eventBus._on("pagenumberchanged", webViewerPageNumberChanged, { signal }); eventBus._on("pagenumberchanged", onPageNumberChanged.bind(this), {
signal,
});
eventBus._on( eventBus._on(
"scalechanged", "scalechanged",
evt => (pdfViewer.currentScaleValue = evt.value), evt => (pdfViewer.currentScaleValue = evt.value),
@ -1965,28 +1969,36 @@ const PDFViewerApplication = {
eventBus._on("switchscrollmode", evt => (pdfViewer.scrollMode = evt.mode), { eventBus._on("switchscrollmode", evt => (pdfViewer.scrollMode = evt.mode), {
signal, signal,
}); });
eventBus._on("scrollmodechanged", webViewerScrollModeChanged, { signal }); eventBus._on("scrollmodechanged", onScrollModeChanged.bind(this), {
signal,
});
eventBus._on("switchspreadmode", evt => (pdfViewer.spreadMode = evt.mode), { eventBus._on("switchspreadmode", evt => (pdfViewer.spreadMode = evt.mode), {
signal, signal,
}); });
eventBus._on("spreadmodechanged", webViewerSpreadModeChanged, { signal }); eventBus._on("spreadmodechanged", onSpreadModeChanged.bind(this), {
eventBus._on("imagealttextsettings", webViewerImageAltTextSettings, { signal,
});
eventBus._on("imagealttextsettings", onImageAltTextSettings.bind(this), {
signal, signal,
}); });
eventBus._on("documentproperties", () => pdfDocumentProperties?.open(), { eventBus._on("documentproperties", () => pdfDocumentProperties?.open(), {
signal, signal,
}); });
eventBus._on("findfromurlhash", webViewerFindFromUrlHash, { signal }); eventBus._on("findfromurlhash", onFindFromUrlHash.bind(this), { signal });
eventBus._on("updatefindmatchescount", webViewerUpdateFindMatchesCount, { eventBus._on(
signal, "updatefindmatchescount",
}); onUpdateFindMatchesCount.bind(this),
eventBus._on("updatefindcontrolstate", webViewerUpdateFindControlState, { { signal }
signal, );
}); eventBus._on(
"updatefindcontrolstate",
onUpdateFindControlState.bind(this),
{ signal }
);
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
eventBus._on("fileinputchange", webViewerFileInputChange, { signal }); eventBus._on("fileinputchange", onFileInputChange.bind(this), { signal });
eventBus._on("openfile", webViewerOpenFile, { signal }); eventBus._on("openfile", onOpenFile.bind(this), { signal });
} }
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) { if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
eventBus._on( eventBus._on(
@ -2039,25 +2051,25 @@ const PDFViewerApplication = {
} }
addWindowResolutionChange(); addWindowResolutionChange();
window.addEventListener("wheel", webViewerWheel, { window.addEventListener("wheel", onWheel.bind(this), {
passive: false, passive: false,
signal, signal,
}); });
window.addEventListener("touchstart", webViewerTouchStart, { window.addEventListener("touchstart", onTouchStart.bind(this), {
passive: false, passive: false,
signal, signal,
}); });
window.addEventListener("touchmove", webViewerTouchMove, { window.addEventListener("touchmove", onTouchMove.bind(this), {
passive: false, passive: false,
signal, signal,
}); });
window.addEventListener("touchend", webViewerTouchEnd, { window.addEventListener("touchend", onTouchEnd.bind(this), {
passive: false, passive: false,
signal, signal,
}); });
window.addEventListener("click", webViewerClick, { signal }); window.addEventListener("click", onClick.bind(this), { signal });
window.addEventListener("keydown", webViewerKeyDown, { signal }); window.addEventListener("keydown", onKeyDown.bind(this), { signal });
window.addEventListener("keyup", webViewerKeyUp, { signal }); window.addEventListener("keyup", onKeyUp.bind(this), { signal });
window.addEventListener( window.addEventListener(
"resize", "resize",
() => eventBus.dispatch("resize", { source: window }), () => eventBus.dispatch("resize", { source: window }),
@ -2256,29 +2268,45 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
throw ex; throw ex;
} }
}; };
// eslint-disable-next-line no-var
var onFileInputChange = function (evt) {
if (this.pdfViewer?.isInPresentationMode) {
return; // Opening a new PDF file isn't supported in Presentation Mode.
}
const file = evt.fileInput.files[0];
this.open({
url: URL.createObjectURL(file),
originalUrl: file.name,
});
};
// eslint-disable-next-line no-var
var onOpenFile = function (evt) {
this._openFileInput?.click();
};
} }
function webViewerPageRender({ pageNumber }) { function onPageRender({ pageNumber }) {
// If the page is (the most) visible when it starts rendering, // If the page is (the most) visible when it starts rendering,
// ensure that the page number input loading indicator is displayed. // ensure that the page number input loading indicator is displayed.
if (pageNumber === PDFViewerApplication.page) { if (pageNumber === this.page) {
PDFViewerApplication.toolbar?.updateLoadingIndicatorState(true); this.toolbar?.updateLoadingIndicatorState(true);
} }
} }
function webViewerPageRendered({ pageNumber, error }) { function onPageRendered({ pageNumber, error }) {
// If the page is still visible when it has finished rendering, // If the page is still visible when it has finished rendering,
// ensure that the page number input loading indicator is hidden. // ensure that the page number input loading indicator is hidden.
if (pageNumber === PDFViewerApplication.page) { if (pageNumber === this.page) {
PDFViewerApplication.toolbar?.updateLoadingIndicatorState(false); this.toolbar?.updateLoadingIndicatorState(false);
} }
// Use the rendered page to set the corresponding thumbnail image. // Use the rendered page to set the corresponding thumbnail image.
if (PDFViewerApplication.pdfSidebar?.visibleView === SidebarView.THUMBS) { if (this.pdfSidebar?.visibleView === SidebarView.THUMBS) {
const pageView = PDFViewerApplication.pdfViewer.getPageView( const pageView = this.pdfViewer.getPageView(/* index = */ pageNumber - 1);
/* index = */ pageNumber - 1 const thumbnailView = this.pdfThumbnailViewer?.getThumbnail(
);
const thumbnailView = PDFViewerApplication.pdfThumbnailViewer?.getThumbnail(
/* index = */ pageNumber - 1 /* index = */ pageNumber - 1
); );
if (pageView) { if (pageView) {
@ -2287,11 +2315,11 @@ function webViewerPageRendered({ pageNumber, error }) {
} }
if (error) { if (error) {
PDFViewerApplication._otherError("pdfjs-rendering-error", error); this._otherError("pdfjs-rendering-error", error);
} }
} }
function webViewerPageMode({ mode }) { function onPageMode({ mode }) {
// Handle the 'pagemode' hash parameter, see also `PDFLinkService_setHash`. // Handle the 'pagemode' hash parameter, see also `PDFLinkService_setHash`.
let view; let view;
switch (mode) { switch (mode) {
@ -2315,49 +2343,48 @@ function webViewerPageMode({ mode }) {
console.error('Invalid "pagemode" hash parameter: ' + mode); console.error('Invalid "pagemode" hash parameter: ' + mode);
return; return;
} }
PDFViewerApplication.pdfSidebar?.switchView(view, /* forceOpen = */ true); this.pdfSidebar?.switchView(view, /* forceOpen = */ true);
} }
function webViewerNamedAction(evt) { function onNamedAction(evt) {
// Processing a couple of named actions that might be useful, see also // Processing a couple of named actions that might be useful, see also
// `PDFLinkService.executeNamedAction`. // `PDFLinkService.executeNamedAction`.
switch (evt.action) { switch (evt.action) {
case "GoToPage": case "GoToPage":
PDFViewerApplication.appConfig.toolbar?.pageNumber.select(); this.appConfig.toolbar?.pageNumber.select();
break; break;
case "Find": case "Find":
if (!PDFViewerApplication.supportsIntegratedFind) { if (!this.supportsIntegratedFind) {
PDFViewerApplication.findBar?.toggle(); this.findBar?.toggle();
} }
break; break;
case "Print": case "Print":
PDFViewerApplication.triggerPrinting(); this.triggerPrinting();
break; break;
case "SaveAs": case "SaveAs":
PDFViewerApplication.downloadOrSave(); this.downloadOrSave();
break; break;
} }
} }
function webViewerSidebarViewChanged({ view }) { function onSidebarViewChanged({ view }) {
PDFViewerApplication.pdfRenderingQueue.isThumbnailViewEnabled = this.pdfRenderingQueue.isThumbnailViewEnabled = view === SidebarView.THUMBS;
view === SidebarView.THUMBS;
if (PDFViewerApplication.isInitialViewSet) { if (this.isInitialViewSet) {
// Only update the storage when the document has been loaded *and* rendered. // Only update the storage when the document has been loaded *and* rendered.
PDFViewerApplication.store?.set("sidebarView", view).catch(() => { this.store?.set("sidebarView", view).catch(() => {
// Unable to write to storage. // Unable to write to storage.
}); });
} }
} }
function webViewerUpdateViewarea({ location }) { function onUpdateViewarea({ location }) {
if (PDFViewerApplication.isInitialViewSet) { if (this.isInitialViewSet) {
// Only update the storage when the document has been loaded *and* rendered. // Only update the storage when the document has been loaded *and* rendered.
PDFViewerApplication.store this.store
?.setMultiple({ ?.setMultiple({
page: location.pageNumber, page: location.pageNumber,
zoom: location.scale, zoom: location.scale,
@ -2369,41 +2396,32 @@ function webViewerUpdateViewarea({ location }) {
// Unable to write to storage. // Unable to write to storage.
}); });
} }
if (PDFViewerApplication.appConfig.secondaryToolbar) { if (this.appConfig.secondaryToolbar) {
const href = PDFViewerApplication.pdfLinkService.getAnchorUrl( this.appConfig.secondaryToolbar.viewBookmarkButton.href =
location.pdfOpenParams this.pdfLinkService.getAnchorUrl(location.pdfOpenParams);
);
PDFViewerApplication.appConfig.secondaryToolbar.viewBookmarkButton.href =
href;
} }
} }
function webViewerScrollModeChanged(evt) { function onScrollModeChanged(evt) {
if ( if (this.isInitialViewSet && !this.pdfViewer.isInPresentationMode) {
PDFViewerApplication.isInitialViewSet &&
!PDFViewerApplication.pdfViewer.isInPresentationMode
) {
// Only update the storage when the document has been loaded *and* rendered. // Only update the storage when the document has been loaded *and* rendered.
PDFViewerApplication.store?.set("scrollMode", evt.mode).catch(() => { this.store?.set("scrollMode", evt.mode).catch(() => {
// Unable to write to storage. // Unable to write to storage.
}); });
} }
} }
function webViewerSpreadModeChanged(evt) { function onSpreadModeChanged(evt) {
if ( if (this.isInitialViewSet && !this.pdfViewer.isInPresentationMode) {
PDFViewerApplication.isInitialViewSet &&
!PDFViewerApplication.pdfViewer.isInPresentationMode
) {
// Only update the storage when the document has been loaded *and* rendered. // Only update the storage when the document has been loaded *and* rendered.
PDFViewerApplication.store?.set("spreadMode", evt.mode).catch(() => { this.store?.set("spreadMode", evt.mode).catch(() => {
// Unable to write to storage. // Unable to write to storage.
}); });
} }
} }
function webViewerResize() { function onResize() {
const { pdfDocument, pdfViewer, pdfRenderingQueue } = PDFViewerApplication; const { pdfDocument, pdfViewer, pdfRenderingQueue } = this;
if (pdfRenderingQueue.printing && window.matchMedia("print").matches) { if (pdfRenderingQueue.printing && window.matchMedia("print").matches) {
// Work-around issue 15324 by ignoring "resize" events during printing. // Work-around issue 15324 by ignoring "resize" events during printing.
@ -2425,44 +2443,24 @@ function webViewerResize() {
pdfViewer.update(); pdfViewer.update();
} }
function webViewerHashchange(evt) { function onHashchange(evt) {
const hash = evt.hash; const hash = evt.hash;
if (!hash) { if (!hash) {
return; return;
} }
if (!PDFViewerApplication.isInitialViewSet) { if (!this.isInitialViewSet) {
PDFViewerApplication.initialBookmark = hash; this.initialBookmark = hash;
} else if (!PDFViewerApplication.pdfHistory?.popStateInProgress) { } else if (!this.pdfHistory?.popStateInProgress) {
PDFViewerApplication.pdfLinkService.setHash(hash); this.pdfLinkService.setHash(hash);
} }
} }
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { function onPageNumberChanged(evt) {
// eslint-disable-next-line no-var const { pdfViewer } = this;
var webViewerFileInputChange = function (evt) {
if (PDFViewerApplication.pdfViewer?.isInPresentationMode) {
return; // Opening a new PDF file isn't supported in Presentation Mode.
}
const file = evt.fileInput.files[0];
PDFViewerApplication.open({
url: URL.createObjectURL(file),
originalUrl: file.name,
});
};
// eslint-disable-next-line no-var
var webViewerOpenFile = function (evt) {
PDFViewerApplication._openFileInput?.click();
};
}
function webViewerPageNumberChanged(evt) {
const pdfViewer = PDFViewerApplication.pdfViewer;
// Note that for `<input type="number">` HTML elements, an empty string will // Note that for `<input type="number">` HTML elements, an empty string will
// be returned for non-number inputs; hence we simply do nothing in that case. // be returned for non-number inputs; hence we simply do nothing in that case.
if (evt.value !== "") { if (evt.value !== "") {
PDFViewerApplication.pdfLinkService.goToPage(evt.value); this.pdfLinkService.goToPage(evt.value);
} }
// Ensure that the page number input displays the correct value, even if the // Ensure that the page number input displays the correct value, even if the
@ -2471,15 +2469,15 @@ function webViewerPageNumberChanged(evt) {
evt.value !== pdfViewer.currentPageNumber.toString() && evt.value !== pdfViewer.currentPageNumber.toString() &&
evt.value !== pdfViewer.currentPageLabel evt.value !== pdfViewer.currentPageLabel
) { ) {
PDFViewerApplication.toolbar?.setPageNumber( this.toolbar?.setPageNumber(
pdfViewer.currentPageNumber, pdfViewer.currentPageNumber,
pdfViewer.currentPageLabel pdfViewer.currentPageLabel
); );
} }
} }
function webViewerImageAltTextSettings() { function onImageAltTextSettings() {
PDFViewerApplication.imageAltTextSettings?.open({ this.imageAltTextSettings?.open({
enableGuessAltText: AppOptions.get("enableGuessAltText"), enableGuessAltText: AppOptions.get("enableGuessAltText"),
enableNewAltTextWhenAddingImage: AppOptions.get( enableNewAltTextWhenAddingImage: AppOptions.get(
"enableNewAltTextWhenAddingImage" "enableNewAltTextWhenAddingImage"
@ -2487,8 +2485,8 @@ function webViewerImageAltTextSettings() {
}); });
} }
function webViewerFindFromUrlHash(evt) { function onFindFromUrlHash(evt) {
PDFViewerApplication.eventBus.dispatch("find", { this.eventBus.dispatch("find", {
source: evt.source, source: evt.source,
type: "", type: "",
query: evt.query, query: evt.query,
@ -2500,23 +2498,23 @@ function webViewerFindFromUrlHash(evt) {
}); });
} }
function webViewerUpdateFindMatchesCount({ matchesCount }) { function onUpdateFindMatchesCount({ matchesCount }) {
if (PDFViewerApplication.supportsIntegratedFind) { if (this.supportsIntegratedFind) {
PDFViewerApplication.externalServices.updateFindMatchesCount(matchesCount); this.externalServices.updateFindMatchesCount(matchesCount);
} else { } else {
PDFViewerApplication.findBar?.updateResultsCount(matchesCount); this.findBar?.updateResultsCount(matchesCount);
} }
} }
function webViewerUpdateFindControlState({ function onUpdateFindControlState({
state, state,
previous, previous,
entireWord, entireWord,
matchesCount, matchesCount,
rawQuery, rawQuery,
}) { }) {
if (PDFViewerApplication.supportsIntegratedFind) { if (this.supportsIntegratedFind) {
PDFViewerApplication.externalServices.updateFindControlState({ this.externalServices.updateFindControlState({
result: state, result: state,
findPrevious: previous, findPrevious: previous,
entireWord, entireWord,
@ -2524,52 +2522,48 @@ function webViewerUpdateFindControlState({
rawQuery, rawQuery,
}); });
} else { } else {
PDFViewerApplication.findBar?.updateUIState(state, previous, matchesCount); this.findBar?.updateUIState(state, previous, matchesCount);
} }
} }
function webViewerScaleChanging(evt) { function onScaleChanging(evt) {
PDFViewerApplication.toolbar?.setPageScale(evt.presetValue, evt.scale); this.toolbar?.setPageScale(evt.presetValue, evt.scale);
PDFViewerApplication.pdfViewer.update(); this.pdfViewer.update();
} }
function webViewerRotationChanging(evt) { function onRotationChanging(evt) {
if (PDFViewerApplication.pdfThumbnailViewer) { if (this.pdfThumbnailViewer) {
PDFViewerApplication.pdfThumbnailViewer.pagesRotation = evt.pagesRotation; this.pdfThumbnailViewer.pagesRotation = evt.pagesRotation;
} }
PDFViewerApplication.forceRendering(); this.forceRendering();
// Ensure that the active page doesn't change during rotation. // Ensure that the active page doesn't change during rotation.
PDFViewerApplication.pdfViewer.currentPageNumber = evt.pageNumber; this.pdfViewer.currentPageNumber = evt.pageNumber;
} }
function webViewerPageChanging({ pageNumber, pageLabel }) { function onPageChanging({ pageNumber, pageLabel }) {
PDFViewerApplication.toolbar?.setPageNumber(pageNumber, pageLabel); this.toolbar?.setPageNumber(pageNumber, pageLabel);
PDFViewerApplication.secondaryToolbar?.setPageNumber(pageNumber); this.secondaryToolbar?.setPageNumber(pageNumber);
if (PDFViewerApplication.pdfSidebar?.visibleView === SidebarView.THUMBS) { if (this.pdfSidebar?.visibleView === SidebarView.THUMBS) {
PDFViewerApplication.pdfThumbnailViewer?.scrollThumbnailIntoView( this.pdfThumbnailViewer?.scrollThumbnailIntoView(pageNumber);
pageNumber
);
} }
// Show/hide the loading indicator in the page number input element. // Show/hide the loading indicator in the page number input element.
const currentPage = PDFViewerApplication.pdfViewer.getPageView( const currentPage = this.pdfViewer.getPageView(/* index = */ pageNumber - 1);
/* index = */ pageNumber - 1 this.toolbar?.updateLoadingIndicatorState(
);
PDFViewerApplication.toolbar?.updateLoadingIndicatorState(
currentPage?.renderingState === RenderingStates.RUNNING currentPage?.renderingState === RenderingStates.RUNNING
); );
} }
function webViewerWheel(evt) { function onWheel(evt) {
const { const {
pdfViewer, pdfViewer,
supportsMouseWheelZoomCtrlKey, supportsMouseWheelZoomCtrlKey,
supportsMouseWheelZoomMetaKey, supportsMouseWheelZoomMetaKey,
supportsPinchToZoom, supportsPinchToZoom,
} = PDFViewerApplication; } = this;
if (pdfViewer.isInPresentationMode) { if (pdfViewer.isInPresentationMode) {
return; return;
@ -2598,7 +2592,7 @@ function webViewerWheel(evt) {
FeatureTest.platform.isMac; FeatureTest.platform.isMac;
const isPinchToZoom = const isPinchToZoom =
evt.ctrlKey && evt.ctrlKey &&
!PDFViewerApplication._isCtrlKeyDown && !this._isCtrlKeyDown &&
deltaMode === WheelEvent.DOM_DELTA_PIXEL && deltaMode === WheelEvent.DOM_DELTA_PIXEL &&
evt.deltaX === 0 && evt.deltaX === 0 &&
(Math.abs(scaleFactor - 1) < 0.05 || isBuiltInMac) && (Math.abs(scaleFactor - 1) < 0.05 || isBuiltInMac) &&
@ -2614,20 +2608,20 @@ function webViewerWheel(evt) {
evt.preventDefault(); evt.preventDefault();
// NOTE: this check must be placed *after* preventDefault. // NOTE: this check must be placed *after* preventDefault.
if ( if (
PDFViewerApplication._isScrolling || this._isScrolling ||
document.visibilityState === "hidden" || document.visibilityState === "hidden" ||
PDFViewerApplication.overlayManager.active this.overlayManager.active
) { ) {
return; return;
} }
if (isPinchToZoom && supportsPinchToZoom) { if (isPinchToZoom && supportsPinchToZoom) {
scaleFactor = PDFViewerApplication._accumulateFactor( scaleFactor = this._accumulateFactor(
pdfViewer.currentScale, pdfViewer.currentScale,
scaleFactor, scaleFactor,
"_wheelUnusedFactor" "_wheelUnusedFactor"
); );
PDFViewerApplication.updateZoom(null, scaleFactor, origin); this.updateZoom(null, scaleFactor, origin);
} else { } else {
const delta = normalizeWheelEventDirection(evt); const delta = normalizeWheelEventDirection(evt);
@ -2640,41 +2634,35 @@ function webViewerWheel(evt) {
// OSs have different defaults for the number lines. But we generally // OSs have different defaults for the number lines. But we generally
// want one "clicky" roll of the wheel (which produces one event) to // want one "clicky" roll of the wheel (which produces one event) to
// adjust the zoom by one step. // adjust the zoom by one step.
if (Math.abs(delta) >= 1) { //
ticks = Math.sign(delta);
} else {
// If we're getting fractional lines (I can't think of a scenario // If we're getting fractional lines (I can't think of a scenario
// this might actually happen), be safe and use the accumulator. // this might actually happen), be safe and use the accumulator.
ticks = PDFViewerApplication._accumulateTicks( ticks =
delta, Math.abs(delta) >= 1
"_wheelUnusedTicks" ? Math.sign(delta)
); : this._accumulateTicks(delta, "_wheelUnusedTicks");
}
} else { } else {
// pixel-based devices // pixel-based devices
const PIXELS_PER_LINE_SCALE = 30; const PIXELS_PER_LINE_SCALE = 30;
ticks = PDFViewerApplication._accumulateTicks( ticks = this._accumulateTicks(
delta / PIXELS_PER_LINE_SCALE, delta / PIXELS_PER_LINE_SCALE,
"_wheelUnusedTicks" "_wheelUnusedTicks"
); );
} }
PDFViewerApplication.updateZoom(ticks, null, origin); this.updateZoom(ticks, null, origin);
} }
} }
} }
function webViewerTouchStart(evt) { function onTouchStart(evt) {
if ( if (this.pdfViewer.isInPresentationMode || evt.touches.length < 2) {
PDFViewerApplication.pdfViewer.isInPresentationMode ||
evt.touches.length < 2
) {
return; return;
} }
evt.preventDefault(); evt.preventDefault();
if (evt.touches.length !== 2 || PDFViewerApplication.overlayManager.active) { if (evt.touches.length !== 2 || this.overlayManager.active) {
PDFViewerApplication._touchInfo = null; this._touchInfo = null;
return; return;
} }
@ -2682,7 +2670,7 @@ function webViewerTouchStart(evt) {
if (touch0.identifier > touch1.identifier) { if (touch0.identifier > touch1.identifier) {
[touch0, touch1] = [touch1, touch0]; [touch0, touch1] = [touch1, touch0];
} }
PDFViewerApplication._touchInfo = { this._touchInfo = {
touch0X: touch0.pageX, touch0X: touch0.pageX,
touch0Y: touch0.pageY, touch0Y: touch0.pageY,
touch1X: touch1.pageX, touch1X: touch1.pageX,
@ -2690,12 +2678,12 @@ function webViewerTouchStart(evt) {
}; };
} }
function webViewerTouchMove(evt) { function onTouchMove(evt) {
if (!PDFViewerApplication._touchInfo || evt.touches.length !== 2) { if (!this._touchInfo || evt.touches.length !== 2) {
return; return;
} }
const { pdfViewer, _touchInfo, supportsPinchToZoom } = PDFViewerApplication; const { pdfViewer, _touchInfo, supportsPinchToZoom } = this;
let [touch0, touch1] = evt.touches; let [touch0, touch1] = evt.touches;
if (touch0.identifier > touch1.identifier) { if (touch0.identifier > touch1.identifier) {
[touch0, touch1] = [touch1, touch0]; [touch0, touch1] = [touch1, touch0];
@ -2764,61 +2752,61 @@ function webViewerTouchMove(evt) {
const distance = Math.hypot(page0X - page1X, page0Y - page1Y) || 1; const distance = Math.hypot(page0X - page1X, page0Y - page1Y) || 1;
const pDistance = Math.hypot(pTouch0X - pTouch1X, pTouch0Y - pTouch1Y) || 1; const pDistance = Math.hypot(pTouch0X - pTouch1X, pTouch0Y - pTouch1Y) || 1;
if (supportsPinchToZoom) { if (supportsPinchToZoom) {
const newScaleFactor = PDFViewerApplication._accumulateFactor( const newScaleFactor = this._accumulateFactor(
pdfViewer.currentScale, pdfViewer.currentScale,
distance / pDistance, distance / pDistance,
"_touchUnusedFactor" "_touchUnusedFactor"
); );
PDFViewerApplication.updateZoom(null, newScaleFactor, origin); this.updateZoom(null, newScaleFactor, origin);
} else { } else {
const PIXELS_PER_LINE_SCALE = 30; const PIXELS_PER_LINE_SCALE = 30;
const ticks = PDFViewerApplication._accumulateTicks( const ticks = this._accumulateTicks(
(distance - pDistance) / PIXELS_PER_LINE_SCALE, (distance - pDistance) / PIXELS_PER_LINE_SCALE,
"_touchUnusedTicks" "_touchUnusedTicks"
); );
PDFViewerApplication.updateZoom(ticks, null, origin); this.updateZoom(ticks, null, origin);
} }
} }
function webViewerTouchEnd(evt) { function onTouchEnd(evt) {
if (!PDFViewerApplication._touchInfo) { if (!this._touchInfo) {
return; return;
} }
evt.preventDefault(); evt.preventDefault();
PDFViewerApplication._touchInfo = null; this._touchInfo = null;
PDFViewerApplication._touchUnusedTicks = 0; this._touchUnusedTicks = 0;
PDFViewerApplication._touchUnusedFactor = 1; this._touchUnusedFactor = 1;
} }
function webViewerClick(evt) { function onClick(evt) {
if (!PDFViewerApplication.secondaryToolbar?.isOpen) { if (!this.secondaryToolbar?.isOpen) {
return; return;
} }
const appConfig = PDFViewerApplication.appConfig; const appConfig = this.appConfig;
if ( if (
PDFViewerApplication.pdfViewer.containsElement(evt.target) || this.pdfViewer.containsElement(evt.target) ||
(appConfig.toolbar?.container.contains(evt.target) && (appConfig.toolbar?.container.contains(evt.target) &&
evt.target !== appConfig.secondaryToolbar?.toggleButton) evt.target !== appConfig.secondaryToolbar?.toggleButton)
) { ) {
PDFViewerApplication.secondaryToolbar.close(); this.secondaryToolbar.close();
} }
} }
function webViewerKeyUp(evt) { function onKeyUp(evt) {
// evt.ctrlKey is false hence we use evt.key. // evt.ctrlKey is false hence we use evt.key.
if (evt.key === "Control") { if (evt.key === "Control") {
PDFViewerApplication._isCtrlKeyDown = false; this._isCtrlKeyDown = false;
} }
} }
function webViewerKeyDown(evt) { function onKeyDown(evt) {
PDFViewerApplication._isCtrlKeyDown = evt.key === "Control"; this._isCtrlKeyDown = evt.key === "Control";
if (PDFViewerApplication.overlayManager.active) { if (this.overlayManager.active) {
return; return;
} }
const { eventBus, pdfViewer } = PDFViewerApplication; const { eventBus, pdfViewer } = this;
const isViewerInPresentationMode = pdfViewer.isInPresentationMode; const isViewerInPresentationMode = pdfViewer.isInPresentationMode;
let handled = false, let handled = false,
@ -2835,14 +2823,14 @@ function webViewerKeyDown(evt) {
// either CTRL or META key with optional SHIFT. // either CTRL or META key with optional SHIFT.
switch (evt.keyCode) { switch (evt.keyCode) {
case 70: // f case 70: // f
if (!PDFViewerApplication.supportsIntegratedFind && !evt.shiftKey) { if (!this.supportsIntegratedFind && !evt.shiftKey) {
PDFViewerApplication.findBar?.open(); this.findBar?.open();
handled = true; handled = true;
} }
break; break;
case 71: // g case 71: // g
if (!PDFViewerApplication.supportsIntegratedFind) { if (!this.supportsIntegratedFind) {
const { state } = PDFViewerApplication.findController; const { state } = this.findController;
if (state) { if (state) {
const newState = { const newState = {
source: window, source: window,
@ -2858,40 +2846,37 @@ function webViewerKeyDown(evt) {
case 107: // FF '+' and '=' case 107: // FF '+' and '='
case 187: // Chrome '+' case 187: // Chrome '+'
case 171: // FF with German keyboard case 171: // FF with German keyboard
PDFViewerApplication.zoomIn(); this.zoomIn();
handled = true; handled = true;
break; break;
case 173: // FF/Mac '-' case 173: // FF/Mac '-'
case 109: // FF '-' case 109: // FF '-'
case 189: // Chrome '-' case 189: // Chrome '-'
PDFViewerApplication.zoomOut(); this.zoomOut();
handled = true; handled = true;
break; break;
case 48: // '0' case 48: // '0'
case 96: // '0' on Numpad of Swedish keyboard case 96: // '0' on Numpad of Swedish keyboard
if (!isViewerInPresentationMode) { if (!isViewerInPresentationMode) {
// keeping it unhandled (to restore page zoom to 100%) // keeping it unhandled (to restore page zoom to 100%)
setTimeout(function () { setTimeout(() => {
// ... and resetting the scale after browser adjusts its scale // ... and resetting the scale after browser adjusts its scale
PDFViewerApplication.zoomReset(); this.zoomReset();
}); });
handled = false; handled = false;
} }
break; break;
case 38: // up arrow case 38: // up arrow
if (isViewerInPresentationMode || PDFViewerApplication.page > 1) { if (isViewerInPresentationMode || this.page > 1) {
PDFViewerApplication.page = 1; this.page = 1;
handled = true; handled = true;
ensureViewerFocused = true; ensureViewerFocused = true;
} }
break; break;
case 40: // down arrow case 40: // down arrow
if ( if (isViewerInPresentationMode || this.page < this.pagesCount) {
isViewerInPresentationMode || this.page = this.pagesCount;
PDFViewerApplication.page < PDFViewerApplication.pagesCount
) {
PDFViewerApplication.page = PDFViewerApplication.pagesCount;
handled = true; handled = true;
ensureViewerFocused = true; ensureViewerFocused = true;
} }
@ -2922,17 +2907,17 @@ function webViewerKeyDown(evt) {
if (cmd === 3 || cmd === 10) { if (cmd === 3 || cmd === 10) {
switch (evt.keyCode) { switch (evt.keyCode) {
case 80: // p case 80: // p
PDFViewerApplication.requestPresentationMode(); this.requestPresentationMode();
handled = true; handled = true;
PDFViewerApplication.externalServices.reportTelemetry({ this.externalServices.reportTelemetry({
type: "buttons", type: "buttons",
data: { id: "presentationModeKeyboard" }, data: { id: "presentationModeKeyboard" },
}); });
break; break;
case 71: // g case 71: // g
// focuses input#pageNumber field // focuses input#pageNumber field
if (PDFViewerApplication.appConfig.toolbar) { if (this.appConfig.toolbar) {
PDFViewerApplication.appConfig.toolbar.pageNumber.select(); this.appConfig.toolbar.pageNumber.select();
handled = true; handled = true;
} }
break; break;
@ -2971,11 +2956,8 @@ function webViewerKeyDown(evt) {
turnOnlyIfPageFit = false; turnOnlyIfPageFit = false;
switch (evt.keyCode) { switch (evt.keyCode) {
case 38: // up arrow case 38: // up arrow
if (PDFViewerApplication.supportsCaretBrowsingMode) { if (this.supportsCaretBrowsingMode) {
PDFViewerApplication.moveCaret( this.moveCaret(/* isUp = */ true, /* select = */ false);
/* isUp = */ true,
/* select = */ false
);
handled = true; handled = true;
break; break;
} }
@ -2994,7 +2976,7 @@ function webViewerKeyDown(evt) {
turnPage = -1; turnPage = -1;
break; break;
case 37: // left arrow case 37: // left arrow
if (PDFViewerApplication.supportsCaretBrowsingMode) { if (this.supportsCaretBrowsingMode) {
return; return;
} }
// horizontal scrolling using arrow keys // horizontal scrolling using arrow keys
@ -3007,24 +2989,18 @@ function webViewerKeyDown(evt) {
turnPage = -1; turnPage = -1;
break; break;
case 27: // esc key case 27: // esc key
if (PDFViewerApplication.secondaryToolbar?.isOpen) { if (this.secondaryToolbar?.isOpen) {
PDFViewerApplication.secondaryToolbar.close(); this.secondaryToolbar.close();
handled = true; handled = true;
} }
if ( if (!this.supportsIntegratedFind && this.findBar?.opened) {
!PDFViewerApplication.supportsIntegratedFind && this.findBar.close();
PDFViewerApplication.findBar?.opened
) {
PDFViewerApplication.findBar.close();
handled = true; handled = true;
} }
break; break;
case 40: // down arrow case 40: // down arrow
if (PDFViewerApplication.supportsCaretBrowsingMode) { if (this.supportsCaretBrowsingMode) {
PDFViewerApplication.moveCaret( this.moveCaret(/* isUp = */ false, /* select = */ false);
/* isUp = */ false,
/* select = */ false
);
handled = true; handled = true;
break; break;
} }
@ -3044,7 +3020,7 @@ function webViewerKeyDown(evt) {
turnPage = 1; turnPage = 1;
break; break;
case 39: // right arrow case 39: // right arrow
if (PDFViewerApplication.supportsCaretBrowsingMode) { if (this.supportsCaretBrowsingMode) {
return; return;
} }
// horizontal scrolling using arrow keys // horizontal scrolling using arrow keys
@ -3058,36 +3034,33 @@ function webViewerKeyDown(evt) {
break; break;
case 36: // home case 36: // home
if (isViewerInPresentationMode || PDFViewerApplication.page > 1) { if (isViewerInPresentationMode || this.page > 1) {
PDFViewerApplication.page = 1; this.page = 1;
handled = true; handled = true;
ensureViewerFocused = true; ensureViewerFocused = true;
} }
break; break;
case 35: // end case 35: // end
if ( if (isViewerInPresentationMode || this.page < this.pagesCount) {
isViewerInPresentationMode || this.page = this.pagesCount;
PDFViewerApplication.page < PDFViewerApplication.pagesCount
) {
PDFViewerApplication.page = PDFViewerApplication.pagesCount;
handled = true; handled = true;
ensureViewerFocused = true; ensureViewerFocused = true;
} }
break; break;
case 83: // 's' case 83: // 's'
PDFViewerApplication.pdfCursorTools?.switchTool(CursorTool.SELECT); this.pdfCursorTools?.switchTool(CursorTool.SELECT);
break; break;
case 72: // 'h' case 72: // 'h'
PDFViewerApplication.pdfCursorTools?.switchTool(CursorTool.HAND); this.pdfCursorTools?.switchTool(CursorTool.HAND);
break; break;
case 82: // 'r' case 82: // 'r'
PDFViewerApplication.rotatePages(90); this.rotatePages(90);
break; break;
case 115: // F4 case 115: // F4
PDFViewerApplication.pdfSidebar?.toggle(); this.pdfSidebar?.toggle();
break; break;
} }
@ -3121,15 +3094,15 @@ function webViewerKeyDown(evt) {
break; break;
case 38: // up arrow case 38: // up arrow
PDFViewerApplication.moveCaret(/* isUp = */ true, /* select = */ true); this.moveCaret(/* isUp = */ true, /* select = */ true);
handled = true; handled = true;
break; break;
case 40: // down arrow case 40: // down arrow
PDFViewerApplication.moveCaret(/* isUp = */ false, /* select = */ true); this.moveCaret(/* isUp = */ false, /* select = */ true);
handled = true; handled = true;
break; break;
case 82: // 'r' case 82: // 'r'
PDFViewerApplication.rotatePages(-90); this.rotatePages(-90);
break; break;
} }
} }