diff --git a/src/components.tsx b/src/components.tsx index fe8192f..578477e 100644 --- a/src/components.tsx +++ b/src/components.tsx @@ -1,10 +1,15 @@ import { createContext, + Dispatch, forwardRef, + InputHTMLAttributes, + MouseEvent as ReactMouseEvent, ReactNode, + SetStateAction, useCallback, useContext, useEffect, + useId, useLayoutEffect, useRef, useState, @@ -2029,6 +2034,60 @@ export function usePersistedState(key: string, initial: T): [T, (v: T) => voi return [val, set]; } +// --- Range selection ----------------------------------------------------- + +/** + * Selection checkboxes with the usual shift-click: shift-clicking a box + * selects (or deselects) it and every box between it and the last one + * clicked. "Between" is the order the boxes are on the page, so it follows + * sorting, grouping and sections without each view working that out. + * Returns the props for one card's checkbox; `onSelect` sees the ids each + * click changes before they're applied. + */ +export function useRangeSelect( + selected: Set, + setSelected: Dispatch>>, + onSelect?: (ids: string[]) => void, +): (id: string) => InputHTMLAttributes { + const scope = useId(); + const anchor = useRef(null); + return (id) => ({ + type: "checkbox", + checked: selected.has(id), + ...{ "data-range-select": scope, "data-id": id }, + // The click below does the toggling; it's the event that knows about shift. + onChange: () => {}, + // Keep shift-click from also selecting the text between the two boxes. + onMouseDown: (e) => { + if (e.shiftKey) e.preventDefault(); + }, + onClick: (e: ReactMouseEvent) => { + let ids = [id]; + if (e.shiftKey && anchor.current !== null && anchor.current !== id) { + const boxes = [ + ...document.querySelectorAll(`input[data-range-select="${CSS.escape(scope)}"]`), + ]; + const from = boxes.findIndex((b) => b.dataset.id === anchor.current); + const to = boxes.indexOf(e.currentTarget); + if (from >= 0 && to >= 0) { + ids = boxes.slice(Math.min(from, to), Math.max(from, to) + 1).map((b) => b.dataset.id!); + } + } + anchor.current = id; + const on = !selected.has(id); + onSelect?.(ids); + setSelected((s) => { + const next = new Set(s); + for (const i of ids) { + if (on) next.add(i); + else next.delete(i); + } + return next; + }); + }, + }); +} + // --- View mode (list vs. image tiles) ------------------------------------ export type ViewMode = "list" | "tiles"; diff --git a/src/views/CardSearch.tsx b/src/views/CardSearch.tsx index c910c9e..d5cbcae 100644 --- a/src/views/CardSearch.tsx +++ b/src/views/CardSearch.tsx @@ -21,6 +21,7 @@ import { sortCards, usePersistedState, useColumns, + useRangeSelect, useTileColumns, useViewMode, } from "../components"; @@ -128,14 +129,7 @@ export function CardSearch({ } }, [selected, rows]); - function toggleOne(oracleId: string) { - setSelected((s) => { - const next = new Set(s); - if (next.has(oracleId)) next.delete(oracleId); - else next.add(oracleId); - return next; - }); - } + const selectBox = useRangeSelect(selected, setSelected); function toggleSelectAll() { setSelected((s) => (s.size === rows.length ? new Set() : new Set(rows.map((r) => r.oracle_id)))); @@ -247,11 +241,7 @@ export function CardSearch({ {cards.map((c) => ( - toggleOne(c.oracle_id)} - /> + {columns.shown("image") && ( @@ -311,11 +301,7 @@ export function CardSearch({ selected={selected.has(c.oracle_id)} rotated={horizontalView} cornerLeft={ - toggleOne(c.oracle_id)} - /> + } corner={ { - const next = new Set(s); - if (next.has(oracleId)) next.delete(oracleId); - else next.add(oracleId); - return next; - }); - } + // A manual (de)select always means the whole entry. + const selectBox = useRangeSelect(selectedCards, setSelectedCards, (ids) => ids.forEach(dropPartial)); function dropPartial(oracleId: string) { setPartialRemoval((m) => { @@ -1415,11 +1408,7 @@ function DeckBuilder({ selected={selectedCards.has(e.oracle_id)} cornerLeft={ - toggleCard(e.oracle_id)} - /> + } corner={ @@ -1489,11 +1478,7 @@ function DeckBuilder({ {entries.map((e) => ( - toggleCard(e.oracle_id)} - /> + {columns.shown("image") && ( diff --git a/src/views/LibraryBrowser.tsx b/src/views/LibraryBrowser.tsx index c970500..26d13f7 100644 --- a/src/views/LibraryBrowser.tsx +++ b/src/views/LibraryBrowser.tsx @@ -33,6 +33,7 @@ import { sortCards, usePersistedState, useColumns, + useRangeSelect, useTileColumns, useViewMode, } from "../components"; @@ -177,14 +178,7 @@ export function LibraryBrowser({ } } - function toggleOne(oracleId: string) { - setSelected((s) => { - const next = new Set(s); - if (next.has(oracleId)) next.delete(oracleId); - else next.add(oracleId); - return next; - }); - } + const selectBox = useRangeSelect(selected, setSelected); function toggleSelectAll() { setSelected((s) => (s.size === rows.length ? new Set() : new Set(rows.map((r) => r.oracle_id)))); @@ -388,11 +382,7 @@ export function LibraryBrowser({ {cards.map((c) => ( - toggleOne(c.oracle_id)} - /> + {columns.shown("image") && ( @@ -448,11 +438,7 @@ export function LibraryBrowser({ selected={selected.has(c.oracle_id)} cornerLeft={ - toggleOne(c.oracle_id)} - /> + } corner={