diff --git a/frontend/src/views/RoutineEdit.drag.test.jsx b/frontend/src/views/RoutineEdit.drag.test.jsx index 5427387..63749cc 100644 --- a/frontend/src/views/RoutineEdit.drag.test.jsx +++ b/frontend/src/views/RoutineEdit.drag.test.jsx @@ -52,6 +52,13 @@ function mount(entries) { act(() => root.render(} />)) return geometry() } +function remountCurrentState() { + host = document.createElement('div') + document.body.appendChild(host) + root = createRoot(host) + act(() => root.render(} />)) + return geometry() +} function pointer(target, type, { id = 7, kind = 'touch', primary = true, button = 0, x = 120, y = 120 } = {}) { const event = new Event(type, { bubbles: true, cancelable: true }) for (const [key, value] of Object.entries({ pointerId: id, pointerType: kind, isPrimary: primary, button, clientX: x, clientY: y })) { @@ -269,6 +276,126 @@ describe('routine long-press reorder', () => { expect(release).toHaveBeenCalledWith(7) expect(localStorage.getItem('gym_state_v1')).toBe(before) }) + + it('covers reverse measured slots and an unchanged original slot', () => { + let layout = mount([configured('a'), configured('b'), configured('c'), configured('d')]) + let item = rows()[3].querySelector('.item') + lift(rows()[3], layout.centers[3]) + pointer(item, 'pointermove', { y: layout.tops[1] + 1 }) + pointer(item, 'pointerup', { y: layout.tops[1] + 1 }) + expect(exercises().map(e => e.id)).toEqual(['a', 'd', 'b', 'c']) + + act(() => root.unmount()); host.remove(); root = null; host = null + layout = mount([configured('a'), configured('b'), configured('c')]) + const before = localStorage.getItem('gym_state_v1') + item = rows()[1].querySelector('.item') + lift(rows()[1], layout.centers[1]) + pointer(item, 'pointermove', { y: layout.centers[1] + ROUTINE_DRAG_SLOP + 1 }) + pointer(item, 'pointerup', { y: layout.centers[1] + ROUTINE_DRAG_SLOP + 1 }) + expect(exercises().map(e => e.id)).toEqual(['a', 'b', 'c']) + expect(localStorage.getItem('gym_state_v1')).toBe(before) + }) + + it('reads the persisted changed order back after a fresh remount', () => { + const layout = mount([configured('a'), configured('b'), configured('c')]) + const item = rows()[0].querySelector('.item') + lift(rows()[0], layout.centers[0]) + pointer(item, 'pointermove', { y: layout.centers[2] + 1 }) + pointer(item, 'pointerup', { y: layout.centers[2] + 1 }) + const saved = JSON.parse(localStorage.getItem('gym_state_v1')) + act(() => root.unmount()); host.remove(); root = null; host = null + useStore.setState({ S: saved, user: null }) + remountCurrentState() + expect(exercises().map(e => e.id)).toEqual(['b', 'c', 'a']) + }) + + it('cancels active ownership for a second pointer outside the list and pending lost capture', () => { + let layout = mount([configured('a'), configured('b'), configured('c')]) + let item = rows()[0].querySelector('.item') + const before = localStorage.getItem('gym_state_v1') + lift(rows()[0], layout.centers[0]) + pointer(item, 'pointermove', { y: layout.centers[2] }) + pointer(document.body, 'pointerdown', { id: 8, y: 20 }) + pointer(item, 'pointerup', { y: layout.centers[2] }) + expect(exercises().map(e => e.id)).toEqual(['a', 'b', 'c']) + expect(localStorage.getItem('gym_state_v1')).toBe(before) + expect(host.querySelector('.is-dragging')).toBeNull() + + act(() => root.unmount()); host.remove(); root = null; host = null + layout = mount([configured('a'), configured('b')]) + item = rows()[0].querySelector('.item') + pointer(item, 'pointerdown', { y: layout.centers[0] }) + pointer(item, 'lostpointercapture', { y: layout.centers[0] }) + act(() => vi.advanceTimersByTime(ROUTINE_LONG_PRESS_MS)) + expect(host.querySelector('.is-dragging')).toBeNull() + }) + + it('cancels when equal serialized exercises acquire a new current routine/list identity', () => { + let layout = mount([configured('a'), configured('b'), configured('c')]) + let item = rows()[0].querySelector('.item') + lift(rows()[0], layout.centers[0]) + pointer(item, 'pointermove', { y: layout.centers[2] }) + act(() => useStore.getState().update(s => { + const current = s.routines[0] + s.routines[0] = { ...current, ex: clone(current.ex) } + })) + pointer(item, 'pointerup', { y: layout.centers[2] }) + expect(exercises().map(e => e.id)).toEqual(['a', 'b', 'c']) + expect(host.querySelector('.is-dragging')).toBeNull() + + act(() => root.unmount()); host.remove(); root = null; host = null + layout = mount([configured('a'), configured('b')]) + item = rows()[0].querySelector('.item') + pointer(item, 'pointerdown', { y: layout.centers[0] }) + act(() => useStore.getState().update(s => { + const current = s.routines[0] + s.routines[0] = { ...current, ex: clone(current.ex) } + })) + act(() => vi.advanceTimersByTime(ROUTINE_LONG_PRESS_MS)) + expect(host.querySelector('.is-dragging')).toBeNull() + }) + + it('never owns nested link, Move up, or Move down controls', () => { + const layout = mount([configured('a'), configured('b')]) + const controls = [ + rows()[1].querySelector('button[title]'), + rows()[1].querySelector('button[aria-label="Move up"]'), + rows()[1].querySelector('button[aria-label="Move down"]'), + ] + for (const control of controls) { + pointer(control, 'pointerdown', { y: layout.centers[1] }) + act(() => vi.advanceTimersByTime(ROUTINE_LONG_PRESS_MS)) + expect(host.querySelector('.is-dragging')).toBeNull() + pointer(control, 'pointerup', { y: layout.centers[1] }) + } + }) + + it('falls back to page/window autoscroll without a nested scroll host', () => { + const layout = mount([configured('a'), configured('b'), configured('c')]) + const rootElement = document.documentElement + Object.defineProperty(document, 'scrollingElement', { configurable: true, value: rootElement }) + Object.defineProperty(rootElement, 'clientHeight', { configurable: true, value: 300 }) + Object.defineProperty(rootElement, 'scrollHeight', { configurable: true, value: 900 }) + Object.defineProperty(window, 'innerHeight', { configurable: true, value: 300 }) + Object.defineProperty(window, 'scrollY', { configurable: true, writable: true, value: 100 }) + const scrollBy = vi.spyOn(window, 'scrollBy').mockImplementation((_, delta) => { window.scrollY += delta }) + const item = rows()[2].querySelector('.item') + lift(rows()[2], 295) + act(() => vi.advanceTimersByTime(16)) + expect(scrollBy).toHaveBeenCalled() + expect(window.scrollY).toBeGreaterThan(100) + pointer(item, 'pointercancel', { y: 295 }) + }) + + it('invalidates ownership before cleanup synchronously emits lost capture', () => { + const layout = mount([configured('a'), configured('b')]) + const item = rows()[0].querySelector('.item') + const release = vi.fn(pointerId => pointer(document, 'lostpointercapture', { id: pointerId, y: layout.centers[0] })) + item.releasePointerCapture = release + lift(rows()[0], layout.centers[0]) + act(() => root.unmount()); root = null + expect(release).toHaveBeenCalledTimes(1) + }) }) describe('reorderRoutineUnit', () => { diff --git a/frontend/src/views/RoutineEdit.jsx b/frontend/src/views/RoutineEdit.jsx index 8adf9ad..bc25c47 100644 --- a/frontend/src/views/RoutineEdit.jsx +++ b/frontend/src/views/RoutineEdit.jsx @@ -79,15 +79,17 @@ function autoScrollStep(host, clientY) { return 0 } -function useRoutineReorder(exercises, onDrop) { +function useRoutineReorder(routineIdentity, exercises, onDrop) { const listRef = useRef(null) const gestureRef = useRef(null) const exercisesRef = useRef(exercises) + const routineIdentityRef = useRef(routineIdentity) const onDropRef = useRef(onDrop) const suppressClickRef = useRef(false) const suppressTimerRef = useRef(null) const [drag, setDrag] = useState(null) exercisesRef.current = exercises + routineIdentityRef.current = routineIdentity onDropRef.current = onDrop const hasRows = exercises.length > 0 @@ -95,6 +97,7 @@ function useRoutineReorder(exercises, onDrop) { const list = listRef.current if (!list) return undefined let frame = null + setDrag(current => current ? null : current) const clearFrame = () => { if (frame != null) window.cancelAnimationFrame(frame) @@ -104,7 +107,9 @@ function useRoutineReorder(exercises, onDrop) { if (gesture?.timer != null) window.clearTimeout(gesture.timer) if (gesture) gesture.timer = null } - const matchesSnapshot = gesture => JSON.stringify(exercisesRef.current) === gesture.snapshot + const matchesSnapshot = gesture => routineIdentityRef.current === gesture.routineIdentity + && exercisesRef.current === gesture.listIdentity + && JSON.stringify(exercisesRef.current) === gesture.snapshot const releaseCapture = gesture => { const target = gesture?.captureTarget if (!target?.releasePointerCapture) return @@ -167,7 +172,8 @@ function useRoutineReorder(exercises, onDrop) { if (gestureRef.current === gesture) frame = window.requestAnimationFrame(runAutoScroll) } const lift = gesture => { - if (gestureRef.current !== gesture || !matchesSnapshot(gesture)) return + if (gestureRef.current !== gesture) return + if (!matchesSnapshot(gesture)) { finish(gesture, false); return } const geometry = unitGeometry(list, exercisesRef.current) const source = geometry?.find(unit => unit.unit.includes(gesture.sourceIndex)) if (!source) { finish(gesture, false); return } @@ -198,6 +204,8 @@ function useRoutineReorder(exercises, onDrop) { const gesture = { pointerId: event.pointerId, sourceIndex, downTarget: target, startX: event.clientX, startY: event.clientY, lastX: event.clientX, lastY: event.clientY, + routineIdentity: routineIdentityRef.current, + listIdentity: exercisesRef.current, snapshot: JSON.stringify(exercisesRef.current), active: false, timer: null, } gesture.timer = window.setTimeout(() => lift(gesture), ROUTINE_LONG_PRESS_MS) @@ -227,7 +235,7 @@ function useRoutineReorder(exercises, onDrop) { } const onLostCapture = event => { const gesture = gestureRef.current - if (gesture?.active && event.pointerId === gesture.pointerId) finish(gesture, false) + if (gesture && event.pointerId === gesture.pointerId) finish(gesture, false) } const cancelActive = () => finish(gestureRef.current, false) const onKeyDown = event => { @@ -238,7 +246,7 @@ function useRoutineReorder(exercises, onDrop) { const onContextMenu = event => { if (gestureRef.current?.active && event.target.closest?.('[data-routine-row]')) event.preventDefault() } const onDragStart = event => { if (event.target.closest?.('[data-routine-row]')) event.preventDefault() } - list.addEventListener('pointerdown', onPointerDown) + document.addEventListener('pointerdown', onPointerDown) document.addEventListener('pointermove', onPointerMove, { passive: false }) document.addEventListener('pointerup', onPointerUp, { passive: false }) document.addEventListener('pointercancel', onPointerCancel) @@ -250,10 +258,10 @@ function useRoutineReorder(exercises, onDrop) { window.addEventListener('blur', cancelActive) return () => { const gesture = gestureRef.current - clearTimer(gesture); clearFrame(); releaseCapture(gesture) - gestureRef.current = null; suppressClickRef.current = false + clearTimer(gesture); clearFrame(); gestureRef.current = null + releaseCapture(gesture); suppressClickRef.current = false window.clearTimeout(suppressTimerRef.current) - list.removeEventListener('pointerdown', onPointerDown) + document.removeEventListener('pointerdown', onPointerDown) document.removeEventListener('pointermove', onPointerMove) document.removeEventListener('pointerup', onPointerUp) document.removeEventListener('pointercancel', onPointerCancel) @@ -264,7 +272,7 @@ function useRoutineReorder(exercises, onDrop) { list.removeEventListener('dragstart', onDragStart) window.removeEventListener('blur', cancelActive) } - }, [hasRows]) + }, [hasRows, routineIdentity, exercises]) const onClickCapture = event => { if (!suppressClickRef.current) return @@ -287,7 +295,7 @@ export default function RoutineEdit() { // included: this still unmounts after the delete button navigates away. useEffect(() => () => useStore.getState().autoBackupNow(), []) const edit = fn => update(s => { fn(s.routines.find(x => x.id === id).ex) }) - const reorder = useRoutineReorder(r?.ex || [], (sourceIndex, targetSlot) => { + const reorder = useRoutineReorder(r, r?.ex || [], (sourceIndex, targetSlot) => { edit(exercises => { reorderRoutineUnit(exercises, sourceIndex, targetSlot) }) }) if (!r) return null