Files
forumlify/js/user.js
T
Furrium Network 0ea921186a feat(ui): 加入骨架屏,并让页面转场区分前进与后退
一、骨架屏

原先 12 处加载态都是同一句「加载中...」文字。改为与真实内容同构的
灰块占位,用户在等待时就能预判版面结构,内容出现时也不会发生明显的
布局跳动。

- 新增 js/skeleton.js,提供 postList / postDetail / replyItem /
  tableRows / userProfile 五种形态,按内容特征设定灰块宽度,
  避免每行等长显得机械
- 已接入帖子列表、帖子详情(含两条回复占位)、用户主页,以及管理
  后台的用户列表与日志列表
- 扫光动画(skeletonShimmer)提示「正在加载」而非「内容就是这样」
- 减少动画时停掉扫光,保留静态灰块

一个必须处理的时机问题:真正的列表渲染要等 i18n 初始化完成后才触发
(DOMContentLoaded 之后还有 200ms 延迟)。首版把骨架屏放在 renderFeed
里,实测前 2.8 秒仍是「加载中...」文字,等骨架出现时数据往往已经
回来了,等于白做。现改为在 skeleton.js 脚本执行期(早于
DOMContentLoaded)就写入容器,实测从第一帧起即可见。

翻页时不再显示骨架屏:列表已有内容,重画一遍会造成明显闪动,反而
比直接留白更差。通过容器的 data-loaded 标记区分首次加载与翻页。

二、页面转场区分方向

原先只有 slideIn 一个方向,前进与后退都从右侧滑入——返回上一页时
方向感是错的。现新增 slideInBack(自左侧滑入),在 popstate 上标记
后退导航,由 switchPage 与各 showXxxPage 读取该标记后决定用哪套动画。
标记只在紧接的一次切换生效,渲染完成后立即清除。

实测(Puppeteer,1200ms 延迟 + 150KB/s 限速):
- 骨架屏:从首次采样起即为 4 张骨架卡片、16 行占位、4 个头像圆形、
  2 个缩略图占位,扫光动画为 skeletonShimmer
- 数据返回后骨架卡片残留为 0,正常渲染真实帖子

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

97 lines
4.5 KiB
JavaScript

// ============================================================
// 👤 用户主页
// ============================================================
async function renderUserProfile(username) {
const container = document.getElementById('userProfileContent');
container.innerHTML = Skeleton.userProfile();
try {
// 使用公开接口 /users/profile/:username
const user = await apiFetch('/users/profile/' + encodeURIComponent(username));
if (user.error || !user.id) {
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:40px 0;">用户不存在</div>';
return;
}
document.getElementById('userPageTitle').textContent = '👤 ' + user.username;
const result = await apiFetch('/posts?user_id=' + user.id + '&page=1&limit=100');
const posts = result.data || [];
const avatar = user.avatar_url || 'https://ui-avatars.com/api/?name=' + encodeURIComponent(user.username) + '&background=6366f1&color=fff&size=128';
let html = `
<div style="background:var(--surface);border:1px solid var(--border);border-radius:8px;padding:32px;text-align:center;">
<img src="${escapeHTML(safeURL(avatar, { image: true }))}" style="width:96px;height:96px;border-radius:50%;object-fit:cover;border:3px solid var(--primary);" />
<h2 style="margin:16px 0 4px;font-size:24px;">${escapeHTML(user.username)}</h2>
<p style="color:var(--text-secondary);font-size:14px;">${escapeHTML(user.bio || '这个人很懒,什么都没写')}</p>
<div style="display:flex;justify-content:center;gap:32px;margin-top:16px;font-size:14px;color:var(--text-secondary);flex-wrap:wrap;">
<span>📅 加入于 ${user.created_at ? new Date(user.created_at).toLocaleDateString('zh-CN') : '未知'}</span>
<span>📝 发了 ${posts.length} 个帖子</span>
${user.role === 'admin' ? '<span style="color:var(--primary);font-weight:600;">🛡️ 管理员</span>' : ''}
</div>
${currentUser && currentUser.id !== user.id ? `
<button id="dmBtn" class="btn-primary" style="margin-top:16px;padding:8px 24px;">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display:inline;vertical-align:middle;margin-right:6px;"><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>
发私信
</button>
` : ''}
</div>
<h3 style="margin:24px 0 16px;font-size:18px;">📝 发布的帖子</h3>
`;
if (posts.length === 0) {
html += '<div style="color:#94a3b8;padding:20px 0;text-align:center;">还没有发帖</div>';
} else {
posts.forEach(p => {
const time = p.created_at ? new Date(p.created_at).toLocaleString('zh-CN') : '';
html += `
<div class="post-card" data-postid="${escapeHTML(p.id)}" style="cursor:pointer;">
<div class="post-title" style="font-size:16px;font-weight:600;">${escapeHTML(p.title || '无标题')}</div>
<div class="post-content" style="font-size:14px;color:var(--text-secondary);">${escapeHTML((p.content || '').substring(0, 100))}${(p.content || '').length > 100 ? '...' : ''}</div>
<div style="font-size:12px;color:var(--text-light);margin-top:8px;">${time}</div>
</div>
`;
});
}
container.innerHTML = sanitizeHTML(html);
container.querySelectorAll('.post-card[data-postid]').forEach(card => {
card.addEventListener('click', function() {
switchToPost(this.dataset.postid);
});
});
const dmBtn = document.getElementById('dmBtn');
if (dmBtn) {
dmBtn.addEventListener('click', function() {
openPrivateChat(user.id, user.username);
});
}
} catch (err) {
container.innerHTML = '<div style="text-align:center;color:#ef4444;padding:40px 0;">加载失败:' + escapeHTML(err.message) + '</div>';
}
}
function showUserPage(username) {
document.getElementById('app').style.display = 'none';
document.querySelectorAll('.page-slide').forEach(el => {
el.classList.remove('active', 'slide-out', 'slide-back');
});
const el = document.getElementById('pageUser');
// 后退导航时用反向滑入,与主动进入区分
if (typeof window.isNavigatingBack === 'function' && window.isNavigatingBack()) {
el.classList.add('slide-back');
}
el.classList.add('active');
el.style.animation = 'none';
void el.offsetHeight;
el.style.animation = '';
currentPage = 'user';
renderUserProfile(username);
}