Skip to content

Commit a09556d

Browse files
committed
Trying to make react-dnd work. Going to try other libraries now.
1 parent eed655f commit a09556d

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

src/react-sortable-tree.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class ReactSortableTree extends Component {
7777
};
7878

7979
this.startDrag = this.startDrag.bind(this);
80+
this.endDrag = this.endDrag.bind(this);
81+
this.dragHover = this.dragHover.bind(this);
8082
}
8183

8284
componentWillMount() {
@@ -117,6 +119,23 @@ class ReactSortableTree extends Component {
117119
});
118120
}
119121

122+
dragHover({ node, parentPath, childIndex }) {
123+
const draggingTreeData = addNodeUnderParentPath({
124+
treeData: this.state.draggingTreeData,
125+
newNode: {
126+
...node,
127+
expanded: false
128+
},
129+
newParentPath: parentPath,
130+
newChildIndex: childIndex,
131+
getNodeKey: this.getNodeKey,
132+
});
133+
134+
this.setState({
135+
rows: this.getRows(draggingTreeData),
136+
});
137+
}
138+
120139
endDrag({ node, path }, dropResult) {
121140
if (!dropResult) {
122141
return this.setState({
@@ -216,13 +235,15 @@ class ReactSortableTree extends Component {
216235
path={path}
217236
lowerSiblingCounts={lowerSiblingCounts}
218237
scaffoldBlockPxWidth={this.props.scaffoldBlockPxWidth}
238+
dragHover={this.dragHover}
219239
>
220240
<NodeContentRenderer
221241
node={node}
222242
path={path}
223243
lowerSiblingCounts={lowerSiblingCounts}
224244
treeIndex={treeIndex}
225245
startDrag={this.startDrag}
246+
endDrag={this.endDrag}
226247
toggleChildrenVisibility={this.toggleChildrenVisibility}
227248
scaffoldBlockPxWidth={this.props.scaffoldBlockPxWidth}
228249
{...nodeProps}

src/utils/drag-and-drop-utils.js

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const myDragSource = {
1111
props.startDrag(props);
1212

1313
return {
14+
node: props.node,
1415
path: props.path,
1516
};
1617
},
@@ -20,16 +21,38 @@ const myDragSource = {
2021
},
2122
};
2223

24+
export function getParentPathFromOffset(
25+
sourcePath,
26+
targetPath,
27+
initialOffsetDifferenceX,
28+
scaffoldBlockPxWidth
29+
) {
30+
const blocksOffset = Math.round(initialOffsetDifferenceX / scaffoldBlockPxWidth);
31+
return targetPath.slice(0, Math.max(0, sourcePath.length + blocksOffset));
32+
}
33+
2334
const myDropTarget = {
24-
drop: (props, _monitor) => {
25-
// TODO: use the offset to determine the path
26-
const { path } = props;
27-
return {
35+
drop: ({ path, scaffoldBlockPxWidth }, monitor) => ({
36+
path: getParentPathFromOffset(
37+
monitor.getItem().path,
2838
path,
29-
};
30-
},
39+
monitor.getDifferenceFromInitialOffset().x,
40+
scaffoldBlockPxWidth
41+
)
42+
}),
43+
44+
hover({ path, dragHover, scaffoldBlockPxWidth }, monitor) {
45+
dragHover({
46+
node: monitor.getItem().node,
47+
parentPath: getParentPathFromOffset(
48+
monitor.getItem().path,
49+
path,
50+
monitor.getDifferenceFromInitialOffset().x,
51+
scaffoldBlockPxWidth
52+
),
53+
childIndex: 0,
54+
});
3155

32-
hover(_props, _monitor) {
3356
// const { treeIndex: draggedId } = monitor.getItem();
3457
// const { treeIndex: overId } = props;
3558

@@ -41,7 +64,7 @@ const myDropTarget = {
4164

4265
canDrop(props, monitor) {
4366
// Cannot drag into a child path, and cannot drag into your current path
44-
return monitor.getItem().path
67+
return typeof props.children !== 'function' && monitor.getItem().path
4568
.some((key, index) => (props.path[index] !== key));
4669
},
4770
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {
2+
getParentPathFromOffset,
3+
} from './drag-and-drop-utils';
4+
5+
describe('getParentPathFromOffset', () => {
6+
it('should handle same-size path', () => {
7+
expect(getParentPathFromOffset(['a', 'b'], [1, 2], -999, 10)).toEqual([]);
8+
expect(getParentPathFromOffset(['a', 'b'], [1, 2], -16, 10)).toEqual([]);
9+
expect(getParentPathFromOffset(['a', 'b'], [1, 2], -6, 10)).toEqual([1]);
10+
expect(getParentPathFromOffset(['a', 'b'], [1, 2], 0, 10)).toEqual([1, 2]);
11+
expect(getParentPathFromOffset(['a', 'b'], [1, 2], 1, 10)).toEqual([1, 2]);
12+
expect(getParentPathFromOffset(['a', 'b'], [1, 2], 6, 10)).toEqual([1, 2]);
13+
expect(getParentPathFromOffset(['a', 'b'], [1, 2], 16, 10)).toEqual([1, 2]);
14+
expect(getParentPathFromOffset(['a', 'b'], [1, 2], 999, 10)).toEqual([1, 2]);
15+
});
16+
17+
it('should handle longer targetPath', () => {
18+
expect(getParentPathFromOffset(['a', 'b'], [1, 2, 3], -999, 10)).toEqual([]);
19+
expect(getParentPathFromOffset(['a', 'b'], [1, 2, 3], -16, 10)).toEqual([]);
20+
expect(getParentPathFromOffset(['a', 'b'], [1, 2, 3], -6, 10)).toEqual([1]);
21+
expect(getParentPathFromOffset(['a', 'b'], [1, 2, 3], 0, 10)).toEqual([1, 2]);
22+
expect(getParentPathFromOffset(['a', 'b'], [1, 2, 3], 1, 10)).toEqual([1, 2]);
23+
expect(getParentPathFromOffset(['a', 'b'], [1, 2, 3], 6, 10)).toEqual([1, 2, 3]);
24+
expect(getParentPathFromOffset(['a', 'b'], [1, 2, 3], 16, 10)).toEqual([1, 2, 3]);
25+
expect(getParentPathFromOffset(['a', 'b'], [1, 2, 3], 999, 10)).toEqual([1, 2, 3]);
26+
});
27+
28+
it('should handle longer sourcePath', () => {
29+
expect(getParentPathFromOffset(['a', 'b', 'c'], [1, 2], -999, 10)).toEqual([]);
30+
expect(getParentPathFromOffset(['a', 'b', 'c'], [1, 2], -16, 10)).toEqual([1]);
31+
expect(getParentPathFromOffset(['a', 'b', 'c'], [1, 2], -6, 10)).toEqual([1, 2]);
32+
expect(getParentPathFromOffset(['a', 'b', 'c'], [1, 2], 0, 10)).toEqual([1, 2]);
33+
expect(getParentPathFromOffset(['a', 'b', 'c'], [1, 2], 1, 10)).toEqual([1, 2]);
34+
expect(getParentPathFromOffset(['a', 'b', 'c'], [1, 2], 6, 10)).toEqual([1, 2]);
35+
expect(getParentPathFromOffset(['a', 'b', 'c'], [1, 2], 16, 10)).toEqual([1, 2]);
36+
expect(getParentPathFromOffset(['a', 'b', 'c'], [1, 2], 999, 10)).toEqual([1, 2]);
37+
});
38+
});

0 commit comments

Comments
 (0)