curated2: !41 (a move re-points the rest countdown instead of cancelling it)
This commit is contained in:
commit
250b164d2d
4 files changed
+202
No files matched your search
@@ -0,0 +1,31 @@
|
||||
import { supersetUnits } from './history.js'
|
||||
|
||||
function moveTarget(active, index, direction) {
|
||||
if (!active || !Array.isArray(active.entries) || (direction !== -1 && direction !== 1)) return null
|
||||
const units = supersetUnits(active.entries)
|
||||
const source = units.findIndex(unit => unit.includes(index))
|
||||
const target = source + direction
|
||||
if (source < 0 || target < 0 || target >= units.length) return null
|
||||
return { units, source, target }
|
||||
}
|
||||
|
||||
export function canMoveActiveWorkoutUnit(active, index, direction) {
|
||||
return moveTarget(active, index, direction) !== null
|
||||
}
|
||||
|
||||
export function moveActiveWorkoutUnit(active, index, direction) {
|
||||
const move = moveTarget(active, index, direction)
|
||||
if (!move) return null
|
||||
|
||||
const selected = active.entries[index]
|
||||
const reorderedUnits = [...move.units]
|
||||
const sourceUnit = reorderedUnits[move.source]
|
||||
reorderedUnits[move.source] = reorderedUnits[move.target]
|
||||
reorderedUnits[move.target] = sourceUnit
|
||||
const indices = reorderedUnits.flat()
|
||||
const reorderedEntries = indices.map(entryIndex => active.entries[entryIndex])
|
||||
|
||||
active.entries.splice(0, active.entries.length, ...reorderedEntries)
|
||||
active.cur = active.entries.indexOf(selected)
|
||||
return { indices }
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
import { describe, expect, it } from 'vitest'
|
||||
import { canMoveActiveWorkoutUnit, moveActiveWorkoutUnit } from './active-workout-order.js'
|
||||
import { LANGS } from './i18n-core.js'
|
||||
import { PT_BR_OVERRIDES } from '../locales/pt-BR.js'
|
||||
|
||||
const entry = (id, extra = {}) => ({
|
||||
id,
|
||||
target: { sets: 1, reps: 5 },
|
||||
sets: [{ w: 0, r: 5, done: false }],
|
||||
...extra,
|
||||
})
|
||||
|
||||
describe('active workout whole-unit order', () => {
|
||||
it('moves one standalone occurrence without conflating duplicate exercise ids', () => {
|
||||
const duplicateA = entry('duplicate', { occurrenceId: 'duplicate#1' })
|
||||
const selected = entry('duplicate', {
|
||||
occurrenceId: 'duplicate#2',
|
||||
target: { sets: 2, reps: 7, weight: 82.5, notes: 'Keep this target' },
|
||||
sets: [{ w: 77.5, r: 6, done: true, rir: 2 }],
|
||||
})
|
||||
const active = { cur: 2, entries: [duplicateA, entry('middle'), selected] }
|
||||
|
||||
expect(moveActiveWorkoutUnit(active, active.cur, -1)?.indices).toEqual([0, 2, 1])
|
||||
expect(active.entries.map(item => item.occurrenceId || item.id)).toEqual(['duplicate#1', 'duplicate#2', 'middle'])
|
||||
expect(active.entries[0]).toBe(duplicateA)
|
||||
expect(active.entries[1]).toBe(selected)
|
||||
expect(active.entries[1].target).toBe(selected.target)
|
||||
expect(active.entries[1].sets).toBe(selected.sets)
|
||||
expect(active.cur).toBe(1)
|
||||
})
|
||||
|
||||
it('moves a complete contiguous group one unit and preserves the selected member identity', () => {
|
||||
const first = entry('group-a', { sg: 'pair', occurrenceId: 'group-a#1' })
|
||||
const selected = entry('group-b', { sg: 'pair', occurrenceId: 'group-b#1' })
|
||||
const groupMeta = { pair: { kind: 'complex', label: 'Carry pair', cues: 'Stay braced.' } }
|
||||
const active = { cur: 2, entries: [entry('before'), first, selected, entry('after')], groupMeta }
|
||||
|
||||
expect(moveActiveWorkoutUnit(active, active.cur, -1)?.indices).toEqual([1, 2, 0, 3])
|
||||
expect(active.entries.map(item => item.id)).toEqual(['group-a', 'group-b', 'before', 'after'])
|
||||
expect(active.entries.slice(0, 2)).toEqual([first, selected])
|
||||
expect(active.entries.map(item => item.sg)).toEqual(['pair', 'pair', undefined, undefined])
|
||||
expect(active.groupMeta).toBe(groupMeta)
|
||||
expect(active.entries[active.cur]).toBe(selected)
|
||||
})
|
||||
|
||||
it('moves a group down by exactly one neighbouring unit', () => {
|
||||
const first = entry('group-a', { sg: 'pair' })
|
||||
const selected = entry('group-b', { sg: 'pair' })
|
||||
const active = { cur: 1, entries: [first, selected, entry('middle'), entry('last')] }
|
||||
|
||||
expect(moveActiveWorkoutUnit(active, active.cur, 1)?.indices).toEqual([2, 0, 1, 3])
|
||||
expect(active.entries.map(item => item.id)).toEqual(['middle', 'group-a', 'group-b', 'last'])
|
||||
expect(active.entries[active.cur]).toBe(selected)
|
||||
})
|
||||
|
||||
it('rejects boundaries and invalid directions without mutating the active workout', () => {
|
||||
const active = { cur: 0, entries: [entry('first'), entry('last')] }
|
||||
const entries = [...active.entries]
|
||||
|
||||
expect(canMoveActiveWorkoutUnit(active, 0, -1)).toBe(false)
|
||||
expect(canMoveActiveWorkoutUnit(active, 1, 1)).toBe(false)
|
||||
expect(moveActiveWorkoutUnit(active, 0, 0)).toBeNull()
|
||||
expect(moveActiveWorkoutUnit(active, 0, -1)).toBeNull()
|
||||
expect(active.entries).toEqual(entries)
|
||||
expect(active.cur).toBe(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('active workout move locale coverage', () => {
|
||||
const packs = import.meta.glob('../locales/*.js', { eager: true, import: 'default' })
|
||||
const localeCodes = Object.keys(LANGS).filter(code => code !== 'en')
|
||||
|
||||
it('defines both visible move labels in every current locale pack', () => {
|
||||
expect(Object.keys(packs)).toHaveLength(localeCodes.length)
|
||||
for (const code of localeCodes) {
|
||||
const pack = packs[`../locales/${code}.js`]
|
||||
expect(pack, `${code} locale pack is missing`).toBeTruthy()
|
||||
for (const key of ['Move up', 'Move down']) {
|
||||
expect(Object.hasOwn(pack, key), `${code} is missing ${key}`).toBe(true)
|
||||
expect(pack[key], `${code} has a blank ${key}`).toEqual(expect.any(String))
|
||||
expect(pack[key].trim(), `${code} has a blank ${key}`).not.toBe('')
|
||||
}
|
||||
}
|
||||
expect(Object.hasOwn(PT_BR_OVERRIDES, 'Move up')).toBe(true)
|
||||
expect(Object.hasOwn(PT_BR_OVERRIDES, 'Move down')).toBe(true)
|
||||
})
|
||||
})
|
||||
@@ -16,6 +16,7 @@ import { Button, Check, NumberField } from '../components/ui.jsx'
|
||||
import { nextPrescription, applyPrescription, defaultIncrement } from '../lib/progression.js'
|
||||
import { glyphOf } from '../lib/glyphs.js'
|
||||
import { isWarmupRow, isDropSet, isRestPauseSet, dropsOf, clustersOf, addDrop, addCluster, removeDropAt, removeClusterAt, setDropAt, setClusterAt, nextDropWeight, nextBurstReps } from '../lib/workout-model.js'
|
||||
import { canMoveActiveWorkoutUnit, moveActiveWorkoutUnit } from '../lib/active-workout-order.js'
|
||||
|
||||
const SWIPE_MIN_DISTANCE = 48
|
||||
const SWIPE_AXIS_RATIO = 1.25
|
||||
@@ -379,6 +380,24 @@ function ActiveWorkout() {
|
||||
})
|
||||
const onPairPrev = !isSuperset && cur > 0 ? () => pairAt(cur - 1, cur) : null
|
||||
const onPairNext = !isSuperset && cur < A.entries.length - 1 ? () => pairAt(cur, cur + 1) : null
|
||||
const moveCurrentUnit = direction => {
|
||||
const ui = useUI.getState()
|
||||
const active = useStore.getState().S.active
|
||||
if (ui.work || !canMoveActiveWorkoutUnit(active, active?.cur, direction)) return
|
||||
// Invalidate an old timed callback before indexes shift. A running rest is not cancelled:
|
||||
// it belongs to an exercise (timer.forIdx), and that exercise only changes position.
|
||||
ui.stopWork()
|
||||
update(s => {
|
||||
const moved = moveActiveWorkoutUnit(s.active, s.active?.cur, direction)
|
||||
if (!moved) return
|
||||
progressHighWater.current = moved.indices.map(index => progressHighWater.current[index])
|
||||
const rest = useUI.getState().timer
|
||||
if (rest && rest.forIdx != null) {
|
||||
const forIdx = moved.indices.indexOf(rest.forIdx)
|
||||
if (forIdx >= 0) useUI.setState({ timer: { ...rest, forIdx } })
|
||||
}
|
||||
}, true)
|
||||
}
|
||||
|
||||
const navigateUnit = direction => {
|
||||
const targetFor = active => {
|
||||
@@ -618,6 +637,13 @@ function ActiveWorkout() {
|
||||
}), null, routine, seed)
|
||||
})} icon="plus">{t('Add exercise')}</Button>
|
||||
{A.entries.length > 0 && <>
|
||||
<div style={{ height: 6 }} />
|
||||
<div className="row">
|
||||
<Button size="sm" icon="chevronUp" aria-label={t('Move up')}
|
||||
disabled={!!work || !canMoveActiveWorkoutUnit(A, cur, -1)} onClick={() => moveCurrentUnit(-1)}>{t('Move up')}</Button>
|
||||
<Button size="sm" trailingIcon="chevronDown" aria-label={t('Move down')}
|
||||
disabled={!!work || !canMoveActiveWorkoutUnit(A, cur, 1)} onClick={() => moveCurrentUnit(1)}>{t('Move down')}</Button>
|
||||
</div>
|
||||
<div style={{ height: 6 }} />
|
||||
<div style={{ display: 'flex', justifyContent: 'center' }}>
|
||||
<Button size="sm" icon="minus" style={{ color: 'var(--red)' }} disabled={!!work} onClick={removeExerciseSheet}>{t('Remove exercise')}</Button>
|
||||
|
||||
@@ -517,3 +517,61 @@ describe('superset actionable-set centring', () => {
|
||||
expect(mocks.scrollCalls).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe('active workout whole-unit move controls', () => {
|
||||
const action = label => container.querySelector(`button[aria-label="${label}"]`)
|
||||
|
||||
it('shows labelled controls and moves the selected standalone exercise one unit', async () => {
|
||||
const selected = exercise('duplicate', [false], {
|
||||
occurrenceId: 'duplicate#2',
|
||||
target: { mode: 'reps', reps: 7, weight: 82.5, notes: 'Keep this target' },
|
||||
sets: [{ w: 77.5, r: 6, done: true, rir: 2 }],
|
||||
})
|
||||
await mount([
|
||||
exercise('duplicate', [false], { occurrenceId: 'duplicate#1' }),
|
||||
exercise('middle', [false]),
|
||||
selected,
|
||||
], 2)
|
||||
|
||||
expect(action('Move up')?.textContent.trim()).toBe('Move up')
|
||||
expect(action('Move down')?.textContent.trim()).toBe('Move down')
|
||||
await act(async () => { action('Move up').dispatchEvent(new dom.Event('click', { bubbles: true })) })
|
||||
|
||||
expect(mocks.S.active.entries.map(entry => entry.occurrenceId || entry.id)).toEqual(['duplicate#1', 'duplicate#2', 'middle'])
|
||||
expect(mocks.S.active.entries[1]).toEqual(selected)
|
||||
expect(mocks.S.active.entries[1].target).toEqual({ mode: 'reps', reps: 7, weight: 82.5, notes: 'Keep this target' })
|
||||
expect(mocks.S.active.entries[1].sets).toEqual([{ w: 77.5, r: 6, done: true, rir: 2 }])
|
||||
expect(mocks.S.active.cur).toBe(1)
|
||||
expect(mocks.stopWork).toHaveBeenCalledOnce()
|
||||
expect(mocks.stopRest).toHaveBeenCalledOnce()
|
||||
})
|
||||
|
||||
it('moves the selected contiguous group as one unit without changing its metadata', async () => {
|
||||
const first = exercise('group-a', [false], { sg: 'pair', occurrenceId: 'group-a#1' })
|
||||
const selected = exercise('group-b', [true], { sg: 'pair', occurrenceId: 'group-b#1' })
|
||||
const groupMeta = { pair: { kind: 'complex', label: 'Carry pair', cues: 'Stay braced.' } }
|
||||
await mount([
|
||||
exercise('before', [false]),
|
||||
first,
|
||||
selected,
|
||||
exercise('after', [false]),
|
||||
], 2)
|
||||
mocks.S.active.groupMeta = groupMeta
|
||||
|
||||
await act(async () => { action('Move up').dispatchEvent(new dom.Event('click', { bubbles: true })) })
|
||||
|
||||
expect(mocks.S.active.entries.map(entry => entry.id)).toEqual(['group-a', 'group-b', 'before', 'after'])
|
||||
expect(mocks.S.active.entries.slice(0, 2)).toEqual([first, selected])
|
||||
expect(mocks.S.active.entries.slice(0, 2).map(entry => entry.sg)).toEqual(['pair', 'pair'])
|
||||
expect(mocks.S.active.groupMeta).toEqual(groupMeta)
|
||||
expect(mocks.S.active.entries[mocks.S.active.cur]).toEqual(selected)
|
||||
})
|
||||
|
||||
it('disables both moves while a work timer can still write by index', async () => {
|
||||
mocks.work = { left: 5, total: 5, endsAt: Date.now() + 5000 }
|
||||
await mount([exercise('first', [false]), exercise('second', [false])], 1)
|
||||
|
||||
expect(action('Move up')?.disabled).toBe(true)
|
||||
expect(action('Move down')?.disabled).toBe(true)
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user