Skip to content

Commit b4aa3bb

Browse files
authored
Merge pull request #80 from gtmun/dnd-kit-0.4
Bump to dnd-kit 0.4.0
2 parents 1a0a827 + 91d173c commit b4aa3bb

5 files changed

Lines changed: 58 additions & 55 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
},
4040
"type": "module",
4141
"dependencies": {
42-
"@dnd-kit/svelte": "^0.3.2",
42+
"@dnd-kit/dom": "^0.4.0",
43+
"@dnd-kit/svelte": "^0.4.0",
4344
"chroma-js": "^3.2.0",
4445
"csv": "^6.4.1",
4546
"dexie": "^4.3.0",

pnpm-lock.yaml

Lines changed: 35 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/lib/components/SpeakerList.svelte

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import { type Delegate, findDelegate } from "$lib/db/delegates";
1818
import type { DelegateID, Speaker, SpeakerEntryID } from "$lib/types";
1919
import { a11yLabel } from "$lib/util";
20-
import { createSortable, handleDrag, move } from "$lib/util/dnd";
20+
import { createSortable, handleDrag } from "$lib/util/dnd";
2121
import { proxify } from "$lib/util/sv.svelte";
2222
import MdiCancel from "~icons/mdi/cancel";
2323
import MdiDelete from "~icons/mdi/delete";
@@ -285,10 +285,8 @@
285285
</h5>
286286

287287
<DragDropProvider
288-
onDragMove={handleDrag(dndItems)}
289-
onDragEnd={handleDrag((oldIdx, newIdx) => {
290-
move(dndItems, oldIdx, newIdx);
291-
288+
onDragOver={handleDrag(dndItems)}
289+
onDragEnd={handleDrag(() => {
292290
if (insertPoint > 0) {
293291
const original = order.slice(-insertPoint);
294292
const dragged = dndItems.slice(-insertPoint);
@@ -306,14 +304,14 @@
306304
aria-labelledby="sl-header-{sid}"
307305
>
308306
{#each dndItems as speaker, i (speaker.id)}
309-
{@const sortable = createSortable({ id: speaker.id, index: i })}
307+
{@const sortable = createSortable({ id: speaker.id, get index() { return i; } })}
310308
{@const selected = speaker.id === selectedSpeakerId}
311309
{@const delAttrs = findDelegate(delegates, speaker.key)}
312310
{@const speakerLabel = delAttrs?.name ?? "unknown"}
313311

314312
<li
315313
class={[
316-
"flex items-center gap-1 p-1",
314+
"flex items-center gap-1 p-1 preset-ui",
317315
"data-dnd-dragging:rounded data-dnd-dragging:preset-tonal-primary",
318316
"data-dnd-placeholder:rounded data-dnd-placeholder:*:invisible data-dnd-placeholder:bg-surface-200-800",
319317
// If insert point exists, color the border where the insert point starts EXCEPT when dragging

src/lib/util/dnd.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,15 @@
1111
* or the moving element by using data-dnd-dragging:... as Tailwind classes.
1212
*/
1313

14+
import { Feedback, type FeedbackType } from "@dnd-kit/dom";
1415
import type { DragDropEventHandlers } from "@dnd-kit/svelte";
1516
import { createSortable as _createSortable, isSortable, type CreateSortableInput } from "@dnd-kit/svelte/sortable";
1617

17-
export function createSortable(input: CreateSortableInput) {
18-
return _createSortable({ feedback: "clone", ...input });
18+
export function createSortable(input: CreateSortableInput, feedback: FeedbackType = "clone") {
19+
return _createSortable({
20+
...input,
21+
plugins: (defaults) => [...defaults, Feedback.configure({ feedback })]
22+
});
1923
}
2024

2125
type OnMove = (oldIdx: number, newIdx: number) => void;
@@ -30,9 +34,9 @@ type DndEventHandlerParam = Pick<Parameters<DndEventHandler>[0], "operation">;
3034
*
3135
* ```svelte
3236
* <DragDropProvider
33-
* onDragMove={handleDrag(dndItems)}
37+
* onDragOver={handleDrag(dndItems)}
3438
* onDragEnd={handleDrag(
35-
* (oldIdx, newIdx) => order = move(dndItems, oldIdx, newIdx),
39+
* () => order = dndItems,
3640
* { delay: 300 }
3741
* )}
3842
* >
@@ -66,12 +70,9 @@ export function handleDrag(
6670

6771
// Actual event handler, which finds the indexes and performs the move on the indices.
6872
return (e: DndEventHandlerParam) => {
69-
const { source, canceled } = e.operation;
70-
if (!canceled && isSortable(source)) {
71-
const oldIdx = source.sortable.initialIndex;
72-
const newIdx = source.sortable.index;
73-
74-
callback(oldIdx, newIdx);
73+
const { source, target, canceled } = e.operation;
74+
if (!canceled && isSortable(source) && isSortable(target)) {
75+
callback(source.index, target.index);
7576
}
7677
};
7778
}

src/routes/dashboard/points-motions/+page.svelte

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import { compareMotions as motionComparator } from "$lib/motions/sort";
2424
import type { Motion } from "$lib/types";
2525
import { a11yLabel, hasKey } from "$lib/util";
26-
import { createSortable, handleDrag, move } from "$lib/util/dnd";
26+
import { createSortable, handleDrag } from "$lib/util/dnd";
2727
import { proxify } from "$lib/util/sv.svelte";
2828
import { stringifyTime } from "$lib/util/time";
2929
import MdiAccountClock from "~icons/mdi/account-clock";
@@ -197,17 +197,17 @@
197197
class="bg-surface-50-950"
198198
>
199199
<DragDropProvider
200-
onDragMove={handleDrag(dndItems)}
201-
onDragEnd={handleDrag((oldIdx, newIdx) => $motions = move(dndItems, oldIdx, newIdx), { delay: 300 })}
200+
onDragOver={handleDrag(dndItems)}
201+
onDragEnd={handleDrag(() => $motions = dndItems, { delay: 300 })}
202202
>
203203
{#each dndItems as motion, i (motion.id)}
204204
{@const delAttrs = findDelegate($delegates, motion.delegate)}
205205
{@const delName = delAttrs?.name ?? "unknown"}
206-
{@const sortable = createSortable({ id: motion.id, index: i, feedback: "default" })}
206+
{@const sortable = createSortable({ id: motion.id, get index() { return i; }}, "default")}
207207
<tr
208208
{@attach sortable.attach}
209209
class={[
210-
"hover:preset-tonal-primary [&_td]:tabular-nums",
210+
"preset-tonal-surface hover:preset-tonal-primary [&_td]:tabular-nums",
211211
"data-dnd-dragging:preset-tonal-primary"
212212
]}
213213
animate:flip={{ duration: 150 }}

0 commit comments

Comments
 (0)