Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -1020,6 +1027,7 @@ export {
isPdfFile,
isValidFetchUrl,
loadScript,
noContextMenu,
PageViewport,
PDFDateString,
PixelsPerInch,
Expand Down
9 changes: 3 additions & 6 deletions src/display/editor/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -589,10 +590,6 @@ class AnnotationEditor {
return [0, 0];
}

static #noContextMenu(e) {
e.preventDefault();
}

#createResizers() {
if (this.#resizersDiv) {
return;
Expand All @@ -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);
}
Expand Down Expand Up @@ -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",
Expand Down
18 changes: 3 additions & 15 deletions src/display/editor/ink.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
isDataScheme,
isPdfFile,
loadScript,
noContextMenu,
PDFDateString,
PixelsPerInch,
RenderingCancelledException,
Expand Down Expand Up @@ -104,6 +105,7 @@ export {
isPdfFile,
loadScript,
MissingPDFException,
noContextMenu,
normalizeUnicode,
OPS,
PasswordResponses,
Expand Down
2 changes: 2 additions & 0 deletions test/unit/pdf_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
isDataScheme,
isPdfFile,
loadScript,
noContextMenu,
PDFDateString,
PixelsPerInch,
RenderingCancelledException,
Expand Down Expand Up @@ -100,6 +101,7 @@ describe("pdfjs_api", function () {
isPdfFile,
loadScript,
MissingPDFException,
noContextMenu,
normalizeUnicode,
OPS,
PasswordResponses,
Expand Down
5 changes: 2 additions & 3 deletions web/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 0 additions & 8 deletions web/ui_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -881,7 +874,6 @@ export {
MAX_AUTO_SCALE,
MAX_SCALE,
MIN_SCALE,
noContextMenuHandler,
normalizeWheelEventDelta,
normalizeWheelEventDirection,
OutputScale,
Expand Down