#49 : Update swipe event handling
This commit is contained in:
@@ -8,6 +8,7 @@ import {
|
|||||||
removeEndEventListener,
|
removeEndEventListener,
|
||||||
} from './event'
|
} from './event'
|
||||||
import { createDispatcher } from '../../utils/event'
|
import { createDispatcher } from '../../utils/event'
|
||||||
|
import { SWIPE_MIN_DURATION_MS, SWIPE_MIN_DISTANCE_PX } from '../../units'
|
||||||
|
|
||||||
function getCoords(event) {
|
function getCoords(event) {
|
||||||
if ('TouchEvent' in window && event instanceof TouchEvent) {
|
if ('TouchEvent' in window && event instanceof TouchEvent) {
|
||||||
@@ -28,9 +29,18 @@ export function swipeable(node, { thresholdProvider }) {
|
|||||||
let x
|
let x
|
||||||
let y
|
let y
|
||||||
let moved = 0
|
let moved = 0
|
||||||
|
let swipeStartedAt
|
||||||
|
let isTouching = false
|
||||||
|
|
||||||
|
function isValidSwipe() {
|
||||||
|
const swipeDurationMs = Date.now() - swipeStartedAt
|
||||||
|
return swipeDurationMs >= SWIPE_MIN_DURATION_MS && Math.abs(moved) >= SWIPE_MIN_DISTANCE_PX
|
||||||
|
}
|
||||||
|
|
||||||
function handleMousedown(event) {
|
function handleMousedown(event) {
|
||||||
|
swipeStartedAt = Date.now()
|
||||||
moved = 0
|
moved = 0
|
||||||
|
isTouching = true
|
||||||
const coords = getCoords(event)
|
const coords = getCoords(event)
|
||||||
x = coords.x
|
x = coords.x
|
||||||
y = coords.y
|
y = coords.y
|
||||||
@@ -40,6 +50,7 @@ export function swipeable(node, { thresholdProvider }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleMousemove(event) {
|
function handleMousemove(event) {
|
||||||
|
if (!isTouching) return
|
||||||
const coords = getCoords(event)
|
const coords = getCoords(event)
|
||||||
const dx = coords.x - x
|
const dx = coords.x - x
|
||||||
const dy = coords.y - y
|
const dy = coords.y - y
|
||||||
@@ -59,10 +70,11 @@ export function swipeable(node, { thresholdProvider }) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function handleMouseup(event) {
|
function handleMouseup(event) {
|
||||||
|
isTouching = false
|
||||||
|
if (!isValidSwipe()) return
|
||||||
|
|
||||||
const coords = getCoords(event)
|
const coords = getCoords(event)
|
||||||
x = coords.x
|
dispatch('end', { x: coords.x, y: coords.y })
|
||||||
y = coords.y
|
|
||||||
dispatch('end', { x, y })
|
|
||||||
removeEndEventListener(window, handleMouseup)
|
removeEndEventListener(window, handleMouseup)
|
||||||
removeMoveEventListener(window, handleMousemove)
|
removeMoveEventListener(window, handleMousemove)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,19 +5,19 @@ import {
|
|||||||
addFocusoutEventListener,
|
addFocusoutEventListener,
|
||||||
removeFocusoutEventListener,
|
removeFocusoutEventListener,
|
||||||
} from './event'
|
} from './event'
|
||||||
|
import { TAP_DURATION_MS } from '../../units'
|
||||||
|
|
||||||
let timeStart
|
let tapStartedAt = 0
|
||||||
|
|
||||||
export function tappable(node) {
|
export function tappable(node) {
|
||||||
const dispatch = createDispatcher(node)
|
const dispatch = createDispatcher(node)
|
||||||
|
|
||||||
function handleTapstart() {
|
function handleTapstart() {
|
||||||
timeStart = Date.now()
|
tapStartedAt = Date.now()
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleTapend() {
|
function handleTapend() {
|
||||||
const diffMs = Date.now() - timeStart
|
if (Date.now() - tapStartedAt <= TAP_DURATION_MS) {
|
||||||
if (diffMs <= 200) {
|
|
||||||
dispatch('tapped')
|
dispatch('tapped')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -250,6 +250,7 @@
|
|||||||
// Disable page change while animation is in progress
|
// Disable page change while animation is in progress
|
||||||
let disabled = false
|
let disabled = false
|
||||||
async function changePage(updateStoreFn, options) {
|
async function changePage(updateStoreFn, options) {
|
||||||
|
progressManager.reset()
|
||||||
if (disabled) return
|
if (disabled) return
|
||||||
disabled = true
|
disabled = true
|
||||||
|
|
||||||
|
|||||||
3
src/units.js
Normal file
3
src/units.js
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
export const TAP_DURATION_MS = 200
|
||||||
|
export const SWIPE_MIN_DURATION_MS = 250
|
||||||
|
export const SWIPE_MIN_DISTANCE_PX = 20
|
||||||
Reference in New Issue
Block a user