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

GridMeasurementHelper component to continuously measure grid cells #6517

Merged
merged 26 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d1f7ae8
Show invisible grid controls on all grids during interactions
gbalint Oct 10, 2024
0f92b36
Update snapshot
gbalint Oct 10, 2024
1509747
remove unnecessary nullish coalescing
gbalint Oct 10, 2024
145fe5a
Add missing visible prop
gbalint Oct 10, 2024
ed1a261
Deduplicate grid controls
gbalint Oct 11, 2024
e995db3
Better deduplication
gbalint Oct 11, 2024
46e414a
Remove unnecessary change
gbalint Oct 11, 2024
a81df07
Use GridControl directly in canvas controls
gbalint Oct 11, 2024
30d0bdf
Even better
gbalint Oct 11, 2024
d4814cf
Separate measurement grid helper
gbalint Oct 14, 2024
a92c086
Remove latestMetadata hack from interactions
gbalint Oct 14, 2024
bf86fa4
Remove unused import
gbalint Oct 14, 2024
2d42dc0
Merge branch 'master' into fix/reparent-blinking
gbalint Oct 14, 2024
4310311
Fix build and refactor
gbalint Oct 14, 2024
0df1258
fix
gbalint Oct 14, 2024
b4e204e
Merge branch 'master' into fix/reparent-blinking
gbalint Oct 14, 2024
1696fd1
Update keys and types
gbalint Oct 14, 2024
d167a0a
Flatten props
gbalint Oct 14, 2024
3d8be9f
Move grid measurement helpers together into component
gbalint Oct 15, 2024
5866765
Added missing displayName
gbalint Oct 15, 2024
d14e7df
Restrict GridMeasurmentHelper prop type
gbalint Oct 15, 2024
06aff88
fastDeepEqual hack
gbalint Oct 15, 2024
dfc4349
Added comment
gbalint Oct 15, 2024
d6530e5
Remove magic constant
gbalint Oct 15, 2024
5e69431
Small refactor to remove duplicated code
gbalint Oct 15, 2024
360a854
Fix compile error
gbalint Oct 15, 2024
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
Expand Up @@ -43,7 +43,7 @@ import {
getStyleAttributesForFrameInAbsolutePosition,
updateInsertionSubjectWithAttributes,
} from './draw-to-insert-metastrategy'
import { getMetadataWithGridCellBounds, setGridPropsCommands } from './grid-helpers'
import { setGridPropsCommands } from './grid-helpers'
import { newReparentSubjects } from './reparent-helpers/reparent-strategy-helpers'
import { getReparentTargetUnified } from './reparent-helpers/reparent-strategy-parent-lookup'
import { getGridCellUnderMouseFromMetadata } from './grid-cell-bounds'
Expand Down Expand Up @@ -132,13 +132,10 @@ const gridDrawToInsertStrategyInner =
canvasState.propertyControlsInfo,
)?.newParent.intendedParentPath

const { metadata: parent, customStrategyState: updatedCustomState } =
getMetadataWithGridCellBounds(
targetParent,
canvasState.startingMetadata,
interactionSession.latestMetadata,
customStrategyState,
)
const parent = MetadataUtils.findElementByElementPath(
canvasState.startingMetadata,
targetParent,
)

