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)。
This commit is contained in:
1 parent
e6f9eda60f
commit
d035836a51
6 files changed
+254
-73
No files matched your search
+91
-62
@@ -41,6 +41,15 @@ import {
|
||||
type GraphBox,
|
||||
} from './tasks-graph-layout.ts'
|
||||
import { useTweenedLayout } from './tasks-layout-tween.ts'
|
||||
import { easeOut as glideEase } from './tasks-layout-tween.ts'
|
||||
import { fitTransform, GLIDE_MS, lerpTransform, type ViewTransform } from './tasks-view-glide.ts'
|
||||
|
||||
/** Whether the reader asked the platform for less motion. */
|
||||
function prefersReducedMotion(): boolean {
|
||||
return typeof window !== 'undefined'
|
||||
&& typeof window.matchMedia === 'function'
|
||||
&& window.matchMedia('(prefers-reduced-motion: reduce)').matches
|
||||
}
|
||||
import {
|
||||
activityLine, agentChip, agentMeta, foldErrorTally, foldPreviews, FoldGlyph, foldTally,
|
||||
TaskLine, WorkflowGlyph, workflowMeta,
|
||||
@@ -223,35 +232,69 @@ export function TasksGraph(props: TasksGraphProps): ReactNode {
|
||||
const edges = useMemo(() => tasksEdges(nodes), [nodes])
|
||||
const nodeById = useMemo(() => new Map(nodes.map(node => [node.id, node])), [nodes])
|
||||
|
||||
/** A running view glide (see fit): cancelled by any reader gesture. */
|
||||
const glideRef = useRef(0)
|
||||
|
||||
/** Stop a glide where it is (the reader took the wheel). */
|
||||
const stopGlide = useCallback((): void => {
|
||||
if (glideRef.current !== 0) {
|
||||
cancelAnimationFrame(glideRef.current)
|
||||
glideRef.current = 0
|
||||
}
|
||||
}, [])
|
||||
|
||||
/**
|
||||
* Center the canvas and scale it to fill. The fit measures the CONTENT box
|
||||
* (the layout's padding is breathing room, not content): centering the padded
|
||||
* box leaves the graph visibly off-center in a narrow panel, which is exactly
|
||||
* how "the graph looks small and sits in a corner" happens.
|
||||
* The frame the auto-fit wants right now, or undefined before the container
|
||||
* has been measured (the sidebar mounts hidden at zero size). The arithmetic
|
||||
* lives in tasks-view-glide.ts, where a test can reach it.
|
||||
*/
|
||||
const fit = useCallback((): void => {
|
||||
const fitTarget = useCallback((): ViewTransform | undefined => {
|
||||
const container = containerRef.current
|
||||
if (container === null) return
|
||||
const cw = container.clientWidth
|
||||
const ch = container.clientHeight
|
||||
if (cw === 0 || ch === 0) return
|
||||
const contentW = Math.max(GRAPH_NODE_W, layout.width - GRAPH_PAD * 2)
|
||||
const contentH = Math.max(GRAPH_NODE_H, layout.height - GRAPH_PAD * 2)
|
||||
const k = Math.max(
|
||||
GRAPH_FIT_MIN_SCALE,
|
||||
Math.min(
|
||||
(cw - FIT_MARGIN * 2) / contentW,
|
||||
(ch - FIT_MARGIN * 2) / contentH,
|
||||
FIT_MAX_SCALE,
|
||||
),
|
||||
)
|
||||
setTf({
|
||||
x: (cw - contentW * k) / 2 - GRAPH_PAD * k,
|
||||
y: Math.max(FIT_MARGIN / 2, (ch - contentH * k) / 2) - GRAPH_PAD * k,
|
||||
k,
|
||||
if (container === null) return undefined
|
||||
return fitTransform({
|
||||
containerW: container.clientWidth,
|
||||
containerH: container.clientHeight,
|
||||
layoutW: layout.width,
|
||||
layoutH: layout.height,
|
||||
pad: GRAPH_PAD,
|
||||
minContentW: GRAPH_NODE_W,
|
||||
minContentH: GRAPH_NODE_H,
|
||||
margin: FIT_MARGIN,
|
||||
minScale: GRAPH_FIT_MIN_SCALE,
|
||||
maxScale: FIT_MAX_SCALE,
|
||||
})
|
||||
}, [layout.width, layout.height])
|
||||
|
||||
/**
|
||||
* Put the frame where the fit wants it, either at once (a resize while the
|
||||
* reader drags the panel edge, a fresh session) or as a GLIDE — the layout
|
||||
* tween moves the cards, and the frame has to travel with them instead of
|
||||
* jumping to the new framing a frame later. That glide is what makes "stay
|
||||
* auto-fitted" and "never yank the view" the same behavior.
|
||||
*
|
||||
* @param animate - glide instead of snapping.
|
||||
*/
|
||||
const applyFit = useCallback((animate: boolean): void => {
|
||||
const target = fitTarget()
|
||||
if (target === undefined) return
|
||||
stopGlide()
|
||||
if (!animate || prefersReducedMotion()) {
|
||||
setTf(target)
|
||||
return
|
||||
}
|
||||
const from = tfRef.current
|
||||
const started = performance.now()
|
||||
const step = (): void => {
|
||||
const progress = glideEase((performance.now() - started) / GLIDE_MS)
|
||||
setTf(lerpTransform(from, target, progress))
|
||||
glideRef.current = progress < 1 ? requestAnimationFrame(step) : 0
|
||||
}
|
||||
glideRef.current = requestAnimationFrame(step)
|
||||
}, [fitTarget, stopGlide])
|
||||
|
||||
/** The reader's own re-fit (the ⌂ button and a background double-click). */
|
||||
const fit = useCallback((): void => { applyFit(true) }, [applyFit])
|
||||
|
||||
/**
|
||||
* Keep the canvas reachable: a pan or a zoom-out may never push the whole
|
||||
* graph off screen (the classic "I dragged it away and cannot find it").
|
||||
@@ -281,22 +324,24 @@ export function TasksGraph(props: TasksGraphProps): ReactNode {
|
||||
fit()
|
||||
}, [fit])
|
||||
|
||||
/** The newest `fit`, so the effects below can depend on WHAT changed. */
|
||||
const fitRef = useRef(fit)
|
||||
fitRef.current = fit
|
||||
/** The newest `applyFit`, so the effects below can depend on WHAT changed. */
|
||||
const fitRef = useRef(applyFit)
|
||||
fitRef.current = applyFit
|
||||
|
||||
// A different tree is a different canvas: re-fit AND hand the view back to the
|
||||
// auto-fit, whatever the reader did to the previous one.
|
||||
// auto-fit, whatever the reader did to the previous one. Snapped, because the
|
||||
// content changes wholesale.
|
||||
useEffect(() => {
|
||||
touchedRef.current = false
|
||||
fitRef.current()
|
||||
fitRef.current(false)
|
||||
}, [rootId])
|
||||
|
||||
// Auto-fit on layout growth — but never over a view the reader has taken over
|
||||
// (a pan, a zoom, or a fold: see markTouched). The container's own resize
|
||||
// path below fits under the same rule.
|
||||
// Layout growth while the reader has left the view alone: glide to the new
|
||||
// framing, so expanding a group KEEPS the graph centred and inside the panel
|
||||
// without the frame jumping out from under the cards travelling to it. A
|
||||
// reader who panned or zoomed owns the view, and nothing moves it.
|
||||
useEffect(() => {
|
||||
if (!touchedRef.current) fitRef.current()
|
||||
if (!touchedRef.current) fitRef.current(true)
|
||||
}, [layout.width, layout.height])
|
||||
|
||||
useEffect(() => {
|
||||
@@ -328,6 +373,7 @@ export function TasksGraph(props: TasksGraphProps): ReactNode {
|
||||
const next = Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, k * Math.exp(-event.deltaY * unit * 0.0014)))
|
||||
if (next === k) return
|
||||
touchedRef.current = true
|
||||
stopGlide()
|
||||
setTf(clampTf({
|
||||
x: mx - (mx - x) * (next / k),
|
||||
y: my - (my - y) * (next / k),
|
||||
@@ -336,7 +382,7 @@ export function TasksGraph(props: TasksGraphProps): ReactNode {
|
||||
}
|
||||
container.addEventListener('wheel', onWheel, { passive: false })
|
||||
return () => { container.removeEventListener('wheel', onWheel) }
|
||||
}, [clampTf])
|
||||
}, [clampTf, stopGlide])
|
||||
|
||||
const zoomBy = useCallback((factor: number): void => {
|
||||
const container = containerRef.current
|
||||
@@ -344,10 +390,11 @@ export function TasksGraph(props: TasksGraphProps): ReactNode {
|
||||
const next = Math.min(ZOOM_MAX, Math.max(ZOOM_MIN, k * factor))
|
||||
if (next === k || container === null) return
|
||||
touchedRef.current = true
|
||||
stopGlide()
|
||||
const mx = container.clientWidth / 2
|
||||
const my = container.clientHeight / 2
|
||||
setTf(clampTf({ x: mx - (mx - x) * (next / k), y: my - (my - y) * (next / k), k: next }))
|
||||
}, [clampTf])
|
||||
}, [clampTf, stopGlide])
|
||||
|
||||
/**
|
||||
* Drag-pan. The gesture only ever starts on the BACKGROUND: a pointerdown
|
||||
@@ -370,6 +417,7 @@ export function TasksGraph(props: TasksGraphProps): ReactNode {
|
||||
|
||||
const onPointerDown = useCallback((event: ReactPointerEvent<HTMLDivElement>): void => {
|
||||
if (event.button !== 0) return
|
||||
stopGlide()
|
||||
const target = event.target as HTMLElement
|
||||
if (target.closest('[data-graph-node]') !== null) return
|
||||
if (target.closest('[data-graph-controls]') !== null) return
|
||||
@@ -395,36 +443,17 @@ export function TasksGraph(props: TasksGraphProps): ReactNode {
|
||||
window.addEventListener('pointermove', onMove)
|
||||
window.addEventListener('pointerup', onUp)
|
||||
window.addEventListener('pointercancel', onUp)
|
||||
}, [clampTf])
|
||||
}, [clampTf, stopGlide])
|
||||
|
||||
/** Suppress node clicks that ended a drag gesture. */
|
||||
const clickAllowed = useCallback((): boolean => travelRef.current <= CLICK_TOLERANCE_PX, [])
|
||||
|
||||
/**
|
||||
* The reader driving the FOLD state takes the view over, exactly like a pan
|
||||
* or a zoom does: from then on the canvas stops re-framing itself when the
|
||||
* layout changes, so folding a group never yanks the tree out from under the
|
||||
* cards the reader is watching. The ⌂ button hands the view back.
|
||||
/*
|
||||
* Folding does NOT claim the view. The frame stays auto-fitted for as long as
|
||||
* the reader has not panned or zoomed, and the fit GLIDES (applyFit), so a
|
||||
* group that expands larger than the panel stays centred and inside it
|
||||
* without the jump that claiming the view used to trade for.
|
||||
*/
|
||||
const markTouched = useCallback((): void => { touchedRef.current = true }, [])
|
||||
|
||||
/** The aggregate's click: reveal its group, with the view under the reader. */
|
||||
const expandGroupTouched = useCallback((node: TasksFoldNode): void => {
|
||||
markTouched()
|
||||
onExpandGroup(node)
|
||||
}, [markTouched, onExpandGroup])
|
||||
|
||||
/** The card's own chevron: fold that node, with the view under the reader. */
|
||||
const foldNodeTouched = useCallback((node: TasksAgentNode): void => {
|
||||
markTouched()
|
||||
onFoldNode?.(node)
|
||||
}, [markTouched, onFoldNode])
|
||||
|
||||
/** The cluster's page-level switch: the same claim on the view. */
|
||||
const toggleFoldTouched = useCallback((): void => {
|
||||
markTouched()
|
||||
onToggleFold()
|
||||
}, [markTouched, onToggleFold])
|
||||
|
||||
// Phase frames: group each run's members by phase and box them.
|
||||
const phaseFrames = useMemo((): PhaseFrame[] => {
|
||||
@@ -561,16 +590,16 @@ export function TasksGraph(props: TasksGraphProps): ReactNode {
|
||||
*/
|
||||
animationDelay: `${Math.min(index, 9) * 26}ms`,
|
||||
}
|
||||
if (node.kind === 'fold') return renderFoldNode(node, style, expandGroupTouched, clickAllowed)
|
||||
if (node.kind === 'fold') return renderFoldNode(node, style, onExpandGroup, clickAllowed)
|
||||
if (node.kind === 'workflow') {
|
||||
return renderWorkflowNode(node, style, onWorkflowInfo, clickAllowed)
|
||||
}
|
||||
return renderAgentNode(node, style, onNodeInfo, onOpenTask, clickAllowed, foldNodeTouched)
|
||||
return renderAgentNode(node, style, onNodeInfo, onOpenTask, clickAllowed, onFoldNode)
|
||||
})}
|
||||
</div>
|
||||
<div className={css.controls} data-graph-controls>
|
||||
<ViewModeToggle mode={mode} onModeChange={onModeChange} />
|
||||
<FoldToggleButton folded={folded} onToggleFold={toggleFoldTouched} />
|
||||
<FoldToggleButton folded={folded} onToggleFold={onToggleFold} />
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* The Tasks canvas's VIEW transforms: where the auto-fit puts the frame, and
|
||||
* how the frame travels when that answer changes.
|
||||
*
|
||||
* Kept pure and separate because the graph itself cannot be measured in a test
|
||||
* (jsdom reports every container as 0×0): the centring rule and the glide's
|
||||
* interpolation are the two things worth pinning, and both are just arithmetic
|
||||
* over numbers the caller already has.
|
||||
*
|
||||
* The rule the fit follows: scale so the CONTENT box (never the padded canvas)
|
||||
* fills the container, never below the readability floor and never above the
|
||||
* cap, then centre it on both axes. Centring the padded box is what makes a
|
||||
* narrow panel look off-centre, so the padding is subtracted before the scale
|
||||
* is chosen and folded back into the offset.
|
||||
*/
|
||||
/** One canvas transform: translation in px plus the scale. */
|
||||
export interface ViewTransform {
|
||||
x: number
|
||||
y: number
|
||||
k: number
|
||||
}
|
||||
|
||||
/** How long the frame takes to glide to a new auto-fit (ms). */
|
||||
export const GLIDE_MS = 220
|
||||
|
||||
/** The inputs of the auto-fit (all px, pre-scale). */
|
||||
export interface FitInput {
|
||||
/** The canvas viewport. */
|
||||
containerW: number
|
||||
containerH: number
|
||||
/** The layout's extent, padding included. */
|
||||
layoutW: number
|
||||
layoutH: number
|
||||
/** The layout's own padding (breathing room, not content). */
|
||||
pad: number
|
||||
/** The smallest content box the fit will measure (one card). */
|
||||
minContentW: number
|
||||
minContentH: number
|
||||
/** Breathing room the fit leaves around the content, per axis. */
|
||||
margin: number
|
||||
/** The readability floor and the ballooning cap. */
|
||||
minScale: number
|
||||
maxScale: number
|
||||
}
|
||||
|
||||
/**
|
||||
* The transform that fits and centres the content, or undefined when the
|
||||
* container has no size yet (the sidebar mounts hidden at zero size, so a fit
|
||||
* before its first measure must do nothing rather than divide by zero).
|
||||
*/
|
||||
export function fitTransform(input: FitInput): ViewTransform | undefined {
|
||||
const { containerW, containerH } = input
|
||||
if (containerW === 0 || containerH === 0) return undefined
|
||||
const contentW = Math.max(input.minContentW, input.layoutW - input.pad * 2)
|
||||
const contentH = Math.max(input.minContentH, input.layoutH - input.pad * 2)
|
||||
const k = Math.max(
|
||||
input.minScale,
|
||||
Math.min(
|
||||
(containerW - input.margin * 2) / contentW,
|
||||
(containerH - input.margin * 2) / contentH,
|
||||
input.maxScale,
|
||||
),
|
||||
)
|
||||
return {
|
||||
x: (containerW - contentW * k) / 2 - input.pad * k,
|
||||
y: Math.max(input.margin / 2, (containerH - contentH * k) / 2) - input.pad * k,
|
||||
k,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* One frame of a glide between two transforms.
|
||||
*
|
||||
* @param from - where the frame is.
|
||||
* @param to - where it is going.
|
||||
* @param progress - 0…1, already eased by the caller.
|
||||
*/
|
||||
export function lerpTransform(from: ViewTransform, to: ViewTransform, progress: number): ViewTransform {
|
||||
const mix = (a: number, b: number): number => a + (b - a) * progress
|
||||
return { x: mix(from.x, to.x), y: mix(from.y, to.y), k: mix(from.k, to.k) }
|
||||
}
|
||||
Reference in new issue
Block a user