Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.
JavaScript apple-watchbodyweightdockerfitnessfitness-trackergymhealthiosmcpnodejspasskeysprogressive-web-applicationspwareactself-hostedself-hostingvitewebauthnweightliftingworkout-tracker
Routine editor: a drag handle on computers, and pressing it with the mouse picks the exercise up at once (#277, #114)
Duarte Santos0b89bfbfecc0
diff --git a/frontend/src/components/Icon.jsx b/frontend/src/components/Icon.jsxindex 41ca2ef99..fa3095048 100644--- a/frontend/src/components/Icon.jsx+++ b/frontend/src/components/Icon.jsx@@ -112,6 +112,8 @@ const P = {chartLine: <path d="M3.6 20.2V4.4M3.6 20.2h16.8M6.4 16.4l3.9-4.8 3.1 2.7 5.2-6.6" />,dot: <circle cx="12" cy="12" r="4.2" fill="currentColor" stroke="none" />,more: <><circle cx="5.5" cy="12" r="1.6" fill="currentColor" stroke="none" /><circle cx="12" cy="12" r="1.6" fill="currentColor" stroke="none" /><circle cx="18.5" cy="12" r="1.6" fill="currentColor" stroke="none" /></>,+ // the drag handle: two columns of three dots, the grip a pointer-device user looks for+ grip: <>{[6.2, 12, 17.8].map(y => [9.2, 14.8].map(x => <circle key={x + '-' + y} cx={x} cy={y} r="1.5" fill="currentColor" stroke="none" />))}</>,history: <><path d="M4.5 12.2a7.6 7.6 0 1 0 2.5-5.6" /><path d="M4.1 4.4v4.3h4.3" /><path d="M12 8.3v4.2l3.1 1.9" /></>,signOut: <><path d="M14.2 4.6H7a1.9 1.9 0 0 0-1.9 1.9v11a1.9 1.9 0 0 0 1.9 1.9h7.2" /><path d="m16.8 8.4 3.6 3.6-3.6 3.6M20.4 12H10.2" /></>,shuffle: <><path d="M3.6 7.2h2.9c1.6 0 2.8.9 3.8 2.4l3 4.8c1 1.5 2.2 2.4 3.8 2.4h2.9M3.6 16.8h2.9c1.6 0 2.8-.9 3.8-2.4l.7-1.1M15.6 9.9l.7-1.1c1-1.5 2.2-2.4 3.8-2.4h1.9" /><path d="m17.9 4.3 2.8 2.1-2.8 2.1M17.9 14.7l2.8 2.1-2.8 2.1" /></>,diff --git a/frontend/src/index.css b/frontend/src/index.cssindex 62f4550ea..297f550bc 100644--- a/frontend/src/index.css+++ b/frontend/src/index.css@@ -464,6 +464,19 @@ h4.sec{position:absolute;left:6px;right:6px;height:3px;border-radius:99px;background:var(--acc);transform:translateY(-50%);pointer-events:none;z-index:3;}+/* The drag handle (#277, #114). A long press anywhere on a row reorders it on every device,+ but nothing on screen said so, and with a mouse nobody holds a row to find out. So a pointer+ device gets the grip it looks for, with a grab cursor, and pressing it picks the row up at+ once (RoutineEdit.jsx). A touch screen keeps the long press and the row's full width. */+.routine-grip{+ display:none;flex:none;align-self:stretch;align-items:center;margin:-10px -6px -10px -4px;padding:0 4px;+ color:var(--label-3);font-size:16px;cursor:grab;touch-action:none;user-select:none;-webkit-user-select:none;+}+@media (hover:hover) and (pointer:fine){+ .routine-grip{display:flex}+ .routine-grip:hover{color:var(--label-2)}+}+.routine-list.is-reordering,.routine-list.is-reordering *{cursor:grabbing}.thumb{width:50px;height:50px;border-radius:9px;object-fit:cover;background:#fff;flex:none}/* exercise pictures are decoration inside tappable rows/cards: no save-image callout ona long press and no drag ghost when a scroll starts on the picture */diff --git a/frontend/src/views/RoutineEdit.grip.test.jsx b/frontend/src/views/RoutineEdit.grip.test.jsxnew file mode 100644index 000000000..77b0c3c05--- /dev/null+++ b/frontend/src/views/RoutineEdit.grip.test.jsx@@ -0,0 +1,148 @@+// @vitest-environment happy-dom+// The drag handle on pointer devices (#277, #114): a visible grip with a grab cursor, and a mouse+// press on it picks the row up at once. The long press everywhere else is unchanged.+import { readFileSync } from 'node:fs'+import { resolve } from 'node:path'+import React, { act } from 'react'+import { createRoot } from 'react-dom/client'+import { MemoryRouter, Route, Routes } from 'react-router-dom'+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'++const sheets = vi.hoisted(() => ({+ exConfigSheet: vi.fn(), exercisePicker: vi.fn(), glyphPicker: vi.fn(), confirmSheet: vi.fn(),+}))+vi.mock('../lib/api.js', () => ({ api: vi.fn(() => Promise.resolve({})) }))+vi.mock('../sheets.jsx', () => sheets)+vi.mock('../components/Media.jsx', () => ({ Thumb: ({ ex }) => <span data-thumb={ex.id} /> }))+vi.mock('../components/BodyMap.jsx', () => ({ default: () => null }))++import RoutineEdit, { ROUTINE_LONG_PRESS_MS } from './RoutineEdit.jsx'+import { DEF, useStore } from '../store/useStore.js'++globalThis.IS_REACT_ACT_ENVIRONMENT = true+const clone = value => JSON.parse(JSON.stringify(value))+const configured = (id, extra = {}) => ({ id, mode: 'reps', sets: 3, reps: 5, weight: 0, ...extra })+let root+let host++function stateFor(entries) {+ const S = clone(DEF)+ S.routines = [{ id: 'r1', name: 'Drag routine', emoji: 'dumbbell', prog: 'linear', ex: entries }]+ return S+}+function rect(top, height, left = 20, width = 340) {+ return { x: left, y: top, left, right: left + width, top, bottom: top + height, width, height }+}+const rows = () => [...host.querySelectorAll('[data-routine-row]')]+function geometry(heights = rows().map(() => 70)) {+ const list = host.querySelector('.routine-list')+ let top = 100+ const tops = [], bottoms = [], centers = []+ rows().forEach((row, i) => {+ const height = heights[i] ?? 70+ tops.push(top); bottoms.push(top + height); centers.push(top + height / 2)+ vi.spyOn(row, 'getBoundingClientRect').mockImplementation(() => rect(tops[i], height))+ top += height + 10+ })+ const listRect = rect(80, top - 80)+ vi.spyOn(list, 'getBoundingClientRect').mockImplementation(() => listRect)+ return { list, tops, bottoms, centers, listRect }+}+function mount(entries) {+ useStore.setState({ S: stateFor(entries), user: null })+ host = document.createElement('div')+ document.body.appendChild(host)+ root = createRoot(host)+ act(() => root.render(<MemoryRouter initialEntries={['/plan/r/r1']}><Routes><Route path="/plan/r/:id" element={<RoutineEdit />} /></Routes></MemoryRouter>))+ 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 })) {+ Object.defineProperty(event, key, { configurable: true, value })+ }+ act(() => target.dispatchEvent(event))+ return event+}+const exercises = () => useStore.getState().S.routines[0].ex++beforeEach(() => {+ vi.useFakeTimers()+ vi.spyOn(window, 'requestAnimationFrame').mockImplementation(cb => window.setTimeout(() => cb(Date.now()), 16))+ vi.spyOn(window, 'cancelAnimationFrame').mockImplementation(id => window.clearTimeout(id))+ localStorage.clear()+ Object.values(sheets).forEach(mock => mock.mockReset())+ root = null; host = null+})+afterEach(() => {+ if (root) act(() => root.unmount())+ host?.remove()+ vi.clearAllTimers(); vi.restoreAllMocks(); vi.useRealTimers()+})++const grip = i => rows()[i].querySelector('[data-drag-handle]')+const cssSource = readFileSync(resolve(process.cwd(), 'src/index.css'), 'utf8')++describe('routine drag handle', () => {+ it('puts a grip on every row, out of the accessibility tree, named for what it does', () => {+ mount([configured('1001'), configured('1002')])+ expect(rows().map(row => !!row.querySelector('.item [data-drag-handle] svg'))).toEqual([true, true])+ expect(grip(0).getAttribute('aria-hidden')).toBe('true')+ expect(grip(0).getAttribute('title')).toBe('Reorder exercises')+ })++ it('shows the grip, with a grab cursor, only where there is a mouse or trackpad', () => {+ const base = cssSource.match(/\.routine-grip\{[^}]*\}/)?.[0] || ''+ expect(base).toMatch(/display:none/)+ expect(base).toMatch(/cursor:grab/)+ const fine = cssSource.match(/@media \(hover:hover\) and \(pointer:fine\)\{\s*\.routine-grip\{display:flex\}/)+ expect(fine).toBeTruthy()+ expect(cssSource).toMatch(/\.routine-list\.is-reordering,\.routine-list\.is-reordering \*\{cursor:grabbing\}/)+ })++ it('picks the row up at once under a mouse and drops it where it is let go', () => {+ const layout = mount([configured('a'), configured('b'), configured('c'), configured('d')])+ const down = pointer(grip(0), 'pointerdown', { kind: 'mouse', y: layout.centers[0] })+ // No hold: lifted on the press itself, and the press does not go on to select text.+ expect(rows()[0].classList.contains('is-dragging')).toBe(true)+ expect(down.defaultPrevented).toBe(true)+ pointer(grip(0), 'pointermove', { kind: 'mouse', y: layout.centers[2] + 1 })+ pointer(grip(0), 'pointerup', { kind: 'mouse', y: layout.centers[2] + 1 })+ expect(exercises().map(e => e.id)).toEqual(['b', 'c', 'a', 'd'])+ // The click that follows the drop does not open the exercise's settings.+ act(() => grip(2).click())+ expect(sheets.exConfigSheet).not.toHaveBeenCalled()+ })++ it('leaves a click on the grip without a move as nothing at all', () => {+ const layout = mount([configured('a'), configured('b')])+ pointer(grip(1), 'pointerdown', { kind: 'mouse', y: layout.centers[1] })+ pointer(grip(1), 'pointerup', { kind: 'mouse', y: layout.centers[1] })+ act(() => grip(1).click())+ expect(exercises().map(e => e.id)).toEqual(['a', 'b'])+ expect(sheets.exConfigSheet).not.toHaveBeenCalled()+ })++ it('keeps the long press for a mouse on the rest of the row, and for a finger on the grip', () => {+ const layout = mount([configured('a'), configured('b')])+ const item = rows()[0].querySelector('.item')+ pointer(item, 'pointerdown', { kind: 'mouse', y: layout.centers[0] })+ expect(rows()[0].classList.contains('is-dragging')).toBe(false)+ act(() => vi.advanceTimersByTime(ROUTINE_LONG_PRESS_MS))+ expect(rows()[0].classList.contains('is-dragging')).toBe(true)+ pointer(item, 'pointercancel', { kind: 'mouse', y: layout.centers[0] })++ act(() => vi.advanceTimersByTime(200))+ const touch = pointer(grip(1), 'pointerdown', { kind: 'touch', y: layout.centers[1] })+ expect(touch.defaultPrevented).toBe(false)+ expect(rows()[1].classList.contains('is-dragging')).toBe(false)+ act(() => vi.advanceTimersByTime(ROUTINE_LONG_PRESS_MS))+ expect(rows()[1].classList.contains('is-dragging')).toBe(true)+ })++ it('ignores a right-click on the grip', () => {+ const layout = mount([configured('a'), configured('b')])+ pointer(grip(0), 'pointerdown', { kind: 'mouse', button: 2, y: layout.centers[0] })+ expect(rows()[0].classList.contains('is-dragging')).toBe(false)+ })+})diff --git a/frontend/src/views/RoutineEdit.jsx b/frontend/src/views/RoutineEdit.jsxindex 968eb1312..aef7ca517 100644--- a/frontend/src/views/RoutineEdit.jsx+++ b/frontend/src/views/RoutineEdit.jsx@@ -224,8 +224,17 @@ function useRoutineReorder(routineIdentity, exercises, onDrop) {listIdentity: exercisesRef.current,snapshot: JSON.stringify(exercisesRef.current), active: false, timer: null,}- gesture.timer = window.setTimeout(() => lift(gesture), ROUTINE_LONG_PRESS_MS)gestureRef.current = gesture+ // The grip, pressed with a mouse, picks the row up at once (#277): the hold exists so a+ // finger can still scroll the list, and a mouse scrolls with its wheel. preventDefault+ // keeps the press from starting a text selection or the row's swipe-to-delete, which+ // listen to the mouse events a pointerdown would otherwise go on to fire.+ if (event.pointerType === 'mouse' && target.closest('[data-drag-handle]')) {+ event.preventDefault()+ lift(gesture)+ return+ }+ gesture.timer = window.setTimeout(() => lift(gesture), ROUTINE_LONG_PRESS_MS)}const onPointerMove = event => {const gesture = gestureRef.current@@ -429,6 +438,9 @@ export default function RoutineEdit() {onClick={() => {exConfigSheet(ex, e, cfg => edit(x => { x[i] = { id: x[i].id, sg: x[i].sg, ...cfg } }), () => edit(x => { x.splice(i, 1); cleanupSg(x) }), r, null, () => replace(i))}}>+ {/* Shown on pointer devices only (index.css .routine-grip); the Move buttons and the+ long press stay the way in for a keyboard and a finger. */}+ <span className="routine-grip" data-drag-handle aria-hidden="true" title={t('Reorder exercises')}><Icon name="grip" /></span><Thumb ex={ex} /><div className="grow"><div className="tt capitalize">{exerciseNameFor(ex)}</div><div className="ss">{exLine(e, S.unit, speedUnitOf(S))}</div>{e.note && <div className="small dim" style={{ marginTop: 2 }}>{e.note}</div>}</div>