feat: protect responsive board layouts

This commit is contained in:
ajnart committed 2026-08-08 15:41:14 +02:00
1 parent fa9444323b
commit 6728228387
46 files changed
+8933 -477

No files matched your search

@@ -60,9 +60,13 @@ const createItemLayouts = (board: Board, currentSection: EmptySection, kind: Wid
return layouts.map((layoutId) => {
const boardLayout = board.layouts.find((layout) => layout.id === layoutId);
const elements = getSectionElements(board, { sectionId: currentSection.id, layoutId });
const sizeForLayout = {
...itemSize,
width: Math.min(boardLayout?.columnCount ?? itemSize.width, itemSize.width),
};
const emptyPosition = boardLayout
? getFirstEmptyPosition(elements, boardLayout.columnCount, 9999, itemSize)
? getFirstEmptyPosition(elements, boardLayout.columnCount, 9999, sizeForLayout)
: { xOffset: 0, yOffset: 0 };
if (!emptyPosition) {
@@ -70,8 +74,8 @@ const createItemLayouts = (board: Board, currentSection: EmptySection, kind: Wid
}
return {
width: itemSize.width,
height: itemSize.height,
width: sizeForLayout.width,
height: sizeForLayout.height,
...emptyPosition,
sectionId: currentSection.id,
layoutId,
@@ -10,6 +10,20 @@ import { ItemMockBuilder } from "./mocks/item-mock";
import { LayoutMockBuilder } from "./mocks/layout-mock";
describe("item actions create-item", () => {
test("clips oversized widgets to each layout's column count", () => {
const board = new BoardMockBuilder()
.addLayout({ id: "mobile", name: "Mobile", role: "mobile", columnCount: 3, breakpoint: 0 })
.addEmptySection({ id: "section" })
.build();
const baseLayout = board.layouts.find((layout) => layout.role === "base");
const result = createItemCallback({ kind: "mediaMissing" })(board);
const createdItem = result.items.at(0);
expect(createdItem?.layouts.find((layout) => layout.layoutId === "mobile")?.width).toBe(3);
expect(createdItem?.layouts.find((layout) => layout.layoutId === baseLayout?.id)?.width).toBe(4);
});
test("should add it to first section", () => {
// Arrange
const itemKind = "clock";
@@ -45,7 +45,8 @@ export class BoardMockBuilder {
id: createId(),
name: "Base",
columnCount: 12,
breakpoint: 0,
breakpoint: 768,
role: "base",
},
],
...board,
@@ -10,7 +10,8 @@ export class LayoutMockBuilder {
id: createId(),
name: "Base",
columnCount: 12,
breakpoint: 0,
breakpoint: 768,
role: "base",
...layout,
} satisfies Board["layouts"][0];
}