3957e36c41
- 详情页补齐原站 MDX 组件:左侧目录(2xl 固定定位、滚动高亮 top≤96、 点击平滑滚动)、标题锚点(hover #、点击复制链接 + replaceState)、 代码块(语言标签 + COPY/COPIED + 原站 prism token HTML 按行号渲染, --code-* 配色变量)、页脚 Metadata(实时视口尺寸 / 正文字符数)与 Work/Contact 按钮(sessionStorage hq:pendingScrollAnchor → 首页过渡后 滚动到对应区块,SmoothScroller 消费) - content/workArticles.ts 重新提取:内联 strong/em/code 片段、标题 id、 代码块语言 + 高亮 HTML + 纯文本(bs4 全量解析 6 篇文章) - 详情页不再显示十字网格(BackgroundGrid 按 pathname 门控,置于全部 hooks 之后避免 hook 数量变化崩溃) - 首页层叠修复:WebGL 容器改为原站的 top-0 left-0 -z-1 fixed(此前 z-auto fixed 按 CSS 绘制顺序盖住 banner 文案与作品说明文字),footer 改为原站的 z-10 relative,body 背景移至 html 根节点(否则普通流 body 背景会盖住 -z-1 场景)
134 lines
4.9 KiB
TypeScript
134 lines
4.9 KiB
TypeScript
'use client';
|
|
|
|
import { useEffect } from 'react';
|
|
import { usePathname } from 'next/navigation';
|
|
import Lenis from 'lenis';
|
|
import { scrollBus, notifyScroll } from '@/lib/scrollBus';
|
|
import { scrollEnv } from '@/lib/scrollEnv';
|
|
|
|
const clamp01 = (v: number) => Math.min(1, Math.max(0, v));
|
|
|
|
/**
|
|
* Lenis smooth scrolling + the original sP camera-progress computation
|
|
* (banner/footer section geometry):
|
|
* f1 = clamp((vh - bannerBottom) / max(1, vh - .25vh), 0, 1)
|
|
* f2 = footer ? clamp(footerTop / vh, 0, 1) : 1
|
|
* sP = f1 * f2
|
|
* f2 measures the footer's distance ABOVE: it is 1 while the footer is
|
|
* further than one viewport away and falls to 0 as the footer top reaches
|
|
* the viewport top. Until the footer section exists we keep f2 at 1 —
|
|
* matching the original's behavior for most of the page (sP → 1 after
|
|
* ~0.75 viewport of scroll, which drives the dot-overlay fade and the
|
|
* camera pull-back).
|
|
*/
|
|
export default function SmoothScroller() {
|
|
// Original route wiring (chunk 3c6cc5b2 module 95212): the home route is
|
|
// wrapped in ReactLenis; every other route scrolls its container
|
|
// natively. Our Lenis is window-level, so on detail routes we destroy it
|
|
// entirely (native window scroll) and reset the scroll to the top — the
|
|
// transition overlay covers the jump.
|
|
const pathname = usePathname();
|
|
const isHome = pathname === '/';
|
|
|
|
// Scroll reset per route (original scrolls its per-route container to
|
|
// top on mount; the transition cover hides the jump).
|
|
useEffect(() => {
|
|
if (window.location.hash) return;
|
|
window.scrollTo(0, 0);
|
|
requestAnimationFrame(() => window.scrollTo(0, 0));
|
|
}, [isHome]);
|
|
|
|
useEffect(() => {
|
|
if (!isHome) {
|
|
scrollEnv.setLenis(null);
|
|
return;
|
|
}
|
|
let disposed = false;
|
|
let rafId = 0;
|
|
// Refresh must always start at the top (original behavior: no anchor
|
|
// → scroll to 0; also re-asserted on bfcache pageshow).
|
|
if ('scrollRestoration' in window.history) {
|
|
window.history.scrollRestoration = 'manual';
|
|
}
|
|
const resetScroll = () => {
|
|
if (window.location.hash) return;
|
|
window.scrollTo(0, 0);
|
|
requestAnimationFrame(() => window.scrollTo(0, 0));
|
|
};
|
|
resetScroll();
|
|
|
|
// Pending anchor from a detail page (original module 54450: the
|
|
// footer "Work"/"Contact" buttons store the anchor in sessionStorage
|
|
// and navigate "/" through the transition; the home scroller then
|
|
// smooth-scrolls to it).
|
|
const pendingAnchor = sessionStorage.getItem('hq:pendingScrollAnchor');
|
|
if (pendingAnchor) {
|
|
sessionStorage.removeItem('hq:pendingScrollAnchor');
|
|
const target = document.querySelector(pendingAnchor);
|
|
if (target) {
|
|
requestAnimationFrame(() => requestAnimationFrame(() => {
|
|
lenis.scrollTo(target as HTMLElement, { lerp: 0.1 });
|
|
}));
|
|
}
|
|
}
|
|
|
|
const onPageShow = (e: PageTransitionEvent) => {
|
|
if (e.persisted) resetScroll();
|
|
};
|
|
window.addEventListener('pageshow', onPageShow);
|
|
|
|
const updateCameraProgress = () => {
|
|
const vh = Math.max(1, window.innerHeight);
|
|
const banner = document.querySelector('[data-section="banner"]');
|
|
const footer = document.querySelector('[data-section="footer"]');
|
|
if (!banner) {
|
|
scrollBus.sP = 0;
|
|
return;
|
|
}
|
|
const bannerRect = banner.getBoundingClientRect();
|
|
const f1 = clamp01((vh - bannerRect.bottom) / Math.max(1, vh - 0.25 * vh));
|
|
const f2 = footer
|
|
? clamp01(footer.getBoundingClientRect().top / vh)
|
|
: 1;
|
|
scrollBus.sP = f1 * f2;
|
|
};
|
|
|
|
const lenis = new Lenis({
|
|
lerp: 0.1,
|
|
smoothWheel: true,
|
|
});
|
|
scrollEnv.setLenis(lenis);
|
|
|
|
lenis.scrollTo(0, { immediate: true });
|
|
|
|
lenis.on('scroll', (e: any) => {
|
|
scrollBus.current = e.scroll;
|
|
scrollBus.velocity = e.velocity;
|
|
scrollBus.progress = e.progress;
|
|
updateCameraProgress();
|
|
notifyScroll();
|
|
});
|
|
window.addEventListener('resize', updateCameraProgress);
|
|
updateCameraProgress();
|
|
|
|
function raf(time: number) {
|
|
if (disposed) return;
|
|
lenis.raf(time);
|
|
rafId = requestAnimationFrame(raf);
|
|
}
|
|
|
|
rafId = requestAnimationFrame(raf);
|
|
|
|
return () => {
|
|
disposed = true;
|
|
cancelAnimationFrame(rafId);
|
|
window.removeEventListener('resize', updateCameraProgress);
|
|
window.removeEventListener('pageshow', onPageShow);
|
|
scrollEnv.setLenis(null);
|
|
lenis.destroy();
|
|
};
|
|
}, [isHome]);
|
|
|
|
return null;
|
|
}
|