@@ -7,6 +7,10 @@ interface DragConfig {
77 end ?: ( dx : number , dy : number , complete : boolean ) => void ;
88}
99
10+ function snapUnits ( delta : number , gridSize : number ) : number {
11+ return Math . floor ( ( delta + gridSize / 2 ) / gridSize ) ;
12+ }
13+
1014export function drag ( { source, gridSize, start, move, snap, end } : DragConfig ) : ( ) => void {
1115 let startX = 0 ;
1216 let startY = 0 ;
@@ -15,7 +19,6 @@ export function drag({ source, gridSize, start, move, snap, end }: DragConfig):
1519 let lastSnapDx = 0 ;
1620 let lastSnapDy = 0 ;
1721 let activePointerId : number | null = null ;
18- const halfGridSize = gridSize / 2 ;
1922
2023 const onPointerDown = ( e : PointerEvent ) => {
2124 e . preventDefault ( ) ;
@@ -38,12 +41,8 @@ export function drag({ source, gridSize, start, move, snap, end }: DragConfig):
3841 lastDx = e . clientX - startX ;
3942 lastDy = e . clientY - startY ;
4043 move ?.( lastDx , lastDy ) ;
41- const snapDx = lastDx + halfGridSize < 0
42- ? Math . ceil ( ( lastDx + halfGridSize ) / gridSize )
43- : Math . floor ( ( lastDx + halfGridSize ) / gridSize ) ;
44- const snapDy = lastDy + halfGridSize < 0
45- ? Math . ceil ( ( lastDy + halfGridSize ) / gridSize )
46- : Math . floor ( ( lastDy + halfGridSize ) / gridSize ) ;
44+ const snapDx = snapUnits ( lastDx , gridSize ) ;
45+ const snapDy = snapUnits ( lastDy , gridSize ) ;
4746 if ( snapDx !== lastSnapDx || snapDy !== lastSnapDy ) {
4847 lastSnapDx = snapDx ;
4948 lastSnapDy = snapDy ;
@@ -52,40 +51,24 @@ export function drag({ source, gridSize, start, move, snap, end }: DragConfig):
5251 } ;
5352
5453 const onPointerUp = ( e : PointerEvent ) => {
55- const dx = e . clientX + halfGridSize - startX < 0
56- ? Math . ceil ( ( e . clientX + halfGridSize - startX ) / gridSize )
57- : Math . floor ( ( e . clientX + halfGridSize - startX ) / gridSize ) ;
58- const dy = e . clientY + halfGridSize - startY < 0
59- ? Math . ceil ( ( e . clientY + halfGridSize - startY ) / gridSize )
60- : Math . floor ( ( e . clientY + halfGridSize - startY ) / gridSize ) ;
54+ const dx = snapUnits ( e . clientX - startX , gridSize ) ;
55+ const dy = snapUnits ( e . clientY - startY , gridSize ) ;
6156 stopDrag ( ) ;
6257 end ?.( dx , dy , true ) ;
6358 } ;
6459
6560 const onPointerCancel = ( ) => {
66- const dx = lastDx < 0
67- ? Math . ceil ( ( lastDx + halfGridSize ) / gridSize )
68- : Math . floor ( ( lastDx + halfGridSize ) / gridSize ) ;
69- const dy = lastDy < 0
70- ? Math . ceil ( ( lastDy + halfGridSize ) / gridSize )
71- : Math . floor ( ( lastDy + halfGridSize ) / gridSize ) ;
7261 stopDrag ( ) ;
73- end ?.( dx , dy , false ) ;
62+ end ?.( snapUnits ( lastDx , gridSize ) , snapUnits ( lastDy , gridSize ) , false ) ;
7463 } ;
7564
7665 const onKeyDown = ( e : KeyboardEvent ) => {
7766 if ( e . key !== 'Escape' ) { return ; }
78- const dx = lastDx < 0
79- ? Math . ceil ( ( lastDx + halfGridSize ) / gridSize )
80- : Math . floor ( ( lastDx + halfGridSize ) / gridSize ) ;
81- const dy = lastDy < 0
82- ? Math . ceil ( ( lastDy + halfGridSize ) / gridSize )
83- : Math . floor ( ( lastDy + halfGridSize ) / gridSize ) ;
8467 if ( activePointerId !== null ) {
8568 source . releasePointerCapture ( activePointerId ) ;
8669 }
8770 stopDrag ( ) ;
88- end ?.( dx , dy , false ) ;
71+ end ?.( snapUnits ( lastDx , gridSize ) , snapUnits ( lastDy , gridSize ) , false ) ;
8972 } ;
9073
9174 const stopDrag = ( ) => {
0 commit comments