From 4fffa825733de15bcff4c28268973463155a5623 Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Mon, 24 Oct 2022 12:05:59 +0100 Subject: [PATCH] Make grid rows and columns configurable and simplify grid style application --- packages/client/manifest.json | 18 +++++- .../client/src/components/Component.svelte | 58 ------------------- .../client/src/components/app/Grid.svelte | 52 ++++++++++++++--- .../components/preview/GridDNDHandler.svelte | 35 +++++------ 4 files changed, 79 insertions(+), 84 deletions(-) diff --git a/packages/client/manifest.json b/packages/client/manifest.json index bedc915d42..09dd7aa015 100644 --- a/packages/client/manifest.json +++ b/packages/client/manifest.json @@ -4585,7 +4585,23 @@ "width": 800, "height": 400 }, - "showEmptyState": false + "showEmptyState": false, + "settings": [ + { + "type": "number", + "label": "Rows", + "key": "rows", + "defaultValue": 12, + "min": 1 + }, + { + "type": "number", + "label": "Columns", + "key": "cols", + "defaultValue": 12, + "min": 1 + } + ] }, "formblock": { "name": "Form Block", diff --git a/packages/client/src/components/Component.svelte b/packages/client/src/components/Component.svelte index c59ef050cd..dc2f8c1d5f 100644 --- a/packages/client/src/components/Component.svelte +++ b/packages/client/src/components/Component.svelte @@ -170,21 +170,11 @@ $: pad = pad || (interactive && hasChildren && inDndPath) $: $dndIsDragging, (pad = false) - // We can apply additional styles automatically if required. - // One use case for this is ensuring grid children have proper styles to - // display properly inside a grid. - $: additionalStyles = getAdditionalStyles( - instance._styles?.normal || {}, - parentType, - definition - ) - // Compute overall styles $: styles = { ...instance._styles, normal: { ...instance._styles?.normal, - ...additionalStyles, ...ephemeralStyles, }, custom: customCSS, @@ -460,54 +450,6 @@ }) } - const getAdditionalStyles = (styles, parentType, definition) => { - let newStyles = {} - - // Ensure grid styles are set - if (parentType?.endsWith("/grid")) { - newStyles = { - ...newStyles, - overflow: "hidden", - width: "auto", - height: "auto", - } - - // Guess rough grid size from definition size - let columns = 6 - let rows = 4 - if (definition.size?.width) { - columns = Math.min(12, Math.round(definition.size.width / 100)) - } - if (definition.size?.height) { - rows = Math.min(12, Math.round(definition.size.height / 50)) - } - - // Ensure grid position styles are set - if (!styles["grid-column-start"]) { - newStyles["grid-column-start"] = 1 - } - if (!styles["grid-column-end"]) { - newStyles["grid-column-end"] = columns + 1 - } - if (!styles["grid-row-start"]) { - newStyles["grid-row-start"] = 1 - } - if (!styles["grid-row-end"]) { - newStyles["grid-row-end"] = rows + 1 - } - - // Ensure grid end styles aren't before grid start styles - if (newStyles["grid-column-end"] <= newStyles["grid-column-start"]) { - newStyles["grid-column-end"] = newStyles["grid-column-start"] + 1 - } - if (newStyles["grid-row-end"] <= newStyles["grid-row-start"]) { - newStyles["grid-row-end"] = newStyles["grid-row-start"] + 1 - } - } - - return newStyles - } - onMount(() => { if ( $appStore.isDevApp && diff --git a/packages/client/src/components/app/Grid.svelte b/packages/client/src/components/app/Grid.svelte index 8348a3ba91..1c50d8c5b9 100644 --- a/packages/client/src/components/app/Grid.svelte +++ b/packages/client/src/components/app/Grid.svelte @@ -4,15 +4,21 @@ const component = getContext("component") const { styleable } = getContext("sdk") - const cols = 12 + export let cols = 12 + export let rows = 12 let node - $: coords = generateCoords(cols) - const generateCoords = num => { + // 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) + + const generateCoords = (rows, cols) => { let grid = [] - for (let row = 0; row < num; row++) { - for (let col = 0; col < num; col++) { + for (let row = 0; row < rows; row++) { + for (let col = 0; col < cols; col++) { grid.push({ row, col }) } } @@ -23,7 +29,17 @@
@@ -35,6 +51,26 @@