import { useDragDropContext } from "./drag-drop-context" ; import { onCleanup , onMount } from "solid-js" ; const createPointerSensor = ( id = "pointer-sensor" ) : void => { const [ state , { addSensor , removeSensor , sensorStart , sensorEnd , dragStart , dragMove , dragEnd , anySensorActive , } , ] = useDragDropContext ( ) ! ; const activationDelay = 250 ; const activationDistance = 10 ; onMount ( ( ) => { addSensor ( { id , activators : { pointerdown : attach } } ) ; } ) ; onCleanup ( ( ) => { removeSensor ( id ) ; } ) ; const isActiveSensor = ( ) => state . active . sensor === id ; const initialCoordinates = { x : 0 , y : 0 } ; let activationDelayTimeoutId = null ; let activationDraggableId = null ; const attach = ( event , draggableId ) : void => { event . preventDefault ( ) ; document . addEventListener ( "pointermove" , onPointerMove ) ; document . addEventListener ( "pointerup" , onPointerUp ) ; activationDraggableId = draggableId ; initialCoordinates . x = event . clientX ; initialCoordinates . y = event . clientY ; activationDelayTimeoutId = window . setTimeout ( onActivate , activationDelay ) ; } ; const detach = ( ) : void => { if ( activationDelayTimeoutId ) { clearTimeout ( activationDelayTimeoutId ) ; activationDelayTimeoutId = null ; } document . removeEventListener ( "pointermove" , onPointerMove ) ; document . removeEventListener ( "pointerup" , onPointerUp ) ; } ; const onActivate = ( ) : void => { if ( ! anySensorActive ( ) ) { sensorStart ( id ) ; dragStart ( activationDraggableId ! ) ; } else if ( ! isActiveSensor ( ) ) { detach ( ) ; } } ; const onPointerMove = ( event ) : void => { const transform = { x : event . clientX - initialCoordinates . x , y : event . clientY - initialCoordinates . y , } ; if ( ! anySensorActive ( ) ) { if ( Math . sqrt ( transform . x ** 2 + transform . y ** 2 ) > activationDistance ) { onActivate ( ) ; } } if ( isActiveSensor ( ) ) { event . preventDefault ( ) ; dragMove ( transform ) ; } } ; const onPointerUp = ( event ) : void => { detach ( ) ; if ( isActiveSensor ( ) ) { event . preventDefault ( ) ; dragEnd ( ) ; sensorEnd ( ) ; } } ; } ; export { createPointerSensor } ;