eac0785cf6
- WorkSection.tsx:1:1 还原原站 work 区块(selected-work 网格、rowGap=clientWidth/12、纵横比缓存、Coding Project 徽章、元数据行、外链/内链卡片) - workItems.ts:从 d59f7a97 数据文件提取的 10 个作品条目(passcode 保护的 /2026 TikTok 条目按原站默认过滤) - imageLayers.ts:重构为注册表(defs/targets/订阅版本号),ImageLayers 通过 useSyncExternalStore 响应 DOM 卡片注册 - HeroScene:Canvas 容器强制 pointer-events:none,指针跟踪全部走窗口级,避免吞掉 work 卡片悬停 - Header:Work 按钮滚动至 selected-work
177 lines
5.8 KiB
TypeScript
177 lines
5.8 KiB
TypeScript
"use client";
|
|
|
|
import { useEffect, useMemo, useRef, useState } from "react";
|
|
import {
|
|
SELECTED_WORK_SECTION_ID,
|
|
WORK_ITEMS,
|
|
type WorkItem,
|
|
} from "@/lib/workItems";
|
|
import {
|
|
getLayerTarget,
|
|
setLayerDef,
|
|
touchLayerRegistry,
|
|
} from "@/lib/imageLayers";
|
|
|
|
/**
|
|
* Selected Work grid — 1:1 port of the original section `h` (chunk
|
|
* 2689132c4e070b68.js, exports SELECTED_WORK_SECTION_ID / default):
|
|
*
|
|
* - `<section id="selected-work" className="px-4 lg:px-14 py-18 lg:py-24 w-full">`
|
|
* with an inner `grid grid-cols-12` whose `rowGap` is the measured column
|
|
* width (container.clientWidth / 12, tracked by ResizeObserver).
|
|
* - Each card is `<article className={gridClass}>` wrapping a `group block
|
|
* space-y-3 p-2` anchor (next Link for internal routes, <a> for external).
|
|
* - The visual is NOT a DOM <img>: the anchor's first child is an
|
|
* aspect-ratio placeholder div registered as a WebGL image-layer target;
|
|
* the canvas-side ImageLayers projects imageUrl/hoverImageUrl onto its
|
|
* screen rect (curl + negative reveal + hover dot mask).
|
|
* - Aspect ratios come from preloading each imageUrl and reading
|
|
* naturalWidth/naturalHeight into a module-level cache (original `o`).
|
|
* - The passcode-protected first item (/2026, brand "TikTok") is omitted
|
|
* exactly like the original's default-unlocked state.
|
|
*/
|
|
|
|
// Module-level cache mirroring the original `o` (persists across remounts).
|
|
const ratioCache: Record<string, string> = {};
|
|
|
|
export default function WorkSection() {
|
|
const [ratios, setRatios] = useState<Record<string, string>>({ ...ratioCache });
|
|
const [colWidth, setColWidth] = useState(0);
|
|
const gridRef = useRef<HTMLDivElement | null>(null);
|
|
|
|
// Register canvas layer defs for every visible item (idempotent data
|
|
// write; the version bump happens once targets exist, in the effect).
|
|
useMemo(() => {
|
|
for (const item of WORK_ITEMS) {
|
|
setLayerDef(item.href, {
|
|
imageUrl: item.imageUrl,
|
|
hoverImageUrl: item.hoverImageUrl,
|
|
});
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
touchLayerRegistry();
|
|
}, []);
|
|
|
|
// rowGap = column width (original RO on the grid container).
|
|
useEffect(() => {
|
|
const el = gridRef.current;
|
|
if (!el) return;
|
|
const measure = () => setColWidth(el.clientWidth / 12);
|
|
measure();
|
|
const ro = new ResizeObserver(measure);
|
|
ro.observe(el);
|
|
return () => ro.disconnect();
|
|
}, []);
|
|
|
|
// Preload images → natural aspect ratios (original effect).
|
|
useEffect(() => {
|
|
let cancelled = false;
|
|
const update = (imageUrl: string, w: number, h: number) => {
|
|
if (cancelled || w <= 0 || h <= 0) return;
|
|
const ratio = `${w} / ${h}`;
|
|
if (ratioCache[imageUrl] !== ratio) ratioCache[imageUrl] = ratio;
|
|
setRatios((prev) => (prev[imageUrl] === ratio ? prev : { ...prev, [imageUrl]: ratio }));
|
|
};
|
|
const images = WORK_ITEMS.map((item) => {
|
|
const img = new Image();
|
|
img.decoding = "async";
|
|
img.onload = () => update(item.imageUrl, img.naturalWidth, img.naturalHeight);
|
|
img.src = item.imageUrl;
|
|
if (img.complete && img.naturalWidth > 0 && img.naturalHeight > 0) {
|
|
update(item.imageUrl, img.naturalWidth, img.naturalHeight);
|
|
}
|
|
return img;
|
|
});
|
|
return () => {
|
|
cancelled = true;
|
|
for (const img of images) img.onload = null;
|
|
};
|
|
}, []);
|
|
|
|
return (
|
|
<section
|
|
id={SELECTED_WORK_SECTION_ID}
|
|
className="px-4 lg:px-14 py-18 lg:py-24 w-full"
|
|
>
|
|
<div
|
|
ref={gridRef}
|
|
className="grid grid-cols-12 w-full"
|
|
style={{ rowGap: `${colWidth}px` }}
|
|
>
|
|
{WORK_ITEMS.map((item) => (
|
|
<WorkCard key={item.href} item={item} ratio={ratios[item.imageUrl]} />
|
|
))}
|
|
</div>
|
|
</section>
|
|
);
|
|
}
|
|
|
|
function WorkCard({ item, ratio }: { item: WorkItem; ratio?: string }) {
|
|
const external = item.href.startsWith("http://") || item.href.startsWith("https://");
|
|
const cls = "group block space-y-3 p-2";
|
|
const label = `${item.name} - ${item.year}${external ? " (external)" : ""}`;
|
|
const target = getLayerTarget(item.href);
|
|
const body = (
|
|
<>
|
|
<div
|
|
ref={(el) => {
|
|
target.current = el;
|
|
}}
|
|
aria-hidden="true"
|
|
className="relative w-full pointer-events-none select-none"
|
|
style={{ aspectRatio: ratio ?? "1 / 1" }}
|
|
>
|
|
{item.codingProject && (
|
|
<span
|
|
className="top-0 right-0 z-10 absolute bg-selection px-1 font-mono-2 text-black text-xs uppercase pointer-events-none select-none"
|
|
aria-hidden="true"
|
|
>
|
|
Coding Project
|
|
</span>
|
|
)}
|
|
</div>
|
|
<div className="flex justify-between items-center gap-3 min-w-0 text-xs lg:text-sm uppercase">
|
|
<span className="flex-1 min-w-0 truncate">{item.name}</span>
|
|
<div className="flex items-center gap-2 sm:gap-3 font-mono-2 tabular-nums whitespace-nowrap shrink-0">
|
|
<span>{item.year}</span>
|
|
{item.type !== "post" && (
|
|
<span className="hidden lg:inline-flex items-center gap-1" aria-hidden="true">
|
|
<span>{item.type}</span>
|
|
<span>↗</span>
|
|
</span>
|
|
)}
|
|
</div>
|
|
</div>
|
|
</>
|
|
);
|
|
|
|
return (
|
|
<article className={item.gridClass ?? "col-span-12 lg:col-span-6 xl:col-span-4"}>
|
|
{external ? (
|
|
<a
|
|
href={item.href}
|
|
target="_blank"
|
|
rel="noopener noreferrer"
|
|
className={cls}
|
|
aria-label={label}
|
|
>
|
|
{body}
|
|
</a>
|
|
) : (
|
|
// Internal detail routes (/reunimos, /inspire_mono, …) are not ported
|
|
// yet — keep the anchor for markup parity but don't navigate to a 404.
|
|
<a
|
|
href={item.href}
|
|
className={cls}
|
|
aria-label={label}
|
|
onClick={(e) => e.preventDefault()}
|
|
>
|
|
{body}
|
|
</a>
|
|
)}
|
|
</article>
|
|
);
|
|
}
|