Skip to content

Commit a83625c

Browse files
committed
preview: add duplicate checking to external dnd
1 parent 80a4ba6 commit a83625c

File tree

1 file changed

+48
-36
lines changed

1 file changed

+48
-36
lines changed

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

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
type FileTree,
99
type FileTreeNode,
1010
type FolderNode,
11-
type OnChildrenChangeArgs,
1211
type OnCircularReferenceArgs,
1312
type OnMoveArgs,
1413
type OnRemoveArgs,
@@ -47,15 +46,9 @@
4746
nodes.sort((a, b) => a.name.localeCompare(b.name));
4847
}
4948
50-
function onChildrenChange(args: OnChildrenChangeArgs) {
51-
if (args.operation === "insert") {
52-
sortByName(args.children);
53-
}
54-
}
55-
56-
function onResolveNameConflict(args: OnResolveNameConflictArgs) {
49+
function onResolveNameConflict({ operation, name }: OnResolveNameConflictArgs) {
5750
let title: string;
58-
switch (args.operation) {
51+
switch (operation) {
5952
case "copy": {
6053
title = "Failed to copy items";
6154
break;
@@ -68,64 +61,84 @@
6861
6962
return resolveConflictDialog!.show({
7063
title,
71-
description: `An item named "${args.name}" already exists in this location. Do you want to skip it or cancel the operation entirely?`,
64+
description: `An item named "${name}" already exists in this location. Do you want to skip it or cancel the operation entirely?`,
7265
});
7366
}
7467
75-
function onCircularReference(args: OnCircularReferenceArgs) {
76-
toast.error(`Cannot move "${args.source.node.name}" inside itself`);
68+
function onCircularReference({ source }: OnCircularReferenceArgs) {
69+
toast.error(`Cannot move "${source.node.name}" inside itself`);
7770
}
7871
79-
function canRemove(args: OnRemoveArgs) {
72+
function canRemove({ removed }: OnRemoveArgs) {
8073
return confirmRemoveDialog!.show({
81-
title: `Are you sure you want to delete ${args.removed.length} item(s)?`,
74+
title: `Are you sure you want to delete ${removed.length} item(s)?`,
8275
description: "They will be permanently deleted. This action cannot be undone.",
8376
});
8477
}
8578
86-
function onCopy(args: OnMoveArgs) {
87-
if (args.destination.type === "folder") {
88-
startBorderAnimation(args.destination.id);
79+
function onCopy({ destination }: OnMoveArgs) {
80+
sortByName(destination.children);
81+
82+
if (destination.type === "folder") {
83+
startBorderAnimation(destination.id);
8984
}
9085
}
9186
92-
function onMove(args: OnMoveArgs) {
93-
if (args.destination.type === "folder") {
94-
startBorderAnimation(args.destination.id);
87+
function onMove({ destination }: OnMoveArgs) {
88+
sortByName(destination.children);
89+
90+
if (destination.type === "folder") {
91+
startBorderAnimation(destination.id);
9592
}
9693
}
9794
98-
function onDrag(args: DragEventArgs) {
99-
dropDestination = args.destination;
95+
function onDrag({ destination }: DragEventArgs) {
96+
dropDestination = destination;
10097
}
10198
10299
function onDragLeave() {
103100
dropDestination = undefined;
104101
}
105102
106-
function onDrop(args: DragEventArgs) {
103+
function onDrop({ type, items, destination }: DragEventArgs) {
107104
dropDestination = undefined;
108105
109-
if (args.type !== "external") {
106+
if (type !== "external") {
110107
return;
111108
}
112109
110+
const uniqueNames = new Set();
111+
for (const child of destination.children) {
112+
uniqueNames.add(child.name);
113+
}
114+
113115
const files: Array<FileNode> = [];
114-
for (const item of args.items) {
116+
for (const item of items) {
115117
const file = item.getAsFile();
116-
if (file !== null) {
117-
files.push(
118-
new FileNode({
119-
id: crypto.randomUUID(),
120-
name: file.name,
121-
}),
122-
);
118+
if (file === null) {
119+
continue;
120+
}
121+
122+
const fileName = file.name;
123+
if (uniqueNames.has(fileName)) {
124+
toast.error(`An item named "${fileName}" already exists in this location`);
125+
return;
123126
}
127+
128+
files.push(
129+
new FileNode({
130+
id: crypto.randomUUID(),
131+
name: fileName,
132+
}),
133+
);
124134
}
125135
126-
const children = args.destination.children;
127-
children.push(...files);
128-
sortByName(children);
136+
destination.children.push(...files);
137+
sortByName(destination.children);
138+
139+
if (destination.type === "folder") {
140+
startBorderAnimation(destination.id);
141+
}
129142
}
130143
131144
function onExpand(item: TreeItemState) {
@@ -160,7 +173,6 @@
160173
<Tree
161174
{root}
162175
{expandedIds}
163-
{onChildrenChange}
164176
{onResolveNameConflict}
165177
{onCircularReference}
166178
{canRemove}

0 commit comments

Comments
 (0)