import type React from 'react'; import { useState } from 'react'; /** Where a dragged place would land relative to the row under it. */ export type DropSide = 'before' | 'after'; /** * Moves `id` next to `targetId` in `order`. Returns the order unchanged when either id * is missing or the place would land where it already is. */ export function movePlace(order: readonly number[], id: number, targetId: number, side: DropSide): number[] { if (id === targetId || !order.includes(id) || !order.includes(targetId)) return [...order]; const rest = order.filter((x) => x !== id); const at = rest.indexOf(targetId) + (side === 'after' ? 1 : 0); return [...rest.slice(0, at), id, ...rest.slice(at)]; } /** One step up or down, for the keyboard and for the phone's move buttons. */ export function stepPlace(order: readonly number[], id: number, delta: -1 | 1): number[] { const from = order.indexOf(id); const to = from + delta; if (from < 0 || to < 0 || to >= order.length) return [...order]; const next = [...order]; next.splice(from, 1); next.splice(to, 0, id); return next; } const sameOrder = (a: readonly number[], b: readonly number[]) => a.length === b.length && a.every((x, i) => x === b[i]); /** * The manual order of a list's places (#1457), behind the desktop list and the phone * cards, which render their own handles over it. Drag works with the mouse and, through * the shell's touch bridge, with a finger; the handle also answers the arrow keys. * * `enabled` is false whenever a reorder would be a lie: another sort, a search or a * filter that hides places, a list the reader may not edit, or "All saved", which mixes * lists. Every change hands the whole order of the list to `onCommit`. */ export function usePlaceReorder(order: readonly number[], enabled: boolean, onCommit: (orderedIds: number[]) => void) { const [dragId, setDragId] = useState(null); const [over, setOver] = useState<{ id: number; side: DropSide } | null>(null); const commit = (next: number[]) => { if (!sameOrder(next, order)) onCommit(next); }; const end = () => { setDragId(null); setOver(null); }; /** For the drag handle of one row. */ const handleProps = (id: number) => enabled ? { draggable: true, onDragStart: (e: React.DragEvent) => { e.stopPropagation(); e.dataTransfer.effectAllowed = 'move'; e.dataTransfer.setData('text/plain', String(id)); setDragId(id); }, onDragEnd: end, onKeyDown: (e: React.KeyboardEvent) => { if (e.key !== 'ArrowUp' && e.key !== 'ArrowDown') return; e.preventDefault(); commit(stepPlace(order, id, e.key === 'ArrowUp' ? -1 : 1)); }, } : {}; /** For the row itself: where the drop would land, and the drop. */ const rowProps = (id: number) => enabled && dragId != null ? { onDragOver: (e: React.DragEvent) => { e.preventDefault(); e.dataTransfer.dropEffect = 'move'; const box = e.currentTarget.getBoundingClientRect(); const side: DropSide = e.clientY - box.top < box.height / 2 ? 'before' : 'after'; if (over?.id !== id || over.side !== side) setOver({ id, side }); }, onDrop: (e: React.DragEvent) => { e.preventDefault(); if (dragId != null && over) commit(movePlace(order, dragId, over.id, over.side)); end(); }, } : {}; /** The line a row shows while something is dragged over it. */ const dropSide = (id: number): DropSide | null => dragId != null && dragId !== id && over?.id === id ? over.side : null; return { enabled, dragId, handleProps, rowProps, dropSide, moveUp: (id: number) => commit(stepPlace(order, id, -1)), moveDown: (id: number) => commit(stepPlace(order, id, 1)), }; }