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 {
|
import {
|
||||||
createContext,
|
createContext,
|
||||||
|
Dispatch,
|
||||||
forwardRef,
|
forwardRef,
|
||||||
|
InputHTMLAttributes,
|
||||||
|
MouseEvent as ReactMouseEvent,
|
||||||
ReactNode,
|
ReactNode,
|
||||||
|
SetStateAction,
|
||||||
useCallback,
|
useCallback,
|
||||||
useContext,
|
useContext,
|
||||||
useEffect,
|
useEffect,
|
||||||
|
useId,
|
||||||
useLayoutEffect,
|
useLayoutEffect,
|
||||||
useRef,
|
useRef,
|
||||||
useState,
|
useState,
|
||||||
@@ -2029,6 +2034,60 @@ export function usePersistedState<T>(key: string, initial: T): [T, (v: T) => voi
|
|||||||
return [val, set];
|
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) ------------------------------------
|
// --- View mode (list vs. image tiles) ------------------------------------
|
||||||
|
|
||||||
export type ViewMode = "list" | "tiles";
|
export type ViewMode = "list" | "tiles";
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import {
|
|||||||
sortCards,
|
sortCards,
|
||||||
usePersistedState,
|
usePersistedState,
|
||||||
useColumns,
|
useColumns,
|
||||||
|
useRangeSelect,
|
||||||
useTileColumns,
|
useTileColumns,
|
||||||
useViewMode,
|
useViewMode,
|
||||||
} from "../components";
|
} from "../components";
|
||||||
@@ -128,14 +129,7 @@ export function CardSearch({
|
|||||||
}
|
}
|
||||||
}, [selected, rows]);
|
}, [selected, rows]);
|
||||||
|
|
||||||
function toggleOne(oracleId: string) {
|
const selectBox = useRangeSelect(selected, setSelected);
|
||||||
setSelected((s) => {
|
|
||||||
const next = new Set(s);
|
|
||||||
if (next.has(oracleId)) next.delete(oracleId);
|
|
||||||
else next.add(oracleId);
|
|
||||||
return next;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function toggleSelectAll() {
|
function toggleSelectAll() {
|
||||||
setSelected((s) => (s.size === rows.length ? new Set() : new Set(rows.map((r) => r.oracle_id))));
|
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) => (
|
{cards.map((c) => (
|
||||||
<tr key={c.oracle_id}>
|
<tr key={c.oracle_id}>
|
||||||
<td className="cell-check">
|
<td className="cell-check">
|
||||||
<input
|
<input {...selectBox(c.oracle_id)} />
|
||||||
type="checkbox"
|
|
||||||
checked={selected.has(c.oracle_id)}
|
|
||||||
onChange={() => toggleOne(c.oracle_id)}
|
|
||||||
/>
|
|
||||||
</td>
|
</td>
|
||||||
{columns.shown("image") && (
|
{columns.shown("image") && (
|
||||||
<td className="cell-thumb">
|
<td className="cell-thumb">
|
||||||
@@ -311,11 +301,7 @@ export function CardSearch({
|
|||||||
selected={selected.has(c.oracle_id)}
|
selected={selected.has(c.oracle_id)}
|
||||||
rotated={horizontalView}
|
rotated={horizontalView}
|
||||||
cornerLeft={
|
cornerLeft={
|
||||||
<input
|
<input {...selectBox(c.oracle_id)} />
|
||||||
type="checkbox"
|
|
||||||
checked={selected.has(c.oracle_id)}
|
|
||||||
onChange={() => toggleOne(c.oracle_id)}
|
|
||||||
/>
|
|
||||||
}
|
}
|
||||||
corner={
|
corner={
|
||||||
<QtyStepper
|
<QtyStepper
|
||||||
|
|||||||
@@ -49,6 +49,7 @@ import {
|
|||||||
useHoverPreview,
|
useHoverPreview,
|
||||||
useColumns,
|
useColumns,
|
||||||
usePersistedState,
|
usePersistedState,
|
||||||
|
useRangeSelect,
|
||||||
useTileColumns,
|
useTileColumns,
|
||||||
useViewMode,
|
useViewMode,
|
||||||
fold,
|
fold,
|
||||||
@@ -1007,16 +1008,8 @@ function DeckBuilder({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleCard(oracleId: string) {
|
// A manual (de)select always means the whole entry.
|
||||||
// A manual (de)select always means the whole entry.
|
const selectBox = useRangeSelect(selectedCards, setSelectedCards, (ids) => ids.forEach(dropPartial));
|
||||||
dropPartial(oracleId);
|
|
||||||
setSelectedCards((s) => {
|
|
||||||
const next = new Set(s);
|
|
||||||
if (next.has(oracleId)) next.delete(oracleId);
|
|
||||||
else next.add(oracleId);
|
|
||||||
return next;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function dropPartial(oracleId: string) {
|
function dropPartial(oracleId: string) {
|
||||||
setPartialRemoval((m) => {
|
setPartialRemoval((m) => {
|
||||||
@@ -1415,11 +1408,7 @@ function DeckBuilder({
|
|||||||
selected={selectedCards.has(e.oracle_id)}
|
selected={selectedCards.has(e.oracle_id)}
|
||||||
cornerLeft={
|
cornerLeft={
|
||||||
<span className="tile-select">
|
<span className="tile-select">
|
||||||
<input
|
<input {...selectBox(e.oracle_id)} />
|
||||||
type="checkbox"
|
|
||||||
checked={selectedCards.has(e.oracle_id)}
|
|
||||||
onChange={() => toggleCard(e.oracle_id)}
|
|
||||||
/>
|
|
||||||
</span>
|
</span>
|
||||||
}
|
}
|
||||||
corner={
|
corner={
|
||||||
@@ -1489,11 +1478,7 @@ function DeckBuilder({
|
|||||||
{entries.map((e) => (
|
{entries.map((e) => (
|
||||||
<tr key={e.oracle_id}>
|
<tr key={e.oracle_id}>
|
||||||
<td className="cell-check">
|
<td className="cell-check">
|
||||||
<input
|
<input {...selectBox(e.oracle_id)} />
|
||||||
type="checkbox"
|
|
||||||
checked={selectedCards.has(e.oracle_id)}
|
|
||||||
onChange={() => toggleCard(e.oracle_id)}
|
|
||||||
/>
|
|
||||||
</td>
|
</td>
|
||||||
{columns.shown("image") && (
|
{columns.shown("image") && (
|
||||||
<td className="cell-thumb">
|
<td className="cell-thumb">
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ import {
|
|||||||
sortCards,
|
sortCards,
|
||||||
usePersistedState,
|
usePersistedState,
|
||||||
useColumns,
|
useColumns,
|
||||||
|
useRangeSelect,
|
||||||
useTileColumns,
|
useTileColumns,
|
||||||
useViewMode,
|
useViewMode,
|
||||||
} from "../components";
|
} from "../components";
|
||||||
@@ -177,14 +178,7 @@ export function LibraryBrowser({
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function toggleOne(oracleId: string) {
|
const selectBox = useRangeSelect(selected, setSelected);
|
||||||
setSelected((s) => {
|
|
||||||
const next = new Set(s);
|
|
||||||
if (next.has(oracleId)) next.delete(oracleId);
|
|
||||||
else next.add(oracleId);
|
|
||||||
return next;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function toggleSelectAll() {
|
function toggleSelectAll() {
|
||||||
setSelected((s) => (s.size === rows.length ? new Set() : new Set(rows.map((r) => r.oracle_id))));
|
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) => (
|
{cards.map((c) => (
|
||||||
<tr key={c.oracle_id}>
|
<tr key={c.oracle_id}>
|
||||||
<td className="cell-check">
|
<td className="cell-check">
|
||||||
<input
|
<input {...selectBox(c.oracle_id)} />
|
||||||
type="checkbox"
|
|
||||||
checked={selected.has(c.oracle_id)}
|
|
||||||
onChange={() => toggleOne(c.oracle_id)}
|
|
||||||
/>
|
|
||||||
</td>
|
</td>
|
||||||
{columns.shown("image") && (
|
{columns.shown("image") && (
|
||||||
<td className="cell-thumb">
|
<td className="cell-thumb">
|
||||||
@@ -448,11 +438,7 @@ export function LibraryBrowser({
|
|||||||
selected={selected.has(c.oracle_id)}
|
selected={selected.has(c.oracle_id)}
|
||||||
cornerLeft={
|
cornerLeft={
|
||||||
<span className="tile-select">
|
<span className="tile-select">
|
||||||
<input
|
<input {...selectBox(c.oracle_id)} />
|
||||||
type="checkbox"
|
|
||||||
checked={selected.has(c.oracle_id)}
|
|
||||||
onChange={() => toggleOne(c.oracle_id)}
|
|
||||||
/>
|
|
||||||
</span>
|
</span>
|
||||||
}
|
}
|
||||||
corner={
|
corner={
|
||||||
|
|||||||
Reference in New Issue
Block a user