Thanks to visit codestin.com
Credit goes to github.com

Skip to content

more initMouseEvent replacement #2934

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

Merged
merged 1 commit into from
Jan 26, 2025
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
44 changes: 3 additions & 41 deletions src/dd-touch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { DDManager } from './dd-manager';
import { Utils } from './gridstack';

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

const touch = e.changedTouches[0], simulatedEvent = new MouseEvent(simulatedType, {
bubbles: true,
composed: true,
cancelable: true,
view: window,
detail: 1,
screenX: touch.screenX,
screenY: touch.screenY,
clientX: touch.clientX,
clientY: touch.clientY,
ctrlKey: false,
altKey: false,
shiftKey: false,
metaKey: false,
button: 0,
relatedTarget: null
});

// Dispatch the simulated event to the target element
e.target.dispatchEvent(simulatedEvent);
Utils.simulateMouseEvent(e.changedTouches[0], simulatedType);
}

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

const simulatedEvent = document.createEvent('MouseEvents');

// Initialize the simulated mouse event using the touch event's coordinates
simulatedEvent.initMouseEvent(
simulatedType, // type
true, // bubbles
true, // cancelable
window, // view
1, // detail
e.screenX, // screenX
e.screenY, // screenY
e.clientX, // clientX
e.clientY, // clientY
false, // ctrlKey
false, // altKey
false, // shiftKey
false, // metaKey
0, // button
null // relatedTarget
);

// Dispatch the simulated event to the target element
e.target.dispatchEvent(simulatedEvent);
Utils.simulateMouseEvent(e, simulatedType);
}


Expand Down
41 changes: 21 additions & 20 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,26 +562,27 @@ export class Utils {
return {...evt, ...obj} as unknown as T;
}

/** copies the MouseEvent properties and sends it as another event to the given target */
public static simulateMouseEvent(e: MouseEvent, simulatedType: string, target?: EventTarget): void {
const simulatedEvent = document.createEvent('MouseEvents');
simulatedEvent.initMouseEvent(
simulatedType, // type
true, // bubbles
true, // cancelable
window, // view
1, // detail
e.screenX, // screenX
e.screenY, // screenY
e.clientX, // clientX
e.clientY, // clientY
e.ctrlKey, // ctrlKey
e.altKey, // altKey
e.shiftKey, // shiftKey
e.metaKey, // metaKey
0, // button
e.target // relatedTarget
);
/** copies the MouseEvent (or convert Touch) properties and sends it as another event to the given target */
public static simulateMouseEvent(e: MouseEvent | Touch, simulatedType: string, target?: EventTarget): void {
const me = e as MouseEvent;
const simulatedEvent = new MouseEvent(simulatedType, {
bubbles: true,
composed: true,
cancelable: true,
view: window,
detail: 1,
screenX: e.screenX,
screenY: e.screenY,
clientX: e.clientX,
clientY: e.clientY,
ctrlKey: me.ctrlKey??false,
altKey: me.altKey??false,
shiftKey: me.shiftKey??false,
metaKey: me.metaKey??false,
button: 0,
relatedTarget: e.target
});

(target || e.target).dispatchEvent(simulatedEvent);
}

Expand Down