@@ -195,19 +195,19 @@ export function useHeTree<T extends Record<string, any>>(
195
195
const { rootIds, rootStats, getStat, } = mainCache ;
196
196
// about drag ==================================
197
197
const indent = props . indent !
198
- const [ draggedStat , setDraggedStat ] = useState < Stat < T > > ( ) ;
198
+ const [ draggingStat , setDraggingStat ] = useState < Stat < T > > ( ) ;
199
199
const [ dragOverStat , setDragOverStat ] = useState < Stat < T > > ( ) ;
200
200
const virtualListRef = useRef < VirtualListHandle > ( null ) ;
201
201
const rootRef = useRef < HTMLDivElement > ( null )
202
202
const [ placeholder , setPlaceholder ] = useState < { parentStat : Stat < T > | null , level : number , index : number } | null > ( ) ;
203
- const isExternal = ! draggedStat
203
+ const isExternal = ! draggingStat
204
204
const cacheForVisible = useMemo (
205
205
( ) => {
206
206
const visibleIds : Id [ ] = [ ]
207
207
const attrsList : NodeAttrs [ ] = [ ] ;
208
208
for ( const [ stat , { skipChildren } ] of walkTreeDataGenerator ( rootStats , 'childStats' ) ) {
209
209
const attrs = createAttrs ( stat )
210
- if ( stat === draggedStat ) {
210
+ if ( stat === draggingStat ) {
211
211
// hide dragged node but don't remove it. Because dragend event won't be triggered if without it.
212
212
Object . assign ( attrs . style ! , {
213
213
position : 'fixed' ,
@@ -220,7 +220,7 @@ export function useHeTree<T extends Record<string, any>>(
220
220
}
221
221
attrsList . push ( attrs )
222
222
visibleIds . push ( stat . id )
223
- if ( ! stat . open || stat === draggedStat ) {
223
+ if ( ! stat . open || stat === draggingStat ) {
224
224
skipChildren ( )
225
225
}
226
226
}
@@ -298,7 +298,7 @@ export function useHeTree<T extends Record<string, any>>(
298
298
e . dataTransfer . setDragImage ( node , ! rtl ? 0 : node . offsetWidth , 0 ) ;
299
299
}
300
300
setTimeout ( ( ) => {
301
- setDraggedStat ( stat )
301
+ setDraggingStat ( stat )
302
302
setPlaceholder ( {
303
303
...placeholder ! ,
304
304
parentStat : stat . parentStat ,
@@ -430,20 +430,6 @@ export function useHeTree<T extends Record<string, any>>(
430
430
}
431
431
}
432
432
const onDragOverRoot : React . DragEventHandler < HTMLElement > = ( e ) => {
433
- // ignore if placeholder exists
434
- if ( placeholder ) {
435
- e . preventDefault ( ) ; // droppable
436
- return
437
- }
438
- // ignore if has visible tree node
439
- if ( visibleIds . length > 0 ) {
440
- return
441
- }
442
- // ignore if already over node box
443
- // but it seems to duplicated with the above condition.
444
- if ( isAnyNodeOver ( ) ) {
445
- return
446
- }
447
433
if ( isExternal && ! props . onExternalDragOver ?.( e ) ) {
448
434
return
449
435
}
@@ -472,6 +458,10 @@ export function useHeTree<T extends Record<string, any>>(
472
458
}
473
459
return r
474
460
}
461
+ function getCloest ( ) {
462
+ const rootEl = rootRef . current as HTMLElement
463
+ // rootEl.querySelectorAll([])
464
+ }
475
465
}
476
466
const onDropToRoot : React . DragEventHandler < HTMLElement > = ( e ) => {
477
467
if ( isExternal && ! props . onExternalDragOver ?.( e ) ) {
@@ -487,23 +477,23 @@ export function useHeTree<T extends Record<string, any>>(
487
477
}
488
478
}
489
479
function onDragEndOnRoot ( e : React . DragEvent < HTMLElement > ) {
490
- // draggedStat may not be null. This condition is tell typescript that.
491
- if ( ! draggedStat ) {
480
+ // draggingStat may not be null. This condition is tell typescript that.
481
+ if ( ! draggingStat ) {
492
482
return
493
483
}
494
484
// listen dragend. dragend only trigger in dragstart node
495
485
const isOutside = ! placeholder // placeholder is removed if dragleave the tree
496
- const customized = props . onDragEnd ?.( e , draggedStat ! , isOutside ) === false
486
+ const customized = props . onDragEnd ?.( e , draggingStat ! , isOutside ) === false
497
487
if ( ! customized && ! isOutside ) {
498
488
let targetIndexInSiblings = placeholder . index
499
- if ( placeholder . parentStat === draggedStat . parentStat && draggedStat . index < targetIndexInSiblings ) {
489
+ if ( placeholder . parentStat === draggingStat . parentStat && draggingStat . index < targetIndexInSiblings ) {
500
490
targetIndexInSiblings --
501
491
}
502
492
const newData = [ ...props . data ] ;
503
493
if ( props . dataType === 'flat' ) {
504
494
const targetParentId = placeholder . parentStat ?. id ?? props . rootId
505
- const removed = removeByIdInFlatData ( newData , draggedStat . id , flatOpt )
506
- const newNode = { ...draggedStat . node , [ PID ] : targetParentId }
495
+ const removed = removeByIdInFlatData ( newData , draggingStat . id , flatOpt )
496
+ const newNode = { ...draggingStat . node , [ PID ] : targetParentId }
507
497
removed [ 0 ] = newNode
508
498
const targetTreeIndex = convertIndexToTreeIndexInFlatData ( newData , targetParentId , targetIndexInSiblings , flatOpt )
509
499
newData . splice ( targetTreeIndex , 0 , ...removed )
@@ -523,20 +513,20 @@ export function useHeTree<T extends Record<string, any>>(
523
513
siblings [ stat . index ] = newNode ;
524
514
return children
525
515
}
526
- const newSiblingsOfDragged = copyNode ( draggedStat . parentStat )
527
- const newSiblingsOfTarget = placeholder . parentStat === draggedStat . parentStat ? newSiblingsOfDragged : copyNode ( placeholder . parentStat )
516
+ const newSiblingsOfDragged = copyNode ( draggingStat . parentStat )
517
+ const newSiblingsOfTarget = placeholder . parentStat === draggingStat . parentStat ? newSiblingsOfDragged : copyNode ( placeholder . parentStat )
528
518
// remove
529
- newSiblingsOfDragged . splice ( draggedStat . index , 1 )
519
+ newSiblingsOfDragged . splice ( draggingStat . index , 1 )
530
520
// add
531
- newSiblingsOfTarget . splice ( targetIndexInSiblings , 0 , draggedStat . node )
521
+ newSiblingsOfTarget . splice ( targetIndexInSiblings , 0 , draggingStat . node )
532
522
}
533
523
props . onChange ! ( newData )
534
524
}
535
525
reset ( )
536
526
}
537
527
function reset ( ) {
538
528
setDragOverStat ( undefined ) ;
539
- setDraggedStat ( undefined ) ;
529
+ setDraggingStat ( undefined ) ;
540
530
setPlaceholder ( undefined ) ;
541
531
}
542
532
function getDroppable ( stat : Stat < T > | null , index ?: number ) : boolean {
@@ -559,7 +549,7 @@ export function useHeTree<T extends Record<string, any>>(
559
549
let closest = stat
560
550
let index = visibleIds . indexOf ( stat . id ) // index of closest node
561
551
let atTop = false
562
- const isPlaceholderOrDraggedNode = ( id : Id ) => id === placeholderId || getStat ( id ) === draggedStat
552
+ const isPlaceholderOrDraggedNode = ( id : Id ) => id === placeholderId || getStat ( id ) === draggingStat
563
553
const find = ( startIndex : number , step : number ) => {
564
554
let i = startIndex , cur
565
555
do {
@@ -597,7 +587,7 @@ export function useHeTree<T extends Record<string, any>>(
597
587
return index
598
588
}
599
589
return { visibleIds, attrsList, onDragOverRoot, onDropToRoot, onDragEndOnRoot }
600
- } , [ mainCache , indent , draggedStat ,
590
+ } , [ mainCache , indent , draggingStat ,
601
591
// watch placeholder position
602
592
placeholder ?. parentStat , placeholder ?. index ,
603
593
// watch props
@@ -635,7 +625,7 @@ export function useHeTree<T extends Record<string, any>>(
635
625
useAddEventListener ( t2 . getEl , 'dragover' , t2 . onDragOverWindow )
636
626
//
637
627
const { visibleIds, attrsList, onDragOverRoot, onDropToRoot, onDragEndOnRoot } = cacheForVisible
638
- const persistentIndices = useMemo ( ( ) => draggedStat ? [ visibleIds . indexOf ( draggedStat . id ) ] : [ ] , [ draggedStat , visibleIds ] ) ;
628
+ const persistentIndices = useMemo ( ( ) => draggingStat ? [ visibleIds . indexOf ( draggingStat . id ) ] : [ ] , [ draggingStat , visibleIds ] ) ;
639
629
// render
640
630
const renderHeTree = ( options ?: { className ?: string , style ?: React . CSSProperties } ) => {
641
631
let renderNodeBox = props . renderNodeBox !
@@ -663,7 +653,7 @@ export function useHeTree<T extends Record<string, any>>(
663
653
// ref
664
654
virtualListRef,
665
655
// drag states
666
- draggedStat , dragOverStat, placeholder,
656
+ draggingStat , dragOverStat, placeholder,
667
657
// render
668
658
renderHeTree,
669
659
}
0 commit comments