Skip to content

Commit 26626fb

Browse files
committed
fix(grid): correctly calculate x coordinate when dragging in a one colum grid
Closes #128
1 parent a4fdee8 commit 26626fb

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ export function ktdGridCompact(layout: KtdGridLayout, compactType: KtdGridCompac
3232
}
3333

3434
function screenXToGridX(screenXPos: number, cols: number, width: number, gap: number): number {
35-
const widthMinusGaps = width - (gap * (cols - 1));
36-
const itemWidth = widthMinusGaps / cols;
37-
const widthMinusOneItem = width - itemWidth;
38-
const colWidthWithGap = widthMinusOneItem / (cols - 1);
39-
return Math.round(screenXPos / colWidthWithGap);
35+
if (cols <= 1) {
36+
return 0;
37+
}
38+
39+
const totalGapsWidth = gap * (cols - 1);
40+
const totalItemsWidth = width - totalGapsWidth;
41+
const itemPlusGapWidth = totalItemsWidth / cols + gap;
42+
return Math.round(screenXPos / itemPlusGapWidth);
4043
}
4144

4245
function screenYToGridY(screenYPos: number, rowHeight: number, height: number, gap: number): number {

0 commit comments

Comments
 (0)