Files
forumlify/js/post.js
T
Furrium Network 7176bea12c feat(ui): 重做页面转场为推入式,旧页面下沉、新页面从屏幕外滑入
原先的「滑入」只是 translateX(40px),几十像素的位移,在 1280px 宽的
屏上几乎看不出来,不是真正的页面转场。

现改为两段协同的推入效果:

1. 新页面(.page-slide)从屏幕外整体滑入:translateX(100%) → 0,
   同时 scale 0.94 → 1、opacity 0 → 1。用百分比而非固定像素,
   任何视口宽度下都从边缘之外开始。
2. 旧内容(#app)留在原位下沉:scale 1 → 0.94、右移 4%,
   并叠加一层暗色遮罩(#app::after,opacity 0 → 1)。

关键改动:#app 不再被 display:none 直接隐藏——那样根本没有动画可做。
现由 body.page-stack-open 与 .page-stack-sink 控制下沉,转场结束后
才清理状态。相关逻辑抽成 sinkBackground / riseBackground / showSlide /
hideSlide 四个函数,供 switchPage、showPostPage、showUserPage、
showCustomPage 共用。返回按钮走 navigateBackToFeed,与浏览器后退键
方向一致(新页面向左滑出、首页上浮)。

过程中修掉两个真实问题:

- 样式表靠后位置本有一条 `#app { animation: fadeIn ... }`(交互动效层)。
  ID 选择器优先级高于类选择器,导致最初的 .page-stack-sink 完全不生效,
  实测首页 transform 始终为 none。改为 #app.page-stack-sink 组合选择器
  后正常。reduced-motion 分支同样需要组合选择器才能压过它。
- app.js 中 showCustomPage 仍保留旧的 `#app.style.display = 'none'`,
  且给容器写了内联 display,会覆盖转场所需的 display 切换。已一并移除。

实测(Puppeteer 逐帧采样,视口 1280×900):
- 新页面 X 坐标:1280 → 860 → 541 → 421 → 326 → 60 → 6 → 0,
  起始 1280 恰好等于视口宽度,确认从屏幕外开始
- 首页:scale 1 → 0.940,右移 0 → 48px,遮罩不透明度 0 → 1.00
- 中间帧截图可见新页面自右侧滑入、旧页面在背景中缩小变暗

完整测试套件 25/25 通过。
2026-09-22 21:22:07 +08:00

267 lines
12 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// ============================================================
// 📄 帖子详情 + 回复
// ============================================================
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 = '<div class="post-images">';
post.images.forEach(img => {
imagesHtml += '<img src="' + escapeHTML(safeURL(img, { image: true })) + '" class="post-image" style="cursor:pointer;" />';
});
imagesHtml += '</div>';
}
const renderedContent = renderMarkdown(post.content || '');
// 签名渲染
// 详情页保留签名展示。补上类名以便统一样式定位——列表页的同类元素
// 正是靠 .post-signature 隐藏的,此前详情页只有行内样式,无法选中。
let signatureHtml = '';
if (post.signature) {
const sigContent = renderMarkdown(post.signature);
signatureHtml = `
<div class="post-signature" style="margin-top:16px;padding-top:12px;border-top:1px solid var(--border-light);font-size:12px;color:var(--text-secondary);">
${sigContent}
</div>
`;
}
let html = `
<div class="post-detail-card">
<div class="post-header">
<img src="${escapeHTML(safeURL(avatar, { image: true }))}" class="post-avatar" />
<span class="post-username" data-username="${escapeHTML(username)}" style="cursor:pointer;color:var(--primary);">${escapeHTML(username)}</span>
<span class="post-time">${time}</span>
${post.edited_at ? `<span style="font-size:12px;color:var(--text-light);margin-left:8px;">(已编辑 ${new Date(post.edited_at).toLocaleString('zh-CN')})</span>` : ''}
${post.is_pinned ? '<span style="font-size:12px;color:var(--primary);margin-left:8px;">📌 置顶</span>' : ''}
<div style="display:flex;gap:4px;margin-left:auto;">
${currentUser && currentUser.role === 'admin' ? `
<button class="btn-sm btn-secondary" id="pinPostBtn" data-postid="${post.id}" style="padding:4px 10px;">
${post.is_pinned ? '📌 取消置顶' : '📌 置顶'}
</button>
` : ''}
${currentUser && currentUser.id === post.user_id ? `<button class="btn-sm btn-secondary" id="editPostBtn" data-postid="${post.id}" style="padding:4px 10px;">✏️ 编辑</button>` : ''}
${currentUser && currentUser.id === post.user_id ? `<button class="btn-sm btn-danger" id="detailDeleteBtn" data-postid="${post.id}" style="padding:4px 10px;">
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display:inline;vertical-align:middle;"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
删除
</button>` : ''}
${currentUser ? `<button class="btn-sm btn-secondary" id="detailReportBtn" data-postid="${post.id}" style="padding:4px 10px;">
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display:inline;vertical-align:middle;"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"/><path d="M12 8v4"/><path d="M12 16h.01"/></svg>
举报
</button>` : ''}
</div>
</div>
<div class="post-content" style="font-size:16px;line-height:1.8;">${renderedContent}</div>
${imagesHtml}
${signatureHtml}
</div>
<div style="margin-top:20px;font-size:14px;color:#64748b;">
<svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display:inline;vertical-align:middle;"><path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/></svg>
${replies.length} 条回复
</div>
<div style="margin-top:12px;" id="repliesList">
`;
if (replies.length === 0) {
html += '<div style="color:#94a3b8;padding:20px 0;text-align:center;">还没有回复,快来抢沙发吧 🛋️</div>';
} 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 += `
<div class="reply-item" data-replyid="${r.id}">
<div class="reply-header">
<img src="${escapeHTML(safeURL(rAvatar, { image: true }))}" class="reply-avatar" />
<span class="reply-username" data-username="${escapeHTML(rUsername)}" style="cursor:pointer;color:var(--primary);">${escapeHTML(rUsername)}</span>
<span class="reply-time">${rTime}</span>
${currentUser && (currentUser.id === r.user_id || currentUser.role === 'admin') ? `<button class="btn-sm btn-danger reply-delete-btn" data-replyid="${r.id}">
<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display:inline;vertical-align:middle;"><polyline points="3 6 5 6 21 6"/><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"/></svg>
删除
</button>` : ''}
</div>
<div class="reply-content">${renderPlainText(r.content || '')}</div>
</div>
`;
});
}
html += '</div>';
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 = '<div style="text-align:center;color:#ef4444;padding:40px 0;">加载失败:' + escapeHTML(err.message) +
'</div>';
replyArea.style.display = 'none';
}
}
async function handleReplySubmit(postId) {
if (!currentUser) { showToast('请先登录', 'warning'); return; }
const content = document.getElementById('replyContent').value.trim();
const captchaInput = document.getElementById('replyCaptchaInput').value.trim();
const captchaAnswer = parseInt(document.getElementById('replyCaptchaInput').dataset.answer);
if (!content) { showToast('请填写回复内容', 'warning'); return; }
if (parseInt(captchaInput) !== captchaAnswer) { showToast('验证码错误,请重新计算', 'error'); refreshCaptcha('reply'); return; }
try {
await API.createReply(postId, content);
renderPostDetail(postId);
if (currentPage === 'feed') {
renderFeed();
}
renderStats();
} catch (err) {
showToast('回复失败:' + err.message, 'error');
}
}