Skip to content

Commit

Permalink
Merge pull request #19121 from Snuffleupagus/more-stopEvent
Browse files Browse the repository at this point in the history
Use the `stopEvent` helper function everywhere possible
  • Loading branch information
Snuffleupagus authored Nov 28, 2024
2 parents c784a24 + e1760aa commit 65f20b0
Show file tree
Hide file tree
Showing 10 changed files with 32 additions and 31 deletions.
21 changes: 10 additions & 11 deletions src/display/editor/highlight.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import {
HighlightAnnotationElement,
InkAnnotationElement,
} from "../annotation_layer.js";
import { noContextMenu, stopEvent } from "../display_utils.js";
import { AnnotationEditor } from "./editor.js";
import { ColorPicker } from "./color_picker.js";
import { noContextMenu } from "../display_utils.js";

/**
* Basic draw editor in order to generate an Highlight annotation.
Expand Down Expand Up @@ -778,22 +778,21 @@ class HighlightEditor extends AnnotationEditor {
const ac = new AbortController();
const signal = parent.combinedSignal(ac);

const pointerDown = e => {
// Avoid to have undesired clicks during the drawing.
e.preventDefault();
e.stopPropagation();
};
const pointerUpCallback = e => {
ac.abort();
this.#endHighlight(parent, e);
};
window.addEventListener("blur", pointerUpCallback, { signal });
window.addEventListener("pointerup", pointerUpCallback, { signal });
window.addEventListener("pointerdown", pointerDown, {
capture: true,
passive: false,
signal,
});
window.addEventListener(
"pointerdown",
stopEvent /* Avoid to have undesired clicks during the drawing. */,
{
capture: true,
passive: false,
signal,
}
);
window.addEventListener("contextmenu", noContextMenu, { signal });

textLayer.addEventListener(
Expand Down
8 changes: 3 additions & 5 deletions src/display/editor/toolbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* limitations under the License.
*/

import { noContextMenu } from "../display_utils.js";
import { noContextMenu, stopEvent } from "../display_utils.js";

class EditorToolbar {
#toolbar = null;
Expand Down Expand Up @@ -81,14 +81,12 @@ class EditorToolbar {

#focusIn(e) {
this.#editor._focusEventsAllowed = false;
e.preventDefault();
e.stopPropagation();
stopEvent(e);
}

#focusOut(e) {
this.#editor._focusEventsAllowed = true;
e.preventDefault();
e.stopPropagation();
stopEvent(e);
}

#addListenersToElement(element) {
Expand Down
4 changes: 2 additions & 2 deletions src/display/editor/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
getColorValues,
getRGB,
PixelsPerInch,
stopEvent,
} from "../display_utils.js";
import { HighlightToolbar } from "./toolbar.js";

Expand Down Expand Up @@ -516,8 +517,7 @@ class KeyboardManager {
// For example, ctrl+s in a FreeText must be handled by the viewer, hence
// the event must bubble.
if (!bubbles) {
event.stopPropagation();
event.preventDefault();
stopEvent(event);
}
}
}
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 {
PixelsPerInch,
RenderingCancelledException,
setLayerDimensions,
stopEvent,
} from "./display/display_utils.js";
import { AnnotationEditorLayer } from "./display/editor/annotation_editor_layer.js";
import { AnnotationEditorUIManager } from "./display/editor/tools.js";
Expand Down Expand Up @@ -124,6 +125,7 @@ export {
RenderingCancelledException,
setLayerDimensions,
shadow,
stopEvent,
TextLayer,
UnexpectedResponseException,
Util,
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 @@ -53,6 +53,7 @@ import {
PixelsPerInch,
RenderingCancelledException,
setLayerDimensions,
stopEvent,
} from "../../src/display/display_utils.js";
import { AnnotationEditorLayer } from "../../src/display/editor/annotation_editor_layer.js";
import { AnnotationEditorUIManager } from "../../src/display/editor/tools.js";
Expand Down Expand Up @@ -102,6 +103,7 @@ const expectedAPI = Object.freeze({
RenderingCancelledException,
setLayerDimensions,
shadow,
stopEvent,
TextLayer,
UnexpectedResponseException,
Util,
Expand Down
7 changes: 3 additions & 4 deletions web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import {
MissingPDFException,
PDFWorker,
shadow,
stopEvent,
UnexpectedResponseException,
version,
} from "pdfjs-lib";
Expand Down Expand Up @@ -715,8 +716,7 @@ const PDFViewerApplication = {
if (item.type === "application/pdf") {
evt.dataTransfer.dropEffect =
evt.dataTransfer.effectAllowed === "copy" ? "copy" : "move";
evt.preventDefault();
evt.stopPropagation();
stopEvent(evt);
return;
}
}
Expand All @@ -725,8 +725,7 @@ const PDFViewerApplication = {
if (evt.dataTransfer.files?.[0].type !== "application/pdf") {
return;
}
evt.preventDefault();
evt.stopPropagation();
stopEvent(evt);
eventBus.dispatch("fileinputchange", {
source: this,
fileInput: evt.dataTransfer,
Expand Down
5 changes: 3 additions & 2 deletions web/grab_to_pan.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { stopEvent } from "pdfjs-lib";

// Class name of element which can be grabbed.
const CSS_CLASS_GRAB = "grab-to-pan-grab";

Expand Down Expand Up @@ -131,8 +133,7 @@ class GrabToPan {
capture: true,
signal: this.#scrollAC.signal,
});
event.preventDefault();
event.stopPropagation();
stopEvent(event);

const focusedElement = document.activeElement;
if (focusedElement && !focusedElement.contains(event.target)) {
Expand Down
7 changes: 3 additions & 4 deletions web/pdf_viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
PermissionFlag,
PixelsPerInch,
shadow,
stopEvent,
version,
} from "pdfjs-lib";
import {
Expand Down Expand Up @@ -748,8 +749,7 @@ class PDFViewer {
this.#getAllTextInProgress ||
textLayerMode === TextLayerMode.ENABLE_PERMISSIONS
) {
event.preventDefault();
event.stopPropagation();
stopEvent(event);
return;
}
this.#getAllTextInProgress = true;
Expand Down Expand Up @@ -786,8 +786,7 @@ class PDFViewer {
classList.remove("copyAll");
});

event.preventDefault();
event.stopPropagation();
stopEvent(event);
}
}

Expand Down
2 changes: 2 additions & 0 deletions web/pdfjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const {
RenderingCancelledException,
setLayerDimensions,
shadow,
stopEvent,
TextLayer,
UnexpectedResponseException,
Util,
Expand Down Expand Up @@ -97,6 +98,7 @@ export {
RenderingCancelledException,
setLayerDimensions,
shadow,
stopEvent,
TextLayer,
UnexpectedResponseException,
Util,
Expand Down
5 changes: 2 additions & 3 deletions web/text_layer_builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
// eslint-disable-next-line max-len
/** @typedef {import("./text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */

import { normalizeUnicode, TextLayer } from "pdfjs-lib";
import { normalizeUnicode, stopEvent, TextLayer } from "pdfjs-lib";
import { removeNullCharacters } from "./ui_utils.js";

/**
Expand Down Expand Up @@ -162,8 +162,7 @@ class TextLayerBuilder {
removeNullCharacters(normalizeUnicode(selection.toString()))
);
}
event.preventDefault();
event.stopPropagation();
stopEvent(event);
});

TextLayerBuilder.#textLayers.set(div, end);
Expand Down

0 comments on commit 65f20b0

Please sign in to comment.