Skip to content

Commit 0d3bbb9

Browse files
committed
expose drag event callbacks
1 parent bc2c4db commit 0d3bbb9

File tree

3 files changed

+100
-5
lines changed

3 files changed

+100
-5
lines changed

packages/svelte-file-tree/src/lib/components/TreeItem.svelte

Lines changed: 80 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
item,
2020
ref = $bindable(null),
2121
onDragStart = noop,
22+
onDragEnd = noop,
23+
onDragEnter = noop,
24+
onDragOver = noop,
25+
onDragLeave = noop,
2226
onDrop = noop,
2327
onfocusin,
2428
onkeydown,
@@ -45,9 +49,23 @@
4549
canDrag: (args) => context.canDrag(item, args),
4650
onDragStart: (args) => {
4751
context.onDragStart(item, args);
48-
onDragStart();
52+
onDragStart({
53+
input: args.location.current.input,
54+
source: {
55+
id: item.node.id,
56+
element: args.source.element,
57+
},
58+
});
59+
},
60+
onDrop: (args) => {
61+
onDragEnd({
62+
input: args.location.current.input,
63+
source: {
64+
id: item.node.id,
65+
element: args.source.element,
66+
},
67+
});
4968
},
50-
onDrop: () => onDrop(),
5169
});
5270
});
5371
@@ -63,6 +81,66 @@
6381
});
6482
return canDrop;
6583
},
84+
onDragEnter: (args) => {
85+
const source = args.source;
86+
const sourceId = source.data.id;
87+
if (typeof sourceId !== "string") {
88+
return;
89+
}
90+
91+
onDragEnter({
92+
input: args.location.current.input,
93+
source: {
94+
id: sourceId,
95+
element: source.element,
96+
},
97+
});
98+
},
99+
onDrag: (args) => {
100+
const source = args.source;
101+
const sourceId = source.data.id;
102+
if (typeof sourceId !== "string") {
103+
return;
104+
}
105+
106+
onDragOver({
107+
input: args.location.current.input,
108+
source: {
109+
id: sourceId,
110+
element: source.element,
111+
},
112+
});
113+
},
114+
onDragLeave: (args) => {
115+
const source = args.source;
116+
const sourceId = source.data.id;
117+
if (typeof sourceId !== "string") {
118+
return;
119+
}
120+
121+
onDragLeave({
122+
input: args.location.current.input,
123+
source: {
124+
id: sourceId,
125+
element: source.element,
126+
},
127+
});
128+
},
129+
onDrop: (args) => {
130+
const source = args.source;
131+
const sourceId = source.data.id;
132+
if (typeof sourceId !== "string") {
133+
return;
134+
}
135+
136+
onDrop({
137+
input: args.location.current.input,
138+
source: {
139+
id: sourceId,
140+
element: source.element,
141+
},
142+
});
143+
},
66144
});
67145
});
68146

packages/svelte-file-tree/src/lib/components/types.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { Input as DragInput } from "@atlaskit/pragmatic-drag-and-drop/types";
12
import type { Snippet } from "svelte";
23
import type { HTMLAttributes } from "svelte/elements";
34
import type { SvelteSet } from "svelte/reactivity";
@@ -9,6 +10,8 @@ import type {
910
TreeItemState,
1011
} from "$lib/tree.svelte.js";
1112

13+
export type { DragInput };
14+
1215
export type MaybePromise<T> = T | Promise<T>;
1316

1417
export type TreeChildrenSnippetArgs<
@@ -109,6 +112,16 @@ export interface TreeProps<
109112
onRemove?: (args: OnRemoveArgs<TFile, TFolder>) => void;
110113
}
111114

115+
export type DragSource = {
116+
id: string;
117+
element: HTMLElement;
118+
};
119+
120+
export type DragEventArgs = {
121+
input: DragInput;
122+
source: DragSource;
123+
};
124+
112125
export interface TreeItemProps<
113126
TFile extends FileNode = FileNode,
114127
TFolder extends FolderNode<TFile | TFolder> = DefaultTFolder<TFile>,
@@ -126,6 +139,10 @@ export interface TreeItemProps<
126139
children: Snippet;
127140
item: TreeItemState<TFile, TFolder>;
128141
ref?: HTMLElement | null;
129-
onDragStart?: () => void;
130-
onDrop?: () => void;
142+
onDragStart?: (args: DragEventArgs) => void;
143+
onDragEnd?: (args: DragEventArgs) => void;
144+
onDragEnter?: (args: DragEventArgs) => void;
145+
onDragOver?: (args: DragEventArgs) => void;
146+
onDragLeave?: (args: DragEventArgs) => void;
147+
onDrop?: (args: DragEventArgs) => void;
131148
}

sites/preview/src/lib/components/TreeItem.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
onDragStart={() => {
2323
dragged = true;
2424
}}
25-
onDrop={() => {
25+
onDragEnd={() => {
2626
dragged = false;
2727
}}
2828
>

0 commit comments

Comments
 (0)