diff --git a/web/src/components/home-edit-board.tsx b/web/src/components/home-edit-board.tsx index 960140f..8d773f4 100644 --- a/web/src/components/home-edit-board.tsx +++ b/web/src/components/home-edit-board.tsx @@ -66,6 +66,12 @@ import type { LinkStyle, SiteCategory, SiteLink, TileSize } from "@/lib/types"; * `rectSortingStrategy` 只是按实测矩形做位移预览,砖全部等尺寸反而更准; * `closestCorners` 也不依赖网格。 * 空分类的落点就是那块固定尺寸的「+」砖,命中面积随砖确定。 + * + * 抖动(iOS 主屏式)分两层实现,互不打架:dnd-kit 的拖拽位移 `transform` 留在 + * 外层 `
`(`setNodeRef` 那层),抖动的 `rotate` 挂在内层砖壳上(`TILE_JIGGLE_CLASS` + * + `tileJiggleStyle` 给的相位/周期)。正在拖的那块去掉动画类停抖,其余照抖; + * `DragOverlay` 的预览砖不抖。旋转不改布局盒,所以外层 div 的实测矩形不变, + * `rectSortingStrategy` / `closestCorners` 的输入稳定。 */ /** 分类标题行投放目标(落到该分类末尾)的 id 前缀 */ @@ -98,6 +104,41 @@ const PLACEHOLDER_CLASS: Partial> = { wide: "min-h-[82px]", }; +/** + * 砖块抖动的工具类(Tailwind 从 `index.css` 的 `--animate-nav-tile-jiggle` 生成)。 + * 必须是完整字面量:Tailwind 只扫源码正文,拼出来的类名不会被生成。 + */ +export const TILE_JIGGLE_CLASS = "animate-nav-tile-jiggle"; + +/** + * 单程摆动的三档时长(ms):配 `alternate` 就是完整往返 320 / 360 / 400ms + * (规格 0.3–0.4s)。逐砖取其中一档,避免同周期同相位的砖在视觉上锁成一条线。 + */ +const JIGGLE_SWING_MS = [160, 180, 200] as const; + +/** 相位打散的窗口(ms):负延迟取 0–399,足够覆盖任何一个抖动周期 */ +const JIGGLE_PHASE_MS = 400; + +/** + * 一块砖的抖动相位与周期(内联 CSS,不用 JS 逐帧驱动)。 + * + * 用 key(链接 id / `add:<分类 id>`)的 FNV-1a 32 位散列取延迟与时长:同一块砖 + * 每次渲染算出来都一样(重渲染不会把动画拽回起点,拖拽结束后也回到自己的相位), + * 相邻砖的延迟又天然错开。负延迟 = 动画一开始就已经跑在周期中途。 + */ +export function tileJiggleStyle(key: string): React.CSSProperties { + let hash = 2166136261; + for (let i = 0; i < key.length; i += 1) { + hash ^= key.charCodeAt(i); + hash = Math.imul(hash, 16777619); + } + const h = hash >>> 0; + return { + animationDelay: `-${h % JIGGLE_PHASE_MS}ms`, + animationDuration: `${JIGGLE_SWING_MS[h % JIGGLE_SWING_MS.length]}ms`, + }; +} + type Order = Record; function containerOf(order: Order, id: string): string | null { @@ -224,6 +265,9 @@ function EditableTile({ {...listeners} aria-label={link.title} title={link.title} + // 抖动挂在内层砖壳:拖拽位移的 transform 在外层 div(分层见文件头注释), + // 拖起中这块停抖,松手后按自己的相位继续 + style={isDragging ? undefined : tileJiggleStyle(link.id)} className={cn( linkTileClassName(linkStyle, tileSize), // compact / wide 仍在网格里,砖壳要撑满格子(
+ {/* 拖拽预览:刻意不加抖动类(被拖起的那块整体停抖) */} {activeLink ? (
{ expect(screen.queryByRole("button", { name: "删除 GitHub" })).toBeNull(); }); + it("编辑模式下链接砖与「+」砖都抖、相位各自错开;退出后一个都不留", async () => { + const user = userEvent.setup(); + const { container } = renderPage(); + await enterEdit(user); + + const tiles = [ + screen.getByRole("button", { name: "GitHub" }), + screen.getByRole("button", { name: "MDN" }), + screen.getByRole("button", { name: "向「开发」新增链接" }), + // 「空」分类只有这块「+」砖:它空/非空都照抖 + screen.getByRole("button", { name: "向「空」新增链接" }), + ]; + const delays = new Set(); + for (const tile of tiles) { + expect(tile).toHaveClass("animate-nav-tile-jiggle"); + expect(tile.style.animationDelay).toMatch(/^-\d+ms$/); + expect(["160ms", "180ms", "200ms"]).toContain( + tile.style.animationDuration, + ); + delays.add(tile.style.animationDelay); + } + // 不得整齐划一地同相位摆动 + expect(delays.size).toBeGreaterThan(1); + + await user.click(screen.getByRole("button", { name: "完成编辑" })); + + // 普通模式(外跳链接)一个抖动类都不带 + expect(container.querySelectorAll(".animate-nav-tile-jiggle")).toHaveLength( + 0, + ); + }); + it("Esc 退出编辑模式", async () => { const user = userEvent.setup(); renderPage(); diff --git a/web/test/tile-jiggle.test.ts b/web/test/tile-jiggle.test.ts new file mode 100644 index 0000000..608e8bc --- /dev/null +++ b/web/test/tile-jiggle.test.ts @@ -0,0 +1,52 @@ +import { describe, expect, it } from "vitest"; +import { + TILE_JIGGLE_CLASS, + tileJiggleStyle, +} from "@/components/home-edit-board"; + +/** + * 编辑模式砖块抖动的相位/周期(home-edit-board.tsx 的 `tileJiggleStyle`)。 + * + * 规格:每块砖的相位/周期要错开(不得整齐划一同相位摆动),但同一块砖必须稳定 + * —— 用 id 的散列而不是 Math.random,重渲染才不会把动画拽回起点。 + */ + +/** 与实现同口径的六个 key:三个链接 id + 三块「+」砖(含空分类那块) */ +const KEYS = ["l1", "l2", "l3", "add:c-dev", "add:c-empty", "add:c-work"]; + +/** `-180ms` → 180(毫秒数,延迟本身取负) */ +function delayMs(style: { animationDelay?: string | number }): number { + const raw = String(style.animationDelay); + expect(raw.startsWith("-")).toBe(true); + expect(raw.endsWith("ms")).toBe(true); + return Number(raw.slice(1, -2)); +} + +describe("tileJiggleStyle:每块砖的抖动相位与周期", () => { + it("同一个 key 每次算出来都一样(重渲染不重启动画)", () => { + for (const key of KEYS) { + expect(tileJiggleStyle(key)).toEqual(tileJiggleStyle(key)); + } + }); + + it("延迟是负值、落在 0–399ms;时长只取 160 / 180 / 200 三档", () => { + for (const key of KEYS) { + const style = tileJiggleStyle(key); + const delay = delayMs(style); + expect(delay).toBeGreaterThanOrEqual(0); + expect(delay).toBeLessThan(400); + expect(["160ms", "180ms", "200ms"]).toContain( + String(style.animationDuration), + ); + } + }); + + it("六块砖的相位错开:延迟去重后至少三个值", () => { + const delays = new Set(KEYS.map((key) => delayMs(tileJiggleStyle(key)))); + expect(delays.size).toBeGreaterThanOrEqual(3); + }); + + it("工具类是完整字面量(Tailwind 只扫字面量)", () => { + expect(TILE_JIGGLE_CLASS).toBe("animate-nav-tile-jiggle"); + }); +});