@@ -28,7 +28,12 @@ export const cardLinks: LinksFunction = () => [
28
28
29
29
export function Card ( { card, index } : { card : CardType ; index : number } ) {
30
30
const [ isDragging , setIsDragging ] = useState ( false ) ;
31
- const [ startPosition , setStartPosition ] = useState ( { x : 0 , y : 0 } ) ;
31
+
32
+ // Needed to calculate the distance between the cursor and the card's top-left corner
33
+ // Otherwise, the card would jump to the cursor's position when dragging
34
+ // This would be a bad user experience
35
+ // Hence needed to maintain relative position between the cursor and the card
36
+ const [ dragStartOffset , setDragStartOffset ] = useState ( { x : 0 , y : 0 } ) ;
32
37
33
38
// Needed to properly move cursor to the end of the contentEditable span
34
39
const [ content , setContent ] = useState ( card . html ) ;
@@ -61,8 +66,8 @@ export function Card({ card, index }: { card: CardType; index: number }) {
61
66
if ( isDragging ) {
62
67
function handleGlobalMouseMove ( event : MouseEvent ) {
63
68
if ( ! isDragging ) return ;
64
- const newX = event . clientX - startPosition . x ;
65
- const newY = event . clientY - startPosition . y ;
69
+ const newX = event . clientX - dragStartOffset . x ;
70
+ const newY = event . clientY - dragStartOffset . y ;
66
71
updateCardPosition ( card . id , newX , newY ) ;
67
72
}
68
73
@@ -83,13 +88,15 @@ export function Card({ card, index }: { card: CardType; index: number }) {
83
88
} , [
84
89
card . id ,
85
90
isDragging ,
86
- startPosition . x ,
87
- startPosition . y ,
91
+ dragStartOffset . x ,
92
+ dragStartOffset . y ,
88
93
updateCardPosition ,
89
94
] ) ;
90
95
91
96
function handleMouseDown ( event : React . MouseEvent < HTMLButtonElement > ) {
92
- if ( document . activeElement === cardContentRef . current ) {
97
+ const isUserEditingCardContent =
98
+ document . activeElement === cardContentRef . current ;
99
+ if ( isUserEditingCardContent ) {
93
100
return ;
94
101
}
95
102
@@ -98,7 +105,7 @@ export function Card({ card, index }: { card: CardType; index: number }) {
98
105
99
106
setIsDragging ( true ) ;
100
107
101
- setStartPosition ( {
108
+ setDragStartOffset ( {
102
109
x : startX - card . positionX ,
103
110
y : startY - card . positionY ,
104
111
} ) ;
0 commit comments