fix: morph post card content by element
This commit is contained in:
1 parent
ae2d5e82ca
commit
0060034dec
4 files changed
+208
-21
No files matched your search
+35
-14
@@ -7,6 +7,34 @@ import { API, getTheme, setToken, getToken } from '@/lib/api';
|
||||
|
||||
const AppContext = createContext(null);
|
||||
|
||||
const POST_TRANSITION_PARTS = [
|
||||
['.post-avatar', 'post-avatar'],
|
||||
['.post-username', 'post-author'],
|
||||
['.post-title', 'post-heading'],
|
||||
['.post-time', 'post-time'],
|
||||
['.post-content', 'post-body'],
|
||||
['.post-images', 'post-feed-media'],
|
||||
['.post-pin-state', 'post-pin-state'],
|
||||
['.post-edited-state', 'post-edited-state'],
|
||||
['.post-actions', 'post-feed-actions'],
|
||||
];
|
||||
|
||||
function namePostTransitionParts(card) {
|
||||
if (!card) return [];
|
||||
return POST_TRANSITION_PARTS.flatMap(([selector, name]) => {
|
||||
const element = card.querySelector(selector);
|
||||
if (!element) return [];
|
||||
element.style.viewTransitionName = name;
|
||||
return [element];
|
||||
});
|
||||
}
|
||||
|
||||
function clearPostTransitionParts(elements) {
|
||||
elements.forEach((element) => {
|
||||
element.style.viewTransitionName = '';
|
||||
});
|
||||
}
|
||||
|
||||
function stageSharedGeometry(transition, { name, duration, moveOffset }) {
|
||||
transition.ready.then(() => {
|
||||
const groupAnimation = document.getAnimations().find(
|
||||
@@ -221,8 +249,7 @@ export default function AppProvider({ children, cachedName = '' }) {
|
||||
let targetCard = currentPostId
|
||||
? Array.from(document.querySelectorAll('[data-post-id]')).find((element) => element.dataset.postId === String(currentPostId))
|
||||
: null;
|
||||
let targetTitle = null;
|
||||
let targetTime = null;
|
||||
let targetParts = [];
|
||||
document.documentElement.classList.add('home-view-transition');
|
||||
if (targetCard) document.documentElement.classList.add('returning-home');
|
||||
|
||||
@@ -231,17 +258,14 @@ export default function AppProvider({ children, cachedName = '' }) {
|
||||
targetCard = currentPostId
|
||||
? Array.from(document.querySelectorAll('[data-post-id]')).find((element) => element.dataset.postId === String(currentPostId))
|
||||
: null;
|
||||
targetTitle = targetCard?.querySelector('.post-title');
|
||||
targetTime = targetCard?.querySelector('.post-time');
|
||||
if (targetCard) targetCard.style.viewTransitionName = 'post-expand';
|
||||
if (targetTitle) targetTitle.style.viewTransitionName = 'post-heading';
|
||||
if (targetTime) targetTime.style.viewTransitionName = 'post-time';
|
||||
targetParts = namePostTransitionParts(targetCard);
|
||||
});
|
||||
stageSharedGeometry(transition, { name: 'post-expand', duration: 480, moveOffset: 0.72 });
|
||||
stageSharedGeometry(transition, { name: 'post-body', duration: 480, moveOffset: 0.72 });
|
||||
const clearTargetNames = () => {
|
||||
if (targetCard) targetCard.style.viewTransitionName = '';
|
||||
if (targetTitle) targetTitle.style.viewTransitionName = '';
|
||||
if (targetTime) targetTime.style.viewTransitionName = '';
|
||||
clearPostTransitionParts(targetParts);
|
||||
};
|
||||
transition.ready.then(clearTargetNames, clearTargetNames);
|
||||
transition.finished.finally(() => {
|
||||
@@ -268,20 +292,17 @@ export default function AppProvider({ children, cachedName = '' }) {
|
||||
return;
|
||||
}
|
||||
|
||||
const sourceTitle = sourceElement.querySelector('.post-title');
|
||||
const sourceTime = sourceElement.querySelector('.post-time');
|
||||
sourceElement.style.viewTransitionName = 'post-expand';
|
||||
if (sourceTitle) sourceTitle.style.viewTransitionName = 'post-heading';
|
||||
if (sourceTime) sourceTime.style.viewTransitionName = 'post-time';
|
||||
const sourceParts = namePostTransitionParts(sourceElement);
|
||||
document.documentElement.classList.add('post-view-transition');
|
||||
const transition = document.startViewTransition(() => {
|
||||
flushSync(commitNavigation);
|
||||
});
|
||||
stageSharedGeometry(transition, { name: 'post-expand', duration: 960, moveOffset: 0.62 });
|
||||
stageSharedGeometry(transition, { name: 'post-body', duration: 960, moveOffset: 0.62 });
|
||||
const clearSourceNames = () => {
|
||||
sourceElement.style.viewTransitionName = '';
|
||||
if (sourceTitle) sourceTitle.style.viewTransitionName = '';
|
||||
if (sourceTime) sourceTime.style.viewTransitionName = '';
|
||||
clearPostTransitionParts(sourceParts);
|
||||
};
|
||||
transition.ready.then(clearSourceNames, clearSourceNames);
|
||||
transition.finished.finally(() => {
|
||||
|
||||
+2
-2
@@ -140,7 +140,7 @@ export default function Feed({ onOpenModal, onReport }) {
|
||||
<div key={p.id} className="post-card" data-post-id={p.id} style={{ cursor: 'pointer' }}
|
||||
onClick={(e) => openPost(p.id, e.currentTarget, p)}>
|
||||
{p.is_pinned && (
|
||||
<div style={{ fontSize: 12, color: 'var(--primary)', fontWeight: 600, marginBottom: 4 }}><Icon name="pin" size={12} /> {t('feed.pinned')}</div>
|
||||
<div className="post-pin-state" style={{ 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 || avatar(p.username)} className="post-avatar" alt="" />
|
||||
@@ -153,7 +153,7 @@ export default function Feed({ onOpenModal, onReport }) {
|
||||
</span>
|
||||
<span className="post-time">{time}</span>
|
||||
{p.edited_at && (
|
||||
<span style={{ fontSize: 11, color: 'var(--text-light)', marginLeft: 6 }}>{t('feed.deleted')}</span>
|
||||
<span className="post-edited-state" style={{ fontSize: 11, color: 'var(--text-light)', marginLeft: 6 }}>{t('feed.deleted')}</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="post-title">{p.title || t('feed.noTitle')}</div>
|
||||
|
||||
@@ -140,14 +140,14 @@ export default function PostDetail({ postId, initialPost = null }) {
|
||||
</span>
|
||||
<span className="post-time">{time}</span>
|
||||
{post.edited_at && (
|
||||
<span style={{ fontSize: 12, color: 'var(--text-light)', marginLeft: 8 }}>
|
||||
<span className="post-edited-state" style={{ fontSize: 12, color: 'var(--text-light)', marginLeft: 8 }}>
|
||||
(已编辑 {new Date(post.edited_at).toLocaleString('zh-CN')})
|
||||
</span>
|
||||
)}
|
||||
{post.is_pinned && (
|
||||
<span style={{ fontSize: 12, color: 'var(--primary)', marginLeft: 8, display: 'inline-flex', alignItems: 'center', gap: 3 }}><Icon name="pin" size={12} /> 置顶</span>
|
||||
<span className="post-pin-state" style={{ fontSize: 12, color: 'var(--primary)', marginLeft: 8, display: 'inline-flex', alignItems: 'center', gap: 3 }}><Icon name="pin" size={12} /> 置顶</span>
|
||||
)}
|
||||
<div style={{ display: 'flex', gap: 4, marginLeft: 'auto' }}>
|
||||
<div className="post-detail-controls" style={{ display: 'flex', gap: 4, marginLeft: 'auto' }}>
|
||||
{currentUser && currentUser.role === 'admin' && (
|
||||
<button className="btn-sm btn-secondary" style={{ padding: '4px 10px' }} onClick={handleTogglePin}>
|
||||
<Icon name="pin" size={12} /> {post.is_pinned ? '取消置顶' : '置顶'}
|
||||
@@ -174,7 +174,7 @@ export default function PostDetail({ postId, initialPost = null }) {
|
||||
</div>
|
||||
)}
|
||||
{post.signature && (
|
||||
<div style={{ marginTop: 16, paddingTop: 12, borderTop: '1px solid var(--border-light)', fontSize: 12, color: 'var(--text-secondary)' }} dangerouslySetInnerHTML={{ __html: renderMarkdown(post.signature) }} />
|
||||
<div className="post-detail-signature" style={{ marginTop: 16, paddingTop: 12, borderTop: '1px solid var(--border-light)', fontSize: 12, color: 'var(--text-secondary)' }} dangerouslySetInnerHTML={{ __html: renderMarkdown(post.signature) }} />
|
||||
)}
|
||||
</div>
|
||||
<ReplyList postId={postId} onRefresh={load} />
|
||||
|
||||
Reference in new issue
Block a user