Skip to content

Commit ba5b311

Browse files
committed
refactor: rename draggedStat to draggingStat
1 parent a4f6cbf commit ba5b311

File tree

1 file changed

+24
-34
lines changed

1 file changed

+24
-34
lines changed

lib/HeTree.tsx

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -195,19 +195,19 @@ export function useHeTree<T extends Record<string, any>>(
195195
const { rootIds, rootStats, getStat, } = mainCache;
196196
// about drag ==================================
197197
const indent = props.indent!
198-
const [draggedStat, setDraggedStat] = useState<Stat<T>>();
198+
const [draggingStat, setDraggingStat] = useState<Stat<T>>();
199199
const [dragOverStat, setDragOverStat] = useState<Stat<T>>();
200200
const virtualListRef = useRef<VirtualListHandle>(null);
201201
const rootRef = useRef<HTMLDivElement>(null)
202202
const [placeholder, setPlaceholder] = useState<{ parentStat: Stat<T> | null, level: number, index: number } | null>();
203-
const isExternal = !draggedStat
203+
const isExternal = !draggingStat
204204
const cacheForVisible = useMemo(
205205
() => {
206206
const visibleIds: Id[] = []
207207
const attrsList: NodeAttrs[] = [];
208208
for (const [stat, { skipChildren }] of walkTreeDataGenerator(rootStats, 'childStats')) {
209209
const attrs = createAttrs(stat)
210-
if (stat === draggedStat) {
210+
if (stat === draggingStat) {
211211
// hide dragged node but don't remove it. Because dragend event won't be triggered if without it.
212212
Object.assign(attrs.style!, {
213213
position: 'fixed',
@@ -220,7 +220,7 @@ export function useHeTree<T extends Record<string, any>>(
220220
}
221221
attrsList.push(attrs)
222222
visibleIds.push(stat.id)
223-
if (!stat.open || stat === draggedStat) {
223+
if (!stat.open || stat === draggingStat) {
224224
skipChildren()
225225
}
226226
}
@@ -298,7 +298,7 @@ export function useHeTree<T extends Record<string, any>>(
298298
e.dataTransfer.setDragImage(node, !rtl ? 0 : node.offsetWidth, 0);
299299
}
300300
setTimeout(() => {
301-
setDraggedStat(stat)
301+
setDraggingStat(stat)
302302
setPlaceholder({
303303
...placeholder!,
304304
parentStat: stat.parentStat,
@@ -430,20 +430,6 @@ export function useHeTree<T extends Record<string, any>>(
430430
}
431431
}
432432
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-
}
447433
if (isExternal && !props.onExternalDragOver?.(e)) {
448434
return
449435
}
@@ -472,6 +458,10 @@ export function useHeTree<T extends Record<string, any>>(
472458
}
473459
return r
474460
}
461+
function getCloest() {
462+
const rootEl = rootRef.current as HTMLElement
463+
// rootEl.querySelectorAll([])
464+
}
475465
}
476466
const onDropToRoot: React.DragEventHandler<HTMLElement> = (e) => {
477467
if (isExternal && !props.onExternalDragOver?.(e)) {
@@ -487,23 +477,23 @@ export function useHeTree<T extends Record<string, any>>(
487477
}
488478
}
489479
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) {
492482
return
493483
}
494484
// listen dragend. dragend only trigger in dragstart node
495485
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
497487
if (!customized && !isOutside) {
498488
let targetIndexInSiblings = placeholder.index
499-
if (placeholder.parentStat === draggedStat.parentStat && draggedStat.index < targetIndexInSiblings) {
489+
if (placeholder.parentStat === draggingStat.parentStat && draggingStat.index < targetIndexInSiblings) {
500490
targetIndexInSiblings--
501491
}
502492
const newData = [...props.data];
503493
if (props.dataType === 'flat') {
504494
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 }
507497
removed[0] = newNode
508498
const targetTreeIndex = convertIndexToTreeIndexInFlatData(newData, targetParentId, targetIndexInSiblings, flatOpt)
509499
newData.splice(targetTreeIndex, 0, ...removed)
@@ -523,20 +513,20 @@ export function useHeTree<T extends Record<string, any>>(
523513
siblings[stat.index] = newNode;
524514
return children
525515
}
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)
528518
// remove
529-
newSiblingsOfDragged.splice(draggedStat.index, 1)
519+
newSiblingsOfDragged.splice(draggingStat.index, 1)
530520
// add
531-
newSiblingsOfTarget.splice(targetIndexInSiblings, 0, draggedStat.node)
521+
newSiblingsOfTarget.splice(targetIndexInSiblings, 0, draggingStat.node)
532522
}
533523
props.onChange!(newData)
534524
}
535525
reset()
536526
}
537527
function reset() {
538528
setDragOverStat(undefined);
539-
setDraggedStat(undefined);
529+
setDraggingStat(undefined);
540530
setPlaceholder(undefined);
541531
}
542532
function getDroppable(stat: Stat<T> | null, index?: number): boolean {
@@ -559,7 +549,7 @@ export function useHeTree<T extends Record<string, any>>(
559549
let closest = stat
560550
let index = visibleIds.indexOf(stat.id) // index of closest node
561551
let atTop = false
562-
const isPlaceholderOrDraggedNode = (id: Id) => id === placeholderId || getStat(id) === draggedStat
552+
const isPlaceholderOrDraggedNode = (id: Id) => id === placeholderId || getStat(id) === draggingStat
563553
const find = (startIndex: number, step: number) => {
564554
let i = startIndex, cur
565555
do {
@@ -597,7 +587,7 @@ export function useHeTree<T extends Record<string, any>>(
597587
return index
598588
}
599589
return { visibleIds, attrsList, onDragOverRoot, onDropToRoot, onDragEndOnRoot }
600-
}, [mainCache, indent, draggedStat,
590+
}, [mainCache, indent, draggingStat,
601591
// watch placeholder position
602592
placeholder?.parentStat, placeholder?.index,
603593
// watch props
@@ -635,7 +625,7 @@ export function useHeTree<T extends Record<string, any>>(
635625
useAddEventListener(t2.getEl, 'dragover', t2.onDragOverWindow)
636626
//
637627
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]);
639629
// render
640630
const renderHeTree = (options?: { className?: string, style?: React.CSSProperties }) => {
641631
let renderNodeBox = props.renderNodeBox!
@@ -663,7 +653,7 @@ export function useHeTree<T extends Record<string, any>>(
663653
// ref
664654
virtualListRef,
665655
// drag states
666-
draggedStat, dragOverStat, placeholder,
656+
draggingStat, dragOverStat, placeholder,
667657
// render
668658
renderHeTree,
669659
}

0 commit comments

Comments
 (0)