"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
); * the container-el surface is kept for API parity — the container is * document.documentElement. */ type LenisLike = { scrollTo: ( target: number | string | HTMLElement, opts?: Record ) => 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";