fix: refine user post transitions
This commit is contained in:
1 parent
352f1d57e6
commit
68b58851e8
6 files changed
+300
-35
No files matched your search
+166
-6
@@ -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,
|
||||
|
||||
+10
-1
@@ -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]);
|
||||
|
||||
@@ -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 (
|
||||
<div id="pagePost" className="page-slide active">
|
||||
<div className="page-header post-page-header">
|
||||
<button className="back-btn" onClick={() => navigate('feed')}>
|
||||
<button className="back-btn" onClick={returnFromPost}>
|
||||
<Icon name="back" size={16} /> 返回
|
||||
</button>
|
||||
<h2>{post.title || '帖子详情'}</h2>
|
||||
@@ -147,11 +163,25 @@ export default function PostDetail({ postId, initialPost = null }) {
|
||||
<div id="postDetailContent" className="post-page-main">
|
||||
<div className="post-detail-card">
|
||||
<div className="post-header">
|
||||
<img src={post.avatar_url || avatar(post.username)} className="post-avatar" alt="" />
|
||||
<img
|
||||
src={post.avatar_url || avatar(post.username)}
|
||||
className="post-avatar"
|
||||
alt={post.username || '匿名用户'}
|
||||
role="button"
|
||||
tabIndex={0}
|
||||
style={{ cursor: 'pointer' }}
|
||||
onClick={handleOpenUser}
|
||||
onKeyDown={(event) => {
|
||||
if (event.key === 'Enter' || event.key === ' ') {
|
||||
event.preventDefault();
|
||||
handleOpenUser(event);
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<span
|
||||
className="post-username"
|
||||
style={{ cursor: 'pointer', color: 'var(--primary)' }}
|
||||
onClick={() => openUser(post.username)}
|
||||
onClick={handleOpenUser}
|
||||
>
|
||||
{post.username || '匿名用户'}
|
||||
</span>
|
||||
|
||||
+58
-12
@@ -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 }) {
|
||||
)}
|
||||
</div>
|
||||
{user.id && (
|
||||
<div className="user-profile-posts">
|
||||
<div className="user-profile-posts" ref={postsRef}>
|
||||
<h3 style={{ margin: '24px 0 16px', fontSize: 18 }}><Icon name="file" size={18} /> 发布的帖子</h3>
|
||||
{posts.length === 0 ? (
|
||||
<div style={{ color: '#94a3b8', padding: '20px 0', textAlign: 'center' }}>还没有发帖</div>
|
||||
) : (
|
||||
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 (
|
||||
<div key={p.id} className="post-card" style={{ cursor: 'pointer' }} onClick={(e) => openPost(p.id, e.currentTarget, p)}>
|
||||
<div className="post-title" style={{ fontSize: 16, fontWeight: 600 }}>{p.title || '无标题'}</div>
|
||||
<div className="post-content" style={{ fontSize: 14, color: 'var(--text-secondary)' }}>
|
||||
{(p.content || '').substring(0, 100)}{(p.content || '').length > 100 ? '...' : ''}
|
||||
<div
|
||||
key={p.id}
|
||||
className="post-card user-post-card"
|
||||
data-post-id={p.id}
|
||||
data-username={user.username || ''}
|
||||
style={{ cursor: 'pointer' }}
|
||||
onClick={(event) => openPost(p.id, event.currentTarget, p, {
|
||||
view: 'user',
|
||||
username: user.username,
|
||||
user,
|
||||
posts,
|
||||
scrollTop: document.getElementById('pageUser')?.scrollTop || 0,
|
||||
})}
|
||||
>
|
||||
{p.is_pinned && (
|
||||
<div className="post-pin-state" style={{ width: 'fit-content', display: 'flex', alignItems: 'center', gap: 3, fontSize: 12, color: 'var(--primary)', fontWeight: 600, marginBottom: 4 }}>
|
||||
<Icon name="pin" size={12} /> {t('feed.pinned')}
|
||||
</div>
|
||||
)}
|
||||
<div className="post-header">
|
||||
<img src={p.avatar_url || user.avatar_url || avatar(user.username, 64)} className="post-avatar" alt="" />
|
||||
<span className="post-username" style={{ color: 'var(--primary)' }}>{user.username}</span>
|
||||
<span className="post-time">{time}</span>
|
||||
{p.edited_at && (
|
||||
<span className="post-edited-state" style={{ fontSize: 11, color: 'var(--text-light)', marginLeft: 6 }}>
|
||||
<span className="post-edited-label">{t('feed.deleted')}</span>
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="post-title">{p.title || t('feed.noTitle')}</div>
|
||||
<div className="post-content user-post-preview" dangerouslySetInnerHTML={{ __html: renderMarkdown(p.content) }} />
|
||||
{p.images && p.images.length > 0 && (
|
||||
<div className="post-images">
|
||||
{p.images.map((image, index) => (
|
||||
<img key={image + index} src={image} className="post-image" alt="" />
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<div className="post-actions">
|
||||
<span><Icon name="message" size={14} /> {p.reply_count || 0}</span>
|
||||
</div>
|
||||
<div style={{ fontSize: 12, color: 'var(--text-light)', marginTop: 8 }}>{time}</div>
|
||||
</div>
|
||||
);
|
||||
})
|
||||
|
||||
Reference in new issue
Block a user