fix event listener registration & unregistration

This commit is contained in:
Grant 2024-03-08 15:38:11 -07:00
parent b07ae0406f
commit f52f7b406b
1 changed files with 248 additions and 145 deletions

View File

@ -261,9 +261,48 @@ export class PanZoom extends EventEmitter<PanZoomEvents> {
} }
registerTouchEvents() { registerTouchEvents() {
console.debug("[PanZoom] Registering touch events to $wrapper");
this.$wrapper.addEventListener( this.$wrapper.addEventListener(
"touchstart", "touchstart",
(event) => { this._touch_touchstart.bind(this),
{
passive: false,
}
);
this.$wrapper.addEventListener(
"touchmove",
this._touch_touchmove.bind(this)
);
this.$wrapper.addEventListener("touchend", this._touch_touchend.bind(this));
}
unregisterTouchEvents() {
console.debug("[PanZoom] Unregistering touch events to $wrapper");
this.$wrapper.removeEventListener(
"touchstart",
this._touch_touchstart.bind(this)
);
this.$wrapper.removeEventListener(
"touchmove",
this._touch_touchmove.bind(this)
);
this.$wrapper.removeEventListener(
"touchend",
this._touch_touchend.bind(this)
);
}
/**
* Handle touchstart event from touch registrations
* This needs to be a variable to correctly pass this context
*
* @param e
*/
private _touch_touchstart = (event: TouchEvent) => {
const isDoubleTap = const isDoubleTap =
this.touch.lastTouch && +new Date() - this.touch.lastTouch < 200; this.touch.lastTouch && +new Date() - this.touch.lastTouch < 200;
@ -284,11 +323,15 @@ export class PanZoom extends EventEmitter<PanZoomEvents> {
this.onPinchStart(event); this.onPinchStart(event);
} }
} }
}, };
{ passive: false }
);
this.$wrapper.addEventListener("touchmove", (event) => { /**
* Handle touchmove event from touch registrations
* This needs to be a variable to correctly pass this context
*
* @param e
*/
private _touch_touchmove = (event: TouchEvent) => {
if (this.panning.enabled && event.touches.length === 1) { if (this.panning.enabled && event.touches.length === 1) {
event.preventDefault(); event.preventDefault();
event.stopPropagation(); event.stopPropagation();
@ -299,9 +342,15 @@ export class PanZoom extends EventEmitter<PanZoomEvents> {
} else if (event.touches.length > 1) { } else if (event.touches.length > 1) {
this.onPinch(event); this.onPinch(event);
} }
}); };
this.$wrapper.addEventListener("touchend", (event) => { /**
* Handle touchend event from touch registrations
* This needs to be a variable to correctly pass this context
*
* @param e
*/
private _touch_touchend = (event: TouchEvent) => {
if (this.panning.enabled) { if (this.panning.enabled) {
this.panning.enabled = false; this.panning.enabled = false;
@ -309,8 +358,7 @@ export class PanZoom extends EventEmitter<PanZoomEvents> {
this.panning.end(touch.clientX, touch.clientY); this.panning.end(touch.clientX, touch.clientY);
} }
}); };
}
/// ///// /// /////
// pinch // pinch
@ -396,10 +444,49 @@ export class PanZoom extends EventEmitter<PanZoomEvents> {
} }
registerMouseEvents() { registerMouseEvents() {
console.debug("[PanZoom] Registering mouse events to $wrapper & document");
// zoom // zoom
this.$wrapper.addEventListener( this.$wrapper.addEventListener("wheel", this._mouse_wheel, {
"wheel", passive: true,
(e) => { });
this.$wrapper.addEventListener("mousedown", this._mouse_mousedown, {
passive: false,
});
// mouse move should not be tied to the element, in case the mouse exits the window
document.addEventListener("mousemove", this._mouse_mousemove, {
passive: false,
});
// mouse up should not be tied to the element, in case the mouse releases outside of the window
document.addEventListener("mouseup", this._mouse_mouseup, {
passive: false,
});
}
unregisterMouseEvents() {
console.debug(
"[PanZoom] Unregistering mouse events to $wrapper & document"
);
this.$wrapper.removeEventListener("wheel", this._mouse_wheel);
this.$wrapper.removeEventListener("mousedown", this._mouse_mousedown);
document.removeEventListener("mousemove", this._mouse_mousemove);
document.removeEventListener("mouseup", this._mouse_mouseup);
}
/**
* Handle the wheel event from the mouse event registration
* This needs to be a variable to correctly pass this context
*
* @param e
*/
private _mouse_wheel = (e: WheelEvent) => {
// if (!self.allowDrag) return; // if (!self.allowDrag) return;
const oldScale = this.transform.scale; const oldScale = this.transform.scale;
@ -433,27 +520,30 @@ export class PanZoom extends EventEmitter<PanZoomEvents> {
this.update(); this.update();
// place.update(); // place.update();
} }
}, };
{ passive: true }
);
this.$wrapper.addEventListener( /**
"mousedown", * Handle mousedown event from mouse registrations
(e) => { * This needs to be a variable to correctly pass this context
*
* @param e
*/
private _mouse_mousedown = (e: MouseEvent) => {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
this.mouse.mouseDown = Date.now(); this.mouse.mouseDown = Date.now();
this.panning.start(e.clientX, e.clientY); this.panning.start(e.clientX, e.clientY);
}, };
{ passive: false }
);
// mouse move should not be tied to the element, in case the mouse exits the window /**
document.addEventListener( * Handle mousemove event from mouse registrations
"mousemove", * This needs to be a variable to correctly pass this context
(e) => { *
* @param e
*/
private _mouse_mousemove = (e: MouseEvent) => {
if (this.panning.enabled) { if (this.panning.enabled) {
e.preventDefault(); e.preventDefault();
e.stopPropagation(); e.stopPropagation();
@ -466,14 +556,27 @@ export class PanZoom extends EventEmitter<PanZoomEvents> {
clientY: e.clientY, clientY: e.clientY,
}); });
} }
}, if (this.panning.enabled) {
{ passive: false } e.preventDefault();
); e.stopPropagation();
// mouse up should not be tied to the element, in case the mouse releases outside of the window this.panning.move(e.clientX, e.clientY);
document.addEventListener( } else {
"mouseup", // not panning
(e) => { this.emit("hover", {
clientX: e.clientX,
clientY: e.clientY,
});
}
};
/**
* Handle mouseup event from mouse registrations
* This needs to be a variable to correctly pass this context
*
* @param e
*/
private _mouse_mouseup = (e: MouseEvent) => {
if (this.mouse.mouseDown && Date.now() - this.mouse.mouseDown <= 500) { if (this.mouse.mouseDown && Date.now() - this.mouse.mouseDown <= 500) {
// if the mouse was down for less than a half a second, it's a click // if the mouse was down for less than a half a second, it's a click
// this can't depend on this.panning.enabled because that'll always be true when mouse is down // this can't depend on this.panning.enabled because that'll always be true when mouse is down
@ -500,10 +603,7 @@ export class PanZoom extends EventEmitter<PanZoomEvents> {
this.panning.end(e.clientX, e.clientY); this.panning.end(e.clientX, e.clientY);
} }
}, };
{ passive: false }
);
}
/** /**
* Update viewport scale and position * Update viewport scale and position
@ -545,6 +645,9 @@ export class PanZoom extends EventEmitter<PanZoomEvents> {
cleanup() { cleanup() {
// remove event handlers // remove event handlers
this.unregisterTouchEvents();
this.unregisterMouseEvents();
} }
// utilities // utilities