Files
personal/components/dom/ScrambleReveal.tsx
T
virtheart 8ce5471c25 feat: 移植首页 passcode 彩蛋(■■■■■■ → /unlock/2026 解锁 TikTok 品牌名与隐藏作品卡)
- lib/passcode.ts:module 58156/28116 移植——primaryPasscodePath("/2026")、buildPasscodeUnlockHref("/unlock/2026?return=%2F")、slotCount:4、品牌名按原站 charCode-1 混淆解码(TikTok)、锁定占位符与字符/散段样式类
- PasscodeAccess.tsx:module 4278 移植——access store(useSyncExternalStore + passcode-access-changed 事件驱动刷新)、usePasscodeAllowed/usePasscodeAccessLookup、submit/logout(POST /api/passcode)
- PasscodeUnlockScreen:4 格圆环输入(inset ring label-1/label-3 + caret-blink 光标)、满 4 位自动提交、错误 shake(WAAPI 等价原 framer x 关键帧抖动)、成功 'Access granted' 600ms 后 replace 回来源、已解锁进入立即跳回
- layout 挂 PasscodeAccessProvider(服务端 cookies 读初始 access);新增 /api/passcode GET/POST(httpOnly cookie 持久化,解锁码经 PASSCODE_CODE 配置,默认 2026)与 /unlock/[scope] 页
- ScrambleReveal 支持 slot part({length,scramble,className,settled}):打散期显示占位字符、收尾替换为交互节点,1:1 原版语义
- 首页 about 段换 c9/c2 移植:锁定点击跳解锁页、解锁显示 TikTok、长按 500ms/右键 confirm 重锁;WorkSection 按 access 过滤,解锁后 TikTok 卡(tt01/tt02 素材)出现在首位
- CDP 全流程验证:锁定→解锁→持久化→重锁 循环 + 错码抖动清空 + 卡片数 10↔11
2026-09-01 19:21:52 +08:00

