feat: home topic lazy load

This commit is contained in:
KUN1007 2023-09-16 15:24:11 +08:00
parent f086fabc03
commit d8e57311f5
8 changed files with 125 additions and 38 deletions

View file

@ -33,6 +33,8 @@ onBeforeMount(() => {
//
const debouncedSearch = debounce((inputValue: string) => {
//
useKUNGalgameHomeStore().resetPageStatus()
keywords.value = inputValue
}, 300) // 300

View file

@ -12,6 +12,8 @@ interface HomeStore {
limit: number
sortField: string
sortOrder: string
// 加载完了是否还需要加载
isLoading: boolean
// 其它的 store
// 是否激活主页的左侧交互面板
@ -41,6 +43,7 @@ export const useKUNGalgameHomeStore = defineStore({
limit: 17,
sortField: 'updated',
sortOrder: 'desc',
isLoading: true,
// 其它的 store
// 是否激活主页的左侧交互面板
@ -57,8 +60,8 @@ export const useKUNGalgameHomeStore = defineStore({
const requestData: HomeTopicRequestData = {
keywords: this.keywords,
category: this.category,
page: this.page || 1,
limit: this.limit || 17,
page: this.page,
limit: this.limit,
sortField: this.sortField || 'updated',
sortOrder: this.sortOrder || 'desc',
}
@ -72,5 +75,10 @@ export const useKUNGalgameHomeStore = defineStore({
})
})
},
// 重置页数,是否加载,这样排序才能生效
resetPageStatus() {
this.page = 1
this.isLoading = true
},
},
})

View file

@ -57,6 +57,8 @@ interface Topic {
isShowAdvance: boolean
// 是否激活左侧交互面板
isActiveAside: boolean
// 是否滚动到顶部
isScrollToTop: boolean
// 回复的缓存
replyDraft: ReplyDraft
@ -74,6 +76,7 @@ export const useKUNGalgameTopicStore = defineStore({
isEdit: false,
isShowAdvance: false,
isActiveAside: false,
isScrollToTop: false,
replyDraft: {
tid: 0,
r_uid: 0,

View file

@ -1,5 +1,12 @@
<script setup lang="ts">
import { ref, watch } from 'vue'
import {
ref,
watch,
onMounted,
onBeforeUnmount,
onBeforeMount,
nextTick,
} from 'vue'
import SingleTopic from './SingleTopic.vue'
import { HomeTopic } from '@/api/index'
@ -8,23 +15,82 @@ import { HomeTopic } from '@/api/index'
import { useKUNGalgameHomeStore } from '@/store/modules/home'
import { storeToRefs } from 'pinia'
const requestData = storeToRefs(useKUNGalgameHomeStore())
const { page, keywords, sortField, sortOrder, isLoading } = storeToRefs(
useKUNGalgameHomeStore()
)
//
const topics = ref<HomeTopic[]>([])
//
const content = ref<HTMLElement>()
// fetchTopics watch
watch(
[requestData.keywords, requestData.sortField, requestData.sortOrder],
async () => {
topics.value = (await useKUNGalgameHomeStore().getHomeTopic()).data
},
{ immediate: true }
)
watch([keywords, sortField, sortOrder], async () => {
topics.value = (await useKUNGalgameHomeStore().getHomeTopic()).data
})
//
const scrollHandler = async () => {
//
if (isScrollAtBottom() && isLoading.value) {
//
page.value++
const lazyLoadTopics = (await useKUNGalgameHomeStore().getHomeTopic()).data
//
if (!lazyLoadTopics.length) {
isLoading.value = false
}
//
topics.value = [...topics.value, ...lazyLoadTopics]
}
}
//
const isScrollAtBottom = () => {
if (content.value) {
const scrollHeight = content.value.scrollHeight
const scrollTop = content.value.scrollTop
const clientHeight = content.value.clientHeight
// 使 js
// 1007 10 7
const errorMargin = 1.007
return Math.abs(scrollHeight - scrollTop - clientHeight) < errorMargin
}
}
onBeforeMount(async () => {
//
useKUNGalgameHomeStore().resetPageStatus()
})
//
onMounted(async () => {
//
const element = content.value
if (element) {
element.addEventListener('scroll', scrollHandler)
}
topics.value = (await useKUNGalgameHomeStore().getHomeTopic()).data
})
//
onBeforeUnmount(() => {
const element = content.value
if (element) {
element.removeEventListener('scroll', scrollHandler)
}
})
</script>
<template>
<div class="topic-container">
<div class="topic-container" ref="content">
<TransitionGroup name="list" tag="div">
<!-- 该状态为 2 则话题处于被推状态 -->
<div

View file

@ -7,18 +7,21 @@ import { storeToRefs } from 'pinia'
//
import { navSortItem } from './navSortItem'
const homeStore = storeToRefs(useKUNGalgameHomeStore())
const { sortField, sortOrder } = storeToRefs(useKUNGalgameHomeStore())
const handleSortByField = (sortField: string) => {
homeStore.sortField.value = sortField
const handleSortByField = (field: string) => {
useKUNGalgameHomeStore().resetPageStatus()
sortField.value = field
}
const orderAscending = () => {
homeStore.sortOrder.value = 'asc'
useKUNGalgameHomeStore().resetPageStatus()
sortOrder.value = 'asc'
}
const orderDescending = () => {
homeStore.sortOrder.value = 'desc'
useKUNGalgameHomeStore().resetPageStatus()
sortOrder.value = 'desc'
}
</script>

View file

@ -33,14 +33,9 @@ import { storeToRefs } from 'pinia'
//
const route = useRoute()
// 使 store
const settingsStore = useKUNGalgameSettingsStore()
// 使 store
const topicStore = useKUNGalgameTopicStore()
const { showKUNGalgamePageWidth } = storeToRefs(settingsStore)
const { isShowAdvance, isEdit, replyDraft, replyRequest } =
storeToRefs(topicStore)
const { showKUNGalgamePageWidth } = storeToRefs(useKUNGalgameSettingsStore())
const { isShowAdvance, isEdit, replyDraft, replyRequest, isScrollToTop } =
storeToRefs(useKUNGalgameTopicStore())
const tid = computed(() => {
return Number(route.params.tid)
@ -82,6 +77,19 @@ watch(
{ immediate: true }
)
//
watch(isScrollToTop, () => {
if (content.value) {
//
content.value.scrollTo({
top: 0,
behavior: 'smooth',
})
//
isScrollToTop.value = false
}
})
//
const scrollHandler = async () => {
//
@ -112,7 +120,9 @@ const isScrollAtBottom = () => {
const scrollTop = content.value.scrollTop
const clientHeight = content.value.clientHeight
return scrollHeight - scrollTop === clientHeight
// 使 js
const errorMargin = 1.007
return Math.abs(scrollHeight - scrollTop - clientHeight) < errorMargin
}
}
@ -134,6 +144,7 @@ onBeforeUnmount(() => {
element.removeEventListener('scroll', scrollHandler)
}
})
/* 话题界面的页面宽度 */
const topicPageWidth = computed(() => {
return showKUNGalgamePageWidth.value.Topic + '%'
@ -145,10 +156,12 @@ const resetPanelStatus = () => {
isEdit.value = false
}
//
onBeforeRouteLeave(() => {
resetPanelStatus()
})
//
onBeforeMount(() => {
resetPanelStatus()
})

View file

@ -7,7 +7,7 @@ import { asideNavItem } from './asideNavItem'
import { useKUNGalgameTopicStore } from '@/store/modules/topic'
import { storeToRefs } from 'pinia'
const { replyRequest } = storeToRefs(useKUNGalgameTopicStore())
const { isScrollToTop, replyRequest } = storeToRefs(useKUNGalgameTopicStore())
//
const handleSortReply = (sortField: string) => {
@ -26,11 +26,7 @@ const orderDescending = () => {
//
const handleBackToTop = () => {
// 使 window.scrollTo
window.scrollTo({
top: 0, //
behavior: 'smooth', //
})
isScrollToTop.value = true
}
</script>

View file

@ -9,7 +9,7 @@ import { asideNavItem } from './asideNavItem'
import { useKUNGalgameTopicStore } from '@/store/modules/topic'
import { storeToRefs } from 'pinia'
const { replyRequest } = storeToRefs(useKUNGalgameTopicStore())
const { isScrollToTop, replyRequest } = storeToRefs(useKUNGalgameTopicStore())
const handleSortReply = (sortField: string) => {
replyRequest.value.sortField = sortField
@ -24,11 +24,7 @@ const orderDescending = () => {
}
const handleBackToTop = () => {
// 使 window.scrollTo
window.scrollTo({
top: 0, //
behavior: 'smooth', //
})
isScrollToTop.value = true
}
</script>