Files
apes-sidebar/tests/tasks-layout-tween.spec.tsx
T
Menghuan1918 e6f9eda60f feat(tasks): 画布动效改为布局坐标补间 + 错峰入场,FLIP 库只留流式布局
维护者第二轮反馈「还是突然直接出现」。上一版把 FLIP 库从画布摘掉后只留了
150ms 淡入 —— 太弱,看不出是「入场」。而且这个面本身需要的是**位置**动效,
不是入场动效,于是补上画布自己的补间:

- tasks-layout-tween.ts:rAF 逐帧插值每张卡在**画布坐标**里的 x / y / 高度。
  为什么必须是坐标而不是 transform:卡片层带 fit 的 scale(k),left/top 是画布
  单位,缩放对它和对终点位置一样生效;量 rect 再播未缩放 translate 的 FLIP 在
  这里恒差 k 倍,auto-animate 还会把离场卡片钉上无法表达的绝对 left/top。
  fromRef 跟着「已绘制」状态走 —— 中途再次变布局时接着当前位置走,不弹回。
- 连线路径与相位框改为读补间后的盒子(它们本就是盒子的纯函数),于是线跟着
  卡片走,不再在卡片还在飞的时候先瞬移到终点。
- 入场改为 300ms 上浮 + 0.88→1 缩放 + 淡入(轻微过冲),按节点页序错峰 26ms,
  fill-mode: backwards 让等待中的卡片先隐形;reduced-motion 下补间与入场都关。
  修掉首版的一帧空档:新入场的节点在引入它的那一帧就带目标盒子渲染。
- 读者一碰折叠即接管视图(聚合卡 / 卡片 chevron / 控制条开关 → markTouched),
  此后画布不再随布局自动重框;切会话重置并重新适配(顺带修掉换会话不适配的旧账)。
- FLIP 库保留在树视图与任务抽屉(那里离场是原地淡出 + 兄弟滑位的流式语义)。

守护:tests/tasks-layout-tween.spec.tsx(插值 / 缓动 / 中途位置 / 画布尺寸不中途
变化 / 入口盒子不缺席)与 tests/tasks-motion.spec.ts 扩到 5 条(画布不得 import
FLIP 库、必须在画布坐标补间、入场必须错峰、折叠必须接管视图)。

全套 2248 passed / 17 skipped(193 文件)、tsc 与 eslint 0 错误、构建通过;
新构建已装进 sidebar-test,:3080 挂载正常(0 pageerror)。
2026-10-11 23:29:51 +08:00

116 lines
4.2 KiB
TypeScript

/**
* Unit tests for the canvas layout tween: the geometry interpolation, the
* easing, and the hook that hides every screen-space FLIP problem by animating
* in the canvas's OWN coordinates.
*/
// @vitest-environment jsdom
import { afterEach, describe, expect, it, vi } from 'vitest'
import { createElement, useState, type ReactNode } from 'react'
import { act } from 'react-dom/test-utils'
import { renderRoot } from './test-utils.ts'
import { TWEEN_MS, easeOut, tweenBox, useTweenedLayout } from '../src/client/tasks-layout-tween.ts'
import type { GraphBox, GraphLayout } from '../src/client/tasks-graph-layout.ts'
/** One box. */
function box(id: string, x: number, y: number, h = 87): GraphBox {
return { id, x, y, w: 190, h }
}
/** One layout. */
function layout(boxes: GraphBox[]): GraphLayout {
return { boxes: new Map(boxes.map(entry => [entry.id, entry])), width: 400, height: 300 }
}
afterEach(() => {
vi.useRealTimers()
for (const el of document.querySelectorAll('body > div')) el.remove()
})
describe('tweenBox', () => {
it('interpolates every geometry channel', () => {
const from = box('a', 0, 0, 80)
const to = box('a', 100, 40, 120)
expect(tweenBox(from, to, 0)).toMatchObject({ x: 0, y: 0, h: 80 })
expect(tweenBox(from, to, 1)).toMatchObject({ x: 100, y: 40, h: 120 })
expect(tweenBox(from, to, 0.5)).toMatchObject({ x: 50, y: 20, h: 100 })
})
it('leaves an ENTERING box at its target (the entrance animation owns that moment)', () => {
const to = box('fresh', 42, 7)
expect(tweenBox(undefined, to, 0.3)).toBe(to)
})
})
describe('easeOut', () => {
it('runs fast out of the gate and settles exactly at the ends', () => {
expect(easeOut(0)).toBe(0)
expect(easeOut(1)).toBe(1)
// Ease-out: past the halfway position halfway through the time.
expect(easeOut(0.5)).toBeGreaterThan(0.5)
expect(easeOut(0.9)).toBeGreaterThan(easeOut(0.8))
// Out-of-range progress is clamped (a late frame cannot overshoot).
expect(easeOut(1.4)).toBe(1)
expect(easeOut(-1)).toBe(0)
})
})
/** Renders the tweened layout and lets a test swap the target layout. */
function Harness(props: { initial: GraphLayout; onDrawn?: (layout: GraphLayout) => void }): ReactNode {
const [target, setTarget] = useState(props.initial)
const drawn = useTweenedLayout(target)
props.onDrawn?.(drawn)
swap = setTarget
return createElement('span', null, `${drawn.boxes.size}`)
}
/** The harness's setter, captured by the render above (one instance per test). */
let swap: (next: GraphLayout) => void = () => {}
describe('useTweenedLayout', () => {
it('shows the target on the first paint, then travels to the next layout', async () => {
vi.useFakeTimers()
let drawn: GraphLayout = layout([])
const { unmount } = renderRoot(
createElement(Harness, {
initial: layout([box('a', 0, 0)]),
onDrawn: (value) => { drawn = value },
}),
)
// First paint: no animation, the cards are where the layout put them.
expect(drawn.boxes.get('a')?.x).toBe(0)
await act(async () => { swap(layout([box('a', 100, 0)])) })
// One frame in, the card is on its way — not teleported, not waiting.
await act(async () => { await vi.advanceTimersByTimeAsync(16) })
const midway = drawn.boxes.get('a')?.x ?? 0
expect(midway).toBeGreaterThan(0)
expect(midway).toBeLessThan(100)
await act(async () => { await vi.advanceTimersByTimeAsync(TWEEN_MS) })
expect(drawn.boxes.get('a')?.x).toBe(100)
unmount()
})
it('keeps the canvas extent at the target while the cards travel', async () => {
vi.useFakeTimers()
let drawn: GraphLayout = layout([])
const { unmount } = renderRoot(
createElement(Harness, {
initial: layout([box('a', 0, 0)]),
onDrawn: (value) => { drawn = value },
}),
)
const next = layout([box('a', 0, 0)])
next.width = 900
next.height = 700
await act(async () => { swap(next) })
await act(async () => { await vi.advanceTimersByTimeAsync(16) })
// The frame must not resize mid-flight (that would move the reader's view
// under a tween that is still running).
expect(drawn.width).toBe(900)
expect(drawn.height).toBe(700)
expect(drawn.boxes.get('a')?.x).toBe(0)
unmount()
})
})