// ============================================================
// 📄 帖子详情 + 回复
// ============================================================
let currentPostId = null;
let reportTargetPostId = null;
function switchToPost(postId) {
const url = new URL(window.location);
url.searchParams.set('post', postId);
url.searchParams.delete('page');
window.history.pushState({ page: 'post', postId: postId }, '', url);
showPostPage(postId);
}
function showPostPage(postId) {
const goingBack = typeof window.isNavigatingBack === 'function' && window.isNavigatingBack();
const el = document.getElementById('pagePost');
// 先让其他覆盖页滑出(从帖子直接跳到用户主页之类的情况)
document.querySelectorAll('.page-slide.active').forEach(other => {
if (other !== el) hideSlide(other, { back: goingBack });
});
// 首页留在背景中下沉,新页面从屏幕外滑入
showSlide(el, { back: goingBack });
currentPage = 'post';
renderPostDetail(postId);
}
async function renderPostDetail(postId) {
const container = document.getElementById('postDetailContent');
const replyArea = document.getElementById('replyArea');
container.innerHTML = Skeleton.postDetail() + Skeleton.replyItem() + Skeleton.replyItem();
replyArea.style.display = 'none';
try {
const post = await API.getPost(postId);
const replies = await API.getReplies(postId);
document.getElementById('postDetailTitle').textContent = post.title || '帖子详情';
const username = post.username || '匿名用户';
const avatar = post.avatar_url ||
'https://ui-avatars.com/api/?name=' + encodeURIComponent(username) +
'&background=6366f1&color=fff&size=64';
const time = post.created_at ? new Date(post.created_at).toLocaleString('zh-CN') : '';
let imagesHtml = '';
if (post.images && post.images.length > 0) {
imagesHtml = '
';
post.images.forEach(img => {
imagesHtml += '
) + ')
';
});
imagesHtml += '
';
}
const renderedContent = renderMarkdown(post.content || '');
// 签名渲染
// 详情页保留签名展示。补上类名以便统一样式定位——列表页的同类元素
// 正是靠 .post-signature 隐藏的,此前详情页只有行内样式,无法选中。
let signatureHtml = '';
if (post.signature) {
const sigContent = renderMarkdown(post.signature);
signatureHtml = `
${renderedContent}
${imagesHtml}
${signatureHtml}
`;
if (replies.length === 0) {
html += '
还没有回复,快来抢沙发吧 🛋️
';
} else {
replies.forEach(r => {
const rUsername = r.username || '匿名用户';
const rAvatar = r.avatar_url ||
'https://ui-avatars.com/api/?name=' + encodeURIComponent(rUsername) +
'&background=6366f1&color=fff&size=64';
const rTime = r.created_at ? new Date(r.created_at).toLocaleString('zh-CN') : '';
html += `
${renderPlainText(r.content || '')}
`;
});
}
html += '
';
container.innerHTML = sanitizeHTML(html);
replyArea.style.display = 'block';
document.getElementById('replyContent').value = '';
document.getElementById('replyCaptchaInput').value = '';
refreshCaptcha('reply');
container.querySelectorAll('.post-username, .reply-username').forEach(usernameEl => {
usernameEl.addEventListener('click', function() {
switchPage('user', this.dataset.username);
});
});
container.querySelectorAll('.post-image').forEach(image => {
image.addEventListener('click', function() {
openImageViewer(this.src);
});
});
// 置顶按钮
const pinBtn = document.getElementById('pinPostBtn');
if (pinBtn) {
pinBtn.addEventListener('click', async function() {
const postId = this.dataset.postid;
const unpin = this.textContent.includes('取消');
const ok = await showConfirm('确定要' + (unpin ? '取消' : '') + '置顶吗?', {
title: unpin ? '取消置顶' : '置顶帖子',
confirmText: unpin ? '取消置顶' : '置顶',
});
if (!ok) return;
API.togglePinPost(postId).then(() => {
renderPostDetail(postId);
renderFeed();
}).catch(err => showToast('操作失败:' + err.message, 'error'));
});
}
// 编辑按钮
const editBtn = document.getElementById('editPostBtn');
if (editBtn) {
editBtn.addEventListener('click', function() {
const postId = this.dataset.postid;
const currentTitle = post.title || '';
const currentContent = post.content || '';
openEditModal(postId, currentTitle, currentContent);
});
}
const deleteBtn = document.getElementById('detailDeleteBtn');
if (deleteBtn) {
deleteBtn.addEventListener('click', async function() {
const ok = await showConfirm('确定要删除这条帖子吗?', {
title: '删除帖子', confirmText: '删除', danger: true,
});
if (!ok) return;
API.deletePost(postId).then(() => {
showToast('删除成功', 'success');
switchPage('feed');
renderFeed();
renderStats();
}).catch(err => showToast('删除失败:' + err.message, 'error'));
});
}
// 举报按钮:原先只在列表卡片上,现移到详情页。
// 提交逻辑在 app.js 中读取 reportTargetPostId,这里只负责记录目标并打开弹窗。
const reportBtn = document.getElementById('detailReportBtn');
if (reportBtn) {
reportBtn.addEventListener('click', function() {
reportTargetPostId = this.dataset.postid;
document.getElementById('reportModal').classList.add('active');
});
}
document.querySelectorAll('.reply-delete-btn').forEach(btn => {
btn.addEventListener('click', async function(e) {
e.stopPropagation();
const replyId = this.dataset.replyid;
const ok = await showConfirm('确定要删除这条回复吗?', {
title: '删除回复', confirmText: '删除', danger: true,
});
if (!ok) return;
API.deleteReply(replyId).then(() => {
renderPostDetail(postId);
}).catch(err => showToast('删除失败:' + err.message, 'error'));
});
});
const replySubmit = document.getElementById('replySubmit');
const newReplySubmit = replySubmit.cloneNode(true);
replySubmit.parentNode.replaceChild(newReplySubmit, replySubmit);
newReplySubmit.addEventListener('click', async function() {
await handleReplySubmit(postId);
});
const replyInput = document.getElementById('replyContent');
replyInput.addEventListener('keydown', function(e) {
if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) {
e.preventDefault();
handleReplySubmit(postId);
}
});
currentPostId = postId;
} catch (err) {
container.innerHTML = '