Explorar o código

fix(js): use arrow functions for `touchcancel` event to maintain context

Frédéric Guillot hai 2 meses
pai
achega
fb0fcfb266
Modificáronse 1 ficheiros con 7 adicións e 4 borrados
  1. 7 4
      internal/ui/static/js/touch_handler.js

+ 7 - 4
internal/ui/static/js/touch_handler.js

@@ -158,20 +158,23 @@ class TouchHandler {
             element.addEventListener("touchstart", (e) => this.onItemTouchStart(e), eventListenerOptions);
             element.addEventListener("touchmove", (e) => this.onItemTouchMove(e));
             element.addEventListener("touchend", (e) => this.onItemTouchEnd(e), eventListenerOptions);
-            element.addEventListener("touchcancel", this.reset, eventListenerOptions);
+            // Use arrow to keep TouchHandler context; otherwise this would become the DOM element on cancel.
+            element.addEventListener("touchcancel", () => this.reset(), eventListenerOptions);
         });
 
         const element = document.querySelector(".entry-content");
         if (element) {
             if (element.classList.contains("gesture-nav-tap")) {
                 element.addEventListener("touchend", (e) => this.onTapEnd(e), eventListenerOptions);
-                element.addEventListener("touchmove", this.reset, eventListenerOptions);
-                element.addEventListener("touchcancel", this.reset, eventListenerOptions);
+                // Use arrow to keep TouchHandler context; otherwise this would become the DOM element on cancel.
+                element.addEventListener("touchmove", () => this.reset(), eventListenerOptions);
+                element.addEventListener("touchcancel", () => this.reset(), eventListenerOptions);
             } else if (element.classList.contains("gesture-nav-swipe")) {
                 element.addEventListener("touchstart", (e) => this.onContentTouchStart(e), eventListenerOptions);
                 element.addEventListener("touchmove", (e) => this.onContentTouchMove(e), eventListenerOptions);
                 element.addEventListener("touchend", (e) => this.onContentTouchEnd(e), eventListenerOptions);
-                element.addEventListener("touchcancel", this.reset, eventListenerOptions);
+                // Use arrow to keep TouchHandler context; otherwise this would become the DOM element on cancel.
+                element.addEventListener("touchcancel", () => this.reset(), eventListenerOptions);
             }
         }
     }