Files
apes-sidebar/tests/tasks-motion.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

93 lines
4.5 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.
/**
* The Tasks page's motion contract, pinned where a refactor would break it
* silently:
*
* - the CANVAS must not run a FLIP library. Its card layer carries the fit's
* `transform: scale(k)`, and `@formkit/auto-animate` pins a leaving card with
* absolute `left/top` from `getBoundingClientRect()` (a box a scaled layer
* cannot express — the card jumps) and translates movers in unscaled units
* (k× the true distance). Attached there, it was reported as "折叠展开的动效
* 都特别怪"; the canvas uses the CSS entrance on `.node` instead.
* - the FLOW surfaces (tree rows, jobs rows) are where FLIP belongs, and they
* keep it.
*/
import { describe, expect, it } from 'vitest'
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
const ROOT = resolve(import.meta.dirname, '..')
/** One source file of the client half. */
function source(relative: string): string {
return readFileSync(resolve(ROOT, relative), 'utf8')
}
/**
* A source file with its comments removed. The NEGATIVE assertions below are
* about what the code does, and these files deliberately DOCUMENT the library
* they avoid ("measures `getBoundingClientRect()`…"), so matching raw text
* would fail on the very explanation we want to keep.
*/
function code(relative: string): string {
return source(relative)
.replace(/\/\*[\s\S]*?\*\//g, '')
.replace(/^\s*\/\/.*$/gm, '')
}
describe('Tasks page motion', () => {
it('keeps the FLIP library off the scaled canvas, and on the flow surfaces', () => {
expect(code('src/client/TasksGraph.tsx')).not.toContain('use-auto-animate')
expect(source('src/client/TasksTree.tsx')).toContain('useAutoAnimateRef')
expect(source('src/client/JobsDrawer.tsx')).toContain('useAutoAnimateRef')
})
it('gives canvas cards a mount-only CSS entrance that respects reduced motion', () => {
const styles = source('src/client/tasks-graph.module.css')
const node = /\.node \{([\s\S]*?)\n\}/.exec(styles)?.[1]
if (node === undefined) throw new Error('the .node rule must exist')
expect(node).toContain('animation: dsh-card-in')
const frames = /@keyframes dsh-card-in \{([\s\S]*?)\n\}/.exec(styles)?.[1]
if (frames === undefined) throw new Error('the card entrance keyframes must exist')
expect(frames).toContain('opacity: 0')
// Every reduced-motion block in the sheet is a candidate — the sweep has
// its own — and the card entrance must be switched off in one of them.
const reduced = [...styles.matchAll(/@media \(prefers-reduced-motion: reduce\) \{([\s\S]*?)\n\}/g)]
.map(match => match[1])
.join('\n')
expect(reduced).toContain('.node')
})
it('animates the canvas in LAYOUT coordinates, never with screen-space transforms', () => {
// The canvas's own tween: `left`/`top` are canvas units, so the fit's scale
// applies to the tween exactly as it applies to the final positions — and
// the connectors (pure functions of the boxes) travel with the cards.
expect(source('src/client/TasksGraph.tsx')).toContain('useTweenedLayout')
const tween = code('src/client/tasks-layout-tween.ts')
expect(tween).toContain('requestAnimationFrame')
// No rect measuring in the CODE: that is exactly what makes a scaled layer
// unrepresentable for a FLIP library.
expect(tween).not.toContain('getBoundingClientRect')
// The card entrance is staggered, so a revealed group cascades.
expect(source('src/client/TasksGraph.tsx')).toContain('animationDelay')
})
it('keeps the frame auto-fitted across a fold, by GLIDING rather than jumping', () => {
const graph = code('src/client/TasksGraph.tsx')
// Folding must not claim the view: while the reader has not taken over, a
// layout change glides the frame to the new fit, so an expanded group stays
// centred and inside the panel without yanking the view.
expect(graph).not.toContain('markTouched')
// The untouched-view path GLIDES to the new framing; a fresh session snaps
// (the content changes wholesale), and the reader's own gestures cancel a
// glide in flight.
expect(graph).toContain('if (!touchedRef.current) fitRef.current(true)')
expect(graph).toContain('fitRef.current(false)')
expect(graph).toContain('stopGlide()')
})
it('parks the motion host behind a WAAPI guard so tests and old hosts stay inert', () => {
const hook = source('src/client/use-auto-animate.ts')
expect(hook).toContain("typeof Element.prototype.animate === 'function'")
})
})