diff --git a/apps/app/src/library/Grid.tsx b/apps/app/src/library/Grid.tsx index bbef976..f2d59ab 100644 --- a/apps/app/src/library/Grid.tsx +++ b/apps/app/src/library/Grid.tsx @@ -125,6 +125,27 @@ const Cell = memo(function Cell({ photo, index, src, orgId, selected, focused, o ); }); +const DRAG_PX = 5; + +interface DragSelect { + x: number; + y: number; + additive: boolean; + base: string[]; + moved: boolean; +} + +function idsInside(root: HTMLElement, rect: DOMRect): string[] { + const ids: string[] = []; + root.querySelectorAll("[data-photo-id]").forEach((node) => { + const box = node.getBoundingClientRect(); + const hit = box.left < rect.right && box.right > rect.left && box.top < rect.bottom && box.bottom > rect.top; + const id = node.dataset.photoId; + if (hit && id) ids.push(id); + }); + return ids; +} + export function Grid(props: { photos: PhotoSummary[]; selected: ReadonlySet; @@ -134,14 +155,71 @@ export function Grid(props: { onPick: (id: string, e: React.MouseEvent) => void; onOpen: (id: string) => void; onMenu?: (id: string, e: React.MouseEvent) => void; + /** Drag a rectangle across the sheet, as in Photo Mechanic. Replaces the selection, or adds to it with Shift/Cmd. */ + onSelectMany?: (ids: string[], focus: string | null) => void; }) { + const root = useRef(null); + const drag = useRef(null); + const swallowClick = useRef(false); + const [band, setBand] = useState<{ left: number; top: number; width: number; height: number } | null>(null); + + const moveBand = (e: React.PointerEvent) => { + const current = drag.current; + const section = root.current; + if (!current || !section || !props.onSelectMany) return; + const dx = e.clientX - current.x; + const dy = e.clientY - current.y; + if (!current.moved && Math.hypot(dx, dy) < DRAG_PX) return; + current.moved = true; + const left = Math.min(current.x, e.clientX); + const top = Math.min(current.y, e.clientY); + const width = Math.abs(e.clientX - current.x); + const height = Math.abs(e.clientY - current.y); + setBand({ left, top, width, height }); + const hit = idsInside(section, new DOMRect(left, top, width, height)); + const ids = current.additive ? [...current.base, ...hit] : hit; + props.onSelectMany(ids, hit[hit.length - 1] ?? ids[ids.length - 1] ?? null); + }; + return (
e.preventDefault()} + onPointerDown={(e) => { + if (e.button !== 0 || !props.onSelectMany) return; + drag.current = { + x: e.clientX, + y: e.clientY, + additive: e.shiftKey || e.metaKey || e.ctrlKey, + base: [...props.selected], + moved: false, + }; + e.currentTarget.setPointerCapture(e.pointerId); + }} + onPointerMove={moveBand} + onPointerUp={(e) => { + if (drag.current?.moved) swallowClick.current = true; + drag.current = null; + setBand(null); + if (e.currentTarget.hasPointerCapture(e.pointerId)) e.currentTarget.releasePointerCapture(e.pointerId); + }} + onClickCapture={(e) => { + if (!swallowClick.current) return; + swallowClick.current = false; + e.preventDefault(); + e.stopPropagation(); + }} > + {band && ( +