Files
personal/lib/scrollEnv.ts
T
virtheart 520fbb1480 fix: Header 跨页锚点导航 + 后台移除固定 URL 编辑项
- Header 作品/联系:首页内直接平滑滚动;在 /services、/work/* 等
  非首页时写入 hq:pendingScrollAnchor 后经路由过渡回首页,
  SmoothScroller 消费并滚动到位(修复其它页面点击无反应)
- 服务按钮在 /services 页自身不再重复导航
- PENDING_SCROLL_ANCHOR_SESSION_KEY 收敛到 lib/scrollEnv 单一来源,
  work 页与 SmoothScroller 共用,消除三处魔法字符串
- 后台「关于」组移除 about_p2_link_href(站内结构性锚点 #selected-work
  不可配,只保留链接文字可编辑);seed/settings/类型同步清理
2026-09-02 20:10:05 +08:00

85 lines
3.1 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"use client";
/**
* Scroll environment — module singleton mirroring the original
* `scrollEnv` (module 29680): holds the Lenis instance so header
* shortcuts (coords click → top, [T] top, [B] bottom, Work/Contact
* anchors) and the custom scrollbar can scroll from anywhere.
*
* Our clone scrolls the WINDOW (the original scrolls inside <main>);
* the container-el surface is kept for API parity — the container is
* document.documentElement.
*/
type LenisLike = {
scrollTo: (
target: number | string | HTMLElement,
opts?: Record<string, unknown>
) => void;
} | null;
let lenis: LenisLike = null;
let containerEl: HTMLElement | null = null;
const containerListeners = new Set<(el: HTMLElement | null) => void>();
function getContainer(): HTMLElement | null {
if (containerEl) return containerEl;
return typeof document === "undefined" ? null : document.documentElement;
}
export const scrollEnv = {
setLenis(instance: LenisLike) {
lenis = instance;
},
getLenis() {
return lenis;
},
/** Original scrollEnv.setContainerEl (our scroller is the window). */
setContainerEl(el: HTMLElement | null) {
containerEl = el;
containerListeners.forEach((l) => l(el));
},
getContainerEl(): HTMLElement | null {
return getContainer();
},
subscribeContainerEl(listener: (el: HTMLElement | null) => void) {
containerListeners.add(listener);
return () => containerListeners.delete(listener);
},
/** Current scroll top in px (window scroller). */
getScrollTopPx(): number {
return window.scrollY || document.documentElement.scrollTop || 0;
},
/** Original lenisScrollTo: smoothed by Lenis unless immediate. */
lenisScrollTo(target: number | string | HTMLElement, opts?: { immediate?: boolean; lerp?: number }) {
const t = typeof target === "number" ? Math.max(0, target) : target;
if (lenis) lenis.scrollTo(t, { lerp: 0.1, immediate: !!opts?.immediate });
else if (typeof t === "number") window.scrollTo({ top: t, behavior: opts?.immediate ? "auto" : "smooth" });
else (t as HTMLElement).scrollIntoView({ behavior: opts?.immediate ? "auto" : "smooth" });
},
scrollToTop(behavior: "smooth" | "auto" = "smooth") {
if (lenis) lenis.scrollTo(0, { lerp: 0.1, immediate: behavior === "auto" });
else window.scrollTo({ top: 0, behavior });
},
scrollToBottom(behavior: "smooth" | "auto" = "smooth") {
const limit = Math.max(
0,
(document.documentElement?.scrollHeight ?? 0) - window.innerHeight
);
if (lenis) lenis.scrollTo(limit, { lerp: 0.1, immediate: behavior === "auto" });
else window.scrollTo({ top: limit, behavior });
},
scrollToSection(selector: string) {
const el = document.querySelector(selector);
if (!el) return;
if (lenis) lenis.scrollTo(el as HTMLElement, { lerp: 0.1 });
else (el as HTMLElement).scrollIntoView({ behavior: "smooth" });
},
};
/**
* 跨页锚点:详情页/Header 在非首页时把目标 section 存入 sessionStorage,
* startNavigation("/") 回首页后由 SmoothScroller 消费并平滑滚动。
*/
export const PENDING_SCROLL_ANCHOR_SESSION_KEY = "hq:pendingScrollAnchor";