Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assign draggable index to null when outside siblings #286

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/dnd/src/Draggable/Base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Base
extends AbstractDraggable<CoreInstanceInterface>
implements DraggableBaseInterface
{
tempIndex: number;
tempIndex: number | null;

operationID: string;

Expand Down
24 changes: 11 additions & 13 deletions packages/dnd/src/Draggable/Draggable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,12 @@ class Draggable extends Base implements DraggableDnDInterface {
!$.allowLeavingFromBottom);
}

private getLastElmIndex() {
return this.siblingsList!.length - 1;
}

private isFirstOrOutside() {
return this.siblingsList !== null && this.tempIndex <= 0;
return this.siblingsList !== null && this.tempIndex === null;
}

private isLastELm() {
return this.tempIndex === this.getLastElmIndex();
return this.tempIndex === this.siblingsList!.length - 1;
}

private axesRightFilter(x: number, minRight: number) {
Expand Down Expand Up @@ -134,14 +130,14 @@ class Draggable extends Base implements DraggableDnDInterface {
}

private axesBottomFilter(y: number, bottom: number) {
return (this.tempIndex < 0 || this.isLastELm()) &&
return (this.tempIndex === null || this.isLastELm()) &&
y - this.innerOffsetY + this.draggedElm.offset.height >= bottom
? bottom + this.innerOffsetY - this.draggedElm.offset.height
: y;
}

private axesTopFilter(y: number, maxTop: number) {
return this.tempIndex <= 0 && y - this.innerOffsetY <= maxTop
return this.tempIndex === null && y - this.innerOffsetY <= maxTop
? maxTop + this.innerOffsetY
: y;
}
Expand Down Expand Up @@ -212,7 +208,7 @@ class Draggable extends Base implements DraggableDnDInterface {
*/
return (
(this.isLastELm() && this.tempOffset.currentTop > $.maxBottom) ||
(this.tempIndex < 0 && this.tempOffset.currentTop < $.maxTop)
(this.tempIndex == null && this.tempOffset.currentTop < $.maxTop)
);
}

Expand Down Expand Up @@ -358,11 +354,13 @@ class Draggable extends Base implements DraggableDnDInterface {

this.draggedElm.transformElm();

if (this.siblingsList) {
this.draggedElm.assignNewPosition(this.siblingsList, this.tempIndex);
}
if (this.tempIndex !== null) {
if (this.siblingsList) {
this.draggedElm.assignNewPosition(this.siblingsList, this.tempIndex);
}

this.draggedElm.order.self = this.tempIndex;
this.draggedElm.order.self = this.tempIndex;
}
}

endDragging() {
Expand Down
2 changes: 1 addition & 1 deletion packages/dnd/src/Draggable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface Restrictions {

export interface DraggableBaseInterface
extends AbstractDraggableInterface<CoreInstanceInterface> {
tempIndex: number;
tempIndex: number | null;
operationID: string;

opts: FinalDndOpts;
Expand Down
9 changes: 5 additions & 4 deletions packages/dnd/src/Droppable/Droppable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ class Droppable {
}

private switchElement() {
const elmIndex = this.draggable.tempIndex + -1 * this.effectedElemDirection;
const elmIndex =
this.draggable.tempIndex! + -1 * this.effectedElemDirection;
const id = this.draggable.siblingsList![elmIndex];

if (this.isIDEligible2Move(id)) {
Expand All @@ -322,10 +323,10 @@ class Droppable {
}

private liftUp() {
const from = this.draggable.tempIndex + 1;
const from = this.draggable.tempIndex! + 1;

this.leftAtIndex = this.draggable.tempIndex;
this.draggable.tempIndex = -1;
this.leftAtIndex = this.draggable.tempIndex!;
this.draggable.tempIndex = null;

for (let i = from; i < this.draggable.siblingsList!.length; i += 1) {
/**
Expand Down