Skip to content

Commit

Permalink
feat: target parent when dragging on child (#219)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasbach committed Feb 24, 2023
1 parent 2d4c6e3 commit 5f61f4f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 15 deletions.
9 changes: 2 additions & 7 deletions next-release-notes.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
<!--
### Breaking Changes
### Features
### Bug Fixes and Improvements

### Other Changes
-->
* Dragging on a non-folder item with `canDropOnNonFolder=false` and
`canReorderItems=false` will now target the item's parent. (#219)
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const useGetGetParentOfLinearItem = () => {
parentLinearIndex = 0;
}

return parent;
return { parent, parentLinearIndex };
},
[environment.linearItems, environment.trees]
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const useGetViableDragPositions = () => {
const linearItems = environment.linearItems[treeId];
return linearItems
.map<DraggingPosition[]>(({ item, depth }, linearIndex) => {
const parent = getParentOfLinearItem(linearIndex, treeId);
const { parent } = getParentOfLinearItem(linearIndex, treeId);
const childIndex =
environment.items[parent.item].children!.indexOf(item);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const getHoveringPosition = (
treeId: string,
items: Record<TreeItemIndex, TreeItem>,
canDropOnFolder?: boolean,
canDropOnNonFolder?: boolean
canDropOnNonFolder?: boolean,
canReorderItems?: boolean
) => {
const hoveringPosition = (clientY - treeTop) / itemHeight;

Expand All @@ -38,8 +39,11 @@ const getHoveringPosition = (
const targetItem = items[targetLinearItem.item];
let offset: 'top' | 'bottom' | undefined;

const lineThreshold =
(targetItem?.isFolder && canDropOnFolder) || canDropOnNonFolder ? 0.2 : 0.5;
const lineThreshold = !canReorderItems
? 0
: (targetItem?.isFolder && canDropOnFolder) || canDropOnNonFolder
? 0.2
: 0.5;

if (hoveringPosition % 1 < lineThreshold) {
offset = 'top';
Expand Down Expand Up @@ -114,7 +118,8 @@ export const useOnDragOverTreeHandler = (
treeId,
items,
canDropOnFolder,
canDropOnNonFolder
canDropOnNonFolder,
canReorderItems
);

const nextDragCode = outsideContainer
Expand All @@ -137,7 +142,21 @@ export const useOnDragOverTreeHandler = (
return;
}

const targetItem = linearItems[treeId][linearIndex];
let targetItem = linearItems[treeId][linearIndex];
const redirectTargetToParent =
!canReorderItems &&
!canDropOnNonFolder &&
!items[targetItem.item].isFolder;

if (redirectTargetToParent) {
const { parentLinearIndex, parent } = getParentOfLinearItem(
linearIndex,
treeId
);
targetItem = parent;
linearIndex = parentLinearIndex;
}

const { depth } = targetItem;
const targetItemData = items[targetItem.item];

Expand All @@ -156,7 +175,7 @@ export const useOnDragOverTreeHandler = (
return;
}

const parent = getParentOfLinearItem(linearIndex, treeId);
const { parent } = getParentOfLinearItem(linearIndex, treeId);

if (
draggingItems.some(
Expand Down
19 changes: 19 additions & 0 deletions packages/core/test/dnd-restrictions.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,25 @@ describe('dnd restrictions', () => {
await test.expectTreeUnchanged();
await test.expectOpenViewState();
});

it('dropping on undroppable nonfolder targets parent', async () => {
const test = await new TestUtil().renderOpenTree({
canReorderItems: false,
canDropOnNonFolder: false,
});
await test.startDrag('aaa');
await test.dragOver('aba');
await test.drop();
await test.expectVisibleItemContents('aa', ['aab', 'aac', 'aad']);
await test.expectVisibleItemContents('ab', [
'aba',
'abb',
'abc',
'abd',
'aaa',
]);
await test.expectOpenViewState();
});
});

describe('item.canMove', () => {
Expand Down

0 comments on commit 5f61f4f

Please sign in to comment.