File size: 1,541 Bytes
baa8e90 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
import { app } from "../../../scripts/app.js";
// Adds mapping of touch events to mouse events for mobile. This isnt great but it is somewhat usable
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) => {
// Support tap as click if it completes within a delay
if (touchStart) {
clearTimeout(touchStart);
}
touchStart = setTimeout(() => {
touchStart = null;
}, 100);
// Left or right button down
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) {
// We are within the touch start delay so fire this as a click
clearTimeout(touchStart);
fireEvent(e, "click");
}
fireEvent(e, "mouseup");
touchType = 0;
},
true
);
},
});
|