wip: column sorting

This commit is contained in:
Meierschlumpf
2023-01-05 18:00:10 +01:00
parent 6f8f0e90cb
commit 26bcb2fc34

View File

@@ -28,20 +28,31 @@ const nextItem = (start: number, end: number, nodes: Type[]): number => {
return nextItem(start, next.height - 1 + next.y, nodes); return nextItem(start, next.height - 1 + next.y, nodes);
}; };
const nextRowHeight = (nodes: Type[], values: number[], current = 0) => { const nextRowHeight = (
nodes: Type[],
values: { height: number; items: Type[] }[],
maxHeight: number,
current = 0
) => {
const item = nodes.find((x) => x.y >= current); const item = nodes.find((x) => x.y >= current);
if (!item) return; if (!item) return;
if (current < item.y) {
values.push({ height: item.y - current, items: [] });
}
const next = nextItem(item.y, item.y + item.height - 1, nodes); const next = nextItem(item.y, item.y + item.height - 1, nodes);
values.push(next + 1 - item.y); values.push({
nextRowHeight(nodes, values, next); height: next + 1 - item.y,
items: nodes.filter((x) => x.y >= current - 2 && x.y + x.height <= current + next + 1 - item.y),
});
nextRowHeight(nodes, values, maxHeight, next + 1);
}; };
const getRowHeights = (nodes: Type[]) => { const getRowHeights = (nodes: Type[]) => {
const rowHeights: number[] = []; const maxHeightElement = nodes.sort((a, b) => a.y + a.height - (b.y + b.height)).at(-1);
nextRowHeight( if (!maxHeightElement) return [];
nodes, const maxHeight = maxHeightElement.height + maxHeightElement.y;
rowHeights const rowHeights: { height: number; items: Type[] }[] = [];
); nextRowHeight(nodes, rowHeights, maxHeight);
return rowHeights; return rowHeights;
}; };
@@ -79,10 +90,10 @@ export const commonColumnSorting: GridstackColumnSortingFn = (
// TODO: fix issue with spaces between. // TODO: fix issue with spaces between.
let rowTotal = 0; let rowTotal = 0;
rowHeights.forEach(height => { rowHeights.forEach(({ height }) => {
rowItems.push(mappedNodes.filter(node => node.y >= rowTotal && node.y < rowTotal + height)); rowItems.push(mappedNodes.filter((node) => node.y >= rowTotal && node.y < rowTotal + height));
rowTotal += height; rowTotal += height;
}); });
console.log(rowItems); console.log(rowHeights);
}; };