From 1df31c0284844d7193c1f79c5cd9ec717247a306 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Sat, 23 Sep 2023 15:37:13 +0200 Subject: [PATCH] Use one `noContextMenu` function in both the src/- and web/-folders Currently we duplicate this event handler function in multiple places, which seems unnecessary. --- src/display/display_utils.js | 8 ++++++++ src/display/editor/editor.js | 9 +++------ src/display/editor/ink.js | 18 +++--------------- src/pdf.js | 2 ++ test/unit/pdf_spec.js | 2 ++ web/toolbar.js | 5 ++--- web/ui_utils.js | 8 -------- 7 files changed, 20 insertions(+), 32 deletions(-) diff --git a/src/display/display_utils.js b/src/display/display_utils.js index e77f94a0c..b902c67c1 100644 --- a/src/display/display_utils.js +++ b/src/display/display_utils.js @@ -789,6 +789,13 @@ function isValidFetchUrl(url, baseUrl) { } } +/** + * Event handler to suppress context menu. + */ +function noContextMenu(e) { + e.preventDefault(); +} + /** * @param {string} src * @param {boolean} [removeScriptElement] @@ -1020,6 +1027,7 @@ export { isPdfFile, isValidFetchUrl, loadScript, + noContextMenu, PageViewport, PDFDateString, PixelsPerInch, diff --git a/src/display/editor/editor.js b/src/display/editor/editor.js index 4297eddbe..2de0aea40 100644 --- a/src/display/editor/editor.js +++ b/src/display/editor/editor.js @@ -20,6 +20,7 @@ import { bindEvents, ColorManager } from "./tools.js"; import { FeatureTest, shadow, unreachable } from "../../shared/util.js"; +import { noContextMenu } from "../display_utils.js"; /** * @typedef {Object} AnnotationEditorParameters @@ -589,10 +590,6 @@ class AnnotationEditor { return [0, 0]; } - static #noContextMenu(e) { - e.preventDefault(); - } - #createResizers() { if (this.#resizersDiv) { return; @@ -611,7 +608,7 @@ class AnnotationEditor { "pointerdown", this.#resizerPointerdown.bind(this, name) ); - div.addEventListener("contextmenu", AnnotationEditor.#noContextMenu); + div.addEventListener("contextmenu", noContextMenu); } this.div.prepend(this.#resizersDiv); } @@ -834,7 +831,7 @@ class AnnotationEditor { altText.textContent = msg; altText.setAttribute("aria-label", msg); altText.tabIndex = "0"; - altText.addEventListener("contextmenu", AnnotationEditor.#noContextMenu); + altText.addEventListener("contextmenu", noContextMenu); altText.addEventListener("pointerdown", event => event.stopPropagation()); altText.addEventListener( "click", diff --git a/src/display/editor/ink.js b/src/display/editor/ink.js index f56929b05..c475c6659 100644 --- a/src/display/editor/ink.js +++ b/src/display/editor/ink.js @@ -20,6 +20,7 @@ import { } from "../../shared/util.js"; import { AnnotationEditor } from "./editor.js"; import { InkAnnotationElement } from "../annotation_layer.js"; +import { noContextMenu } from "../display_utils.js"; import { opacityToHex } from "./tools.js"; /** @@ -30,8 +31,6 @@ class InkEditor extends AnnotationEditor { #baseWidth = 0; - #boundCanvasContextMenu = this.canvasContextMenu.bind(this); - #boundCanvasPointermove = this.canvasPointermove.bind(this); #boundCanvasPointerleave = this.canvasPointerleave.bind(this); @@ -360,7 +359,7 @@ class InkEditor extends AnnotationEditor { * @param {number} y */ #startDrawing(x, y) { - this.canvas.addEventListener("contextmenu", this.#boundCanvasContextMenu); + this.canvas.addEventListener("contextmenu", noContextMenu); this.canvas.addEventListener("pointerleave", this.#boundCanvasPointerleave); this.canvas.addEventListener("pointermove", this.#boundCanvasPointermove); this.canvas.addEventListener("pointerup", this.#boundCanvasPointerup); @@ -661,14 +660,6 @@ class InkEditor extends AnnotationEditor { this.#startDrawing(event.offsetX, event.offsetY); } - /** - * oncontextmenu callback for the canvas we're drawing on. - * @param {PointerEvent} event - */ - canvasContextMenu(event) { - event.preventDefault(); - } - /** * onpointermove callback for the canvas we're drawing on. * @param {PointerEvent} event @@ -714,10 +705,7 @@ class InkEditor extends AnnotationEditor { // Slight delay to avoid the context menu to appear (it can happen on a long // tap with a pen). setTimeout(() => { - this.canvas.removeEventListener( - "contextmenu", - this.#boundCanvasContextMenu - ); + this.canvas.removeEventListener("contextmenu", noContextMenu); }, 10); this.#stopDrawing(event.offsetX, event.offsetY); diff --git a/src/pdf.js b/src/pdf.js index 7c7b0e88e..8d54f67f7 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -61,6 +61,7 @@ import { isDataScheme, isPdfFile, loadScript, + noContextMenu, PDFDateString, PixelsPerInch, RenderingCancelledException, @@ -104,6 +105,7 @@ export { isPdfFile, loadScript, MissingPDFException, + noContextMenu, normalizeUnicode, OPS, PasswordResponses, diff --git a/test/unit/pdf_spec.js b/test/unit/pdf_spec.js index 6631c883f..f4668cf6f 100644 --- a/test/unit/pdf_spec.js +++ b/test/unit/pdf_spec.js @@ -50,6 +50,7 @@ import { isDataScheme, isPdfFile, loadScript, + noContextMenu, PDFDateString, PixelsPerInch, RenderingCancelledException, @@ -100,6 +101,7 @@ describe("pdfjs_api", function () { isPdfFile, loadScript, MissingPDFException, + noContextMenu, normalizeUnicode, OPS, PasswordResponses, diff --git a/web/toolbar.js b/web/toolbar.js index 051fabecc..550685e59 100644 --- a/web/toolbar.js +++ b/web/toolbar.js @@ -19,10 +19,9 @@ import { DEFAULT_SCALE_VALUE, MAX_SCALE, MIN_SCALE, - noContextMenuHandler, toggleCheckedBtn, } from "./ui_utils.js"; -import { AnnotationEditorType } from "pdfjs-lib"; +import { AnnotationEditorType, noContextMenu } from "pdfjs-lib"; const PAGE_NUMBER_LOADING_INDICATOR = "visiblePageIsLoading"; @@ -201,7 +200,7 @@ class Toolbar { } }); // Suppress context menus for some controls. - scaleSelect.oncontextmenu = noContextMenuHandler; + scaleSelect.oncontextmenu = noContextMenu; this.eventBus._on("localized", () => { this.#wasLocalized = true; diff --git a/web/ui_utils.js b/web/ui_utils.js index c03d23d15..0aebb5a40 100644 --- a/web/ui_utils.js +++ b/web/ui_utils.js @@ -604,13 +604,6 @@ function getVisibleElements({ return { first, last, views: visible, ids }; } -/** - * Event handler to suppress context menu. - */ -function noContextMenuHandler(evt) { - evt.preventDefault(); -} - function normalizeWheelEventDirection(evt) { let delta = Math.hypot(evt.deltaX, evt.deltaY); const angle = Math.atan2(evt.deltaY, evt.deltaX); @@ -881,7 +874,6 @@ export { MAX_AUTO_SCALE, MAX_SCALE, MIN_SCALE, - noContextMenuHandler, normalizeWheelEventDelta, normalizeWheelEventDirection, OutputScale,