Use one noContextMenu function in both the src/- and web/-folders

Currently we duplicate this event handler function in multiple places, which seems unnecessary.
This commit is contained in:
Jonas Jenwald 2023-09-23 15:37:13 +02:00
parent a09b7228fb
commit 1df31c0284
7 changed files with 20 additions and 32 deletions

View File

@ -789,6 +789,13 @@ function isValidFetchUrl(url, baseUrl) {
} }
} }
/**
* Event handler to suppress context menu.
*/
function noContextMenu(e) {
e.preventDefault();
}
/** /**
* @param {string} src * @param {string} src
* @param {boolean} [removeScriptElement] * @param {boolean} [removeScriptElement]
@ -1020,6 +1027,7 @@ export {
isPdfFile, isPdfFile,
isValidFetchUrl, isValidFetchUrl,
loadScript, loadScript,
noContextMenu,
PageViewport, PageViewport,
PDFDateString, PDFDateString,
PixelsPerInch, PixelsPerInch,

View File

@ -20,6 +20,7 @@
import { bindEvents, ColorManager } from "./tools.js"; import { bindEvents, ColorManager } from "./tools.js";
import { FeatureTest, shadow, unreachable } from "../../shared/util.js"; import { FeatureTest, shadow, unreachable } from "../../shared/util.js";
import { noContextMenu } from "../display_utils.js";
/** /**
* @typedef {Object} AnnotationEditorParameters * @typedef {Object} AnnotationEditorParameters
@ -589,10 +590,6 @@ class AnnotationEditor {
return [0, 0]; return [0, 0];
} }
static #noContextMenu(e) {
e.preventDefault();
}
#createResizers() { #createResizers() {
if (this.#resizersDiv) { if (this.#resizersDiv) {
return; return;
@ -611,7 +608,7 @@ class AnnotationEditor {
"pointerdown", "pointerdown",
this.#resizerPointerdown.bind(this, name) this.#resizerPointerdown.bind(this, name)
); );
div.addEventListener("contextmenu", AnnotationEditor.#noContextMenu); div.addEventListener("contextmenu", noContextMenu);
} }
this.div.prepend(this.#resizersDiv); this.div.prepend(this.#resizersDiv);
} }
@ -834,7 +831,7 @@ class AnnotationEditor {
altText.textContent = msg; altText.textContent = msg;
altText.setAttribute("aria-label", msg); altText.setAttribute("aria-label", msg);
altText.tabIndex = "0"; altText.tabIndex = "0";
altText.addEventListener("contextmenu", AnnotationEditor.#noContextMenu); altText.addEventListener("contextmenu", noContextMenu);
altText.addEventListener("pointerdown", event => event.stopPropagation()); altText.addEventListener("pointerdown", event => event.stopPropagation());
altText.addEventListener( altText.addEventListener(
"click", "click",

View File

@ -20,6 +20,7 @@ import {
} from "../../shared/util.js"; } from "../../shared/util.js";
import { AnnotationEditor } from "./editor.js"; import { AnnotationEditor } from "./editor.js";
import { InkAnnotationElement } from "../annotation_layer.js"; import { InkAnnotationElement } from "../annotation_layer.js";
import { noContextMenu } from "../display_utils.js";
import { opacityToHex } from "./tools.js"; import { opacityToHex } from "./tools.js";
/** /**
@ -30,8 +31,6 @@ class InkEditor extends AnnotationEditor {
#baseWidth = 0; #baseWidth = 0;
#boundCanvasContextMenu = this.canvasContextMenu.bind(this);
#boundCanvasPointermove = this.canvasPointermove.bind(this); #boundCanvasPointermove = this.canvasPointermove.bind(this);
#boundCanvasPointerleave = this.canvasPointerleave.bind(this); #boundCanvasPointerleave = this.canvasPointerleave.bind(this);
@ -360,7 +359,7 @@ class InkEditor extends AnnotationEditor {
* @param {number} y * @param {number} y
*/ */
#startDrawing(x, y) { #startDrawing(x, y) {
this.canvas.addEventListener("contextmenu", this.#boundCanvasContextMenu); this.canvas.addEventListener("contextmenu", noContextMenu);
this.canvas.addEventListener("pointerleave", this.#boundCanvasPointerleave); this.canvas.addEventListener("pointerleave", this.#boundCanvasPointerleave);
this.canvas.addEventListener("pointermove", this.#boundCanvasPointermove); this.canvas.addEventListener("pointermove", this.#boundCanvasPointermove);
this.canvas.addEventListener("pointerup", this.#boundCanvasPointerup); this.canvas.addEventListener("pointerup", this.#boundCanvasPointerup);
@ -661,14 +660,6 @@ class InkEditor extends AnnotationEditor {
this.#startDrawing(event.offsetX, event.offsetY); 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. * onpointermove callback for the canvas we're drawing on.
* @param {PointerEvent} event * @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 // Slight delay to avoid the context menu to appear (it can happen on a long
// tap with a pen). // tap with a pen).
setTimeout(() => { setTimeout(() => {
this.canvas.removeEventListener( this.canvas.removeEventListener("contextmenu", noContextMenu);
"contextmenu",
this.#boundCanvasContextMenu
);
}, 10); }, 10);
this.#stopDrawing(event.offsetX, event.offsetY); this.#stopDrawing(event.offsetX, event.offsetY);

View File

@ -61,6 +61,7 @@ import {
isDataScheme, isDataScheme,
isPdfFile, isPdfFile,
loadScript, loadScript,
noContextMenu,
PDFDateString, PDFDateString,
PixelsPerInch, PixelsPerInch,
RenderingCancelledException, RenderingCancelledException,
@ -104,6 +105,7 @@ export {
isPdfFile, isPdfFile,
loadScript, loadScript,
MissingPDFException, MissingPDFException,
noContextMenu,
normalizeUnicode, normalizeUnicode,
OPS, OPS,
PasswordResponses, PasswordResponses,

View File

@ -50,6 +50,7 @@ import {
isDataScheme, isDataScheme,
isPdfFile, isPdfFile,
loadScript, loadScript,
noContextMenu,
PDFDateString, PDFDateString,
PixelsPerInch, PixelsPerInch,
RenderingCancelledException, RenderingCancelledException,
@ -100,6 +101,7 @@ describe("pdfjs_api", function () {
isPdfFile, isPdfFile,
loadScript, loadScript,
MissingPDFException, MissingPDFException,
noContextMenu,
normalizeUnicode, normalizeUnicode,
OPS, OPS,
PasswordResponses, PasswordResponses,

View File

@ -19,10 +19,9 @@ import {
DEFAULT_SCALE_VALUE, DEFAULT_SCALE_VALUE,
MAX_SCALE, MAX_SCALE,
MIN_SCALE, MIN_SCALE,
noContextMenuHandler,
toggleCheckedBtn, toggleCheckedBtn,
} from "./ui_utils.js"; } from "./ui_utils.js";
import { AnnotationEditorType } from "pdfjs-lib"; import { AnnotationEditorType, noContextMenu } from "pdfjs-lib";
const PAGE_NUMBER_LOADING_INDICATOR = "visiblePageIsLoading"; const PAGE_NUMBER_LOADING_INDICATOR = "visiblePageIsLoading";
@ -201,7 +200,7 @@ class Toolbar {
} }
}); });
// Suppress context menus for some controls. // Suppress context menus for some controls.
scaleSelect.oncontextmenu = noContextMenuHandler; scaleSelect.oncontextmenu = noContextMenu;
this.eventBus._on("localized", () => { this.eventBus._on("localized", () => {
this.#wasLocalized = true; this.#wasLocalized = true;

View File

@ -604,13 +604,6 @@ function getVisibleElements({
return { first, last, views: visible, ids }; return { first, last, views: visible, ids };
} }
/**
* Event handler to suppress context menu.
*/
function noContextMenuHandler(evt) {
evt.preventDefault();
}
function normalizeWheelEventDirection(evt) { function normalizeWheelEventDirection(evt) {
let delta = Math.hypot(evt.deltaX, evt.deltaY); let delta = Math.hypot(evt.deltaX, evt.deltaY);
const angle = Math.atan2(evt.deltaY, evt.deltaX); const angle = Math.atan2(evt.deltaY, evt.deltaX);
@ -881,7 +874,6 @@ export {
MAX_AUTO_SCALE, MAX_AUTO_SCALE,
MAX_SCALE, MAX_SCALE,
MIN_SCALE, MIN_SCALE,
noContextMenuHandler,
normalizeWheelEventDelta, normalizeWheelEventDelta,
normalizeWheelEventDirection, normalizeWheelEventDirection,
OutputScale, OutputScale,