add ability to move cards
This commit is contained in:
+16
-2
@@ -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) }))
|
||||
|
||||
Reference in New Issue
Block a user