Skip to content

Commit 92cddd7

Browse files
committed
fix: allow dropping into itself if the drop target is reparented outside of self (#148)
1 parent 27d5913 commit 92cddd7

File tree

2 files changed

+59
-5
lines changed

2 files changed

+59
-5
lines changed

packages/core/src/drag/DraggingPositionEvaluation.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,22 @@ export class DraggingPositionEvaluation {
127127
return undefined;
128128
}
129129

130-
// this.targetItem = newParent.parent;
131130
const reparentedChildIndex =
132131
this.env.items[newParent.parent.item].children!.indexOf(
133132
insertionItemAbove.parent.item
134133
) + 1;
135134

135+
if (
136+
this.draggingItems &&
137+
this.isDescendant(
138+
this.treeId,
139+
newParent.parentLinearIndex + 1,
140+
this.draggingItems
141+
)
142+
) {
143+
return undefined;
144+
}
145+
136146
return {
137147
targetType: 'between-items',
138148
treeId: this.treeId,
@@ -232,10 +242,6 @@ export class DraggingPositionEvaluation {
232242

233243
this.maybeRedirectToParent();
234244

235-
if (this.areDraggingItemsDescendantOfTarget()) {
236-
return undefined;
237-
}
238-
239245
this.maybeRedirectInsideOpenFolder();
240246
this.maybeMapToBottomOffset();
241247

@@ -244,6 +250,10 @@ export class DraggingPositionEvaluation {
244250
return reparented;
245251
}
246252

253+
if (this.areDraggingItemsDescendantOfTarget()) {
254+
return undefined;
255+
}
256+
247257
if (!this.canDropAtCurrentTarget()) {
248258
return undefined;
249259
}
@@ -282,6 +292,7 @@ export class DraggingPositionEvaluation {
282292
itemLinearIndex: number,
283293
potentialParents: TreeItem[]
284294
) {
295+
// console.log('descendant check', itemLinearIndex, potentialParents);
285296
const { parentLinearIndex, parent } = this.getParentOfLinearItem(
286297
itemLinearIndex,
287298
treeId

packages/core/test/dnd-basics.spec.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,14 @@ describe('dnd basics', () => {
446446
]);
447447
});
448448

449+
it('doesnt reparent into itself', async () => {
450+
const test = await new TestUtil().renderOpenTree();
451+
await test.startDrag('a');
452+
await test.dragOver('aad', 'bottom', 3);
453+
await test.drop();
454+
await test.expectTreeUnchanged();
455+
});
456+
449457
it('reparents inner level', async () => {
450458
const test = await new TestUtil().renderOpenTree();
451459
await test.startDrag('bbb');
@@ -487,6 +495,41 @@ describe('dnd basics', () => {
487495
]);
488496
});
489497

498+
it('reparents bottom-most nested item', async () => {
499+
const test = await new TestUtil().renderOpenTree();
500+
await test.startDrag('special');
501+
await test.dragOver('deep5', 'bottom', 10);
502+
await test.drop();
503+
await test.expectVisibleItemContents('deep4', ['deep5', 'special']);
504+
505+
await new Promise(r => {
506+
setTimeout(r);
507+
});
508+
509+
await test.startDrag('special');
510+
await test.dragOver('cannot-rename', 'bottom', 3);
511+
await test.drop();
512+
await test.expectVisibleItemContents('deep4', ['deep5']);
513+
await test.expectVisibleItemContents('deep3', ['deep4', 'special']);
514+
515+
await new Promise(r => {
516+
setTimeout(r);
517+
});
518+
519+
await test.startDrag('special');
520+
await test.dragOver('cannot-rename', 'bottom', 0);
521+
await test.drop();
522+
await test.expectVisibleItemContents('deep3', ['deep4']);
523+
await test.expectItemContents('root', [
524+
'target-parent',
525+
'a',
526+
'b',
527+
'c',
528+
'deep1',
529+
'special',
530+
]);
531+
});
532+
490533
describe('reparent upwards when dragging at top of item below subtree', () => {
491534
it('reparents inner level', async () => {
492535
const test = await new TestUtil().renderOpenTree();

0 commit comments

Comments
 (0)