From e1760aab8dc92b297af1016a74d6bab358aceac0 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 28 Nov 2024 13:25:56 +0100 Subject: [PATCH] Use the `stopEvent` helper function everywhere possible --- src/display/editor/highlight.js | 21 ++++++++++----------- src/display/editor/toolbar.js | 8 +++----- src/display/editor/tools.js | 4 ++-- src/pdf.js | 2 ++ test/unit/pdf_spec.js | 2 ++ web/app.js | 7 +++---- web/grab_to_pan.js | 5 +++-- web/pdf_viewer.js | 7 +++---- web/pdfjs.js | 2 ++ web/text_layer_builder.js | 5 ++--- 10 files changed, 32 insertions(+), 31 deletions(-) diff --git a/src/display/editor/highlight.js b/src/display/editor/highlight.js index d7df763af48af..f709285ef93b5 100644 --- a/src/display/editor/highlight.js +++ b/src/display/editor/highlight.js @@ -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. @@ -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( diff --git a/src/display/editor/toolbar.js b/src/display/editor/toolbar.js index 1d67778197096..b6a71f51a0b2c 100644 --- a/src/display/editor/toolbar.js +++ b/src/display/editor/toolbar.js @@ -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; @@ -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) { diff --git a/src/display/editor/tools.js b/src/display/editor/tools.js index 40b96897babba..325e85bcd2ad7 100644 --- a/src/display/editor/tools.js +++ b/src/display/editor/tools.js @@ -32,6 +32,7 @@ import { getColorValues, getRGB, PixelsPerInch, + stopEvent, } from "../display_utils.js"; import { HighlightToolbar } from "./toolbar.js"; @@ -501,8 +502,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); } } } diff --git a/src/pdf.js b/src/pdf.js index ecb6606dd9b14..b94e30193af62 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -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"; @@ -124,6 +125,7 @@ export { RenderingCancelledException, setLayerDimensions, shadow, + stopEvent, TextLayer, UnexpectedResponseException, Util, diff --git a/test/unit/pdf_spec.js b/test/unit/pdf_spec.js index f64b97a36bead..35681a52bdcca 100644 --- a/test/unit/pdf_spec.js +++ b/test/unit/pdf_spec.js @@ -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"; @@ -102,6 +103,7 @@ const expectedAPI = Object.freeze({ RenderingCancelledException, setLayerDimensions, shadow, + stopEvent, TextLayer, UnexpectedResponseException, Util, diff --git a/web/app.js b/web/app.js index 82fa056801e7c..5dc493d46a379 100644 --- a/web/app.js +++ b/web/app.js @@ -53,6 +53,7 @@ import { MissingPDFException, PDFWorker, shadow, + stopEvent, UnexpectedResponseException, version, } from "pdfjs-lib"; @@ -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; } } @@ -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, diff --git a/web/grab_to_pan.js b/web/grab_to_pan.js index b59e064182b81..05316a024975c 100644 --- a/web/grab_to_pan.js +++ b/web/grab_to_pan.js @@ -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"; @@ -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)) { diff --git a/web/pdf_viewer.js b/web/pdf_viewer.js index 9db9c6adcfdc6..6f05d56fe7c09 100644 --- a/web/pdf_viewer.js +++ b/web/pdf_viewer.js @@ -35,6 +35,7 @@ import { PermissionFlag, PixelsPerInch, shadow, + stopEvent, version, } from "pdfjs-lib"; import { @@ -748,8 +749,7 @@ class PDFViewer { this.#getAllTextInProgress || textLayerMode === TextLayerMode.ENABLE_PERMISSIONS ) { - event.preventDefault(); - event.stopPropagation(); + stopEvent(event); return; } this.#getAllTextInProgress = true; @@ -786,8 +786,7 @@ class PDFViewer { classList.remove("copyAll"); }); - event.preventDefault(); - event.stopPropagation(); + stopEvent(event); } } diff --git a/web/pdfjs.js b/web/pdfjs.js index e9b956dbdf401..2203abc42498c 100644 --- a/web/pdfjs.js +++ b/web/pdfjs.js @@ -51,6 +51,7 @@ const { RenderingCancelledException, setLayerDimensions, shadow, + stopEvent, TextLayer, UnexpectedResponseException, Util, @@ -97,6 +98,7 @@ export { RenderingCancelledException, setLayerDimensions, shadow, + stopEvent, TextLayer, UnexpectedResponseException, Util, diff --git a/web/text_layer_builder.js b/web/text_layer_builder.js index 1d8e2440a507b..f642382b60643 100644 --- a/web/text_layer_builder.js +++ b/web/text_layer_builder.js @@ -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"; /** @@ -162,8 +162,7 @@ class TextLayerBuilder { removeNullCharacters(normalizeUnicode(selection.toString())) ); } - event.preventDefault(); - event.stopPropagation(); + stopEvent(event); }); TextLayerBuilder.#textLayers.set(div, end);