Files
apes-sidebar/tests/tasks-view-glide.spec.ts
T
Menghuan1918 d035836a51 feat(tasks): 自动适配改为滑行——展开后仍保持自适应居中,且不再瞬跳
维护者第三轮反馈:在 fit(自适应)档位下展开后,画面也应回到自适应位置,
避免节点溢出面板 / 不在中间。上一版让折叠「接管视图」正是拿这个换的平滑。

收口方式:把视图变换本身也做成补间。

- 新增 tasks-view-glide.ts:把 fit 的算术抽成纯函数 fitTransform(用 CONTENT 盒而
  非含 padding 的整张画布定缩放、双轴居中、容器未测量时返回 undefined),外加
  lerpTransform 与 GLIDE_MS。这两块在 jsdom 里可达,因此有了单测(居中、上下限、
  未测量、滑行中点)。
- TasksGraph:fit 拆成 fitTarget(算术)+ applyFit(animate)。布局变化且视图仍属自动
  适配时→**滑行**(rAF 220ms,与卡片补间同一条缓动);切会话→直接落位(内容整体
  换掉);⌂ / 背景双击→滑回。任何读者手势(pointerdown / 滚轮 / 缩放按钮)立刻取消
  滑行,并照旧把视图标记为「已接管」。
- 折叠不再接管视图(去掉上一版的 markTouched 包装):展开即滑到新取景,画面保持
  居中且内容完整。

测试:tests/tasks-view-glide.spec.ts(4 条:居中与缩放上限/下限、未测量返回 undefined、
滑行插值)与 tests/tasks-motion.spec.ts 第 4 条改写(布局变化必须滑行、切会话必须落位、
手势必须能取消)。全套 2252 passed / 17 skipped(194 文件)、tsc 与 eslint 零告警、
构建通过;新构建已装进 sidebar-test,:3080 挂载正常(0 pageerror)。
2026-10-11 23:35:52 +08:00

65 lines
2.3 KiB
TypeScript
Raw 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.
/**
* Unit tests for the canvas's auto-fit arithmetic and its glide — the two
* pieces of the view transform that a jsdom test can reach (a container there
* measures 0×0, so the graph's own `fit` cannot be exercised).
*/
import { describe, expect, it } from 'vitest'
import {
fitTransform, lerpTransform, type FitInput,
} from '../src/client/tasks-view-glide.ts'
/** The graph's own fit inputs, centred on a small tree in a narrow panel. */
function input(over: Partial<FitInput> = {}): FitInput {
return {
containerW: 360,
containerH: 600,
layoutW: 430,
layoutH: 260,
pad: 16,
minContentW: 190,
minContentH: 87,
margin: 24,
minScale: 0.78,
maxScale: 1.3,
...over,
}
}
describe('fitTransform', () => {
it('scales to the CONTENT box and centres it on both axes', () => {
const transform = fitTransform(input())
expect(transform).toBeDefined()
const { x, y, k } = transform!
// Content = 430-32 × 260-32 = 398 × 228; the panel is 360 wide, so the
// WIDTH is what limits the scale (the height would allow 2.4×).
expect(k).toBeCloseTo((360 - 48) / 398, 6)
expect(x).toBeCloseTo((360 - 398 * k) / 2 - 16 * k, 6)
expect(y).toBeCloseTo(Math.max(12, (600 - 228 * k) / 2) - 16 * k, 6)
// …and the content really is inside the panel and centred: equal margins.
const leftEdge = x + 16 * k
const rightEdge = x + 16 * k + 398 * k
expect(360 - rightEdge).toBeCloseTo(leftEdge, 6)
})
it('never balloons a small tree past the cap, and never shrinks below the floor', () => {
expect(fitTransform(input({ layoutW: 200, layoutH: 100 }))?.k).toBe(1.3)
expect(fitTransform(input({ layoutW: 4_000, layoutH: 4_000 }))?.k).toBe(0.78)
})
it('answers nothing before the container has been measured', () => {
// The sidebar mounts hidden at zero size: a fit then must not divide by it.
expect(fitTransform(input({ containerW: 0 }))).toBeUndefined()
expect(fitTransform(input({ containerH: 0 }))).toBeUndefined()
})
})
describe('lerpTransform', () => {
it('walks the frame between two transforms', () => {
const from = { x: 0, y: 0, k: 1 }
const to = { x: 100, y: -40, k: 0.5 }
expect(lerpTransform(from, to, 0)).toEqual(from)
expect(lerpTransform(from, to, 1)).toEqual(to)
expect(lerpTransform(from, to, 0.5)).toEqual({ x: 50, y: -20, k: 0.75 })
})
})