refactor: extract common shiftNodeWindow helper function

Apply DRY principle suggestion from coderabbitai
This commit is contained in:
Yourz
2025-12-15 20:01:29 +08:00
parent 8656cc8468
commit 0738773b15

View File

@@ -221,8 +221,12 @@ const handleTreeScroll = useThrottleFn(() => {
} }
}, 100) }, 100)
// Shift window forward for a single node (recursive) // Shift window for a single node in given direction (recursive)
const shiftNodeWindowForward = (node: RenderedTreeExplorerNode) => { type ShiftDirection = 'forward' | 'backward'
const shiftNodeWindow = (
node: RenderedTreeExplorerNode,
direction: ShiftDirection
) => {
if (!node.children || node.leaf) return if (!node.children || node.leaf) return
const isExpanded = expandedKeys.value?.[node.key] ?? false const isExpanded = expandedKeys.value?.[node.key] ?? false
if (!isExpanded) return if (!isExpanded) return
@@ -231,61 +235,31 @@ const shiftNodeWindowForward = (node: RenderedTreeExplorerNode) => {
const range = const range =
parentWindowRanges.value[node.key] ?? parentWindowRanges.value[node.key] ??
createInitialWindowRange(totalChildren, WINDOW_SIZE) createInitialWindowRange(totalChildren, WINDOW_SIZE)
const newRange = shiftWindowForwardUtil(
range, const shiftFn =
totalChildren, direction === 'forward' ? shiftWindowForwardUtil : shiftWindowBackwardUtil
BUFFER_SIZE, const newRange = shiftFn(range, totalChildren, BUFFER_SIZE, WINDOW_SIZE)
WINDOW_SIZE
)
if (newRange) { if (newRange) {
parentWindowRanges.value[node.key] = newRange parentWindowRanges.value[node.key] = newRange
} }
// Recursively process children in current window // Recursively process children in current window
for (let i = range.start; i < range.end && i < node.children.length; i++) { for (let i = range.start; i < range.end && i < node.children.length; i++) {
shiftNodeWindowForward(node.children[i]) shiftNodeWindow(node.children[i], direction)
} }
} }
// Shift all windows forward (load more at end) // Shift all windows in given direction
const shiftWindowsForward = () => { const shiftWindows = (direction: ShiftDirection) => {
for (const parent of renderedRoot.value.children || []) { for (const parent of renderedRoot.value.children || []) {
shiftNodeWindowForward(parent) shiftNodeWindow(parent, direction)
} }
} }
// Shift window backward for a single node (recursive) // Convenience functions for forward/backward
const shiftNodeWindowBackward = (node: RenderedTreeExplorerNode) => { const shiftWindowsForward = () => shiftWindows('forward')
if (!node.children || node.leaf) return const shiftWindowsBackward = () => shiftWindows('backward')
const isExpanded = expandedKeys.value?.[node.key] ?? false
if (!isExpanded) return
const totalChildren = node.children.length
const range =
parentWindowRanges.value[node.key] ??
createInitialWindowRange(totalChildren, WINDOW_SIZE)
const newRange = shiftWindowBackwardUtil(
range,
totalChildren,
BUFFER_SIZE,
WINDOW_SIZE
)
if (newRange) {
parentWindowRanges.value[node.key] = newRange
}
// Recursively process children in current window
for (let i = range.start; i < range.end && i < node.children.length; i++) {
shiftNodeWindowBackward(node.children[i])
}
}
// Shift all windows backward (load more at start)
const shiftWindowsBackward = () => {
for (const parent of renderedRoot.value.children || []) {
shiftNodeWindowBackward(parent)
}
}
const renderedRoot = computed<RenderedTreeExplorerNode>(() => { const renderedRoot = computed<RenderedTreeExplorerNode>(() => {
const renderedRoot = fillNodeInfo(props.root) const renderedRoot = fillNodeInfo(props.root)