Skip to content

Commit 1ff5040

Browse files
authored
Merge pull request #2934 from adumesny/master
more initMouseEvent replacement
2 parents a917afc + d575886 commit 1ff5040

File tree

2 files changed

+24
-61
lines changed

2 files changed

+24
-61
lines changed

src/dd-touch.ts

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
import { DDManager } from './dd-manager';
7+
import { Utils } from './gridstack';
78

89
/**
910
* Detect touch support - Windows Surface devices and other touch devices
@@ -51,26 +52,8 @@ function simulateMouseEvent(e: TouchEvent, simulatedType: string) {
5152
// Prevent "Ignored attempt to cancel a touchmove event with cancelable=false" errors
5253
if (e.cancelable) e.preventDefault();
5354

54-
const touch = e.changedTouches[0], simulatedEvent = new MouseEvent(simulatedType, {
55-
bubbles: true,
56-
composed: true,
57-
cancelable: true,
58-
view: window,
59-
detail: 1,
60-
screenX: touch.screenX,
61-
screenY: touch.screenY,
62-
clientX: touch.clientX,
63-
clientY: touch.clientY,
64-
ctrlKey: false,
65-
altKey: false,
66-
shiftKey: false,
67-
metaKey: false,
68-
button: 0,
69-
relatedTarget: null
70-
});
71-
7255
// Dispatch the simulated event to the target element
73-
e.target.dispatchEvent(simulatedEvent);
56+
Utils.simulateMouseEvent(e.changedTouches[0], simulatedType);
7457
}
7558

7659
/**
@@ -83,29 +66,8 @@ function simulatePointerMouseEvent(e: PointerEvent, simulatedType: string) {
8366
// Prevent "Ignored attempt to cancel a touchmove event with cancelable=false" errors
8467
if (e.cancelable) e.preventDefault();
8568

86-
const simulatedEvent = document.createEvent('MouseEvents');
87-
88-
// Initialize the simulated mouse event using the touch event's coordinates
89-
simulatedEvent.initMouseEvent(
90-
simulatedType, // type
91-
true, // bubbles
92-
true, // cancelable
93-
window, // view
94-
1, // detail
95-
e.screenX, // screenX
96-
e.screenY, // screenY
97-
e.clientX, // clientX
98-
e.clientY, // clientY
99-
false, // ctrlKey
100-
false, // altKey
101-
false, // shiftKey
102-
false, // metaKey
103-
0, // button
104-
null // relatedTarget
105-
);
106-
10769
// Dispatch the simulated event to the target element
108-
e.target.dispatchEvent(simulatedEvent);
70+
Utils.simulateMouseEvent(e, simulatedType);
10971
}
11072

11173

src/utils.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -562,26 +562,27 @@ export class Utils {
562562
return {...evt, ...obj} as unknown as T;
563563
}
564564

565-
/** copies the MouseEvent properties and sends it as another event to the given target */
566-
public static simulateMouseEvent(e: MouseEvent, simulatedType: string, target?: EventTarget): void {
567-
const simulatedEvent = document.createEvent('MouseEvents');
568-
simulatedEvent.initMouseEvent(
569-
simulatedType, // type
570-
true, // bubbles
571-
true, // cancelable
572-
window, // view
573-
1, // detail
574-
e.screenX, // screenX
575-
e.screenY, // screenY
576-
e.clientX, // clientX
577-
e.clientY, // clientY
578-
e.ctrlKey, // ctrlKey
579-
e.altKey, // altKey
580-
e.shiftKey, // shiftKey
581-
e.metaKey, // metaKey
582-
0, // button
583-
e.target // relatedTarget
584-
);
565+
/** copies the MouseEvent (or convert Touch) properties and sends it as another event to the given target */
566+
public static simulateMouseEvent(e: MouseEvent | Touch, simulatedType: string, target?: EventTarget): void {
567+
const me = e as MouseEvent;
568+
const simulatedEvent = new MouseEvent(simulatedType, {
569+
bubbles: true,
570+
composed: true,
571+
cancelable: true,
572+
view: window,
573+
detail: 1,
574+
screenX: e.screenX,
575+
screenY: e.screenY,
576+
clientX: e.clientX,
577+
clientY: e.clientY,
578+
ctrlKey: me.ctrlKey??false,
579+
altKey: me.altKey??false,
580+
shiftKey: me.shiftKey??false,
581+
metaKey: me.metaKey??false,
582+
button: 0,
583+
relatedTarget: e.target
584+
});
585+
585586
(target || e.target).dispatchEvent(simulatedEvent);
586587
}
587588

0 commit comments

Comments
 (0)