refactor: apply coderabbitai suggestions

- Use createInitialWindowRange and calculateSpacerHeights in getTotalSpacerHeights
- Use active range after shift for recursive window processing
- Remove unused export from findNodeByKey
This commit is contained in:
Yourz
2025-12-15 20:20:14 +08:00
parent 0738773b15
commit e094e42c07
2 changed files with 18 additions and 12 deletions

View File

@@ -140,13 +140,17 @@ const getTotalSpacerHeights = () => {
if (!isExpanded) return
const totalChildren = node.children.length
const range = parentWindowRanges.value[node.key] ?? {
start: 0,
end: Math.min(WINDOW_SIZE, totalChildren)
}
const range =
parentWindowRanges.value[node.key] ??
createInitialWindowRange(totalChildren, WINDOW_SIZE)
topTotal += range.start * NODE_HEIGHT
bottomTotal += (totalChildren - range.end) * NODE_HEIGHT
const { topSpacer, bottomSpacer } = calculateSpacerHeights(
totalChildren,
range,
NODE_HEIGHT
)
topTotal += topSpacer
bottomTotal += bottomSpacer
// Recursively check children in the window
for (let i = range.start; i < range.end && i < node.children.length; i++) {
@@ -244,8 +248,13 @@ const shiftNodeWindow = (
parentWindowRanges.value[node.key] = newRange
}
// Recursively process children in current window
for (let i = range.start; i < range.end && i < node.children.length; i++) {
// Recursively process children in the active window (after shift)
const activeRange = newRange ?? range
for (
let i = activeRange.start;
i < activeRange.end && i < node.children.length;
i++
) {
shiftNodeWindow(node.children[i], direction)
}
}

View File

@@ -105,10 +105,7 @@ export function sortedTree(
return newNode
}
export const findNodeByKey = <T extends TreeNode>(
root: T,
key: string
): T | null => {
const findNodeByKey = <T extends TreeNode>(root: T, key: string): T | null => {
if (root.key === key) {
return root
}