add ability to move cards
images / check (push) Successful in 30s
images / image (push) Successful in 21s

This commit is contained in:
2026-11-19 18:07:50 +01:00
parent dacd374946
commit 5001cae163
19 changed files with 1197 additions and 57 deletions
+16 -2
View File
@@ -1,4 +1,4 @@
// Where links sit in their cards. Pure, so drag and drop can be tested without a browser.
// Where the cards sit, and the links in them. Pure, so drag and drop can be tested without a browser.
type Card = { id: string; links: { id: string }[] }
@@ -28,6 +28,20 @@ export function moveOver<TCard extends Card>(
)
}
/** While dragging a card: it takes the place of the card it hovers over. */
export function moveCard<TCard extends Card>(
cards: TCard[],
cardId: string,
overId: string
): TCard[] {
const from = cards.findIndex((c) => c.id === cardId)
const to = cards.findIndex((c) => c.id === overId)
if (from < 0 || to < 0 || from === to) return cards
const next = [...cards]
next.splice(to, 0, ...next.splice(from, 1))
return next
}
/** On drop: the link takes the place of the link it was dropped on. */
export function drop<TCard extends Card>(
cards: TCard[],
@@ -45,6 +59,6 @@ export function drop<TCard extends Card>(
return cards.map((c) => (c === card ? { ...c, links } : c))
}
/** What the server stores: per card, its link ids in order. */
/** What the server stores: the cards in order and, per card, its link ids in order. */
export const order = (cards: Card[]) =>
cards.map((c) => ({ id: c.id, linkIds: c.links.map((l) => l.id) }))