Skip to content

Commit 5ff20f0

Browse files
author
Brian Ryu
committed
Improve translation logic for grid sorting
1 parent c74aea0 commit 5ff20f0

File tree

1 file changed

+30
-9
lines changed

1 file changed

+30
-9
lines changed

src/SortableContainer/index.js

+30-9
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ export default function sortableContainer(
10261026
};
10271027

10281028
keyUpdatePosition = (disableTransition = false) => {
1029-
const {transitionDuration} = this.props;
1029+
const {transitionDuration, disableAutoscroll} = this.props;
10301030
const nodes = this.manager.getOrderedRefs(this.manager.activeKeyLift.collection);
10311031
const targetIndex = getTargetIndex(this.newIndex, this.prevIndex, this.index);
10321032
const targetNode = nodes[nodes.findIndex(({node}) => node.sortableInfo.index === targetIndex)];
@@ -1062,8 +1062,10 @@ export default function sortableContainer(
10621062
};
10631063

10641064
// Correct translate
1065-
translate.x = Math.max(Math.min(translate.x, this.maxTranslate.x), this.minTranslate.x);
1066-
translate.y = Math.max(Math.min(translate.y, this.maxTranslate.y), this.minTranslate.y);
1065+
if (!disableAutoscroll) {
1066+
translate.x = Math.max(Math.min(translate.x, this.maxTranslate.x), this.minTranslate.x);
1067+
translate.y = Math.max(Math.min(translate.y, this.maxTranslate.y), this.minTranslate.y);
1068+
}
10671069

10681070
if (transitionDuration && !disableTransition) {
10691071
this.helper.style[
@@ -1074,7 +1076,9 @@ export default function sortableContainer(
10741076
translate.x
10751077
}px,${translate.y}px,0)`;
10761078

1077-
this.keyAutoscroll(overflow);
1079+
if (!disableAutoscroll) {
1080+
this.keyAutoscroll(overflow);
1081+
}
10781082
};
10791083

10801084
keyAutoscroll = ({left, right, top, bottom}) => {
@@ -1103,17 +1107,20 @@ export default function sortableContainer(
11031107
for (let i = 0, len = nodes.length; i < len; i++) {
11041108
const {node} = nodes[i];
11051109
const index = node.sortableInfo.index;
1110+
const offsetWidth = this.width > node.offsetWidth ? node.offsetWidth / 2 : this.width / 2;
11061111

11071112
const translate = {
11081113
x: 0,
11091114
y: 0,
11101115
};
1111-
let {edgeOffset} = nodes[i];
1116+
let {edgeOffset, boundingRect} = nodes[i];
11121117

11131118
// If we haven't cached the node's offsetTop / offsetLeft value
11141119
if (!edgeOffset) {
11151120
edgeOffset = getEdgeOffset(node, this.container);
1121+
boundingRect = node.getBoundingClientRect();
11161122
nodes[i].edgeOffset = edgeOffset;
1123+
nodes[i].boundingRect = boundingRect;
11171124
}
11181125

11191126
// Get a reference to the next and previous node
@@ -1124,6 +1131,7 @@ export default function sortableContainer(
11241131
// We need this for calculating the animation in a grid setup
11251132
if (nextNode && !nextNode.edgeOffset) {
11261133
nextNode.edgeOffset = getEdgeOffset(nextNode.node, this.container);
1134+
nextNode.boundingRect = nextNode.node.getBoundingClientRect();
11271135
}
11281136

11291137
// If the node is the one we're currently animating, skip it
@@ -1145,12 +1153,25 @@ export default function sortableContainer(
11451153
// grid
11461154
if (index < this.index && index >= this.newIndex) {
11471155
// right/down 1
1148-
translate.x = nextNode.edgeOffset.left - edgeOffset.left;
1149-
translate.y = nextNode.edgeOffset.top - edgeOffset.top;
1156+
translate.x = this.width + this.marginOffset.x;
1157+
if (nextNode &&
1158+
(boundingRect.left + translate.x + node.offsetWidth >
1159+
this.containerBoundingRect.left + offsetWidth + this.containerBoundingRect.width)
1160+
) {
1161+
const widthCorrection =
1162+
node.offsetWidth - nextNode.boundingRect.width +
1163+
nextNode.boundingRect.width - this.width;
1164+
translate.x = nextNode.edgeOffset.left - edgeOffset.left - widthCorrection;
1165+
translate.y = nextNode.edgeOffset.top - edgeOffset.top;
1166+
}
11501167
} else if (index > this.index && index <= this.newIndex) {
11511168
// left/up 1
1152-
translate.x = prevNode.edgeOffset.left - edgeOffset.left;
1153-
translate.y = prevNode.edgeOffset.top - edgeOffset.top;
1169+
translate.x = -(this.width + this.marginOffset.x);
1170+
if (prevNode && boundingRect.left + translate.x < this.containerBoundingRect.left - offsetWidth) {
1171+
const widthCorrection = prevNode.boundingRect.width - this.width;
1172+
translate.x = prevNode.edgeOffset.left - edgeOffset.left + widthCorrection;
1173+
translate.y = prevNode.edgeOffset.top - edgeOffset.top;
1174+
}
11541175
}
11551176
} else {
11561177
// x

0 commit comments

Comments
 (0)