Skip to content

Commit

Permalink
fix(tree-to-tree): Fix node depth when dragging between trees
Browse files Browse the repository at this point in the history
  • Loading branch information
fritz-c committed Aug 6, 2017
1 parent a1005f6 commit 323ccad
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/utils/drag-and-drop-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,20 @@ function getTargetDepth(dropTargetProps, monitor, component) {
}

let blocksOffset;
let dragSourceInitialDepth = (monitor.getItem().path || []).length;

// When adding node from external source
if (monitor.getItem().treeId !== dropTargetProps.treeId) {
// Ignore the tree depth of the source, if it had any to begin with
dragSourceInitialDepth = 0;

if (component) {
const relativePosition = findDOMNode(component).getBoundingClientRect(); // eslint-disable-line react/no-find-dom-node
const leftShift =
monitor.getSourceClientOffset().x - relativePosition.left;
blocksOffset =
Math.round(leftShift / dropTargetProps.scaffoldBlockPxWidth) - 1;
blocksOffset = Math.round(
leftShift / dropTargetProps.scaffoldBlockPxWidth
);
} else {
blocksOffset = dropTargetProps.path.length;
}
Expand All @@ -64,10 +70,9 @@ function getTargetDepth(dropTargetProps, monitor, component) {
);
}

const dragSourcePath = monitor.getItem().path || [];
let targetDepth = Math.min(
dropTargetDepth,
Math.max(0, dragSourcePath.length + blocksOffset - 1)
Math.max(0, dragSourceInitialDepth + blocksOffset - 1)
);

// If a maxDepth is defined, constrain the target depth
Expand Down

0 comments on commit 323ccad

Please sign in to comment.