Skip to content

Commit

Permalink
Handle synthetic events, including when pointerId is not given
Browse files Browse the repository at this point in the history
This is for jspaint, which triggers pointerup when pressing both mouse buttons to cancel a drawing gesture, and on blur.
  • Loading branch information
1j01 committed Nov 20, 2021
1 parent bb4df0f commit 70000c0
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions $Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,8 @@ You can also disable this warning by passing {iframes: {ignoreCrossOrigin: true}

var drag_offset_x, drag_offset_y, drag_pointer_x, drag_pointer_y, drag_pointer_id;
var update_drag = (e) => {
if (drag_pointer_id === (e.pointerId ?? e.originalEvent.pointerId)) {
const pointerId = e.pointerId ?? e.originalEvent?.pointerId; // originalEvent doesn't exist for triggerHandler()
if (drag_pointer_id === pointerId || pointerId === undefined) { // (allowing synthetic events to affect the drag without pointerId)
drag_pointer_x = e.clientX ?? drag_pointer_x;
drag_pointer_y = e.clientY ?? drag_pointer_y;
}
Expand Down Expand Up @@ -1291,13 +1292,14 @@ You can also disable this warning by passing {iframes: {ignoreCrossOrigin: true}
drag_offset_y = e.clientY + scrollY - $w.position().top;
drag_pointer_x = e.clientX;
drag_pointer_y = e.clientY;
drag_pointer_id = (e.pointerId ?? e.originalEvent.pointerId);
drag_pointer_id = (e.pointerId ?? e.originalEvent?.pointerId); // originalEvent doesn't exist for triggerHandler()
$G.on("pointermove", update_drag);
$G.on("scroll", update_drag);
$("body").addClass("dragging"); // for when mouse goes over an iframe
});
$G.on("pointerup pointercancel", (e) => {
if ((e.pointerId ?? e.originalEvent.pointerId) !== drag_pointer_id) { return; }
const pointerId = e.pointerId ?? e.originalEvent?.pointerId; // originalEvent doesn't exist for triggerHandler()
if (pointerId !== drag_pointer_id && pointerId !== undefined) { return; } // (allowing synthetic events to affect the drag without pointerId)
$G.off("pointermove", update_drag);
$G.off("scroll", update_drag);
$("body").removeClass("dragging");
Expand Down Expand Up @@ -1384,20 +1386,22 @@ You can also disable this warning by passing {iframes: {ignoreCrossOrigin: true}
resize_offset_y = e.clientY + scrollY - rect.y - (y_axis === HANDLE_BOTTOM ? rect.height : 0);
resize_pointer_x = e.clientX;
resize_pointer_y = e.clientY;
resize_pointer_id = (e.pointerId ?? e.originalEvent.pointerId);
resize_pointer_id = (e.pointerId ?? e.originalEvent?.pointerId); // originalEvent doesn't exist for triggerHandler()

$handle[0].setPointerCapture(resize_pointer_id); // keeps cursor consistent when mouse moves over other elements

// handle_pointermove(e); // was useful for checking that the offset is correct (should not do anything, if it's correct!)
});
function handle_pointermove(e) {
if ((e.pointerId ?? e.originalEvent.pointerId) !== resize_pointer_id) { return; }
const pointerId = e.pointerId ?? e.originalEvent?.pointerId; // originalEvent doesn't exist for triggerHandler()
if (pointerId !== resize_pointer_id && pointerId !== undefined) { return; } // (allowing synthetic events to affect the drag without pointerId)
resize_pointer_x = e.clientX;
resize_pointer_y = e.clientY;
update_resize();
}
function end_resize(e) {
if ((e.pointerId ?? e.originalEvent.pointerId) !== resize_pointer_id) { return; }
const pointerId = e.pointerId ?? e.originalEvent?.pointerId; // originalEvent doesn't exist for triggerHandler()
if (pointerId !== resize_pointer_id && pointerId !== undefined) { return; } // (allowing synthetic events to affect the drag without pointerId)
$G.off("pointermove", handle_pointermove);
$G.off("scroll", onscroll);
$("body").removeClass("dragging");
Expand Down

0 comments on commit 70000c0

Please sign in to comment.