Skip to content

Commit ec4b07c

Browse files
committed
fix: move element horizontally between two others
Closes #135. includes fix and test.
1 parent f6e4e46 commit ec4b07c

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

projects/angular-grid-layout/src/lib/utils/react-grid-layout-multiple.utils.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export function KtdMoveMultipleElements(
3636
compactType: CompactType,
3737
cols: number
3838
): Layout {
39-
let axes = compactType === 'vertical' ? 'y' : 'x';
39+
const axis = compactType === 'vertical' ? 'y' : 'x';
4040
// Short-circuit if nothing to do.
4141
if (items.every((item) => item.l.y === item.y && item.l.x === item.x)) {
4242
return layout;
@@ -83,11 +83,16 @@ export function KtdMoveMultipleElements(
8383
// can apply a repositioning if the collide item its on the first row/col
8484
let minAxe: number | undefined;
8585
if (itemsSorted && itemsSorted.length) {
86-
minAxe = itemsSorted[0][axes];
86+
minAxe = itemsSorted[0][axis];
8787
}
8888
// For each element, detect collisions and move the collided element by +1
8989
itemsSorted.forEach((item) => {
90+
9091
const collisions: LayoutItem[] = getAllCollisions(sorted, item);
92+
const minCollisionAxis: number | undefined = collisions.length
93+
? Math.min(collisions[0][axis], collisions[collisions.length - 1][axis]) // Take collision closest to 0; sorted but direction unknown
94+
: undefined;
95+
9196
// Move each item that collides away from this element.
9297
for (let i = 0, len = collisions.length; i < len; i++) {
9398
const collision = collisions[i];
@@ -100,13 +105,14 @@ export function KtdMoveMultipleElements(
100105
if (collision.moved) {
101106
continue;
102107
}
108+
const firstAxis: boolean = minAxe === item[axis] && minCollisionAxis===collision[axis];
103109
// Don't move static items - we have to move *this* element away
104110
if (collision.static && !item.static) {
105111
layout = KtdMoveElementsAwayFromCollision(
106112
layout,
107113
collision,
108114
item,
109-
minAxe === item[axes] ? isUserAction : false, // We only allow repositioning the "item" element if "collision" is in the first row of the moved block
115+
firstAxis && isUserAction, // Allow repositioning "item" only if "collision" and "item" are in the first collision row/col
110116
compactType,
111117
cols
112118
);
@@ -115,7 +121,7 @@ export function KtdMoveMultipleElements(
115121
layout,
116122
item,
117123
collision,
118-
minAxe === item[axes] ? isUserAction : false, // We only allow repositioning the "collision" element if "item" is in the first row of the moved block
124+
firstAxis && isUserAction, // Allow repositioning "collision" only if "collision" and "item" are in the first collision row/col
119125
compactType,
120126
cols
121127
);

projects/angular-grid-layout/src/lib/utils/react-grid-layout.utils.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,8 @@ export function moveElement(
429429
l.y
430430
}]`,
431431
);
432+
433+
const axis = compactType === 'vertical' ? 'y' : 'x';
432434
const oldX = l.x;
433435
const oldY = l.y;
434436

@@ -475,6 +477,11 @@ export function moveElement(
475477
} at [${collision.x},${collision.y}]`,
476478
);
477479

480+
const minCollisionAxis: number | undefined =
481+
collisions.length
482+
? Math.min(collisions[0][axis], collisions[collisions.length - 1][axis]) // Take collision closest to 0; sorted but direction unknown
483+
: undefined;
484+
478485
// Short circuit so we can't infinite loop
479486
if (collision.moved) {
480487
continue;
@@ -486,7 +493,7 @@ export function moveElement(
486493
layout,
487494
collision,
488495
l,
489-
isUserAction,
496+
minCollisionAxis===collision[axis] && isUserAction, // Reposition "l" only if "collision" is in the first row/col of collisions list
490497
compactType,
491498
cols,
492499
);
@@ -495,7 +502,7 @@ export function moveElement(
495502
layout,
496503
l,
497504
collision,
498-
isUserAction,
505+
minCollisionAxis===collision[axis] && isUserAction, // Reposition "collision" only if is in the first row/col of collisions list
499506
compactType,
500507
cols,
501508
);

projects/angular-grid-layout/src/lib/utils/tests/react-grid-layout-utils.spec.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ describe('compact horizontal', () => {
405405
});
406406
});
407407

408-
describe('moveElementAffectingOtherItems', () => {
408+
describe('Test to move one element affecting other items', () => {
409409
function compactAndMove(
410410
layout,
411411
layoutItem,
@@ -432,7 +432,7 @@ describe('moveElementAffectingOtherItems', () => {
432432
);
433433
}
434434

435-
it('Move element up, pushing the rest of the grid down', () => {
435+
it('Test that moving an element into the top push the rest of the grid down', () => {
436436
const layout = [
437437
{id: '0', x: 1, y: 0, w: 24, h: 1},
438438
{id: '1', x: 1, y: 1, w: 8, h: 1},
@@ -462,4 +462,33 @@ describe('moveElementAffectingOtherItems', () => {
462462
{id: '5', x: 17, y: 3+8, w: 8, h: 3, moved: false, static: false},
463463
]);
464464
});
465+
466+
it('keeps the original order when moving an element horizontally between two elements', () => {
467+
const layout = [
468+
{id:"0", x:0, y:0, w:6, h:2},
469+
{id:"1", x:12, y:0, w:12, h:2},
470+
{id:"2", x:0, y:2, w:6, h:2},
471+
{id:"3", x:6, y:2, w:12, h:1},
472+
{id:"4", x:6, y:3, w:6, h:2}
473+
];
474+
const layoutItem = layout[2];
475+
expect(
476+
compactAndMove(
477+
layout,
478+
layoutItem,
479+
6, // x
480+
2, // y
481+
true, //isUserAction
482+
false, // preventCollision
483+
'vertical', // compactType,
484+
30 // cols
485+
)
486+
).toEqual([
487+
{id:"0", x:0, y:0, w:6, h:2, moved: false, static: false},
488+
{id:"1", x:12, y:0, w:12, h:2, moved: false, static: false},
489+
{id:"2", x:0+6, y:0, w:6, h:2, moved: false, static: false},
490+
{id:"3", x:6, y:2, w:12, h:1, moved: false, static: false},
491+
{id:"4", x:6, y:3, w:6, h:2, moved: false, static: false},
492+
]);
493+
});
465494
});

0 commit comments

Comments
 (0)