Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Freeze grid template during child move #6201

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import type { ElementPath } from 'utopia-shared/src/types'
import { MetadataUtils } from '../../../../core/model/element-metadata-utils'
import * as EP from '../../../../core/shared/element-path'
import type {
ElementInstanceMetadataMap,
GridAutoOrTemplateBase,
} from '../../../../core/shared/element-template'
import * as PP from '../../../../core/shared/property-path'
import { printGridAutoOrTemplateBase } from '../../../inspector/common/css-utils'
import type { PropertyToUpdate } from '../../commands/set-property-command'
import {
propertyToDelete,
propertyToSet,
updateBulkProperties,
} from '../../commands/set-property-command'
import { GridControls } from '../../controls/grid-controls'
import type { CanvasStrategyFactory } from '../canvas-strategies'
import { onlyFitWhenDraggingThisControl } from '../canvas-strategies'
Expand Down Expand Up @@ -34,6 +47,13 @@ export const gridRearrangeMoveStrategy: CanvasStrategyFactory = (
return null
}

const gridPath = EP.parentPath(selectedElement)

const initialTemplates = getGridTemplates(canvasState.startingMetadata, gridPath)
if (initialTemplates == null) {
return null
}

return {
id: 'rearrange-grid-move-strategy',
name: 'Rearrange Grid (Move)',
Expand Down Expand Up @@ -83,14 +103,98 @@ export const gridRearrangeMoveStrategy: CanvasStrategyFactory = (
return emptyStrategyApplicationResult
}

return strategyApplicationResult(moveCommands, {
grid: {
targetCell: targetGridCell,
draggingFromCell: draggingFromCell,
originalRootCell: originalRootCell,
currentRootCell: targetRootCell,
const midInteractionCommands = [
// during the interaction, freeze the template with the calculated values…
updateBulkProperties('mid-interaction', gridPath, [
propertyToSet(
PP.create('style', 'gridTemplateColumns'),
printGridAutoOrTemplateBase(initialTemplates.calculated.columns),
),
propertyToSet(
PP.create('style', 'gridTemplateRows'),
printGridAutoOrTemplateBase(initialTemplates.calculated.rows),
),
]),
]

const onCompleteCommands = [
// …eventually, restore the grid template on complete.
updateBulkProperties(
'on-complete',
gridPath,
restoreGridTemplateFromProps(initialTemplates.fromProps),
),
]

return strategyApplicationResult(
[...moveCommands, ...midInteractionCommands, ...onCompleteCommands],
{
grid: {
targetCell: targetGridCell,
draggingFromCell: draggingFromCell,
originalRootCell: originalRootCell,
currentRootCell: targetRootCell,
},
},
})
)
},
}
}

function restoreGridTemplateFromProps(params: {
columns: GridAutoOrTemplateBase
rows: GridAutoOrTemplateBase
}): PropertyToUpdate[] {
let properties: PropertyToUpdate[] = []
const newCols = printGridAutoOrTemplateBase(params.columns)
const newRows = printGridAutoOrTemplateBase(params.rows)
if (newCols === '') {
properties.push(propertyToDelete(PP.create('style', 'gridTemplateColumns')))
} else {
properties.push(propertyToSet(PP.create('style', 'gridTemplateColumns'), newCols))
}
if (newRows === '') {
properties.push(propertyToDelete(PP.create('style', 'gridTemplateRows')))
} else {
properties.push(propertyToSet(PP.create('style', 'gridTemplateRows'), newRows))
}
return properties
}

function getGridTemplates(jsxMetadata: ElementInstanceMetadataMap, gridPath: ElementPath) {
const grid = MetadataUtils.findElementByElementPath(jsxMetadata, gridPath)
if (grid == null) {
return null
}

const templateFromProps = grid.specialSizeMeasurements.containerGridPropertiesFromProps
const templateRowsFromProps = templateFromProps.gridTemplateRows
if (templateRowsFromProps == null) {
return null
}
const templateColsFromProps = templateFromProps.gridTemplateColumns
if (templateColsFromProps == null) {
return null
}

const templateCalculated = grid.specialSizeMeasurements.containerGridProperties
const templateRowsCalculated = templateCalculated.gridTemplateRows
if (templateRowsCalculated == null) {
return null
}
const templateColsCalculated = templateCalculated.gridTemplateColumns
if (templateColsCalculated == null) {
return null
}

return {
calculated: {
columns: templateColsCalculated,
rows: templateRowsCalculated,
},
fromProps: {
columns: templateColsFromProps,
rows: templateRowsFromProps,
},
}
}
Loading