Skip to content

Commit 646395b

Browse files
committed
使用 client 拉取最新评论
1 parent 5afc370 commit 646395b

1 file changed

Lines changed: 21 additions & 34 deletions

File tree

src/components/waline/DanmakuComments.astro

Lines changed: 21 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,31 +4,9 @@ import { Image } from 'astro:assets'
44
import commentFlag from '@/assets/other/comment_flag.png'
55
import config from '@/site-config'
66
7-
interface WalineComment {
8-
nick: string
9-
comment: string
10-
avatar: string
11-
url?: string
12-
link?: string
13-
like?: number
14-
type?: string // Add type for admin check
15-
}
16-
17-
let recentComments: WalineComment[] = []
7+
// This frontmatter now only passes the serverURL to the client-side script.
8+
// The actual fetch happens in the browser.
189
const serverURL = config.integ.waline.server
19-
20-
if (serverURL) {
21-
try {
22-
const response = await fetch(`${serverURL}/comment?type=recent&count=50`)
23-
if (response.ok) {
24-
recentComments = await response.json()
25-
} else {
26-
console.error(`[Danmaku Server] Failed to fetch, status: ${response.status}`)
27-
}
28-
} catch (e) {
29-
console.error('[Danmaku Server] Error fetching comments:', e)
30-
}
31-
}
3210
---
3311

3412
<div id='danmaku_container' class='terminal-container my-12 mx-auto'>
@@ -55,18 +33,32 @@ if (serverURL) {
5533

5634
<div class='status-line'>
5735
<span>Lines: <span id='danmaku-lineCount'>0</span></span>
58-
<span id='danmaku-keyboard-shortcuts'
59-
>Press <kbd>Space</kbd> to play <kbd>C</kbd> to clear</span
36+
<span id='danmaku-keyboard-shortcuts'>Press <kbd>Space</kbd> to play <kbd>C</kbd> to clear</span
6037
>
6138
</div>
6239
</div>
6340

64-
<script is:inline define:vars={{ comments: recentComments }}>
65-
function initializeDanmaku() {
41+
<script is:inline define:vars={{ serverURL }}>
42+
async function initializeDanmaku() {
6643
const container = document.getElementById('danmaku_container')
6744
if (!container || container.dataset.initialized) return
6845
container.dataset.initialized = 'true'
6946

47+
// Data is now fetched on the client-side
48+
let comments = []
49+
if (serverURL) {
50+
try {
51+
const response = await fetch(`${serverURL}/comment?type=recent&count=50`)
52+
if (response.ok) {
53+
comments = await response.json()
54+
} else {
55+
console.error(`[Danmaku Server] Failed to fetch, status: ${response.status}`)
56+
}
57+
} catch (e) {
58+
console.error('[Danmaku Server] Error fetching comments:', e)
59+
}
60+
}
61+
7062
if (!comments || comments.length === 0) {
7163
return
7264
}
@@ -122,7 +114,7 @@ if (serverURL) {
122114
}
123115

124116
function createDanmaku(data) {
125-
if (!isPlaying) return
117+
// if (!isPlaying) return
126118
const trackIndex = selectBestTrack()
127119
const track = container.querySelectorAll('.danmaku-track')[trackIndex]
128120
if (!track) return
@@ -137,7 +129,6 @@ if (serverURL) {
137129

138130
const cleanComment = truncateHtmlInline(data.comment, 30)
139131
const likeHtml = data.like > 0 ? `<span class="danmaku-like">👍 ${data.like}</span>` : ''
140-
141132
danmaku.innerHTML = `
142133
<img class="danmaku-avatar" src="${data.avatar}" alt="" onerror="this.style.display='none'">
143134
<span class="danmaku-nick">${data.nick}</span>
@@ -198,7 +189,6 @@ if (serverURL) {
198189
e.clientY <= rect.bottom
199190
) {
200191
hit = danmaku
201-
// No break, so the last one (topmost) is selected in case of overlap
202192
}
203193
}
204194

@@ -227,9 +217,6 @@ if (serverURL) {
227217
break
228218
}
229219
})
230-
231-
if (loopTimeoutId) clearTimeout(loopTimeoutId)
232-
startDanmakuLoop()
233220
}
234221

235222
setTimeout(initializeDanmaku, 1000)

0 commit comments

Comments
 (0)