fix post scroll indexing with BaseTimeline

This commit is contained in:
Tim
2025-07-07 17:55:59 +08:00
parent 15a551d700
commit 05c7c6668d

View File

@@ -21,7 +21,7 @@
</div> </div>
</div> </div>
<div class="info-content-container" ref="postItems"> <div class="info-content-container">
<div class="user-avatar-container"> <div class="user-avatar-container">
<div class="user-avatar-item"> <div class="user-avatar-item">
<img class="user-avatar-item-img" :src="author.avatar" alt="avatar"> <img class="user-avatar-item-img" :src="author.avatar" alt="avatar">
@@ -106,7 +106,7 @@
</template> </template>
<script> <script>
import { ref, computed, onMounted, nextTick } from 'vue' import { ref, computed, onMounted, nextTick, watch } from 'vue'
import { useRoute } from 'vue-router' import { useRoute } from 'vue-router'
import CommentItem from '../components/CommentItem.vue' import CommentItem from '../components/CommentItem.vue'
import CommentEditor from '../components/CommentEditor.vue' import CommentEditor from '../components/CommentEditor.vue'
@@ -137,6 +137,19 @@ export default {
const mainContainer = ref(null) const mainContainer = ref(null)
const currentIndex = ref(1) const currentIndex = ref(1)
const gatherPostItems = () => {
const items = []
if (mainContainer.value) {
const main = mainContainer.value.querySelector('.info-content-container')
if (main) items.push(main)
for (const c of comments.value) {
const el = document.getElementById('comment-' + c.id)
if (el) items.push(el)
}
}
postItems.value = items
}
const mapComment = c => ({ const mapComment = c => ({
id: c.id, id: c.id,
userName: c.author.username, userName: c.author.username,
@@ -183,6 +196,8 @@ export default {
tags.value = data.tags || [] tags.value = data.tags || []
comments.value = (data.comments || []).map(mapComment) comments.value = (data.comments || []).map(mapComment)
postTime.value = new Date(data.createdAt).toLocaleDateString('zh-CN', { month: 'numeric', day: 'numeric' }) postTime.value = new Date(data.createdAt).toLocaleDateString('zh-CN', { month: 'numeric', day: 'numeric' })
await nextTick()
gatherPostItems()
} catch (e) { } catch (e) {
console.error(e) console.error(e)
} }
@@ -193,10 +208,18 @@ export default {
comments.value.length ? comments.value[comments.value.length - 1].time : postTime.value comments.value.length ? comments.value[comments.value.length - 1].time : postTime.value
) )
watch(
() => comments.value.length,
async () => {
await nextTick()
gatherPostItems()
}
)
const updateCurrentIndex = () => { const updateCurrentIndex = () => {
const scrollTop = mainContainer.value ? mainContainer.value.scrollTop : 0 const scrollTop = mainContainer.value ? mainContainer.value.scrollTop : 0
for (let i = 0; i < postItems.value.length; i++) { for (let i = 0; i < postItems.value.length; i++) {
const el = postItems.value[i].$el const el = postItems.value[i]
if (el.offsetTop + el.offsetHeight > scrollTop) { if (el.offsetTop + el.offsetHeight > scrollTop) {
currentIndex.value = i + 1 currentIndex.value = i + 1
break break
@@ -205,7 +228,7 @@ export default {
} }
const onSliderInput = () => { const onSliderInput = () => {
const target = postItems.value[currentIndex.value - 1]?.$el const target = postItems.value[currentIndex.value - 1]
if (target && mainContainer.value) { if (target && mainContainer.value) {
mainContainer.value.scrollTo({ top: target.offsetTop, behavior: 'instant' }) mainContainer.value.scrollTo({ top: target.offsetTop, behavior: 'instant' })
} }
@@ -229,6 +252,8 @@ export default {
if (res.ok) { if (res.ok) {
const data = await res.json() const data = await res.json()
comments.value.push(mapComment(data)) comments.value.push(mapComment(data))
await nextTick()
gatherPostItems()
toast.success('评论成功') toast.success('评论成功')
} else { } else {
toast.error('评论失败') toast.error('评论失败')
@@ -264,6 +289,7 @@ export default {
const hash = location.hash const hash = location.hash
const id = hash.startsWith('#comment-') ? hash.substring('#comment-'.length) : null const id = hash.startsWith('#comment-') ? hash.substring('#comment-'.length) : null
await fetchPost() await fetchPost()
gatherPostItems()
if (id) expandCommentPath(id) if (id) expandCommentPath(id)
updateCurrentIndex() updateCurrentIndex()
await jumpToHashComment() await jumpToHashComment()