refactor(HistoryDatabase): optimize card expansion behavior with debounce and animation lock

- Introduced a debounce mechanism to prevent rapid state changes during card expansion and collapse.
- Added an animation lock to avoid conflicts during transitions.
- Adjusted Intersection Observer thresholds and rootMargin for smoother detection and stability.
- Cleaned up resources on component unmounting to prevent memory leaks.
This commit is contained in:
666ghj 2026-01-09 10:46:32 +08:00
parent d90ec77f5d
commit dfab81a6c0

View file

@ -104,6 +104,8 @@ const isExpanded = ref(false)
const hoveringCard = ref(null) const hoveringCard = ref(null)
const historyContainer = ref(null) const historyContainer = ref(null)
let observer = null let observer = null
let isAnimating = false //
let expandDebounceTimer = null //
// - // -
const CARDS_PER_ROW = 4 const CARDS_PER_ROW = 4
@ -290,19 +292,48 @@ onMounted(() => {
loadHistory() loadHistory()
// 使 Intersection Observer / // 使 Intersection Observer /
// 使
observer = new IntersectionObserver( observer = new IntersectionObserver(
(entries) => { (entries) => {
entries.forEach((entry) => { entries.forEach((entry) => {
if (entry.isIntersecting) { //
isExpanded.value = true if (isAnimating) return
} else {
isExpanded.value = false const shouldExpand = entry.isIntersecting
//
if (shouldExpand === isExpanded.value) return
//
if (expandDebounceTimer) {
clearTimeout(expandDebounceTimer)
expandDebounceTimer = null
} }
// 使
// (50ms)(200ms)
const delay = shouldExpand ? 50 : 200
expandDebounceTimer = setTimeout(() => {
//
if (isAnimating) return
//
isAnimating = true
isExpanded.value = shouldExpand
// 700ms
setTimeout(() => {
isAnimating = false
}, 750)
}, delay)
}) })
}, },
{ {
threshold: [0.5], // 使使
rootMargin: '0px 0px -150px 0px' threshold: [0.3, 0.5, 0.7],
// rootMargin使
rootMargin: '0px 0px -100px 0px'
} }
) )
@ -315,10 +346,16 @@ onMounted(() => {
}) })
onUnmounted(() => { onUnmounted(() => {
// Intersection Observer
if (observer) { if (observer) {
observer.disconnect() observer.disconnect()
observer = null observer = null
} }
//
if (expandDebounceTimer) {
clearTimeout(expandDebounceTimer)
expandDebounceTimer = null
}
}) })
</script> </script>