if (targetParent == null || parent == null || !MetadataUtils.isGridLayoutedContainer(parent)) {
return null
Expand Down Expand Up @@ -175,7 +172,6 @@ const gridDrawToInsertStrategyInner =
updateHighlightedViews('mid-interaction', [targetParent]),
],
[targetParent],
updatedCustomState ?? undefined,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -710,62 +710,6 @@ export function getGridRelatedIndexes(params: {
return expandedRelatedIndexes[params.index] ?? []
}

export function getMetadataWithGridCellBounds(
path: ElementPath | null | undefined,
startingMetadata: ElementInstanceMetadataMap,
latestMetadata: ElementInstanceMetadataMap,
customStrategyState: CustomStrategyState,
): {
metadata: ElementInstanceMetadata | null
customStrategyState: CustomStrategyState | null
} {
if (path == null) {
return {
metadata: null,
customStrategyState: null,
}
}

const fromStartingMetadata = MetadataUtils.findElementByElementPath(startingMetadata, path)

if (fromStartingMetadata?.specialSizeMeasurements.gridCellGlobalFrames != null) {
return {
metadata: fromStartingMetadata,
customStrategyState: null,
}
}

const fromStrategyState = customStrategyState.grid.metadataCacheForGrids[EP.toString(path)]
if (fromStrategyState != null) {
return {
metadata: fromStrategyState,
customStrategyState: null,
}
}

const fromLatestMetadata = MetadataUtils.findElementByElementPath(latestMetadata, path)
if (fromLatestMetadata?.specialSizeMeasurements.gridCellGlobalFrames != null) {
return {
metadata: fromLatestMetadata,
customStrategyState: {
...customStrategyState,
grid: {
...customStrategyState.grid,
metadataCacheForGrids: {
...customStrategyState.grid.metadataCacheForGrids,
[EP.toString(path)]: fromLatestMetadata,
},
},
},
}
}

return {
metadata: fromStartingMetadata,
customStrategyState: null,
}
}

function getOriginalElementGridConfiguration(
gridCellGlobalFrames: GridCellGlobalFrames,
interactionData: DragInteractionData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import type { InsertionPath } from '../../../editor/store/insertion-path'
import { CSSCursor } from '../../canvas-types'
import { setCursorCommand } from '../../commands/set-cursor-command'
import { propertyToSet, updateBulkProperties } from '../../commands/set-property-command'
import { showGridControls } from '../../commands/show-grid-controls-command'
import { updateSelectedViews } from '../../commands/update-selected-views-command'
import { controlsForGridPlaceholders } from '../../controls/grid-controls-for-strategies'
import { ParentBounds } from '../../controls/parent-bounds'
Expand All @@ -39,7 +38,7 @@ import {
import type { DragInteractionData, InteractionSession, UpdatedPathMap } from '../interaction-state'
import { honoursPropsPosition, shouldKeepMovingDraggedGroupChildren } from './absolute-utils'
import { replaceFragmentLikePathsWithTheirChildrenRecursive } from './fragment-like-helpers'
import { getMetadataWithGridCellBounds, runGridRearrangeMove } from './grid-helpers'
import { runGridRearrangeMove } from './grid-helpers'
import { ifAllowedToReparent, isAllowedToReparent } from './reparent-helpers/reparent-helpers'
import { removeAbsolutePositioningProps } from './reparent-helpers/reparent-property-changes'
import type { ReparentTarget } from './reparent-helpers/reparent-strategy-helpers'
Expand Down Expand Up @@ -164,13 +163,10 @@ export function applyGridReparent(
return emptyStrategyApplicationResult
}

const { metadata: grid, customStrategyState: updatedCustomState } =
getMetadataWithGridCellBounds(
newParent.intendedParentPath,
canvasState.startingMetadata,
interactionSession.latestMetadata,
customStrategyState,
)
const grid = MetadataUtils.findElementByElementPath(
canvasState.startingMetadata,
newParent.intendedParentPath,
)

if (grid == null) {
return strategyApplicationResult([], [newParent.intendedParentPath])
Expand Down Expand Up @@ -231,12 +227,6 @@ export function applyGridReparent(
newParent.intendedParentPath,
])

const baseCustomState = updatedCustomState ?? customStrategyState
const customStrategyStatePatch = {
...baseCustomState,
elementsToRerender: elementsToRerender,
}

return strategyApplicationResult(
[
...outcomes.flatMap((c) => c.commands),
Expand All @@ -245,7 +235,9 @@ export function applyGridReparent(
setCursorCommand(CSSCursor.Reparent),
],
elementsToRerender,
customStrategyStatePatch,
{
elementsToRerender: elementsToRerender,
},
)
},
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { MetadataUtils } from '../../../../core/model/element-metadata-utils'
import * as EP from '../../../../core/shared/element-path'
import type { GridElementProperties, GridPosition } from '../../../../core/shared/element-template'
import {
type CanvasRectangle,
isInfinityRectangle,
rectangleIntersection,
} from '../../../../core/shared/math-utils'
import { isCSSKeyword } from '../../../inspector/common/css-utils'
import { isFillOrStretchModeApplied } from '../../../inspector/inspector-common'
import {
controlsForGridPlaceholders,
Expand All @@ -22,13 +20,12 @@ import {
strategyApplicationResult,
} from '../canvas-strategy-types'
import type { InteractionSession } from '../interaction-state'
import { getMetadataWithGridCellBounds, setGridPropsCommands } from './grid-helpers'
import { setGridPropsCommands } from './grid-helpers'
import { resizeBoundingBoxFromSide } from './resize-helpers'

export const gridResizeElementStrategy: CanvasStrategyFactory = (
canvasState: InteractionCanvasState,
interactionSession: InteractionSession | null,
customState,
) => {
const selectedElements = getTargetPathsFromInteractionTarget(canvasState.interactionTarget)
if (selectedElements.length !== 1) {
Expand Down Expand Up @@ -86,13 +83,10 @@ export const gridResizeElementStrategy: CanvasStrategyFactory = (
return emptyStrategyApplicationResult
}

const { metadata: container, customStrategyState: updatedCustomState } =
getMetadataWithGridCellBounds(
EP.parentPath(selectedElement),
canvasState.startingMetadata,
interactionSession.latestMetadata,
customState,
)
const container = MetadataUtils.findElementByElementPath(
canvasState.startingMetadata,
EP.parentPath(selectedElement),
)

if (container == null) {
return emptyStrategyApplicationResult
Expand Down Expand Up @@ -123,7 +117,6 @@ export const gridResizeElementStrategy: CanvasStrategyFactory = (
return strategyApplicationResult(
setGridPropsCommands(selectedElement, gridTemplate, gridProps),
[parentGridPath],
updatedCustomState ?? undefined,
)
},
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/** @jsxRuntime classic */
/** @jsx jsx */
import fastDeepEqual from 'fast-deep-equal'
import type { Sides } from 'utopia-api/core'
import type { ElementPath } from 'utopia-shared/src/types'
import { isStaticGridRepeat, printGridAutoOrTemplateBase } from '../../inspector/common/css-utils'
Expand Down Expand Up @@ -61,22 +62,72 @@ export function getNullableAutoOrTemplateBaseString(
}
}

export type GridData = {
elementPath: ElementPath
export type GridMeasurementHelperData = {
frame: CanvasRectangle
gridTemplateColumns: GridAutoOrTemplateBase | null
gridTemplateRows: GridAutoOrTemplateBase | null
gridTemplateColumnsFromProps: GridAutoOrTemplateBase | null
gridTemplateRowsFromProps: GridAutoOrTemplateBase | null
gap: number | null
justifyContent: string | null
alignContent: string | null
rowGap: number | null
columnGap: number | null
justifyContent: string | null
alignContent: string | null
padding: Sides
rows: number
columns: number
cells: number
}

export function useGridMeasurentHelperData(
elementPath: ElementPath,
): GridMeasurementHelperData | null {
return useEditorState(
Substores.metadata,
(store) => {
const element = MetadataUtils.findElementByElementPath(store.editor.jsxMetadata, elementPath)

const targetGridContainer = MetadataUtils.isGridLayoutedContainer(element) ? element : null

if (
targetGridContainer == null ||
targetGridContainer.globalFrame == null ||
!isFiniteRectangle(targetGridContainer.globalFrame)
) {
return null
}

const columns = getCellsCount(
targetGridContainer.specialSizeMeasurements.containerGridProperties.gridTemplateColumns,
)
const rows = getCellsCount(
targetGridContainer.specialSizeMeasurements.containerGridProperties.gridTemplateRows,
)

return {
frame: targetGridContainer.globalFrame,
gridTemplateColumns:
targetGridContainer.specialSizeMeasurements.containerGridProperties.gridTemplateColumns,
gridTemplateRows:
targetGridContainer.specialSizeMeasurements.containerGridProperties.gridTemplateRows,
gap: targetGridContainer.specialSizeMeasurements.gap,
rowGap: targetGridContainer.specialSizeMeasurements.rowGap,
columnGap: targetGridContainer.specialSizeMeasurements.columnGap,
justifyContent: targetGridContainer.specialSizeMeasurements.justifyContent,
alignContent: targetGridContainer.specialSizeMeasurements.alignContent,
padding: targetGridContainer.specialSizeMeasurements.padding,
columns: columns,
cells: rows * columns,
}
},
'useGridMeasurentHelperData',
fastDeepEqual, //TODO: this should not be needed, but it seems EditorStateKeepDeepEquality is not running, and metadata reference is always updated
)
}

export type GridData = GridMeasurementHelperData & {
elementPath: ElementPath
gridTemplateColumnsFromProps: GridAutoOrTemplateBase | null
gridTemplateRowsFromProps: GridAutoOrTemplateBase | null
rows: number
cells: number
metadata: ElementInstanceMetadata
}

Expand Down Expand Up @@ -156,6 +207,12 @@ export const GridRowColumnResizingControls =
)

export const GridControlsKey = (gridPath: ElementPath) => `grid-controls-${EP.toString(gridPath)}`
export const GridControlKey = (gridPath: ElementPath) => `grid-control-${EP.toString(gridPath)}`

export const GridMeasurementHelpersKey = (gridPath: ElementPath) =>
`grid-measurement-helpers-${EP.toString(gridPath)}`
export const GridMeasurementHelperKey = (gridPath: ElementPath) =>
`grid-measurement-helper-${EP.toString(gridPath)}`

export interface GridControlProps {
grid: GridData
Expand Down
Loading
Loading