From af009cf3c588cfe91873fb49c95ea89ff4b14203 Mon Sep 17 00:00:00 2001 From: Orion Date: Tue, 15 Sep 2026 11:25:43 +0800 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20feat(core):=20=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E8=AF=A6=E6=83=85=E9=9D=A2=E6=9D=BF=E5=8A=A8=E7=94=BB=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=B9=B6=E5=AE=8C=E5=96=84=E7=9B=B8=E5=85=B3=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 新增 detailMotion 工具类以支持更精细的动画控制 * 优化 FileManageView 面板渲染逻辑,改用 v-show 提升交互性能 * 调整 Transition 组件为手动控制模式以适配自定义动画 * 在 package.json 中配置自动化测试脚本 * 补充 detailMotion 相关的单元测试与测试说明文档 --- package.json | 2 + src/utils/detailMotion.ts | 30 ++++ src/views/manage/FileManageView.vue | 234 ++++++++++++---------------- tests/README.md | 26 ++++ tests/detailMotion.browser.mjs | 77 +++++++++ tests/detailMotion.test.mjs | 76 +++++++++ 6 files changed, 307 insertions(+), 138 deletions(-) create mode 100644 src/utils/detailMotion.ts create mode 100644 tests/README.md create mode 100644 tests/detailMotion.browser.mjs create mode 100644 tests/detailMotion.test.mjs diff --git a/package.json b/package.json index ea88377..e5706ec 100644 --- a/package.json +++ b/package.json @@ -5,6 +5,8 @@ "type": "module", "scripts": { "dev": "vite", + "test:motion": "node --experimental-strip-types --test tests/detailMotion.test.mjs", + "test:motion:coverage": "node --experimental-strip-types --test --experimental-test-coverage --test-coverage-include=src/utils/detailMotion.ts tests/detailMotion.test.mjs", "build": "run-s check:architecture type-check build-only", "preview": "vite preview", "build-only": "vite build", diff --git a/src/utils/detailMotion.ts b/src/utils/detailMotion.ts new file mode 100644 index 0000000..b946c22 --- /dev/null +++ b/src/utils/detailMotion.ts @@ -0,0 +1,30 @@ +/** Own only detail animations; release filled effects before returning to auto layout. */ +export function createDetailMotion(reducedMotion: () => boolean) { + const active = new Map void>() + const cancel = (element: HTMLElement) => active.get(element)?.() + const play = ( + element: HTMLElement, + keyframes: Keyframe[], + options: KeyframeAnimationOptions, + done: () => void + ) => { + cancel(element) + if (reducedMotion()) { + done() + return + } + const animation = element.animate(keyframes, { fill: 'both', ...options }) + const settle = () => { + animation.removeEventListener('finish', settle) + animation.removeEventListener('cancel', settle) + active.delete(element) + animation.cancel() + done() + } + active.set(element, settle) + animation.addEventListener('finish', settle, { once: true }) + animation.addEventListener('cancel', settle, { once: true }) + } + const dispose = () => [...active.values()].forEach(settle => settle()) + return { play, cancel, dispose } +} diff --git a/src/views/manage/FileManageView.vue b/src/views/manage/FileManageView.vue index 94203ee..a7e3bb1 100644 --- a/src/views/manage/FileManageView.vue +++ b/src/views/manage/FileManageView.vue @@ -71,8 +71,8 @@ - -
+ +
-
+
+

{{ event.displayDescription }}

+
@@ -1751,6 +1753,7 @@ import BaseModal from '@/components/common/BaseModal.vue' import BaseButton from '@/components/common/BaseButton.vue' import { useAdminFiles, useInjectedDarkMode, useTransferAnalytics } from '@/composables' import { formatFileSize, formatTimestamp } from '@/utils/common' +import { createDetailMotion } from '@/utils/detailMotion' const { t } = useI18n() const isDarkMode = useInjectedDarkMode() @@ -1859,9 +1862,8 @@ const syncMobileViewport = (event: MediaQueryListEvent) => { isMobileViewport.value = event.matches } const isLifecycleExpanded = ref(false) -const lifecycleListRef = ref(null) const lifecycleViewportRef = ref(null) -let lifecycleHeightAnimation: Animation | null = null +let lifecycleHeightRevision = 0 const isRetrievalHistoryExpanded = ref(false) const retrievalTimelineEvents = computed(() => (selectedFileDetail.value?.detailTimeline || []).filter((item) => item.key === 'retrieved_event') @@ -1875,55 +1877,37 @@ const visibleDetailTimeline = computed(() => isLifecycleExpanded.value ? detailTimelineRows.value : detailTimelineRows.value.slice(0, 3) ) -const animateLifecycleHeight = async () => { - await nextTick() - const viewport = lifecycleViewportRef.value - if (!viewport || reducedMotionQuery.matches) return - lifecycleHeightAnimation?.cancel() - lifecycleHeightAnimation = null - viewport.style.height = '' - await nextTick() - const currentHeight = viewport.getBoundingClientRect().height - viewport.style.height = `${currentHeight}px` - await nextTick() - const targetHeight = viewport.scrollHeight - lifecycleHeightAnimation = viewport.animate( - [{ height: `${currentHeight}px` }, { height: `${targetHeight}px` }], - { duration: 420, easing: 'cubic-bezier(0.22, 1, 0.36, 1)', fill: 'forwards' } - ) - const cleanup = () => { - viewport.style.height = '' - lifecycleHeightAnimation = null - } - lifecycleHeightAnimation.addEventListener('finish', cleanup, { once: true }) - lifecycleHeightAnimation.addEventListener('cancel', cleanup, { once: true }) -} - -const LIFECYCLE_ENTER_MS = 160 -const LIFECYCLE_LEAVE_MS = 120 -const lifecycleEasing = 'cubic-bezier(0.22, 1, 0.36, 1)' -const lifecycleExitEasing = 'cubic-bezier(0.4, 0, 1, 1)' -watch(isLifecycleExpanded, animateLifecycleHeight) - const reducedMotionQuery = window.matchMedia('(prefers-reduced-motion: reduce)') +const detailMotion = createDetailMotion(() => reducedMotionQuery.matches) +const playDetailAnimation = detailMotion.play +const PANEL_DURATION_MS = 420 +const lifecycleEasing = 'cubic-bezier(0.32, 0.72, 0, 1)' +const LIFECYCLE_ENTER_MS = 180 +const LIFECYCLE_LEAVE_MS = 120 -const playDetailAnimation = ( - element: HTMLElement, - keyframes: Keyframe[], - options: KeyframeAnimationOptions, - done: () => void -) => { - if (reducedMotionQuery.matches) { - done() - return - } - - element.getAnimations().forEach((animation) => animation.cancel()) - const animation = element.animate(keyframes, { fill: 'both', ...options }) - const finish = () => done() - animation.addEventListener('finish', finish, { once: true }) - animation.addEventListener('cancel', finish, { once: true }) -} +// This watcher runs before Vue patches the rows: capture the visible starting +// height now, then measure the natural list height after leaving rows exit flow. +watch(isLifecycleExpanded, async () => { + const revision = ++lifecycleHeightRevision + const viewport = lifecycleViewportRef.value + if (!viewport) return + const from = viewport.getBoundingClientRect().height + detailMotion.cancel(viewport) + viewport.style.height = `${from}px` + viewport.style.overflow = 'hidden' + await nextTick() + if (revision !== lifecycleHeightRevision || !viewport.isConnected) return + const to = viewport.firstElementChild?.getBoundingClientRect().height ?? 0 + playDetailAnimation( + viewport, + [{ height: `${from}px` }, { height: `${to}px` }], + { duration: PANEL_DURATION_MS, easing: lifecycleEasing }, + () => { + viewport.style.height = '' + viewport.style.overflow = '' + } + ) +}) const beforeLifecycleRowEnter = (element: Element) => { const row = element as HTMLElement @@ -1946,6 +1930,11 @@ const enterLifecycleRow = (element: Element, done: () => void) => { } const leaveLifecycleRow = (element: Element, done: () => void) => { const row = element as HTMLElement + const width = row.getBoundingClientRect().width + const top = row.offsetTop + row.style.position = 'absolute' + row.style.top = `${top}px` + row.style.width = `${width}px` row.style.willChange = 'opacity' playDetailAnimation( row, @@ -1953,86 +1942,66 @@ const leaveLifecycleRow = (element: Element, done: () => void) => { { opacity: 1 }, { opacity: 0 } ], - { duration: LIFECYCLE_LEAVE_MS, easing: lifecycleExitEasing }, + { duration: LIFECYCLE_LEAVE_MS, easing: lifecycleEasing }, () => { row.style.willChange = '' done() } ) } -const beforeExpandPanelEnter = (element: Element) => { - const panel = element as HTMLElement - panel.style.height = '0px' - panel.style.overflow = 'hidden' -} -const enterExpandPanel = (element: Element, done: () => void) => { - const panel = element as HTMLElement - const targetHeight = panel.scrollHeight - playDetailAnimation(panel, [{ height: '0px', opacity: 0 }, { height: `${targetHeight}px`, opacity: 1 }], { duration: 420, easing: 'cubic-bezier(0.22, 1, 0.36, 1)' }, done) -} -const afterExpandPanelEnter = (element: Element) => { - const panel = element as HTMLElement - panel.style.height = '' - panel.style.overflow = '' -} -const beforeExpandPanelLeave = (element: Element) => { - const panel = element as HTMLElement - panel.style.height = `${panel.scrollHeight}px` - panel.style.overflow = 'hidden' -} -const leaveExpandPanel = (element: Element, done: () => void) => { - const panel = element as HTMLElement - const currentHeight = panel.scrollHeight - playDetailAnimation(panel, [{ height: `${currentHeight}px`, opacity: 1 }, { height: '0px', opacity: 0 }], { duration: 320, easing: 'cubic-bezier(0.4, 0, 0.8, 0.2)' }, done) -} - -const beforeRetrievalPanelEnter = (element: Element) => { - const panel = element as HTMLElement - panel.style.height = '0px' - panel.style.overflow = 'clip' - panel.style.willChange = 'height, opacity, transform' -} -const enterRetrievalPanel = (element: Element, done: () => void) => { - const panel = element as HTMLElement - const height = panel.scrollHeight - playDetailAnimation( - panel, - [ - { height: '0px', opacity: 0, transform: 'translate3d(0, -4px, 0)' }, - { height: `${height}px`, opacity: 1, transform: 'translate3d(0, 0, 0)' } - ], - { duration: LIFECYCLE_ENTER_MS, easing: lifecycleEasing }, - done - ) -} -const afterRetrievalPanelEnter = (element: Element) => { +const clearPanelStyles = (element: Element) => { const panel = element as HTMLElement panel.style.height = '' panel.style.overflow = '' panel.style.willChange = '' } -const beforeRetrievalPanelLeave = (element: Element) => { +const beforeExpandPanelEnter = (element: Element) => { const panel = element as HTMLElement - panel.style.height = `${panel.scrollHeight}px` - panel.style.overflow = 'clip' - panel.style.willChange = 'height, opacity, transform' + // v-show briefly sets display:none while reversing leave; computed height + // still includes the running effect, whereas the bounding box is then zero. + const from = panel.style.height ? Number.parseFloat(getComputedStyle(panel).height) : 0 + detailMotion.cancel(panel) + panel.style.height = `${from}px` + panel.style.overflow = 'hidden' + panel.style.willChange = 'height, opacity' } -const leaveRetrievalPanel = (element: Element, done: () => void) => { +const enterExpandPanel = (element: Element, done: () => void) => { const panel = element as HTMLElement - const height = panel.scrollHeight - playDetailAnimation( - panel, - [ - { height: `${height}px`, opacity: 1, transform: 'translate3d(0, 0, 0)' }, - { height: '0px', opacity: 0, transform: 'translate3d(0, -4px, 0)' } - ], - { duration: LIFECYCLE_LEAVE_MS, easing: lifecycleExitEasing }, - () => { - panel.style.willChange = '' - done() - } + // Remove the frozen height while measuring: scrollHeight cannot measure a + // smaller destination when the element still has a larger explicit height. + const from = panel.getBoundingClientRect().height + panel.style.height = '' + const to = panel.getBoundingClientRect().height + panel.style.height = `${from}px` + playDetailAnimation(panel, + [{ height: `${from}px`, opacity: from > 0 ? 1 : 0 }, { height: `${to}px`, opacity: 1 }], + { duration: PANEL_DURATION_MS, easing: lifecycleEasing }, + () => { clearPanelStyles(panel); done() } ) } +const afterExpandPanelEnter = clearPanelStyles +const beforeExpandPanelLeave = (element: Element) => { + const panel = element as HTMLElement + const from = panel.getBoundingClientRect().height + detailMotion.cancel(panel) + panel.style.height = `${from}px` + panel.style.overflow = 'hidden' + panel.style.willChange = 'height, opacity' +} +const leaveExpandPanel = (element: Element, done: () => void) => { + const panel = element as HTMLElement + const from = panel.getBoundingClientRect().height + playDetailAnimation(panel, + [{ height: `${from}px`, opacity: 1 }, { height: '0px', opacity: 0 }], + { duration: PANEL_DURATION_MS, easing: lifecycleEasing }, + () => { clearPanelStyles(panel); done() } + ) +} +const beforeRetrievalPanelEnter = beforeExpandPanelEnter +const enterRetrievalPanel = enterExpandPanel +const afterRetrievalPanelEnter = afterExpandPanelEnter +const beforeRetrievalPanelLeave = beforeExpandPanelLeave +const leaveRetrievalPanel = leaveExpandPanel watch( () => selectedFileDetail.value?.id, () => { @@ -2554,17 +2523,22 @@ onMounted(() => { }) onUnmounted(() => { + lifecycleHeightRevision++ + detailMotion.dispose() mobileMediaQuery.removeEventListener('change', syncMobileViewport) }) diff --git a/tests/README.md b/tests/README.md new file mode 100644 index 0000000..8b2d25d --- /dev/null +++ b/tests/README.md @@ -0,0 +1,26 @@ +# Detail motion regression checks + +Requires Node 22.6+ for the TypeScript unit tests (no extra test dependencies). + +- `npm run test:motion`: animation completion, cancellation, reversal and reduced motion. +- `npm run test:motion:coverage`: coverage of `src/utils/detailMotion.ts` only. + +Browser integration uses the actual Vue file-detail view and backend data. Start +Vite with the desired test backend, sign in, and open a file with more than three +lifecycle rows and at least one retrieval event. In that page's DevTools console: + +```js +await (await import('/theme-assets/tests/detailMotion.browser.mjs')).runDetailMotionChecks() +``` + +Run again at 390px width, then with `prefers-reduced-motion: reduce` emulated: + +```js +await (await import('/theme-assets/tests/detailMotion.browser.mjs')).runDetailMotionChecks({ reducedMotion: true }) +``` + +Checks three expand/collapse cycles, nonidentical height endpoints, return to +natural height, visible retrieval records, no vertical record transform, rapid +reversals and final collapsed layout. It only toggles presentation and never +uploads, deletes, restores or changes file data. These checks do not measure +whole-project coverage or validate the storage implementation. diff --git a/tests/detailMotion.browser.mjs b/tests/detailMotion.browser.mjs new file mode 100644 index 0000000..125c413 --- /dev/null +++ b/tests/detailMotion.browser.mjs @@ -0,0 +1,77 @@ +// Run in the local app with a file detail dialog open (more than 3 lifecycle rows +// and retrieval events). Reads real data and toggles presentation only. +export async function runDetailMotionChecks({ reducedMotion = false } = {}) { + const results = [] + const wait = ms => new Promise(resolve => setTimeout(resolve, ms)) + const assert = (condition, message) => { if (!condition) throw new Error(message) } + const lifecycle = () => document.querySelector('.fcb-lifecycle-viewport') + const toggle = (kind, expanded) => { + const label = kind === 'lifecycle' + ? (expanded ? '展开全部生命周期记录' : '收起生命周期记录') + : (expanded ? '展开全部取件记录' : '收起取件记录') + const button = [...document.querySelectorAll('button')].find(b => b.textContent.includes(label)) + if (button) button.click() + } + const assertNaturalHeight = label => { + const v = lifecycle() + const actual = v.getBoundingClientRect().height + const expected = v.firstElementChild.getBoundingClientRect().height + assert(Math.abs(actual - expected) < 1, `${label}: viewport ${actual}, content ${expected}`) + assert(!v.style.height && v.getAnimations().length === 0, `${label}: retained height effect`) + results.push({ label, height: actual, contentHeight: expected }) + } + assert(lifecycle(), 'Open a file detail dialog first') + toggle('lifecycle', false) + await wait(500) + const initial = lifecycle().getBoundingClientRect().height + for (let cycle = 0; cycle < 3; cycle++) { + toggle('lifecycle', true) + await wait(30) + if (!reducedMotion) { + const animation = lifecycle().getAnimations()[0] + assert(animation, 'Missing outer height animation') + const frames = animation.effect.getKeyframes() + assert(frames[0].height !== frames[1].height, 'Identical start and end heights') + assert(animation.effect.getTiming().duration === 420, 'Unexpected height duration') + } + await wait(500) + assertNaturalHeight(`expanded-${cycle}`) + toggle('lifecycle', false) + await wait(500) + assertNaturalHeight(`collapsed-${cycle}`) + assert(Math.abs(lifecycle().getBoundingClientRect().height - initial) < 1, 'Collapsed height drift') + assert(lifecycle().querySelectorAll('.fcb-lifecycle-row').length === 3, 'Unexpected collapsed rows') + } + toggle('lifecycle', true) + await wait(500) + toggle('retrieval', true) + await wait(500) + const panel = document.querySelector('.fcb-retrieval-panel') + assert(panel && panel.getBoundingClientRect().height > 0, 'Retrieval panel is hidden') + assert(panel.querySelectorAll('.font-medium').length > 0, 'Missing retrieval records') + assert(getComputedStyle(panel).transform === 'none', 'Retrieval records translate vertically') + assertNaturalHeight('retrieval-expanded') + for (let i = 0; i < 6; i++) { + const before = panel.getBoundingClientRect().height + toggle('retrieval', i % 2 !== 0) + await wait(60) + const after = panel.getBoundingClientRect().height + assert(i % 2 === 0 ? after <= before : after >= before, 'Reversal jumps in the wrong direction') + } + await wait(500) + assertNaturalHeight('rapid-retrieval-reversal') + for (let i = 0; i < 6; i++) { + toggle('lifecycle', i % 2 !== 0) + await wait(60) + } + await wait(500) + assertNaturalHeight('rapid-lifecycle-reversal') + toggle('retrieval', false) + await wait(500) + assert(getComputedStyle(document.querySelector('.fcb-retrieval-panel')).display === 'none', 'Retrieval panel remains visible') + assertNaturalHeight('retrieval-collapsed') + toggle('lifecycle', false) + await wait(500) + assertNaturalHeight('final-collapsed') + return results +} diff --git a/tests/detailMotion.test.mjs b/tests/detailMotion.test.mjs new file mode 100644 index 0000000..174cfee --- /dev/null +++ b/tests/detailMotion.test.mjs @@ -0,0 +1,76 @@ +import assert from 'node:assert/strict' +import { test } from 'node:test' +import { createDetailMotion } from '../src/utils/detailMotion.ts' + +class FakeAnimation extends EventTarget { + cancelled = false + cancel() { this.cancelled = true; this.dispatchEvent(new Event('cancel')) } + finish() { this.dispatchEvent(new Event('finish')) } +} +class FakeElement { + animations = [] + animate(frames, options) { + const animation = new FakeAnimation() + Object.assign(animation, { frames, options }) + this.animations.push(animation) + return animation + } + getAnimations() { return this.animations.filter(a => !a.cancelled) } +} +const frames = [{ height: '186px' }, { height: '429px' }] + +test('completed height effects are removed so content can shrink and nested records can grow', () => { + const motion = createDetailMotion(() => false) + const element = new FakeElement() + let done = 0 + motion.play(element, frames, { duration: 420 }, () => done++) + element.animations[0].finish() + assert.equal(done, 1) + assert.equal(element.getAnimations().length, 0) +}) +test('rapid reversal finishes the previous transition once and ignores its stale finish event', () => { + const motion = createDetailMotion(() => false) + const element = new FakeElement() + let first = 0, second = 0 + motion.play(element, frames, {}, () => first++) + const old = element.animations[0] + motion.play(element, frames.toReversed(), {}, () => second++) + old.finish() + assert.equal(first, 1) + assert.equal(second, 0) + assert.equal(element.getAnimations().length, 1) + element.animations[1].finish() + assert.equal(second, 1) + assert.equal(element.getAnimations().length, 0) +}) +test('explicit cancellation removes effects and calls completion only once', () => { + const motion = createDetailMotion(() => false) + const element = new FakeElement() + let done = 0 + motion.play(element, frames, {}, () => done++) + motion.cancel(element) + motion.cancel(element) + element.animations[0].finish() + assert.equal(done, 1) + assert.equal(element.getAnimations().length, 0) +}) +test('reduced motion skips animations and still completes', () => { + const motion = createDetailMotion(() => true) + const element = new FakeElement() + let done = 0 + motion.play(element, frames, {}, () => done++) + assert.equal(done, 1) + assert.equal(element.animations.length, 0) +}) +test('external cancellation and disposal release all owned animations', () => { + const motion = createDetailMotion(() => false) + const a = new FakeElement(), b = new FakeElement() + let done = 0 + motion.play(a, frames, {}, () => done++) + a.animations[0].cancel() + motion.play(a, frames, {}, () => done++) + motion.play(b, frames, {}, () => done++) + motion.dispose() + assert.equal(done, 3) + assert.equal(a.getAnimations().length + b.getAnimations().length, 0) +})