Skip to content

Commit d50c9ab

Browse files
authored
Move drop logic from drag end to drop (#297)
1 parent 0b8577f commit d50c9ab

File tree

3 files changed

+19
-18
lines changed

3 files changed

+19
-18
lines changed

modules/react-arborist/src/dnd/drag-hook.ts

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { NodeApi } from "../interfaces/node-api";
66
import { DragItem } from "../types/dnd";
77
import { DropResult } from "./drop-hook";
88
import { actions as dnd } from "../state/dnd-slice";
9-
import { safeRun } from "../utils";
10-
import { ROOT_ID } from "../data/create-root";
119

1210
export function useDragHook<T>(node: NodeApi<T>): ConnectDragSource {
1311
const tree = useTreeApi();
@@ -24,23 +22,10 @@ export function useDragHook<T>(node: NodeApi<T>): ConnectDragSource {
2422
},
2523
end: () => {
2624
tree.hideCursor();
27-
let { parentId, index, dragIds } = tree.state.dnd;
28-
// If they held down meta, we need to create a copy
29-
// if (drop.dropEffect === "copy")
30-
if (tree.canDrop()) {
31-
safeRun(tree.props.onMove, {
32-
dragIds,
33-
parentId: parentId === ROOT_ID ? null : parentId,
34-
index: index === null ? 0 : index, // When it's null it was dropped over a folder
35-
dragNodes: tree.dragNodes,
36-
parentNode: tree.get(parentId),
37-
});
38-
tree.open(parentId);
39-
}
4025
tree.dispatch(dnd.dragEnd());
4126
},
4227
}),
43-
[ids, node]
28+
[ids, node],
4429
);
4530

4631
useEffect(() => {

modules/react-arborist/src/dnd/drop-hook.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { NodeApi } from "../interfaces/node-api";
55
import { DragItem } from "../types/dnd";
66
import { computeDrop } from "./compute-drop";
77
import { actions as dnd } from "../state/dnd-slice";
8+
import { safeRun } from "../utils";
9+
import { ROOT_ID } from "../data/create-root";
810

911
export type DropResult = {
1012
parentId: string | null;
@@ -13,7 +15,7 @@ export type DropResult = {
1315

1416
export function useDropHook(
1517
el: RefObject<HTMLElement | null>,
16-
node: NodeApi<any>
18+
node: NodeApi<any>,
1719
): ConnectDropTarget {
1820
const tree = useTreeApi();
1921
const [_, dropRef] = useDrop<DragItem, DropResult | null, void>(
@@ -41,9 +43,18 @@ export function useDropHook(
4143
},
4244
drop: (_, m) => {
4345
if (!m.canDrop()) return null;
46+
let { parentId, index, dragIds } = tree.state.dnd;
47+
safeRun(tree.props.onMove, {
48+
dragIds,
49+
parentId: parentId === ROOT_ID ? null : parentId,
50+
index: index === null ? 0 : index, // When it's null it was dropped over a folder
51+
dragNodes: tree.dragNodes,
52+
parentNode: tree.get(parentId),
53+
});
54+
tree.open(parentId);
4455
},
4556
}),
46-
[node, el.current, tree.props]
57+
[node, el.current, tree.props],
4758
);
4859

4960
return dropRef;

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,13 @@
55
],
66
"scripts": {
77
"build": "yarn workspaces foreach --all run build",
8+
"build-lib": "yarn workspace react-arborist build",
89
"test": "yarn workspaces foreach --all run test",
10+
"watch": "yarn workspace react-arborist watch",
911
"bump": "yarn workspace react-arborist version",
12+
"clean": "yarn workspace react-arborist clean",
13+
"showcase": "yarn workspace showcase start",
14+
"start": "run-s clean build-lib && run-p watch showcase",
1015
"publish": "sh bin/publish"
1116
},
1217
"private": true,

0 commit comments

Comments
 (0)