Skip to content

Commit

Permalink
fix: allow dropping into itself if the drop target is reparented outs…
Browse files Browse the repository at this point in the history
…ide of self (#148)
  • Loading branch information
lukasbach committed Mar 15, 2024
1 parent 27d5913 commit 92cddd7
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
21 changes: 16 additions & 5 deletions packages/core/src/drag/DraggingPositionEvaluation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,22 @@ export class DraggingPositionEvaluation {
return undefined;
}

// this.targetItem = newParent.parent;
const reparentedChildIndex =
this.env.items[newParent.parent.item].children!.indexOf(
insertionItemAbove.parent.item
) + 1;

if (
this.draggingItems &&
this.isDescendant(
this.treeId,
newParent.parentLinearIndex + 1,
this.draggingItems
)
) {
return undefined;
}

return {
targetType: 'between-items',
treeId: this.treeId,
Expand Down Expand Up @@ -232,10 +242,6 @@ export class DraggingPositionEvaluation {

this.maybeRedirectToParent();

if (this.areDraggingItemsDescendantOfTarget()) {
return undefined;
}

this.maybeRedirectInsideOpenFolder();
this.maybeMapToBottomOffset();

Expand All @@ -244,6 +250,10 @@ export class DraggingPositionEvaluation {
return reparented;
}

if (this.areDraggingItemsDescendantOfTarget()) {
return undefined;
}

if (!this.canDropAtCurrentTarget()) {
return undefined;
}
Expand Down Expand Up @@ -282,6 +292,7 @@ export class DraggingPositionEvaluation {
itemLinearIndex: number,
potentialParents: TreeItem[]
) {
// console.log('descendant check', itemLinearIndex, potentialParents);
const { parentLinearIndex, parent } = this.getParentOfLinearItem(
itemLinearIndex,
treeId
Expand Down
43 changes: 43 additions & 0 deletions packages/core/test/dnd-basics.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,14 @@ describe('dnd basics', () => {
]);
});

it('doesnt reparent into itself', async () => {
const test = await new TestUtil().renderOpenTree();
await test.startDrag('a');
await test.dragOver('aad', 'bottom', 3);
await test.drop();
await test.expectTreeUnchanged();
});

it('reparents inner level', async () => {
const test = await new TestUtil().renderOpenTree();
await test.startDrag('bbb');
Expand Down Expand Up @@ -487,6 +495,41 @@ describe('dnd basics', () => {
]);
});

it('reparents bottom-most nested item', async () => {
const test = await new TestUtil().renderOpenTree();
await test.startDrag('special');
await test.dragOver('deep5', 'bottom', 10);
await test.drop();
await test.expectVisibleItemContents('deep4', ['deep5', 'special']);

await new Promise(r => {
setTimeout(r);
});

await test.startDrag('special');
await test.dragOver('cannot-rename', 'bottom', 3);
await test.drop();
await test.expectVisibleItemContents('deep4', ['deep5']);
await test.expectVisibleItemContents('deep3', ['deep4', 'special']);

await new Promise(r => {
setTimeout(r);
});

await test.startDrag('special');
await test.dragOver('cannot-rename', 'bottom', 0);
await test.drop();
await test.expectVisibleItemContents('deep3', ['deep4']);
await test.expectItemContents('root', [
'target-parent',
'a',
'b',
'c',
'deep1',
'special',
]);
});

describe('reparent upwards when dragging at top of item below subtree', () => {
it('reparents inner level', async () => {
const test = await new TestUtil().renderOpenTree();
Expand Down

0 comments on commit 92cddd7

Please sign in to comment.