1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-08-01 17:38:37 +00:00

refactor(js): minor refactoring of touch_handler.js

- Mark a method as `static`
- use `Math.sqrt` instead of `Math.pow(…, 0.5)`
- Use `Math.sign` instead of a condition on the sign
- Inline some used-once variables
- Reduce the scope of some variables
This commit is contained in:
Julien Voisin 2025-01-15 04:47:30 +00:00 committed by GitHub
parent 8c3a9184ac
commit fccca0ce1e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -26,7 +26,7 @@ class TouchHandler {
return 0;
}
findElement(element) {
static findElement(element) {
if (element.classList.contains("entry-swipe")) {
return element;
}
@ -42,7 +42,7 @@ class TouchHandler {
this.reset();
this.touch.start.x = event.touches[0].clientX;
this.touch.start.y = event.touches[0].clientY;
this.touch.element = this.findElement(event.touches[0].target);
this.touch.element = TouchHandler.findElement(event.touches[0].target);
this.touch.element.style.transitionDuration = "0s";
}
@ -60,11 +60,7 @@ class TouchHandler {
if (absDistance > 0) {
this.touch.moved = true;
let tx = absDistance > 75 ? Math.pow(absDistance - 75, 0.5) + 75 : absDistance;
if (distance < 0) {
tx = -tx;
}
const tx = (absDistance > 75 ? Math.sqrt(absDistance - 75) + 75 : absDistance) * Math.sign(distance);
this.touch.element.style.transform = "translateX(" + tx + "px)";
@ -78,9 +74,7 @@ class TouchHandler {
}
if (this.touch.element !== null) {
const absDistance = Math.abs(this.calculateDistance());
if (absDistance > 75) {
if (Math.abs(this.calculateDistance()) > 75) {
toggleEntryStatus(this.touch.element);
}
@ -118,15 +112,15 @@ class TouchHandler {
return;
}
const distance = this.calculateDistance();
const absDistance = Math.abs(distance);
const now = Date.now();
if (now - this.touch.time <= 1000 && absDistance > 75) {
if (distance > 0) {
goToPage("previous");
} else {
goToPage("next");
if (Date.now() - this.touch.time <= 1000) {
const distance = this.calculateDistance();
const absDistance = Math.abs(distance);
if (absDistance > 75) {
if (distance > 0) {
goToPage("previous");
} else {
goToPage("next");
}
}
}