Skip to content

Commit

Permalink
fix: Sort items before adjusting
Browse files Browse the repository at this point in the history
fixes #64
  • Loading branch information
vaheqelyan committed Aug 20, 2023
1 parent 82e2bbf commit 324126e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/index.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@
}
containerWidth = width;
})
});
});
sizeObserver.observe(container);
Expand Down
17 changes: 11 additions & 6 deletions src/utils/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,23 +148,28 @@ export function normalize(items, col) {
export function adjust(items, col) {
let matrix = makeMatrix(getRowsCount(items, col), col);

let res = [];
const order = items.toSorted((a, b) => {
const aItem = a[col];
const bItem = b[col];

items.forEach((item) => {
return aItem.x - bItem.x || aItem.y - bItem.y;
});

return order.reduce((acc, item) => {
let position = findFreeSpaceForItem(matrix, item[col]);

res.push({
acc.push({
...item,
[col]: {
...item[col],
...position,
},
});

matrix = makeMatrixFromItems(res, getRowsCount(res, col), col);
});
matrix = makeMatrixFromItems(acc, getRowsCount(acc, col), col);

return res;
return acc;
}, []);
}

export function getUndefinedItems(items, col, breakpoints) {
Expand Down
2 changes: 0 additions & 2 deletions src/utils/matrix.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { getRowsCount } from "./other.js";

export const makeMatrix = (rows, cols) => Array.from(Array(rows), () => new Array(cols)); // make 2d array

export function makeMatrixFromItems(items, _row, _col) {
Expand Down

0 comments on commit 324126e

Please sign in to comment.