11import { getDevice } from './getDevice'
2- import { isStr } from './isStr'
32import { addEventListener } from './addEventListener'
43import type { DragEvent } from './types'
5- import { findElement } from './findElement '
4+ import { mount } from './mount '
65
76export function dragEvent ( target : HTMLElement | string , options : DragEvent = { } , trigger ?: boolean ) {
8- let isMounted = false
9- let hasMounted = false
107 const { os } = getDevice ( )
118 const isPhone = os === 'ios' || os === 'android'
129 const stop : ( ( ) => void ) [ ] = [ ]
13- function update ( ) {
14- if ( hasMounted )
15- return
16- if ( isStr ( target ) )
17- target = findElement ( target ) || target
18- if ( ! isMounted && isStr ( target ) )
19- return isMounted = true
20- else if ( isStr ( target ) )
21- throw new Error ( `${ target } is not a HTMLElement` )
10+ let isStopped = false
11+ mount ( target , ( target ) => {
2212 let down = false
2313 if ( isPhone ) {
24- stop . push ( addEventListener ( target as HTMLElement , 'touchstart' , ( e : any ) => {
14+ stop . push ( addEventListener ( target , 'touchstart' , ( e : any ) => {
2515 options . dragStart && options . dragStart ( wrapperE ( e ) )
2616 } , false ) )
27- options . dragMove && stop . push ( addEventListener ( target as HTMLElement , 'touchmove' , ( e : any ) => {
17+ options . dragMove && stop . push ( addEventListener ( target , 'touchmove' , ( e : any ) => {
2818 if ( ! trigger || down )
2919 options . dragMove ?.( wrapperE ( e ) )
3020 } , false ) )
31- options . dragEnd && stop . push ( addEventListener ( target as HTMLElement , 'touchend' , ( e : any ) => {
21+ options . dragEnd && stop . push ( addEventListener ( target , 'touchend' , ( e : any ) => {
3222 options . dragEnd ?.( wrapperE ( e ) )
3323 down = false
3424 } , false ) )
35- }
25+ }
3626 else {
37- stop . push ( addEventListener ( target as HTMLElement , 'mousedown' , ( e : any ) => {
27+ stop . push ( addEventListener ( target , 'mousedown' , ( e : any ) => {
3828 down = true
3929 options . dragStart && options . dragStart ( e )
4030 } , false ) )
41- options . dragMove && stop . push ( addEventListener ( target as HTMLElement , 'mousemove' , ( e : any ) => {
31+ options . dragMove && stop . push ( addEventListener ( target , 'mousemove' , ( e : any ) => {
4232 if ( ! trigger || down )
4333 options . dragMove ?.( e )
4434 } , false ) )
45- options . dragEnd && stop . push ( addEventListener ( target as HTMLElement , 'mouseup' , ( e : any ) => {
35+ options . dragEnd && stop . push ( addEventListener ( target , 'mouseup' , ( e : any ) => {
4636 options . dragEnd ?.( e )
4737 down = false
4838 } , false ) )
4939 }
50- hasMounted = true
40+ if ( isStopped )
41+ stop . forEach ( stop => stop ( ) )
5142 function wrapperE ( e : any ) {
5243 const { clientX, clientY, pageX, pageY, screenX, screenY } = e ?. changedTouches [ 0 ]
5344 e . clientX = clientX
@@ -58,10 +49,10 @@ export function dragEvent(target: HTMLElement | string, options: DragEvent = {},
5849 e . screenY = screenY
5950 return e
6051 }
61- }
62- update ( )
63- addEventListener ( document , 'DOMContentLoaded' , update )
52+ } )
6453 return ( ) => {
54+ if ( ! stop . length )
55+ return isStopped = true
6556 stop . forEach ( cb => cb ?.( ) )
6657 }
6758}
0 commit comments