226 lines
7.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";
import { useEffect, useMemo, useRef, useState } from "react";
/**
* 1:1 re-implementation of the original scramble-reveal text (chunk
* 2689132c4e070b68.js module 71358 component `d`):
*
* - charset `ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*+-=?/<>[]{}` picked
* randomly per render while a character is "scrambling"
* - char i starts scrambling at startDelayMs + i*letterDelayMs and settles
* after a window of 4*letterDelayMs (2 phases × 2*letterDelayMs)
* - phase 0 color #c0fe04, phase 1 color #607F02 (light theme) unless
* scrambleColors is false
* - refresh tick every 40ms; the whole text settles at
* startDelayMs + (n-1)*letterDelayMs + 4*letterDelayMs
* - starts once when the element enters the viewport (threshold 0.1)
*
* Parts accept strings or **slot objects** `{ length, scramble?, className?,
* settled? }` (original slot parts): while scrambling, slot characters show
* `scramble` (default: spaces) with the same per-char delays; once settled
* the slot renders `settled` (a React node, e.g. the passcode button) in
* place of the characters.
*/
const CHARSET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*+-=?/<>[]{}";
const COLORS_LIGHT = ["#c0fe04", "#607F02"];
const randomChar = () => CHARSET[Math.floor(Math.random() * CHARSET.length)];
export type ScrambleSlotPart = {
/** Number of characters this slot occupies in the reveal sequence. */
length: number;
/** Placeholder characters shown while the slot is scrambling. */
scramble?: string;
/** Class for the slot wrapper span. */
className?: string;
/** React node rendered once the reveal has fully settled. */
settled?: React.ReactNode;
};
type Part = string | ScrambleSlotPart;
type NormalizedPart =
| { kind: "text"; value: string }
| {
kind: "slot";
length: number;
scramble: string;
settled?: React.ReactNode;
className?: string;
};
function normalizeParts(parts: Part[] | undefined, text: string | undefined): NormalizedPart[] {
return (parts ?? [text ?? ""]).map((p) => {
if (typeof p === "string") return { kind: "text" as const, value: p };
const length = Math.max(0, p.length);
const scramble = (p.scramble ?? (length > 0 ? " ".repeat(length) : ""))
.slice(0, length)
.padEnd(length, " ");
return {
kind: "slot" as const,
length,
scramble,
settled: p.settled,
className: p.className,
};
});
}
type Props = {
text?: string;
parts?: Part[];
className?: string;
startDelayMs?: number;
letterDelayMs?: number;
scrambleColors?: boolean;
/** Reveal from the last character to the first (original `reverse`). */
reverse?: boolean;
};
export default function ScrambleReveal({
text,
parts,
className,
startDelayMs = 0,
letterDelayMs = 80,
scrambleColors = true,
reverse = false,
}: Props) {
const normalized = useMemo(
() => normalizeParts(parts, text),
[parts, text]
);
const ref = useRef<HTMLSpanElement>(null);
const [entered, setEntered] = useState(false);
const [startedAt, setStartedAt] = useState<number | null>(null);
const [elapsed, setElapsed] = useState(0);
const F = normalized.reduce(
(acc, p) => acc + (p.kind === "text" ? p.value.length : p.length),
0
);
const phaseLen = 2 * letterDelayMs; // j
const scrambleWindow = 2 * phaseLen; // D
const total = startDelayMs + Math.max(0, F - 1) * letterDelayMs + scrambleWindow;
useEffect(() => {
const el = ref.current;
if (!el) return;
const observer = new IntersectionObserver(
([entry]) => {
if (entry.isIntersecting) {
setEntered(true);
observer.disconnect();
}
},
{ threshold: 0.1 }
);
observer.observe(el);
return () => observer.disconnect();
}, []);
useEffect(() => {
if (!entered || startedAt !== null) return;
const t0 = performance.now();
setStartedAt(t0);
setElapsed(0);
}, [entered, startedAt]);
useEffect(() => {
if (startedAt === null) return;
if (elapsed >= total) return;
const interval = window.setInterval(() => {
setElapsed(performance.now() - startedAt);
}, 40);
return () => window.clearInterval(interval);
}, [startedAt, elapsed, total]);
const settled = startedAt !== null && elapsed >= total;
const V = Math.max(0, elapsed - startDelayMs);
const scrambling = startedAt !== null && !settled;
if (settled || !entered) {
return (
<span ref={ref} className={className}>
{normalized.map((p, i) =>
p.kind === "text" ? (
<span key={`text-${i}`}>{entered ? p.value : ""}</span>
) : (
<span key={`slot-${i}`} className={p.className}>
{entered ? p.settled : null}
</span>
)
)}
</span>
);
}
// recompute random chars on each elapsed tick
let index = 0;
return (
<span ref={ref} className={className}>
{normalized.map((p, pi) => {
if (p.kind === "text") {
return (
<span key={`text-${pi}`}>
{p.value.split("").map((ch, ci) => {
const i = index++;
// Original `reverse`: character delays run last→first.
const order = reverse ? F - 1 - i : i;
if (ch === "\n") return <br key={`br-${pi}-${ci}`} />;
if (ch === " ") return <span key={`sp-${pi}-${ci}`}> </span>;
const n = order * letterDelayMs;
if (V < n) {
return (
<span key={`c-${pi}-${ci}`} style={{ opacity: 0 }}>
{ch}
</span>
);
}
if (V < n + scrambleWindow) {
const phase = Math.min(1, Math.floor((V - n) / phaseLen));
const color = scrambleColors ? COLORS_LIGHT[phase] : undefined;
return (
<span key={`c-${pi}-${ci}`} style={color ? { color } : undefined}>
{randomChar()}
</span>
);
}
return <span key={`c-${pi}-${ci}`}>{ch}</span>;
})}
</span>
);
}
return (
<span key={`slot-${pi}`} className={p.className}>
{p.scramble.split("").map((ch, ci) => {
const i = index++;
const order = reverse ? F - 1 - i : i;
const n = order * letterDelayMs;
if (V < n) {
return (
<span key={`sc-${pi}-${ci}`} style={{ opacity: 0 }}>
{ch}
</span>
);
}
if (V < n + scrambleWindow) {
const phase = Math.min(1, Math.floor((V - n) / phaseLen));
const color = scrambleColors ? COLORS_LIGHT[phase] : undefined;
return (
<span key={`sc-${pi}-${ci}`} style={color ? { color } : undefined}>
{randomChar()}
</span>
);
}
return <span key={`sc-${pi}-${ci}`}>{ch}</span>;
})}
</span>
);
})}
</span>
);
}