void
}) {
+ // while a card is dragged, the arrow keys go from card to card, not through the links
+ const cardDragged = !!useDndContext().active?.data.current?.card
const {
attributes,
listeners,
@@ -92,7 +95,7 @@ export function LinkTile({
transform,
transition,
isDragging,
- } = useSortable({ id: link.id })
+ } = useSortable({ id: link.id, disabled: { droppable: cardDragged } })
return (
dashboard.id, { onDelete: "cascade" }),
+ // order on the dashboard
+ position: integer("position").notNull().default(0),
name: text("name").notNull(),
createdAt: timestamp("created_at").notNull().defaultNow(),
},
diff --git a/src/lib/dashboard.functions.ts b/src/lib/dashboard.functions.ts
index f7bfc29..ccb6910 100644
--- a/src/lib/dashboard.functions.ts
+++ b/src/lib/dashboard.functions.ts
@@ -85,7 +85,8 @@ export const getDashboard = createServerFn({ method: "GET" })
.select({ id: card.id, name: card.name })
.from(card)
.where(and(eq(card.userId, userId), eq(card.dashboardId, dashboardId)))
- .orderBy(asc(card.createdAt)),
+ // cards that were never moved all have position 0: oldest first
+ .orderBy(asc(card.position), asc(card.createdAt)),
db
.select({
id: link.id,
@@ -173,7 +174,13 @@ export const saveCard = createServerFn({ method: "POST" })
.where(and(eq(card.id, id), eq(card.userId, user.id)))
} else {
await requireDashboard(user.id, dashboardId)
- await db.insert(card).values({ name, dashboardId, userId: user.id })
+ await db.insert(card).values({
+ name,
+ dashboardId,
+ userId: user.id,
+ // after the last card of the dashboard
+ position: sql`(select coalesce(max(${card.position}) + 1, 0) from ${card} where ${card.dashboardId} = ${dashboardId})`,
+ })
}
})
@@ -217,8 +224,8 @@ export const deleteCard = createServerFn({ method: "POST" })
.where(and(eq(card.id, data.id), eq(card.userId, user.id)))
})
-/** Stores the result of a drag: for each card, its links in order. */
-export const moveLinks = createServerFn({ method: "POST" })
+/** Stores the result of a drag: the cards in order and, for each card, its links in order. */
+export const saveLayout = createServerFn({ method: "POST" })
.validator(
z.object({
cards: z
@@ -234,15 +241,20 @@ export const moveLinks = createServerFn({ method: "POST" })
.from(card)
.where(and(eq(card.userId, user.id), inArray(card.id, cardIds)))
if (owned.length !== cardIds.length) throw new Error("Unknown card")
- // shortcut: one UPDATE per link, fine for a few hundred links; switch to a
- // single UPDATE ... FROM (VALUES ...) if a save gets slow
+ // shortcut: one UPDATE per card and link, fine for a few hundred links; switch
+ // to a single UPDATE ... FROM (VALUES ...) if a save gets slow
await db.transaction(async (tx) => {
- for (const c of data.cards)
+ for (const [at, c] of data.cards.entries()) {
+ await tx
+ .update(card)
+ .set({ position: at })
+ .where(and(eq(card.id, c.id), eq(card.userId, user.id)))
for (const [position, id] of c.linkIds.entries())
await tx
.update(link)
.set({ cardId: c.id, position })
.where(and(eq(link.id, id), eq(link.userId, user.id)))
+ }
})
})
diff --git a/src/lib/layout.test.ts b/src/lib/layout.test.ts
index e55583b..0b7361b 100644
--- a/src/lib/layout.test.ts
+++ b/src/lib/layout.test.ts
@@ -1,6 +1,6 @@
import assert from "node:assert/strict"
import { test } from "node:test"
-import { drop, moveOver, order } from "./layout.ts"
+import { drop, moveCard, moveOver, order } from "./layout.ts"
const cards = () => [
{ id: "apps", links: [{ id: "a" }, { id: "b" }, { id: "c" }] },
@@ -31,6 +31,18 @@ test("a link dropped on a card goes to its end", () => {
assert.deepEqual(ids(drop(cards(), "a", "empty")), ["bc", "x", "a"])
})
+test("a card takes the place of the card it is over, with its links", () => {
+ assert.deepEqual(ids(moveCard(cards(), "apps", "empty")), ["x", "", "abc"])
+ assert.deepEqual(ids(moveCard(cards(), "empty", "apps")), ["", "abc", "x"])
+})
+
+test("a card over itself, or a link over a card, moves no card", () => {
+ const before = cards()
+ assert.equal(moveCard(before, "apps", "apps"), before)
+ assert.equal(moveCard(before, "a", "web"), before)
+ assert.equal(drop(before, "apps", "web"), before)
+})
+
test("unknown ids change nothing", () => {
const before = cards()
assert.equal(drop(before, "nope", "x"), before)
diff --git a/src/lib/layout.ts b/src/lib/layout.ts
index 4e89f16..4a052e3 100644
--- a/src/lib/layout.ts
+++ b/src/lib/layout.ts
@@ -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(
)
}
+/** While dragging a card: it takes the place of the card it hovers over. */
+export function moveCard(
+ 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(
cards: TCard[],
@@ -45,6 +59,6 @@ export function drop(
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) }))