From 68b58851e8a900972863474a310b2663d8aefa03 Mon Sep 17 00:00:00 2001 From: lezi-fun <177434121+lezi-fun@users.noreply.github.com> Date: Wed, 12 Aug 2026 11:05:32 +0800 Subject: [PATCH] fix: refine user post transitions --- app/HomeClient.js | 4 +- app/globals.css | 40 ++++++--- components/AppProvider.js | 172 ++++++++++++++++++++++++++++++++++++-- components/Feed.js | 11 ++- components/PostDetail.js | 38 ++++++++- components/UserProfile.js | 70 +++++++++++++--- 6 files changed, 300 insertions(+), 35 deletions(-) diff --git a/app/HomeClient.js b/app/HomeClient.js index 604efa1..687ee8b 100644 --- a/app/HomeClient.js +++ b/app/HomeClient.js @@ -18,7 +18,7 @@ import Modals from '@/components/Modals'; import LoadingScreen from '@/components/LoadingScreen'; function HomeInner() { - const { view, currentPostId, currentPostPreview, currentUsername, currentUserPreview, currentPageName, ready, forumName, forumNameLoaded } = useApp(); + const { view, currentPostId, currentPostPreview, currentUsername, currentUserPreview, currentUserPostsPreview, currentPageName, ready, forumName, forumNameLoaded } = useApp(); const [modal, setModal] = useState(null); // null | login | register | report const [reportPostId, setReportPostId] = useState(null); @@ -42,7 +42,7 @@ function HomeInner() { {/* 独立页面 */}
{view === 'post' && currentPostId && } - {view === 'user' && currentUsername && } + {view === 'user' && currentUsername && } {view === 'custom' && currentPageName && } {view === 'new' && } {view === 'admin' && } diff --git a/app/globals.css b/app/globals.css index 13230ce..4da1b20 100644 --- a/app/globals.css +++ b/app/globals.css @@ -1237,16 +1237,16 @@ html.post-view-transition::view-transition-group(post-sidebar) { } html.post-view-transition::view-transition-group(post-heading) { - animation-duration: 0.64s; - animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1); + animation-duration: 0.8s; + animation-timing-function: cubic-bezier(0.42, 0, 0.58, 1); } html.post-view-transition::view-transition-group(post-avatar), html.post-view-transition::view-transition-group(post-author), html.post-view-transition::view-transition-group(post-pin-state), html.post-view-transition::view-transition-group(post-edited-label) { - animation-duration: 0.68s; - animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1); + animation-duration: 0.8s; + animation-timing-function: cubic-bezier(0.42, 0, 0.58, 1); } html.post-view-transition::view-transition-group(post-body) { @@ -1260,8 +1260,8 @@ html.home-view-transition.returning-home::view-transition-image-pair(post-body) } html.post-view-transition::view-transition-group(post-time) { - animation-duration: 1.05s; - animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1); + animation-duration: 0.8s; + animation-timing-function: cubic-bezier(0.42, 0, 0.58, 1); } html.post-view-transition::view-transition-group(persistent-navbar), @@ -1300,11 +1300,11 @@ html.post-view-transition::view-transition-new(post-expand) { } html.post-view-transition::view-transition-old(post-heading) { - animation: sharedTitleMorphOut 0.64s ease-out both; + animation: sharedTitleMorphOut 0.8s cubic-bezier(0.42, 0, 0.58, 1) both; } html.post-view-transition::view-transition-new(post-heading) { - animation: sharedTitleMorphIn 0.64s ease-out both; + animation: sharedTitleMorphIn 0.8s cubic-bezier(0.42, 0, 0.58, 1) both; } html.post-view-transition::view-transition-old(post-time) { @@ -1319,14 +1319,14 @@ html.post-view-transition::view-transition-old(post-avatar), html.post-view-transition::view-transition-old(post-author), html.post-view-transition::view-transition-old(post-pin-state), html.post-view-transition::view-transition-old(post-edited-label) { - animation: sharedPartMorphOut 0.68s ease-out both; + animation: sharedPartMorphOut 0.8s cubic-bezier(0.42, 0, 0.58, 1) both; } html.post-view-transition::view-transition-new(post-avatar), html.post-view-transition::view-transition-new(post-author), html.post-view-transition::view-transition-new(post-pin-state), html.post-view-transition::view-transition-new(post-edited-label) { - animation: sharedPartMorphIn 0.68s ease-out both; + animation: sharedPartMorphIn 0.8s cubic-bezier(0.42, 0, 0.58, 1) both; } html.post-view-transition::view-transition-group(post-edited-detail), @@ -2610,6 +2610,26 @@ html.home-view-transition.returning-home::view-transition-new(post-body) { box-shadow: var(--glass-shadow-hover) !important; } +#userProfileContent .user-post-preview { + position: relative; + max-height: 8.5em; + overflow: hidden; + line-height: 1.7; +} + +#userProfileContent .user-post-preview.has-fade { + -webkit-mask-image: linear-gradient(180deg, #000 58%, transparent 100%); + mask-image: linear-gradient(180deg, #000 58%, transparent 100%); +} + +#userProfileContent .user-post-preview > :first-child { + margin-top: 0; +} + +#userProfileContent .user-post-preview > :last-child { + margin-bottom: 0; +} + /* 亮色模式下稍微实一点,避免太透 */ [data-theme="light"] #userProfileContent > div:first-child { background: rgba(255, 255, 255, 0.5) !important; diff --git a/components/AppProvider.js b/components/AppProvider.js index fef8efe..5270416 100644 --- a/components/AppProvider.js +++ b/components/AppProvider.js @@ -116,6 +116,77 @@ function stageSharedGeometry(transition, { }).catch(() => {}); } +function stagePostBodyGeometry(transition, { + duration, + moveOffset, + moveEasing = 'cubic-bezier(0.42, 0, 0.58, 1)', + resizeEasing = 'cubic-bezier(0.42, 0, 0.58, 1)', +}) { + transition.ready.then(() => { + const animations = document.getAnimations(); + const cardAnimation = animations.find( + (animation) => animation.effect?.pseudoElement === '::view-transition-group(post-expand)' + ); + const bodyAnimation = animations.find( + (animation) => animation.effect?.pseudoElement === '::view-transition-group(post-body)' + ); + const cardFrames = cardAnimation?.effect?.getKeyframes(); + const bodyFrames = bodyAnimation?.effect?.getKeyframes(); + if (!bodyAnimation || !cardFrames || cardFrames.length < 2 || !bodyFrames || bodyFrames.length < 2) return; + + const cardStart = cardFrames[0]; + const cardEnd = cardFrames[cardFrames.length - 1]; + const bodyStart = bodyFrames[0]; + const bodyEnd = bodyFrames[bodyFrames.length - 1]; + const cardStartMatrix = new DOMMatrixReadOnly(cardStart.transform); + const cardEndMatrix = new DOMMatrixReadOnly(cardEnd.transform); + const bodyStartMatrix = new DOMMatrixReadOnly(bodyStart.transform); + const bodyEndMatrix = new DOMMatrixReadOnly(bodyEnd.transform); + const startCardWidth = Number.parseFloat(cardStart.width); + const endCardWidth = Number.parseFloat(cardEnd.width); + const startBodyWidth = Number.parseFloat(bodyStart.width); + if (![startCardWidth, endCardWidth, startBodyWidth].every(Number.isFinite)) return; + + // 第一阶段让正文锁定在卡片内的原始位置;卡片停稳后,正文才调整到详情布局。 + const leftInset = bodyStartMatrix.e - cardStartMatrix.e; + const topInset = bodyStartMatrix.f - cardStartMatrix.f; + const rightInset = startCardWidth - leftInset - startBodyWidth; + const midpointWidth = Math.max(0, endCardWidth - leftInset - rightInset); + const midpointTransform = new DOMMatrix([ + bodyEndMatrix.a, + bodyEndMatrix.b, + bodyEndMatrix.c, + bodyEndMatrix.d, + cardEndMatrix.e + leftInset, + cardEndMatrix.f + topInset, + ]).toString(); + + bodyAnimation.effect.setKeyframes([ + { + offset: 0, + width: bodyStart.width, + height: bodyStart.height, + transform: bodyStart.transform, + easing: moveEasing, + }, + { + offset: moveOffset, + width: `${midpointWidth}px`, + height: bodyStart.height, + transform: midpointTransform, + easing: resizeEasing, + }, + { + offset: 1, + width: bodyEnd.width, + height: bodyEnd.height, + transform: bodyEnd.transform, + }, + ]); + bodyAnimation.effect.updateTiming({ duration, easing: 'linear', fill: 'both' }); + }).catch(() => {}); +} + export function useApp() { return useContext(AppContext); } @@ -128,8 +199,10 @@ export default function AppProvider({ children, cachedName = '' }) { const [view, setView] = useState('feed'); // feed | post | new | admin | settings | messages | user | custom const [currentPostId, setCurrentPostId] = useState(null); const [currentPostPreview, setCurrentPostPreview] = useState(null); + const [currentPostOrigin, setCurrentPostOrigin] = useState(null); const [currentUsername, setCurrentUsername] = useState(null); const [currentUserPreview, setCurrentUserPreview] = useState(null); + const [currentUserPostsPreview, setCurrentUserPostsPreview] = useState(null); const [currentPageName, setCurrentPageName] = useState(null); const [sort, setSort] = useState('latest'); const [refreshKey, setRefreshKey] = useState(0); @@ -285,8 +358,10 @@ export default function AppProvider({ children, cachedName = '' }) { setView(page); setCurrentPostId(null); setCurrentPostPreview(null); + setCurrentPostOrigin(null); setCurrentUsername(null); setCurrentUserPreview(null); + setCurrentUserPostsPreview(null); if (refreshFeed) refresh(); }; @@ -380,17 +455,23 @@ export default function AppProvider({ children, cachedName = '' }) { }); }, [currentPostId, currentUsername, currentUserPreview, refresh, view]); - const openPost = useCallback((postId, sourceElement = null, preview = null) => { + const openPost = useCallback((postId, sourceElement = null, preview = null, origin = null) => { const url = new URL(window.location); url.searchParams.set('post', postId); url.searchParams.delete('page'); url.searchParams.delete('user'); + const resolvedOrigin = origin || { view }; + const historyOrigin = resolvedOrigin?.view === 'user' + ? { view: 'user', username: resolvedOrigin.username } + : { view: resolvedOrigin?.view || 'feed' }; const commitNavigation = () => { - window.history.pushState({ page: 'post', postId }, '', url); + window.history.pushState({ page: 'post', postId, origin: historyOrigin }, '', url); setView('post'); setCurrentPostId(postId); setCurrentPostPreview(preview); + setCurrentPostOrigin(resolvedOrigin); setCurrentUserPreview(null); + setCurrentUserPostsPreview(null); }; const reduceMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches; @@ -414,7 +495,12 @@ export default function AppProvider({ children, cachedName = '' }) { moveEasing: 'cubic-bezier(0.42, 0, 0.58, 1)', resizeEasing: 'cubic-bezier(0.42, 0, 0.58, 1)', }); - stageSharedGeometry(transition, { name: 'post-body', duration: 960, moveOffset: 0.62 }); + stagePostBodyGeometry(transition, { + duration: 960, + moveOffset: 0.62, + moveEasing: 'cubic-bezier(0.42, 0, 0.58, 1)', + resizeEasing: 'cubic-bezier(0.42, 0, 0.58, 1)', + }); const clearSourceNames = () => { sourceElement.style.viewTransitionName = ''; clearPostTransitionParts(sourceParts); @@ -426,7 +512,75 @@ export default function AppProvider({ children, cachedName = '' }) { root.classList.add('post-transition-settled'); window.setTimeout(() => root.classList.remove('post-transition-settled'), 220); }); - }, []); + }, [view]); + + const returnFromPost = useCallback(() => { + if (currentPostOrigin?.view !== 'user' || !currentPostOrigin.username) { + navigate('feed'); + return; + } + + const url = new URL(window.location); + url.searchParams.set('user', currentPostOrigin.username); + url.searchParams.delete('page'); + url.searchParams.delete('post'); + const commitNavigation = () => { + window.history.pushState({ page: 'user', username: currentPostOrigin.username }, '', url); + setView('user'); + setCurrentUsername(currentPostOrigin.username); + setCurrentUserPreview(currentPostOrigin.user || { username: currentPostOrigin.username }); + setCurrentUserPostsPreview(currentPostOrigin.posts || []); + setCurrentPostId(null); + setCurrentPostPreview(null); + }; + + const reduceMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches; + const sourceCard = document.querySelector('#pagePost .post-detail-card'); + if (!sourceCard || !document.startViewTransition || reduceMotion) { + commitNavigation(); + return; + } + + const root = document.documentElement; + let targetCard = null; + let targetParts = []; + root.classList.add('home-view-transition', 'returning-home', 'returning-user-post'); + const transition = document.startViewTransition(() => { + flushSync(commitNavigation); + const userPage = document.getElementById('pageUser'); + if (userPage) userPage.scrollTop = currentPostOrigin.scrollTop || 0; + targetCard = Array.from(document.querySelectorAll('#userProfileContent [data-post-id]')).find( + (element) => element.dataset.postId === String(currentPostId) + ) || null; + if (targetCard) targetCard.style.viewTransitionName = 'post-expand'; + targetParts = namePostTransitionParts(targetCard); + }); + stageSharedGeometry(transition, { + name: 'post-expand', + duration: 1100, + moveOffset: 0.73, + moveEasing: 'cubic-bezier(0.42, 0, 0.58, 1)', + resizeEasing: 'cubic-bezier(0.16, 1, 0.3, 1)', + reverse: true, + }); + stageSharedGeometry(transition, { + name: 'post-body', + duration: 1100, + moveOffset: 0.73, + moveEasing: 'cubic-bezier(0.42, 0, 0.58, 1)', + resizeEasing: 'cubic-bezier(0.16, 1, 0.3, 1)', + reverse: true, + }); + const clearTargetNames = () => { + if (targetCard) targetCard.style.viewTransitionName = ''; + clearPostTransitionParts(targetParts); + }; + transition.ready.then(clearTargetNames, clearTargetNames); + transition.finished.finally(() => { + root.classList.remove('home-view-transition', 'returning-home', 'returning-user-post'); + setCurrentPostOrigin(null); + }); + }, [currentPostId, currentPostOrigin, navigate]); const openUser = useCallback((username, source = null, preview = null) => { const url = new URL(window.location); @@ -440,6 +594,8 @@ export default function AppProvider({ children, cachedName = '' }) { setCurrentUserPreview(preview || { username }); setCurrentPostId(null); setCurrentPostPreview(null); + setCurrentPostOrigin(null); + setCurrentUserPostsPreview(null); }; const avatarElement = source?.avatarElement; @@ -490,10 +646,12 @@ export default function AppProvider({ children, cachedName = '' }) { setView('post'); setCurrentPostId(state.postId); setCurrentPostPreview(null); + setCurrentPostOrigin(state.origin || null); } else if (state.username) { setView('user'); setCurrentUsername(state.username); setCurrentUserPreview(null); + setCurrentUserPostsPreview(null); } else if (state.pageName) { setView('custom'); setCurrentPageName(state.pageName); @@ -501,8 +659,10 @@ export default function AppProvider({ children, cachedName = '' }) { setView(state.page || 'feed'); setCurrentPostId(null); setCurrentPostPreview(null); + setCurrentPostOrigin(null); setCurrentUsername(null); setCurrentUserPreview(null); + setCurrentUserPostsPreview(null); setCurrentPageName(null); } }; @@ -544,8 +704,8 @@ export default function AppProvider({ children, cachedName = '' }) { forumName, forumNameLoaded, updateForumName, theme, toggleTheme, view, navigate, - currentPostId, currentPostPreview, openPost, - currentUsername, currentUserPreview, openUser, + currentPostId, currentPostPreview, currentPostOrigin, openPost, returnFromPost, + currentUsername, currentUserPreview, currentUserPostsPreview, openUser, currentPageName, openCustomPage, sort, setSort, refreshKey, refresh, diff --git a/components/Feed.js b/components/Feed.js index f532329..c5608fa 100644 --- a/components/Feed.js +++ b/components/Feed.js @@ -26,10 +26,11 @@ export default function Feed({ onOpenModal, onReport }) { const [totalPages, setTotalPages] = useState(1); const [viewerSrc, setViewerSrc] = useState(null); const [selectedSort, setSelectedSort] = useState(sort); - const [drawerPhase, setDrawerPhase] = useState('idle'); + const [drawerPhase, setDrawerPhase] = useState('closed'); const postListRef = useRef(null); const sortTimerRef = useRef(null); const pendingSortRef = useRef(null); + const initialDrawerOpenedRef = useRef(false); const PAGE_SIZE = 20; const load = useCallback(async (targetPage) => { @@ -51,6 +52,14 @@ export default function Feed({ onOpenModal, onReport }) { setDrawerPhase('idle'); }, 360); }); + } else if (!initialDrawerOpenedRef.current) { + // 首次加载:先在收起状态完成列表渲染,再像排序切换一样抽屉式展开。 + // Feed 在 SPA 视图间保持挂载,因此从帖子/用户页返回时不会重复播放。 + initialDrawerOpenedRef.current = true; + requestAnimationFrame(() => { + setDrawerPhase('opening'); + sortTimerRef.current = setTimeout(() => setDrawerPhase('idle'), 360); + }); } } }, [sort, refreshKey]); diff --git a/components/PostDetail.js b/components/PostDetail.js index 0078d02..6239793 100644 --- a/components/PostDetail.js +++ b/components/PostDetail.js @@ -18,7 +18,7 @@ function avatar(username) { } export default function PostDetail({ postId, initialPost = null }) { - const { currentUser, navigate, openUser, refreshKey } = useApp(); + const { currentUser, navigate, openUser, returnFromPost, refreshKey } = useApp(); const { t, i18n } = useTranslation(); const { toast, confirmAction } = useToast(); const [post, setPost] = useState(initialPost); @@ -134,10 +134,26 @@ export default function PostDetail({ postId, initialPost = null }) { setEditImages((prev) => prev.filter((u) => u !== url)); }; + const handleOpenUser = (event) => { + const header = event.currentTarget.closest('.post-header'); + openUser( + post.username, + { + avatarElement: header?.querySelector('.post-avatar'), + nameElement: header?.querySelector('.post-username'), + }, + { + username: post.username, + avatar_url: post.avatar_url || avatar(post.username), + sourcePostId: post.id, + } + ); + }; + return (
-

{post.title || '帖子详情'}

@@ -147,11 +163,25 @@ export default function PostDetail({ postId, initialPost = null }) {
- + {post.username { + if (event.key === 'Enter' || event.key === ' ') { + event.preventDefault(); + handleOpenUser(event); + } + }} + /> openUser(post.username)} + onClick={handleOpenUser} > {post.username || '匿名用户'} diff --git a/components/UserProfile.js b/components/UserProfile.js index 5878f0f..0d10111 100644 --- a/components/UserProfile.js +++ b/components/UserProfile.js @@ -1,23 +1,27 @@ 'use client'; // 用户主页:资料卡 + 该用户的帖子列表 -import { useEffect, useState, useCallback } from 'react'; +import { useEffect, useState, useCallback, useRef } from 'react'; import { API } from '@/lib/api'; import { useApp } from './AppProvider'; import { Icon } from './Icons'; import { useOpenPrivateChat } from './chat/ChatManager'; +import { renderMarkdown } from '@/lib/markdown'; +import { useTranslation } from 'react-i18next'; function avatar(username, size = 128) { return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || 'U') + '&background=6366f1&color=fff&size=' + size; } -export default function UserProfile({ username, initialUser = null }) { +export default function UserProfile({ username, initialUser = null, initialPosts = null }) { const { currentUser, openPost } = useApp(); + const { t, i18n } = useTranslation(); const openPrivateChat = useOpenPrivateChat(); const [user, setUser] = useState(initialUser); - const [posts, setPosts] = useState([]); + const [posts, setPosts] = useState(initialPosts || []); const [error, setError] = useState(null); + const postsRef = useRef(null); const load = useCallback(async () => { setError(null); @@ -37,9 +41,16 @@ export default function UserProfile({ username, initialUser = null }) { useEffect(() => { setUser(initialUser); - setPosts([]); + setPosts(initialPosts || []); load(); - }, [initialUser, load]); + }, [initialPosts, initialUser, load]); + + useEffect(() => { + const contents = postsRef.current?.querySelectorAll('.user-post-preview') || []; + contents.forEach((element) => { + element.classList.toggle('has-fade', element.scrollHeight > element.clientHeight + 4); + }); + }, [posts]); if (error) { return ( @@ -82,20 +93,55 @@ export default function UserProfile({ username, initialUser = null }) { )}
{user.id && ( -
+

发布的帖子

{posts.length === 0 ? (
还没有发帖
) : ( posts.map((p) => { - const time = p.created_at ? new Date(p.created_at).toLocaleString('zh-CN') : ''; + const time = p.created_at ? new Date(p.created_at).toLocaleString(i18n.language === 'en' ? 'en-US' : 'zh-CN') : ''; return ( -
openPost(p.id, e.currentTarget, p)}> -
{p.title || '无标题'}
-
- {(p.content || '').substring(0, 100)}{(p.content || '').length > 100 ? '...' : ''} +
openPost(p.id, event.currentTarget, p, { + view: 'user', + username: user.username, + user, + posts, + scrollTop: document.getElementById('pageUser')?.scrollTop || 0, + })} + > + {p.is_pinned && ( +
+ {t('feed.pinned')} +
+ )} +
+ + {user.username} + {time} + {p.edited_at && ( + + {t('feed.deleted')} + + )} +
+
{p.title || t('feed.noTitle')}
+
+ {p.images && p.images.length > 0 && ( +
+ {p.images.map((image, index) => ( + + ))} +
+ )} +
+ {p.reply_count || 0}
-
{time}
); })