|
import { app } from "../../../scripts/app.js"; |
|
|
|
|
|
|
|
app.registerExtension({ |
|
name: "pysssss.TouchEvents", |
|
setup() { |
|
let touchStart = null; |
|
let touchType = 0; |
|
|
|
function fireEvent(originalEvent, type) { |
|
const fakeEvent = document.createEvent("MouseEvent"); |
|
const touch = originalEvent.changedTouches[0]; |
|
fakeEvent.initMouseEvent( |
|
type, |
|
true, |
|
true, |
|
window, |
|
1, |
|
touch.screenX, |
|
touch.screenY, |
|
touch.clientX, |
|
touch.clientY, |
|
false, |
|
false, |
|
false, |
|
false, |
|
0, |
|
null |
|
); |
|
|
|
touch.target.dispatchEvent(fakeEvent); |
|
if (fakeEvent.defaultPrevented) { |
|
originalEvent.preventDefault(); |
|
} |
|
} |
|
|
|
document.addEventListener( |
|
"touchstart", |
|
(e) => { |
|
|
|
if (touchStart) { |
|
clearTimeout(touchStart); |
|
} |
|
touchStart = setTimeout(() => { |
|
touchStart = null; |
|
}, 100); |
|
|
|
|
|
touchType = e.touches.length === 1 ? 0 : 2; |
|
|
|
fireEvent(e, "mousedown"); |
|
}, |
|
true |
|
); |
|
|
|
document.addEventListener("touchmove", (e) => fireEvent(e, "mousemove"), true); |
|
|
|
document.addEventListener( |
|
"touchend", |
|
(e) => { |
|
const isClick = touchStart; |
|
if (isClick) { |
|
|
|
clearTimeout(touchStart); |
|
fireEvent(e, "click"); |
|
} |
|
fireEvent(e, "mouseup"); |
|
touchType = 0; |
|
}, |
|
true |
|
); |
|
}, |
|
}); |
|
|