@@ -63,25 +63,25 @@ export function useDraggableChildren({
63
63
// 拖动目标元素事件
64
64
function getDragSideEvents ( ) {
65
65
const left : DOMAttributes < HTMLElement > = {
66
- onDragStart : ( e : DragEvent ) => {
67
- const dataTransfer = e . dataTransfer as DataTransfer ;
68
- // dataTransfer.setData('text', `draggable:${index}`);
69
- dataTransfer . effectAllowed = 'move' ;
70
- fromRef . current = { children : el , item, id } ;
71
- const target = e . currentTarget as HTMLElement ;
72
- target . classList . add ( cls . __ . ghost ) ;
73
- } ,
74
66
onDragEnd : ( e : DragEvent ) => {
75
- console . log ( 'onDragEnd' ) ;
76
- const el = e . currentTarget as HTMLElement ;
77
- el . classList . remove ( cls . __ . ghost ) ;
67
+ const target = e . currentTarget as HTMLElement ;
68
+ target . classList . remove ( cls . __ . ghost ) ;
69
+ console . log ( 'onDragEnd' , item , target ) ;
78
70
toRef . current ?. cancel ?.( id ) ;
79
71
emit ( ) ;
80
72
fromRef . current = null ;
81
73
toRef . current = null ;
82
74
stateRef . current . index = - 1 ;
83
75
e . preventDefault ( ) ;
84
76
} ,
77
+ onDragStart : ( e : DragEvent ) => {
78
+ const dataTransfer = e . dataTransfer as DataTransfer ;
79
+ // dataTransfer.setData('text', `draggable:${index}`);
80
+ dataTransfer . effectAllowed = 'move' ;
81
+ fromRef . current = { children : el , item, id } ;
82
+ const target = e . currentTarget as HTMLElement ;
83
+ target . classList . add ( cls . __ . ghost ) ;
84
+ } ,
85
85
} ;
86
86
return left ;
87
87
}
@@ -141,12 +141,12 @@ export function useDraggableChildren({
141
141
else moveFromGroup ( from . item , from . children , index ) ;
142
142
} else {
143
143
console . log ( 'uuuuuuuuuuup' ) ;
144
- const temp = childrenRef . current . slice ( ) ;
145
- temp [ fromIndex ] = cloneAndAddGhostClassName (
146
- temp [ fromIndex ] as ReactElement ,
144
+ updateChildrenOf (
145
+ cloneAndAddGhostClassName (
146
+ childrenRef . current [ fromIndex ] as ReactElement ,
147
+ ) ,
148
+ fromIndex ,
147
149
) ;
148
- childrenRef . current = temp ;
149
- forceUpdate ( ) ;
150
150
}
151
151
// target.classList.remove(cls['--'].hidden);
152
152
stateRef . current . index = index ;
@@ -166,16 +166,16 @@ export function useDraggableChildren({
166
166
}
167
167
} ,
168
168
cancel : ( _id2 ) : void => {
169
- console . log ( 'oncancel' ) ;
169
+ console . log ( 'oncancel' , id , _id2 , prever . id , target ) ;
170
170
stateRef . current . index = - 1 ;
171
- if ( prever . id !== id ) {
172
- console . log ( 'hhhhhhhhhh' ) ;
173
- }
174
171
if ( ! list . includes ( from . item ) ) {
175
172
removeItem ( from . item ) ;
176
173
preverRef . current = null ;
177
174
} else {
178
- target . classList . remove ( cls . __ . ghost ) ;
175
+ console . log ( 'ccccccccccccc' ) ;
176
+ const index = listRef . current . indexOf ( from . item ) ;
177
+ // 还原成未加工状态
178
+ updateChildrenOf ( from . children , index ) ;
179
179
}
180
180
} ,
181
181
drop : ( _id2 ) : void => {
@@ -218,6 +218,13 @@ export function useDraggableChildren({
218
218
insertToArray ( item , to , listRef . current ) ;
219
219
forceUpdate ( ) ;
220
220
}
221
+ function updateChildrenOf ( children : ReactElement , index : number ) : void {
222
+ const temp = childrenRef . current . slice ( ) ;
223
+ // 还原成未加工状态
224
+ temp [ index ] = children ;
225
+ childrenRef . current = temp ;
226
+ forceUpdate ( ) ;
227
+ }
221
228
function cloneAndAddGhostClassName ( element : ReactElement ) : ReactElement {
222
229
return cloneElement ( element , {
223
230
className : getClassNames ( element . props . className , cls . __ . ghost ) ,
0 commit comments