Lists: shift-click to select a range of cards
Shift-clicking a selection checkbox in the library, card search, or deck view now selects (or deselects) every card between it and the last one clicked, in on-screen order, so it follows sorting, groups, and deck sections. Shared via a new useRangeSelect hook.
This commit is contained in:
@@ -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<T>(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<string>,
|
||||
setSelected: Dispatch<SetStateAction<Set<string>>>,
|
||||
onSelect?: (ids: string[]) => void,
|
||||
): (id: string) => InputHTMLAttributes<HTMLInputElement> {
|
||||
const scope = useId();
|
||||
const anchor = useRef<string | null>(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<HTMLInputElement>) => {
|
||||
let ids = [id];
|
||||
if (e.shiftKey && anchor.current !== null && anchor.current !== id) {
|
||||
const boxes = [
|
||||
...document.querySelectorAll<HTMLInputElement>(`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";
|
||||
|
||||
@@ -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) => (
|
||||
<tr key={c.oracle_id}>
|
||||
<td className="cell-check">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={selected.has(c.oracle_id)}
|
||||
onChange={() => toggleOne(c.oracle_id)}
|
||||
/>
|
||||
<input {...selectBox(c.oracle_id)} />
|
||||
</td>
|
||||
{columns.shown("image") && (
|
||||
<td className="cell-thumb">
|
||||
@@ -311,11 +301,7 @@ export function CardSearch({
|
||||
selected={selected.has(c.oracle_id)}
|
||||
rotated={horizontalView}
|
||||
cornerLeft={
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={selected.has(c.oracle_id)}
|
||||
onChange={() => toggleOne(c.oracle_id)}
|
||||
/>
|
||||
<input {...selectBox(c.oracle_id)} />
|
||||
}
|
||||
corner={
|
||||
<QtyStepper
|
||||
|
||||
@@ -49,6 +49,7 @@ import {
|
||||
useHoverPreview,
|
||||
useColumns,
|
||||
usePersistedState,
|
||||
useRangeSelect,
|
||||
useTileColumns,
|
||||
useViewMode,
|
||||
fold,
|
||||
@@ -1007,16 +1008,8 @@ function DeckBuilder({
|
||||
}
|
||||
}
|
||||
|
||||
function toggleCard(oracleId: string) {
|
||||
// A manual (de)select always means the whole entry.
|
||||
dropPartial(oracleId);
|
||||
setSelectedCards((s) => {
|
||||
const next = new Set(s);
|
||||
if (next.has(oracleId)) next.delete(oracleId);
|
||||
else next.add(oracleId);
|
||||
return next;
|
||||
});
|
||||
}
|
||||
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={
|
||||
<span className="tile-select">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={selectedCards.has(e.oracle_id)}
|
||||
onChange={() => toggleCard(e.oracle_id)}
|
||||
/>
|
||||
<input {...selectBox(e.oracle_id)} />
|
||||
</span>
|
||||
}
|
||||
corner={
|
||||
@@ -1489,11 +1478,7 @@ function DeckBuilder({
|
||||
{entries.map((e) => (
|
||||
<tr key={e.oracle_id}>
|
||||
<td className="cell-check">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={selectedCards.has(e.oracle_id)}
|
||||
onChange={() => toggleCard(e.oracle_id)}
|
||||
/>
|
||||
<input {...selectBox(e.oracle_id)} />
|
||||
</td>
|
||||
{columns.shown("image") && (
|
||||
<td className="cell-thumb">
|
||||
|
||||
@@ -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) => (
|
||||
<tr key={c.oracle_id}>
|
||||
<td className="cell-check">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={selected.has(c.oracle_id)}
|
||||
onChange={() => toggleOne(c.oracle_id)}
|
||||
/>
|
||||
<input {...selectBox(c.oracle_id)} />
|
||||
</td>
|
||||
{columns.shown("image") && (
|
||||
<td className="cell-thumb">
|
||||
@@ -448,11 +438,7 @@ export function LibraryBrowser({
|
||||
selected={selected.has(c.oracle_id)}
|
||||
cornerLeft={
|
||||
<span className="tile-select">
|
||||
<input
|
||||
type="checkbox"
|
||||
checked={selected.has(c.oracle_id)}
|
||||
onChange={() => toggleOne(c.oracle_id)}
|
||||
/>
|
||||
<input {...selectBox(c.oracle_id)} />
|
||||
</span>
|
||||
}
|
||||
corner={
|
||||
|
||||
Reference in New Issue
Block a user