export type MoveHandler = ( event : MouseEvent , dx : number , dy : number , x : number , y : number ) => void ; export type UpHandler = ( event : MouseEvent , x : number , y : number , moved : boolean ) => void ; export function drag ( event , { move : move , up : up } ) { let startX = event . pageX ; let startY = event . pageY ; let x = startX ; let y = startY ; let moved = false ; function mouseMoveHandler ( event ) { let dx = event . pageX - x ; let dy = event . pageY - y ; x = event . pageX ; y = event . pageY ; if ( dx || dy ) moved = true ; move ( event , dx , dy , x , y ) ; event . preventDefault ( ) ; } function mouseUpHandler ( event ) { x = event . pageX ; y = event . pageY ; document . removeEventListener ( 'mousemove' , mouseMoveHandler ) ; document . removeEventListener ( 'mouseup' , mouseUpHandler ) ; if ( up ) up ( event , x , y , moved ) ; } document . addEventListener ( 'mousemove' , mouseMoveHandler ) ; document . addEventListener ( 'mouseup' , mouseUpHandler ) ; }