1
- import { ref } from ' vue'
2
- import type { ChartRow } from ' ../types'
1
+ import { ref } from " vue"
2
+ import type { ChartRow } from " ../types"
3
3
4
+ /**
5
+ * Interface defining the state for touch-based row dragging
6
+ */
4
7
interface TouchDragState {
5
8
isDragging : boolean
6
9
startY : number
7
10
currentY : number
8
11
draggedRow : ChartRow | null
9
12
dropTarget : {
10
13
row : ChartRow | null
11
- position : ' before' | ' after' | ' child'
14
+ position : " before" | " after" | " child"
12
15
}
13
16
dragElement : HTMLElement | null
14
17
initialTransform : string
15
18
}
16
19
20
+ /**
21
+ * A composable that manages touch-based drag and drop functionality for rows
22
+ * Handles touch events, visual feedback, and drop position detection
23
+ * @returns Object containing touch state and event handlers
24
+ */
17
25
export function useRowTouchDrag ( ) {
18
26
const touchState = ref < TouchDragState > ( {
19
27
isDragging : false ,
@@ -22,12 +30,16 @@ export function useRowTouchDrag() {
22
30
draggedRow : null ,
23
31
dropTarget : {
24
32
row : null ,
25
- position : ' before'
33
+ position : " before"
26
34
} ,
27
35
dragElement : null ,
28
- initialTransform : ''
36
+ initialTransform : ""
29
37
} )
30
38
39
+ /**
40
+ * Resets touch drag state and restores original element position
41
+ * Called when drag operation ends or is cancelled
42
+ */
31
43
const resetTouchState = ( ) => {
32
44
if ( touchState . value . dragElement ) {
33
45
touchState . value . dragElement . style . transform = touchState . value . initialTransform
@@ -40,13 +52,20 @@ export function useRowTouchDrag() {
40
52
draggedRow : null ,
41
53
dropTarget : {
42
54
row : null ,
43
- position : ' before'
55
+ position : " before"
44
56
} ,
45
57
dragElement : null ,
46
- initialTransform : ''
58
+ initialTransform : ""
47
59
}
48
60
}
49
61
62
+ /**
63
+ * Initializes touch drag operation
64
+ * Sets up initial positions and state for dragging
65
+ * @param event - Touch event that started the drag
66
+ * @param row - Row being dragged
67
+ * @param element - DOM element being dragged
68
+ */
50
69
const handleTouchStart = ( event : TouchEvent , row : ChartRow , element : HTMLElement ) => {
51
70
const touch = event . touches [ 0 ]
52
71
if ( ! touch ) return
@@ -56,21 +75,28 @@ export function useRowTouchDrag() {
56
75
event . preventDefault ( )
57
76
}
58
77
} , 100 )
59
-
78
+
60
79
touchState . value = {
61
80
isDragging : true ,
62
81
startY : touch . clientY ,
63
82
currentY : touch . clientY ,
64
83
draggedRow : row ,
65
84
dropTarget : {
66
85
row : null ,
67
- position : ' before'
86
+ position : " before"
68
87
} ,
69
88
dragElement : element ,
70
- initialTransform : element . style . transform || ''
89
+ initialTransform : element . style . transform || ""
71
90
}
72
91
}
73
92
93
+ /**
94
+ * Handles ongoing touch drag movement
95
+ * Updates visual position and calculates drop targets
96
+ * @param event - Touch move event
97
+ * @param targetRow - Row being dragged over
98
+ * @param rowElement - DOM element being dragged over
99
+ */
74
100
const handleTouchMove = ( event : TouchEvent , targetRow : ChartRow , rowElement : HTMLElement ) => {
75
101
const touch = event . touches [ 0 ]
76
102
if ( ! touch || ! touchState . value . isDragging || ! touchState . value . dragElement ) return
@@ -80,29 +106,35 @@ export function useRowTouchDrag() {
80
106
81
107
const deltaY = touch . clientY - touchState . value . startY
82
108
touchState . value . dragElement . style . transform = `translateY(${ deltaY } px)`
83
-
109
+
84
110
const rect = rowElement . getBoundingClientRect ( )
85
111
const relativeY = touch . clientY - rect . top
86
112
const position = relativeY / rect . height
87
113
88
114
if ( touchState . value . draggedRow !== targetRow ) {
89
115
if ( targetRow . children ?. length ) {
90
116
if ( position < 0.25 ) {
91
- touchState . value . dropTarget = { row : targetRow , position : ' before' }
117
+ touchState . value . dropTarget = { row : targetRow , position : " before" }
92
118
} else if ( position > 0.75 ) {
93
- touchState . value . dropTarget = { row : targetRow , position : ' after' }
119
+ touchState . value . dropTarget = { row : targetRow , position : " after" }
94
120
} else {
95
- touchState . value . dropTarget = { row : targetRow , position : ' child' }
121
+ touchState . value . dropTarget = { row : targetRow , position : " child" }
96
122
}
97
123
} else {
98
124
touchState . value . dropTarget = {
99
125
row : targetRow ,
100
- position : position < 0.5 ? ' before' : ' after'
126
+ position : position < 0.5 ? " before" : " after"
101
127
}
102
128
}
103
129
}
104
130
}
105
131
132
+ /**
133
+ * Finalizes touch drag operation
134
+ * Determines final drop position and triggers updates
135
+ * @param event - Touch end event
136
+ * @returns Object containing drag result information or null if invalid
137
+ */
106
138
const handleTouchEnd = ( event : TouchEvent ) => {
107
139
if ( ! touchState . value . isDragging ) return null
108
140
0 commit comments