1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-23 08:38:06 +02:00

[Editor] Disallow to have multiple pointers while dragging an editor

It'll let the user dragging with two fingers.
This commit is contained in:
Calixte Denizet 2024-11-26 15:59:10 +01:00
parent 079eb24621
commit e695d04ca2
4 changed files with 99 additions and 37 deletions

View file

@ -411,6 +411,11 @@ function noContextMenu(e) {
e.preventDefault();
}
function stopEvent(e) {
e.preventDefault();
e.stopPropagation();
}
// Deprecated API function -- display regardless of the `verbosity` setting.
function deprecated(details) {
// eslint-disable-next-line no-console
@ -657,5 +662,6 @@ export {
RenderingCancelledException,
setLayerDimensions,
StatTimer,
stopEvent,
SVG_NS,
};

View file

@ -23,9 +23,9 @@ import {
KeyboardManager,
} from "./tools.js";
import { FeatureTest, shadow, unreachable } from "../../shared/util.js";
import { noContextMenu, stopEvent } from "../display_utils.js";
import { AltText } from "./alt_text.js";
import { EditorToolbar } from "./toolbar.js";
import { noContextMenu } from "../display_utils.js";
/**
* @typedef {Object} AnnotationEditorParameters
@ -48,6 +48,10 @@ class AnnotationEditor {
#disabled = false;
#dragPointerId = null;
#dragPointerType = "";
#keepAspectRatio = false;
#resizersDiv = null;
@ -751,10 +755,7 @@ class AnnotationEditor {
);
window.addEventListener(
"touchmove",
e => {
// Prevent the page from scrolling.
e.preventDefault();
},
stopEvent /* Prevent the page from scrolling */,
{ passive: false, signal }
);
window.addEventListener("contextmenu", noContextMenu, { signal });
@ -1138,46 +1139,67 @@ class AnnotationEditor {
const ac = new AbortController();
const signal = this._uiManager.combinedSignal(ac);
const opts = { capture: true, passive: false, signal };
const cancelDrag = e => {
ac.abort();
this.#dragPointerId = null;
this.#hasBeenClicked = false;
if (!this._uiManager.endDragSession()) {
this.#selectOnPointerEvent(e);
}
};
if (isSelected) {
this.div.classList.add("moving");
this.#prevDragX = event.clientX;
this.#prevDragY = event.clientY;
const pointerMoveCallback = e => {
const { clientX: x, clientY: y } = e;
const [tx, ty] = this.screenToPageTranslation(
x - this.#prevDragX,
y - this.#prevDragY
);
this.#prevDragX = x;
this.#prevDragY = y;
this._uiManager.dragSelectedEditors(tx, ty);
};
window.addEventListener("pointermove", pointerMoveCallback, {
passive: true,
capture: true,
signal,
});
this.#dragPointerId = event.pointerId;
this.#dragPointerType = event.pointerType;
window.addEventListener(
"pointermove",
e => {
const { clientX: x, clientY: y, pointerId } = e;
if (pointerId !== this.#dragPointerId) {
stopEvent(e);
return;
}
const [tx, ty] = this.screenToPageTranslation(
x - this.#prevDragX,
y - this.#prevDragY
);
this.#prevDragX = x;
this.#prevDragY = y;
this._uiManager.dragSelectedEditors(tx, ty);
},
opts
);
window.addEventListener(
"touchmove",
stopEvent /* Prevent the page from scrolling */,
opts
);
window.addEventListener(
"pointerdown",
// If the user drags with one finger and then clicks with another.
e => {
// Prevent the page from scrolling.
e.preventDefault();
if (e.isPrimary && e.pointerType === this.#dragPointerType) {
// We cannot have two primaries at the same time.
// It's possible to be in this state with Firefox and Gnome when
// trying to drag with three fingers (see bug 1933716).
cancelDrag(e);
}
stopEvent(e);
},
{ passive: false, signal }
opts
);
}
const pointerUpCallback = () => {
ac.abort();
if (isSelected) {
this.div.classList.remove("moving");
}
this.#hasBeenClicked = false;
if (!this._uiManager.endDragSession()) {
this.#selectOnPointerEvent(event);
const pointerUpCallback = e => {
if (!this.#dragPointerId || this.#dragPointerId === e.pointerId) {
cancelDrag(e);
return;
}
stopEvent(e);
};
window.addEventListener("pointerup", pointerUpCallback, { signal });
// If the user is using alt+tab during the dragging session, the pointerup