@@ -452,53 +452,38 @@ export function useHeTree<T extends Record<string, any>>(
452
452
customized = true
453
453
}
454
454
if ( ! customized && ! isExternal ) {
455
- // move node
456
- // const draft = getDraft()
457
- // let draggedSiblings: T[], targetSiblings: T[]
458
- // if (!placeholder.parentStat) {
459
- // targetSiblings = draft
460
- // }
461
- // let draggedNodeDraft: T
462
- // for (const [node, { siblings }] of walkTreeDataGenerator(draft, CHILDREN)) {
463
- // if (node[ID] === draggedStat.id) {
464
- // draggedSiblings = siblings
465
- // draggedNodeDraft = node
466
- // }
467
- // if (!targetSiblings! && node[ID] === placeholder.parentStat!.id) {
468
- // targetSiblings = node[CHILDREN]
469
- // }
470
- // if (draggedSiblings! && targetSiblings!) {
471
- // break
472
- // }
473
- // }
474
- // const ds = draggedSiblings!
475
- // const ts = targetSiblings!
476
- // const startIndex = ds.findIndex(v => v[ID] === draggedStat.id)
477
- // let targetIndex = placeholder.index
478
- // // check index
479
- // if (ds === ts && placeholder.index > startIndex) {
480
- // targetIndex -= 1
481
- // }
482
- // // remove from start position
483
- // ds.splice(startIndex, 1)
484
- // // move to target position
485
- // ts.splice(targetIndex, 0, draggedNodeDraft!)
486
- // props.onChange(nextData())
455
+ let targetIndexInSiblings = placeholder . index
456
+ if ( placeholder . parentStat === draggedStat . parentStat && draggedStat . index < targetIndexInSiblings ) {
457
+ targetIndexInSiblings --
458
+ }
459
+ const newData = [ ...props . data ] ;
487
460
if ( props . dataType === 'flat' ) {
488
- const startIndex = props . data . findIndex ( v => v [ ID ] === draggedStat . id )
489
- let targetIndexInSiblings = placeholder . index
490
- if ( placeholder . parentStat === draggedStat . parentStat && draggedStat . index < targetIndexInSiblings ) {
491
- targetIndexInSiblings --
492
- }
493
461
const targetParentId = placeholder . parentStat ?. id ?? null
494
- const newData = [ ...props . data ] ;
495
462
const removed = removeByIdInFlatData ( newData , draggedStat . id , flatOpt )
496
463
const newNode = { ...draggedStat . node , [ PID ] : targetParentId }
497
464
removed [ 0 ] = newNode
498
465
const targetTreeIndex = convertIndexToTreeIndexInFlatData ( newData , targetParentId , targetIndexInSiblings , flatOpt )
499
466
newData . splice ( targetTreeIndex , 0 , ...removed )
500
- props . onChange ! ( newData )
467
+ } else {
468
+ // copy data
469
+ const copyNode = ( stat : Stat < T > | null ) => {
470
+ if ( ! stat ) {
471
+ return newData
472
+ }
473
+ const siblings = copyNode ( stat . parentStat )
474
+ const children = [ ...stat . children ] ;
475
+ const newNode = { ...stat . node , [ CHILDREN ] : children }
476
+ siblings [ stat . index ] = newNode ;
477
+ return children
478
+ }
479
+ const newSiblingsOfDragged = copyNode ( draggedStat . parentStat )
480
+ const newSiblingsOfTarget = placeholder . parentStat === draggedStat . parentStat ? newSiblingsOfDragged : copyNode ( placeholder . parentStat )
481
+ // remove
482
+ newSiblingsOfDragged . splice ( draggedStat . index , 1 )
483
+ // add
484
+ newSiblingsOfTarget . splice ( targetIndexInSiblings , 0 , draggedStat . node )
501
485
}
486
+ props . onChange ! ( newData )
502
487
}
503
488
}
504
489
if ( ! customized ) {
0 commit comments