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
+2
-2
@@ -18,7 +18,7 @@ import Modals from '@/components/Modals';
|
|||||||
import LoadingScreen from '@/components/LoadingScreen';
|
import LoadingScreen from '@/components/LoadingScreen';
|
||||||
|
|
||||||
function HomeInner() {
|
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 [modal, setModal] = useState(null); // null | login | register | report
|
||||||
const [reportPostId, setReportPostId] = useState(null);
|
const [reportPostId, setReportPostId] = useState(null);
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@ function HomeInner() {
|
|||||||
{/* 独立页面 */}
|
{/* 独立页面 */}
|
||||||
<div style={{ display: view !== 'feed' ? 'block' : 'none' }}>
|
<div style={{ display: view !== 'feed' ? 'block' : 'none' }}>
|
||||||
{view === 'post' && currentPostId && <PostDetail postId={currentPostId} initialPost={currentPostPreview} />}
|
{view === 'post' && currentPostId && <PostDetail postId={currentPostId} initialPost={currentPostPreview} />}
|
||||||
{view === 'user' && currentUsername && <UserProfile username={currentUsername} initialUser={currentUserPreview} />}
|
{view === 'user' && currentUsername && <UserProfile username={currentUsername} initialUser={currentUserPreview} initialPosts={currentUserPostsPreview} />}
|
||||||
{view === 'custom' && currentPageName && <CustomPage pageName={currentPageName} />}
|
{view === 'custom' && currentPageName && <CustomPage pageName={currentPageName} />}
|
||||||
{view === 'new' && <NewPost />}
|
{view === 'new' && <NewPost />}
|
||||||
{view === 'admin' && <AdminPage />}
|
{view === 'admin' && <AdminPage />}
|
||||||
|
|||||||
+30
-10
@@ -1237,16 +1237,16 @@ html.post-view-transition::view-transition-group(post-sidebar) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
html.post-view-transition::view-transition-group(post-heading) {
|
html.post-view-transition::view-transition-group(post-heading) {
|
||||||
animation-duration: 0.64s;
|
animation-duration: 0.8s;
|
||||||
animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
|
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-avatar),
|
||||||
html.post-view-transition::view-transition-group(post-author),
|
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-pin-state),
|
||||||
html.post-view-transition::view-transition-group(post-edited-label) {
|
html.post-view-transition::view-transition-group(post-edited-label) {
|
||||||
animation-duration: 0.68s;
|
animation-duration: 0.8s;
|
||||||
animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
|
animation-timing-function: cubic-bezier(0.42, 0, 0.58, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
html.post-view-transition::view-transition-group(post-body) {
|
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) {
|
html.post-view-transition::view-transition-group(post-time) {
|
||||||
animation-duration: 1.05s;
|
animation-duration: 0.8s;
|
||||||
animation-timing-function: cubic-bezier(0.16, 1, 0.3, 1);
|
animation-timing-function: cubic-bezier(0.42, 0, 0.58, 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
html.post-view-transition::view-transition-group(persistent-navbar),
|
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) {
|
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) {
|
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) {
|
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-author),
|
||||||
html.post-view-transition::view-transition-old(post-pin-state),
|
html.post-view-transition::view-transition-old(post-pin-state),
|
||||||
html.post-view-transition::view-transition-old(post-edited-label) {
|
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-avatar),
|
||||||
html.post-view-transition::view-transition-new(post-author),
|
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-pin-state),
|
||||||
html.post-view-transition::view-transition-new(post-edited-label) {
|
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),
|
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;
|
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 {
|
[data-theme="light"] #userProfileContent > div:first-child {
|
||||||
background: rgba(255, 255, 255, 0.5) !important;
|
background: rgba(255, 255, 255, 0.5) !important;
|
||||||
|
|||||||
+166
-6
@@ -116,6 +116,77 @@ function stageSharedGeometry(transition, {
|
|||||||
}).catch(() => {});
|
}).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() {
|
export function useApp() {
|
||||||
return useContext(AppContext);
|
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 [view, setView] = useState('feed'); // feed | post | new | admin | settings | messages | user | custom
|
||||||
const [currentPostId, setCurrentPostId] = useState(null);
|
const [currentPostId, setCurrentPostId] = useState(null);
|
||||||
const [currentPostPreview, setCurrentPostPreview] = useState(null);
|
const [currentPostPreview, setCurrentPostPreview] = useState(null);
|
||||||
|
const [currentPostOrigin, setCurrentPostOrigin] = useState(null);
|
||||||
const [currentUsername, setCurrentUsername] = useState(null);
|
const [currentUsername, setCurrentUsername] = useState(null);
|
||||||
const [currentUserPreview, setCurrentUserPreview] = useState(null);
|
const [currentUserPreview, setCurrentUserPreview] = useState(null);
|
||||||
|
const [currentUserPostsPreview, setCurrentUserPostsPreview] = useState(null);
|
||||||
const [currentPageName, setCurrentPageName] = useState(null);
|
const [currentPageName, setCurrentPageName] = useState(null);
|
||||||
const [sort, setSort] = useState('latest');
|
const [sort, setSort] = useState('latest');
|
||||||
const [refreshKey, setRefreshKey] = useState(0);
|
const [refreshKey, setRefreshKey] = useState(0);
|
||||||
@@ -285,8 +358,10 @@ export default function AppProvider({ children, cachedName = '' }) {
|
|||||||
setView(page);
|
setView(page);
|
||||||
setCurrentPostId(null);
|
setCurrentPostId(null);
|
||||||
setCurrentPostPreview(null);
|
setCurrentPostPreview(null);
|
||||||
|
setCurrentPostOrigin(null);
|
||||||
setCurrentUsername(null);
|
setCurrentUsername(null);
|
||||||
setCurrentUserPreview(null);
|
setCurrentUserPreview(null);
|
||||||
|
setCurrentUserPostsPreview(null);
|
||||||
if (refreshFeed) refresh();
|
if (refreshFeed) refresh();
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -380,17 +455,23 @@ export default function AppProvider({ children, cachedName = '' }) {
|
|||||||
});
|
});
|
||||||
}, [currentPostId, currentUsername, currentUserPreview, refresh, view]);
|
}, [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);
|
const url = new URL(window.location);
|
||||||
url.searchParams.set('post', postId);
|
url.searchParams.set('post', postId);
|
||||||
url.searchParams.delete('page');
|
url.searchParams.delete('page');
|
||||||
url.searchParams.delete('user');
|
url.searchParams.delete('user');
|
||||||
|
const resolvedOrigin = origin || { view };
|
||||||
|
const historyOrigin = resolvedOrigin?.view === 'user'
|
||||||
|
? { view: 'user', username: resolvedOrigin.username }
|
||||||
|
: { view: resolvedOrigin?.view || 'feed' };
|
||||||
const commitNavigation = () => {
|
const commitNavigation = () => {
|
||||||
window.history.pushState({ page: 'post', postId }, '', url);
|
window.history.pushState({ page: 'post', postId, origin: historyOrigin }, '', url);
|
||||||
setView('post');
|
setView('post');
|
||||||
setCurrentPostId(postId);
|
setCurrentPostId(postId);
|
||||||
setCurrentPostPreview(preview);
|
setCurrentPostPreview(preview);
|
||||||
|
setCurrentPostOrigin(resolvedOrigin);
|
||||||
setCurrentUserPreview(null);
|
setCurrentUserPreview(null);
|
||||||
|
setCurrentUserPostsPreview(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
const reduceMotion = window.matchMedia?.('(prefers-reduced-motion: reduce)').matches;
|
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)',
|
moveEasing: 'cubic-bezier(0.42, 0, 0.58, 1)',
|
||||||
resizeEasing: '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 = () => {
|
const clearSourceNames = () => {
|
||||||
sourceElement.style.viewTransitionName = '';
|
sourceElement.style.viewTransitionName = '';
|
||||||
clearPostTransitionParts(sourceParts);
|
clearPostTransitionParts(sourceParts);
|
||||||
@@ -426,7 +512,75 @@ export default function AppProvider({ children, cachedName = '' }) {
|
|||||||
root.classList.add('post-transition-settled');
|
root.classList.add('post-transition-settled');
|
||||||
window.setTimeout(() => root.classList.remove('post-transition-settled'), 220);
|
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 openUser = useCallback((username, source = null, preview = null) => {
|
||||||
const url = new URL(window.location);
|
const url = new URL(window.location);
|
||||||
@@ -440,6 +594,8 @@ export default function AppProvider({ children, cachedName = '' }) {
|
|||||||
setCurrentUserPreview(preview || { username });
|
setCurrentUserPreview(preview || { username });
|
||||||
setCurrentPostId(null);
|
setCurrentPostId(null);
|
||||||
setCurrentPostPreview(null);
|
setCurrentPostPreview(null);
|
||||||
|
setCurrentPostOrigin(null);
|
||||||
|
setCurrentUserPostsPreview(null);
|
||||||
};
|
};
|
||||||
|
|
||||||
const avatarElement = source?.avatarElement;
|
const avatarElement = source?.avatarElement;
|
||||||
@@ -490,10 +646,12 @@ export default function AppProvider({ children, cachedName = '' }) {
|
|||||||
setView('post');
|
setView('post');
|
||||||
setCurrentPostId(state.postId);
|
setCurrentPostId(state.postId);
|
||||||
setCurrentPostPreview(null);
|
setCurrentPostPreview(null);
|
||||||
|
setCurrentPostOrigin(state.origin || null);
|
||||||
} else if (state.username) {
|
} else if (state.username) {
|
||||||
setView('user');
|
setView('user');
|
||||||
setCurrentUsername(state.username);
|
setCurrentUsername(state.username);
|
||||||
setCurrentUserPreview(null);
|
setCurrentUserPreview(null);
|
||||||
|
setCurrentUserPostsPreview(null);
|
||||||
} else if (state.pageName) {
|
} else if (state.pageName) {
|
||||||
setView('custom');
|
setView('custom');
|
||||||
setCurrentPageName(state.pageName);
|
setCurrentPageName(state.pageName);
|
||||||
@@ -501,8 +659,10 @@ export default function AppProvider({ children, cachedName = '' }) {
|
|||||||
setView(state.page || 'feed');
|
setView(state.page || 'feed');
|
||||||
setCurrentPostId(null);
|
setCurrentPostId(null);
|
||||||
setCurrentPostPreview(null);
|
setCurrentPostPreview(null);
|
||||||
|
setCurrentPostOrigin(null);
|
||||||
setCurrentUsername(null);
|
setCurrentUsername(null);
|
||||||
setCurrentUserPreview(null);
|
setCurrentUserPreview(null);
|
||||||
|
setCurrentUserPostsPreview(null);
|
||||||
setCurrentPageName(null);
|
setCurrentPageName(null);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -544,8 +704,8 @@ export default function AppProvider({ children, cachedName = '' }) {
|
|||||||
forumName, forumNameLoaded, updateForumName,
|
forumName, forumNameLoaded, updateForumName,
|
||||||
theme, toggleTheme,
|
theme, toggleTheme,
|
||||||
view, navigate,
|
view, navigate,
|
||||||
currentPostId, currentPostPreview, openPost,
|
currentPostId, currentPostPreview, currentPostOrigin, openPost, returnFromPost,
|
||||||
currentUsername, currentUserPreview, openUser,
|
currentUsername, currentUserPreview, currentUserPostsPreview, openUser,
|
||||||
currentPageName, openCustomPage,
|
currentPageName, openCustomPage,
|
||||||
sort, setSort,
|
sort, setSort,
|
||||||
refreshKey, refresh,
|
refreshKey, refresh,
|
||||||
|
|||||||
+10
-1
@@ -26,10 +26,11 @@ export default function Feed({ onOpenModal, onReport }) {
|
|||||||
const [totalPages, setTotalPages] = useState(1);
|
const [totalPages, setTotalPages] = useState(1);
|
||||||
const [viewerSrc, setViewerSrc] = useState(null);
|
const [viewerSrc, setViewerSrc] = useState(null);
|
||||||
const [selectedSort, setSelectedSort] = useState(sort);
|
const [selectedSort, setSelectedSort] = useState(sort);
|
||||||
const [drawerPhase, setDrawerPhase] = useState('idle');
|
const [drawerPhase, setDrawerPhase] = useState('closed');
|
||||||
const postListRef = useRef(null);
|
const postListRef = useRef(null);
|
||||||
const sortTimerRef = useRef(null);
|
const sortTimerRef = useRef(null);
|
||||||
const pendingSortRef = useRef(null);
|
const pendingSortRef = useRef(null);
|
||||||
|
const initialDrawerOpenedRef = useRef(false);
|
||||||
const PAGE_SIZE = 20;
|
const PAGE_SIZE = 20;
|
||||||
|
|
||||||
const load = useCallback(async (targetPage) => {
|
const load = useCallback(async (targetPage) => {
|
||||||
@@ -51,6 +52,14 @@ export default function Feed({ onOpenModal, onReport }) {
|
|||||||
setDrawerPhase('idle');
|
setDrawerPhase('idle');
|
||||||
}, 360);
|
}, 360);
|
||||||
});
|
});
|
||||||
|
} else if (!initialDrawerOpenedRef.current) {
|
||||||
|
// 首次加载:先在收起状态完成列表渲染,再像排序切换一样抽屉式展开。
|
||||||
|
// Feed 在 SPA 视图间保持挂载,因此从帖子/用户页返回时不会重复播放。
|
||||||
|
initialDrawerOpenedRef.current = true;
|
||||||
|
requestAnimationFrame(() => {
|
||||||
|
setDrawerPhase('opening');
|
||||||
|
sortTimerRef.current = setTimeout(() => setDrawerPhase('idle'), 360);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}, [sort, refreshKey]);
|
}, [sort, refreshKey]);
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ function avatar(username) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function PostDetail({ postId, initialPost = null }) {
|
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 { t, i18n } = useTranslation();
|
||||||
const { toast, confirmAction } = useToast();
|
const { toast, confirmAction } = useToast();
|
||||||
const [post, setPost] = useState(initialPost);
|
const [post, setPost] = useState(initialPost);
|
||||||
@@ -134,10 +134,26 @@ export default function PostDetail({ postId, initialPost = null }) {
|
|||||||
setEditImages((prev) => prev.filter((u) => u !== url));
|
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 (
|
return (
|
||||||
<div id="pagePost" className="page-slide active">
|
<div id="pagePost" className="page-slide active">
|
||||||
<div className="page-header post-page-header">
|
<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} /> 返回
|
<Icon name="back" size={16} /> 返回
|
||||||
</button>
|
</button>
|
||||||
<h2>{post.title || '帖子详情'}</h2>
|
<h2>{post.title || '帖子详情'}</h2>
|
||||||
@@ -147,11 +163,25 @@ export default function PostDetail({ postId, initialPost = null }) {
|
|||||||
<div id="postDetailContent" className="post-page-main">
|
<div id="postDetailContent" className="post-page-main">
|
||||||
<div className="post-detail-card">
|
<div className="post-detail-card">
|
||||||
<div className="post-header">
|
<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
|
<span
|
||||||
className="post-username"
|
className="post-username"
|
||||||
style={{ cursor: 'pointer', color: 'var(--primary)' }}
|
style={{ cursor: 'pointer', color: 'var(--primary)' }}
|
||||||
onClick={() => openUser(post.username)}
|
onClick={handleOpenUser}
|
||||||
>
|
>
|
||||||
{post.username || '匿名用户'}
|
{post.username || '匿名用户'}
|
||||||
</span>
|
</span>
|
||||||
|
|||||||
+58
-12
@@ -1,23 +1,27 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
// 用户主页:资料卡 + 该用户的帖子列表
|
// 用户主页:资料卡 + 该用户的帖子列表
|
||||||
import { useEffect, useState, useCallback } from 'react';
|
import { useEffect, useState, useCallback, useRef } from 'react';
|
||||||
import { API } from '@/lib/api';
|
import { API } from '@/lib/api';
|
||||||
import { useApp } from './AppProvider';
|
import { useApp } from './AppProvider';
|
||||||
import { Icon } from './Icons';
|
import { Icon } from './Icons';
|
||||||
import { useOpenPrivateChat } from './chat/ChatManager';
|
import { useOpenPrivateChat } from './chat/ChatManager';
|
||||||
|
import { renderMarkdown } from '@/lib/markdown';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
function avatar(username, size = 128) {
|
function avatar(username, size = 128) {
|
||||||
return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || 'U') +
|
return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || 'U') +
|
||||||
'&background=6366f1&color=fff&size=' + size;
|
'&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 { currentUser, openPost } = useApp();
|
||||||
|
const { t, i18n } = useTranslation();
|
||||||
const openPrivateChat = useOpenPrivateChat();
|
const openPrivateChat = useOpenPrivateChat();
|
||||||
const [user, setUser] = useState(initialUser);
|
const [user, setUser] = useState(initialUser);
|
||||||
const [posts, setPosts] = useState([]);
|
const [posts, setPosts] = useState(initialPosts || []);
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
|
const postsRef = useRef(null);
|
||||||
|
|
||||||
const load = useCallback(async () => {
|
const load = useCallback(async () => {
|
||||||
setError(null);
|
setError(null);
|
||||||
@@ -37,9 +41,16 @@ export default function UserProfile({ username, initialUser = null }) {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setUser(initialUser);
|
setUser(initialUser);
|
||||||
setPosts([]);
|
setPosts(initialPosts || []);
|
||||||
load();
|
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) {
|
if (error) {
|
||||||
return (
|
return (
|
||||||
@@ -82,20 +93,55 @@ export default function UserProfile({ username, initialUser = null }) {
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
{user.id && (
|
{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>
|
<h3 style={{ margin: '24px 0 16px', fontSize: 18 }}><Icon name="file" size={18} /> 发布的帖子</h3>
|
||||||
{posts.length === 0 ? (
|
{posts.length === 0 ? (
|
||||||
<div style={{ color: '#94a3b8', padding: '20px 0', textAlign: 'center' }}>还没有发帖</div>
|
<div style={{ color: '#94a3b8', padding: '20px 0', textAlign: 'center' }}>还没有发帖</div>
|
||||||
) : (
|
) : (
|
||||||
posts.map((p) => {
|
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 (
|
return (
|
||||||
<div key={p.id} className="post-card" style={{ cursor: 'pointer' }} onClick={(e) => openPost(p.id, e.currentTarget, p)}>
|
<div
|
||||||
<div className="post-title" style={{ fontSize: 16, fontWeight: 600 }}>{p.title || '无标题'}</div>
|
key={p.id}
|
||||||
<div className="post-content" style={{ fontSize: 14, color: 'var(--text-secondary)' }}>
|
className="post-card user-post-card"
|
||||||
{(p.content || '').substring(0, 100)}{(p.content || '').length > 100 ? '...' : ''}
|
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>
|
||||||
<div style={{ fontSize: 12, color: 'var(--text-light)', marginTop: 8 }}>{time}</div>
|
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in new issue
Block a user