Move swipeable action to actions dir
This commit is contained in:
29
src/actions/swipeable/event.js
Normal file
29
src/actions/swipeable/event.js
Normal file
@@ -0,0 +1,29 @@
|
||||
// start event
|
||||
export function addStartEventListener(source, cb) {
|
||||
source.addEventListener('mousedown', cb)
|
||||
source.addEventListener('touchstart', cb)
|
||||
}
|
||||
export function removeStartEventListener(source, cb) {
|
||||
source.removeEventListener('mousedown', cb)
|
||||
source.removeEventListener('touchstart', cb)
|
||||
}
|
||||
|
||||
// end event
|
||||
export function addEndEventListener(source, cb) {
|
||||
source.addEventListener('mouseup', cb)
|
||||
source.addEventListener('touchend', cb)
|
||||
}
|
||||
export function removeEndEventListener(source, cb) {
|
||||
source.removeEventListener('mouseup', cb)
|
||||
source.removeEventListener('touchend', cb)
|
||||
}
|
||||
|
||||
// move event
|
||||
export function addMoveEventListener(source, cb) {
|
||||
source.addEventListener('mousemove', cb)
|
||||
source.addEventListener('touchmove', cb)
|
||||
}
|
||||
export function removeMoveEventListener(source, cb) {
|
||||
source.removeEventListener('mousemove', cb)
|
||||
source.removeEventListener('touchmove', cb)
|
||||
}
|
||||
1
src/actions/swipeable/index.js
Normal file
1
src/actions/swipeable/index.js
Normal file
@@ -0,0 +1 @@
|
||||
export * from './swipeable'
|
||||
@@ -1,4 +1,4 @@
|
||||
import { NEXT, PREV } from '../direction'
|
||||
import { NEXT, PREV } from '../../direction'
|
||||
import {
|
||||
addStartEventListener,
|
||||
removeStartEventListener,
|
||||
@@ -6,20 +6,20 @@ import {
|
||||
removeMoveEventListener,
|
||||
addEndEventListener,
|
||||
removeEndEventListener,
|
||||
createDispatcher
|
||||
} from './event'
|
||||
import { createDispatcher } from '../../utils/event'
|
||||
|
||||
function getCoords(event) {
|
||||
if (event instanceof TouchEvent) {
|
||||
const touch = event.touches[0]
|
||||
return {
|
||||
x: touch ? touch.clientX : 0,
|
||||
y: touch ? touch.clientY : 0
|
||||
y: touch ? touch.clientY : 0,
|
||||
}
|
||||
}
|
||||
return {
|
||||
x: event.clientX,
|
||||
y: event.clientY
|
||||
y: event.clientY,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ export function swipeable(node, { thresholdProvider }) {
|
||||
dispatch('threshold', { direction: moved > 0 ? PREV : NEXT })
|
||||
removeEndEventListener(window, handleMouseup)
|
||||
removeMoveEventListener(window, handleMousemove)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function handleMouseup(event) {
|
||||
@@ -71,6 +71,6 @@ export function swipeable(node, { thresholdProvider }) {
|
||||
return {
|
||||
destroy() {
|
||||
removeStartEventListener(node, handleMousedown)
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
import Dots from '../Dots/Dots.svelte'
|
||||
import Arrow from '../Arrow/Arrow.svelte'
|
||||
import { NEXT, PREV } from '../../direction'
|
||||
import { swipeable } from '../../utils/swipeable'
|
||||
import { swipeable } from '../../actions/swipeable'
|
||||
import { hoverable } from '../../utils/hoverable'
|
||||
import {
|
||||
addResizeEventListener,
|
||||
|
||||
@@ -1,33 +1,3 @@
|
||||
// start event
|
||||
export function addStartEventListener(source, cb) {
|
||||
source.addEventListener('mousedown', cb)
|
||||
source.addEventListener('touchstart', cb)
|
||||
}
|
||||
export function removeStartEventListener(source, cb) {
|
||||
source.removeEventListener('mousedown', cb)
|
||||
source.removeEventListener('touchstart', cb)
|
||||
}
|
||||
|
||||
// end event
|
||||
export function addEndEventListener(source, cb) {
|
||||
source.addEventListener('mouseup', cb)
|
||||
source.addEventListener('touchend', cb)
|
||||
}
|
||||
export function removeEndEventListener(source, cb) {
|
||||
source.removeEventListener('mouseup', cb)
|
||||
source.removeEventListener('touchend', cb)
|
||||
}
|
||||
|
||||
// move event
|
||||
export function addMoveEventListener(source, cb) {
|
||||
source.addEventListener('mousemove', cb)
|
||||
source.addEventListener('touchmove', cb)
|
||||
}
|
||||
export function removeMoveEventListener(source, cb) {
|
||||
source.removeEventListener('mousemove', cb)
|
||||
source.removeEventListener('touchmove', cb)
|
||||
}
|
||||
|
||||
// resize event
|
||||
export function addResizeEventListener(cb) {
|
||||
window.addEventListener('resize', cb)
|
||||
@@ -38,9 +8,11 @@ export function removeResizeEventListener(cb) {
|
||||
|
||||
export function createDispatcher(source) {
|
||||
function dispatch(event, data) {
|
||||
source.dispatchEvent(new CustomEvent(event, {
|
||||
detail: data
|
||||
}))
|
||||
source.dispatchEvent(
|
||||
new CustomEvent(event, {
|
||||
detail: data,
|
||||
})
|
||||
)
|
||||
}
|
||||
return dispatch
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user