fix comment link navigation

This commit is contained in:
Tim
2025-07-07 13:32:02 +08:00
parent ba368f7bd7
commit 61c05cdc66
2 changed files with 44 additions and 5 deletions

View File

@@ -68,7 +68,14 @@
<CommentEditor @submit="postComment" :loading="isWaitingPostingComment" />
<div class="comments-container">
<CommentItem v-for="comment in comments" :key="comment.id" :comment="comment" :level="0" ref="postItems" />
<CommentItem
v-for="comment in comments"
:key="comment.id"
:comment="comment"
:level="0"
:default-show-replies="comment.openReplies"
ref="postItems"
/>
</div>
</div>
@@ -125,9 +132,31 @@ export default {
time: new Date(c.createdAt).toLocaleDateString('zh-CN', { month: 'numeric', day: 'numeric' }),
avatar: c.author.avatar,
text: c.content,
reply: (c.replies || []).map(mapComment)
reply: (c.replies || []).map(mapComment),
openReplies: false
})
const findCommentPath = (id, list) => {
for (const item of list) {
if (item.id === Number(id) || item.id === id) {
return [item]
}
if (item.reply && item.reply.length) {
const sub = findCommentPath(id, item.reply)
if (sub) return [item, ...sub]
}
}
return null
}
const expandCommentPath = (id) => {
const path = findCommentPath(id, comments.value)
if (!path) return
for (let i = 0; i < path.length - 1; i++) {
path[i].openReplies = true
}
}
const fetchPost = async () => {
try {
isWaitingFetchingPost.value = true;
@@ -220,7 +249,10 @@ export default {
}
onMounted(async () => {
const hash = location.hash
const id = hash.startsWith('#comment-') ? hash.substring('#comment-'.length) : null
await fetchPost()
if (id) expandCommentPath(id)
updateCurrentIndex()
await jumpToHashComment()
})