refactor(HistoryDatabase): enhance loading logic and card positioning

- Added route watcher to reload history data when navigating back to the homepage.
- Improved card positioning by adjusting vertical spacing for better visual alignment.
- Initialized IntersectionObserver to manage card visibility more effectively.
- Ensured data loading occurs after DOM rendering for smoother user experience.
This commit is contained in:
666ghj 2026-01-09 11:22:36 +08:00
parent 5e865c0234
commit d79ea2b861

View file

@ -86,11 +86,12 @@
</template>
<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import { ref, computed, onMounted, onUnmounted, onActivated, watch, nextTick } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { getSimulationHistory } from '../api/simulation'
const router = useRouter()
const route = useRoute()
//
const projects = ref([])
@ -150,8 +151,8 @@ const getCardStyle = (index) => {
const colInRow = index % CARDS_PER_ROW
const x = startX + colInRow * (CARD_WIDTH + CARD_GAP)
//
const y = row * (CARD_HEIGHT + CARD_GAP)
//
const y = 20 + row * (CARD_HEIGHT + CARD_GAP)
return {
transform: `translate(${x}px, ${y}px) rotate(0deg) scale(1)`,
@ -167,8 +168,8 @@ const getCardStyle = (index) => {
const offset = index - centerIndex
const x = offset * 35
//
const y = 40 + Math.abs(offset) * 8
//
const y = 25 + Math.abs(offset) * 8
const r = offset * 3
const s = 0.95 - Math.abs(offset) * 0.05
@ -308,11 +309,12 @@ const loadHistory = async () => {
}
}
onMounted(() => {
loadHistory()
// IntersectionObserver
const initObserver = () => {
if (observer) {
observer.disconnect()
}
// 使 Intersection Observer /
// 使
observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
@ -382,14 +384,35 @@ onMounted(() => {
}
)
// DOM
setTimeout(() => {
//
if (historyContainer.value) {
observer.observe(historyContainer.value)
}
}
//
watch(() => route.path, (newPath) => {
if (newPath === '/') {
loadHistory()
}
})
onMounted(async () => {
// DOM
await nextTick()
await loadHistory()
// DOM
setTimeout(() => {
initObserver()
}, 100)
})
// 使 keep-alive
onActivated(() => {
loadHistory()
})
onUnmounted(() => {
// Intersection Observer
if (observer) {
@ -410,8 +433,8 @@ onUnmounted(() => {
position: relative;
width: 100%;
min-height: 280px;
margin-top: 80px;
padding: 60px 0 40px;
margin-top: 40px;
padding: 40px 0 40px;
overflow: visible;
}