Sanity check grid size limits and improve logic around min/max column/row dimensions

This commit is contained in:
Andrew Kingston 2024-07-29 16:36:56 +01:00
parent 1aa7077b9e
commit f91cc26fe1
No known key found for this signature in database
3 changed files with 20 additions and 39 deletions

View File

@ -146,6 +146,7 @@
"label": "Columns", "label": "Columns",
"key": "cols", "key": "cols",
"placeholder": 12, "placeholder": 12,
"min": 1,
"dependsOn": { "dependsOn": {
"setting": "layout", "setting": "layout",
"value": "grid" "value": "grid"
@ -156,6 +157,7 @@
"label": "Rows", "label": "Rows",
"key": "rows", "key": "rows",
"placeholder": 12, "placeholder": 12,
"min": 1,
"dependsOn": { "dependsOn": {
"setting": "layout", "setting": "layout",
"value": "grid" "value": "grid"

View File

@ -9,11 +9,6 @@
$: cols = cols || 12 $: cols = cols || 12
$: rows = rows || 12 $: rows = rows || 12
// Deliberately non-reactive as we want this fixed whenever the grid renders
const defaultColSpan = Math.ceil((cols + 1) / 2)
const defaultRowSpan = Math.ceil((rows + 1) / 2)
$: coords = generateCoords(rows, cols) $: coords = generateCoords(rows, cols)
const generateCoords = (rows, cols) => { const generateCoords = (rows, cols) => {
@ -35,8 +30,6 @@
...$component.styles?.normal, ...$component.styles?.normal,
"--cols": cols, "--cols": cols,
"--rows": rows, "--rows": rows,
"--default-col-span": defaultColSpan,
"--default-row-span": defaultRowSpan,
gap: "0 !important", gap: "0 !important",
}, },
}} }}
@ -73,11 +66,14 @@
var(--cols) var(--cols)
) !important; ) !important;
grid-column-end: min( grid-column-end: min(
max(1, var(--grid-column-end)), max(2, var(--grid-column-end)),
var(--cols) calc(var(--cols) + 1)
) !important; ) !important;
grid-row-start: min(max(1, var(--grid-row-start)), var(--rows)) !important; grid-row-start: min(max(1, var(--grid-row-start)), var(--rows)) !important;
grid-row-end: min(max(1, var(--grid-row-end)), var(--rows)) !important; grid-row-end: min(
max(2, var(--grid-row-end)),
calc(var(--rows) + 1)
) !important;
max-height: 100%; max-height: 100%;
max-width: 100%; max-width: 100%;
} }

View File

@ -17,6 +17,9 @@
...($gridStyles ? { "z-index": 999 } : null), ...($gridStyles ? { "z-index": 999 } : null),
}) })
// Sugar for a combination of both min and max
const minMax = (value, min, max) => Math.min(max, Math.max(min, value))
// Util to check if a DND event originates from a grid (or inside a grid). // Util to check if a DND event originates from a grid (or inside a grid).
// This is important as we do not handle grid DND in this handler. // This is important as we do not handle grid DND in this handler.
const isGridEvent = e => { const isGridEvent = e => {
@ -41,18 +44,7 @@
} }
const { mode, side, gridId, grid } = dragInfo const { mode, side, gridId, grid } = dragInfo
const { const { startX, startY, rowStart, rowEnd, colStart, colEnd } = grid
startX,
startY,
rowStart,
rowEnd,
colStart,
colEnd,
rowDeltaMin,
rowDeltaMax,
colDeltaMin,
colDeltaMax,
} = grid
const domGrid = getDOMNode(gridId) const domGrid = getDOMNode(gridId)
const cols = parseInt(domGrid.dataset.cols) const cols = parseInt(domGrid.dataset.cols)
@ -67,13 +59,11 @@
let deltaY = Math.round(diffY / rowHeight) let deltaY = Math.round(diffY / rowHeight)
if (mode === "move") { if (mode === "move") {
deltaY = Math.min(Math.max(deltaY, rowDeltaMin), rowDeltaMax)
deltaX = Math.min(Math.max(deltaX, colDeltaMin), colDeltaMax)
const newStyles = { const newStyles = {
"--grid-row-start": rowStart + deltaY, "--grid-row-start": minMax(rowStart + deltaY, 1, rows),
"--grid-row-end": rowEnd + deltaY, "--grid-row-end": minMax(rowEnd + deltaY, 2, rows + 1),
"--grid-column-start": colStart + deltaX, "--grid-column-start": minMax(colStart + deltaX, 1, cols),
"--grid-column-end": colEnd + deltaX, "--grid-column-end": minMax(colEnd + deltaX, 2, cols + 1),
} }
gridStyles.set(newStyles) gridStyles.set(newStyles)
} else if (mode === "resize") { } else if (mode === "resize") {
@ -174,21 +164,14 @@
const domNode = getDOMNode(dragInfo.id) const domNode = getDOMNode(dragInfo.id)
const styles = window.getComputedStyle(domNode) const styles = window.getComputedStyle(domNode)
if (domGrid) { if (domGrid) {
const minMax = (value, min, max) => Math.min(max, Math.max(min, value))
const getStyle = x => parseInt(styles?.getPropertyValue(x) || "0") const getStyle = x => parseInt(styles?.getPropertyValue(x) || "0")
const getColStyle = x => minMax(getStyle(x), 1, gridCols + 1)
const getRowStyle = x => minMax(getStyle(x), 1, gridRows + 1)
dragInfo.grid = { dragInfo.grid = {
startX: e.clientX, startX: e.clientX,
startY: e.clientY, startY: e.clientY,
rowStart: getRowStyle("--grid-row-start"), rowStart: minMax(getStyle("--grid-row-start"), 1, gridRows),
rowEnd: getRowStyle("--grid-row-end"), rowEnd: minMax(getStyle("--grid-row-end"), 2, gridRows + 1),
colStart: getColStyle("--grid-column-start"), colStart: minMax(getStyle("--grid-column-start"), 1, gridCols),
colEnd: getColStyle("--grid-column-end"), colEnd: minMax(getStyle("--grid-column-end"), 2, gridCols + 1),
rowDeltaMin: 1 - getRowStyle("--grid-row-start"),
rowDeltaMax: gridRows + 1 - getRowStyle("--grid-row-end"),
colDeltaMin: 1 - getColStyle("--grid-column-start"),
colDeltaMax: gridCols + 1 - getColStyle("--grid-column-end"),
} }
handleEvent(e) handleEvent(e)
} }