From a382e75df7ab76998102595e474f4c5b3633c282 Mon Sep 17 00:00:00 2001 From: Federico Ruggi <1081051+ruggi@users.noreply.github.com> Date: Fri, 25 Oct 2024 15:20:12 +0200 Subject: [PATCH] Grid resize: remove shorthand, serialize other longhand when not auto (#6581) **Problem:** If a grid is configured with the `gridTemplate` shorthand, resizing the grid does not handle that correctly. **Fix:** This PR handles `gridTemplate` shorthands during template changes via the canvas. **An incremental PR will do the same for the inspector template editing**. For example, if a grid is configured as follows: `gridTemplate: 1fr 2fr / 3fr 4fr`, changing its first column to `3.5fr` will result in the following style: ``` gridTemplateColumns: '3.5fr 4fr', gridTemplateRows: '1fr 2fr' ``` If a grid has a shorthand with an axis being the default (a single `auto` keyword), for example `gridTemplate: auto / 3fr 4fr`, the same change as above will result in: ``` gridTemplateColumns: '3.5fr 4fr', ``` Fixes #6580 --- .../strategies/grid-helpers.ts | 9 + .../resize-grid-strategy.spec.browser2.tsx | 388 +++++++++++++++++- .../strategies/resize-grid-strategy.ts | 56 ++- .../src/components/inspector/flex-section.tsx | 11 +- 4 files changed, 429 insertions(+), 35 deletions(-) diff --git a/editor/src/components/canvas/canvas-strategies/strategies/grid-helpers.ts b/editor/src/components/canvas/canvas-strategies/strategies/grid-helpers.ts index 8f5029506b21..079a12531a0c 100644 --- a/editor/src/components/canvas/canvas-strategies/strategies/grid-helpers.ts +++ b/editor/src/components/canvas/canvas-strategies/strategies/grid-helpers.ts @@ -788,3 +788,12 @@ export function findOriginalGrid( return findOriginalGrid(metadata, parentPath) } + +// Returns whether the given dimensions are made of just one item, being a CSS keyword with value "auto". +export function isJustAutoGridDimension(dimensions: GridDimension[]): boolean { + return ( + dimensions.length === 1 && + dimensions[0].type === 'KEYWORD' && + dimensions[0].value.value === 'auto' + ) +} diff --git a/editor/src/components/canvas/canvas-strategies/strategies/resize-grid-strategy.spec.browser2.tsx b/editor/src/components/canvas/canvas-strategies/strategies/resize-grid-strategy.spec.browser2.tsx index f086926e2bc6..a8bc6730953f 100644 --- a/editor/src/components/canvas/canvas-strategies/strategies/resize-grid-strategy.spec.browser2.tsx +++ b/editor/src/components/canvas/canvas-strategies/strategies/resize-grid-strategy.spec.browser2.tsx @@ -5,7 +5,12 @@ import { CanvasControlsContainerID } from '../../controls/new-canvas-controls' import { mouseDownAtPoint, mouseMoveToPoint, mouseUpAtPoint } from '../../event-helpers.test-utils' import { canvasPoint } from '../../../../core/shared/math-utils' -const makeTestProject = (columns: string, rows: string) => ` +const makeTestProject = (params: { + columns: string | null + rows: string | null + shorthand: string | null + height?: number | string +}) => ` import * as React from 'react' import { Storyboard } from 'utopia-api' @@ -21,10 +26,10 @@ export var storyboard = ( display: 'grid', gap: 10, width: 600, - height: 600, - gridTemplateColumns: '${columns}', - gridTemplateRows: '${rows}', - height: 'max-content', + height: ${params.height ?? "'max-content'"}, + ${params.columns != null ? `gridTemplateColumns: '${params.columns}',` : ''} + ${params.rows != null ? `gridTemplateRows: '${params.rows}',` : ''} + ${params.shorthand != null ? `gridTemplate: '${params.shorthand}',` : ''} }} >