|
1 | 1 | /** @jsxRuntime classic */
|
2 | 2 | /** @jsx jsx */
|
| 3 | +import fastDeepEqual from 'fast-deep-equal' |
3 | 4 | import type { Sides } from 'utopia-api/core'
|
4 | 5 | import type { ElementPath } from 'utopia-shared/src/types'
|
5 | 6 | import { isStaticGridRepeat, printGridAutoOrTemplateBase } from '../../inspector/common/css-utils'
|
@@ -61,22 +62,72 @@ export function getNullableAutoOrTemplateBaseString(
|
61 | 62 | }
|
62 | 63 | }
|
63 | 64 |
|
64 |
| -export type GridData = { |
65 |
| - elementPath: ElementPath |
| 65 | +export type GridMeasurementHelperData = { |
66 | 66 | frame: CanvasRectangle
|
67 | 67 | gridTemplateColumns: GridAutoOrTemplateBase | null
|
68 | 68 | gridTemplateRows: GridAutoOrTemplateBase | null
|
69 |
| - gridTemplateColumnsFromProps: GridAutoOrTemplateBase | null |
70 |
| - gridTemplateRowsFromProps: GridAutoOrTemplateBase | null |
71 | 69 | gap: number | null
|
72 |
| - justifyContent: string | null |
73 |
| - alignContent: string | null |
74 | 70 | rowGap: number | null
|
75 | 71 | columnGap: number | null
|
| 72 | + justifyContent: string | null |
| 73 | + alignContent: string | null |
76 | 74 | padding: Sides
|
77 |
| - rows: number |
78 | 75 | columns: number
|
79 | 76 | cells: number
|
| 77 | +} |
| 78 | + |
| 79 | +export function useGridMeasurentHelperData( |
| 80 | + elementPath: ElementPath, |
| 81 | +): GridMeasurementHelperData | null { |
| 82 | + return useEditorState( |
| 83 | + Substores.metadata, |
| 84 | + (store) => { |
| 85 | + const element = MetadataUtils.findElementByElementPath(store.editor.jsxMetadata, elementPath) |
| 86 | + |
| 87 | + const targetGridContainer = MetadataUtils.isGridLayoutedContainer(element) ? element : null |
| 88 | + |
| 89 | + if ( |
| 90 | + targetGridContainer == null || |
| 91 | + targetGridContainer.globalFrame == null || |
| 92 | + !isFiniteRectangle(targetGridContainer.globalFrame) |
| 93 | + ) { |
| 94 | + return null |
| 95 | + } |
| 96 | + |
| 97 | + const columns = getCellsCount( |
| 98 | + targetGridContainer.specialSizeMeasurements.containerGridProperties.gridTemplateColumns, |
| 99 | + ) |
| 100 | + const rows = getCellsCount( |
| 101 | + targetGridContainer.specialSizeMeasurements.containerGridProperties.gridTemplateRows, |
| 102 | + ) |
| 103 | + |
| 104 | + return { |
| 105 | + frame: targetGridContainer.globalFrame, |
| 106 | + gridTemplateColumns: |
| 107 | + targetGridContainer.specialSizeMeasurements.containerGridProperties.gridTemplateColumns, |
| 108 | + gridTemplateRows: |
| 109 | + targetGridContainer.specialSizeMeasurements.containerGridProperties.gridTemplateRows, |
| 110 | + gap: targetGridContainer.specialSizeMeasurements.gap, |
| 111 | + rowGap: targetGridContainer.specialSizeMeasurements.rowGap, |
| 112 | + columnGap: targetGridContainer.specialSizeMeasurements.columnGap, |
| 113 | + justifyContent: targetGridContainer.specialSizeMeasurements.justifyContent, |
| 114 | + alignContent: targetGridContainer.specialSizeMeasurements.alignContent, |
| 115 | + padding: targetGridContainer.specialSizeMeasurements.padding, |
| 116 | + columns: columns, |
| 117 | + cells: rows * columns, |
| 118 | + } |
| 119 | + }, |
| 120 | + 'useGridMeasurentHelperData', |
| 121 | + fastDeepEqual, //TODO: this should not be needed, but it seems EditorStateKeepDeepEquality is not running, and metadata reference is always updated |
| 122 | + ) |
| 123 | +} |
| 124 | + |
| 125 | +export type GridData = GridMeasurementHelperData & { |
| 126 | + elementPath: ElementPath |
| 127 | + gridTemplateColumnsFromProps: GridAutoOrTemplateBase | null |
| 128 | + gridTemplateRowsFromProps: GridAutoOrTemplateBase | null |
| 129 | + rows: number |
| 130 | + cells: number |
80 | 131 | metadata: ElementInstanceMetadata
|
81 | 132 | }
|
82 | 133 |
|
@@ -156,6 +207,12 @@ export const GridRowColumnResizingControls =
|
156 | 207 | )
|
157 | 208 |
|
158 | 209 | export const GridControlsKey = (gridPath: ElementPath) => `grid-controls-${EP.toString(gridPath)}`
|
| 210 | +export const GridControlKey = (gridPath: ElementPath) => `grid-control-${EP.toString(gridPath)}` |
| 211 | + |
| 212 | +export const GridMeasurementHelpersKey = (gridPath: ElementPath) => |
| 213 | + `grid-measurement-helpers-${EP.toString(gridPath)}` |
| 214 | +export const GridMeasurementHelperKey = (gridPath: ElementPath) => |
| 215 | + `grid-measurement-helper-${EP.toString(gridPath)}` |
159 | 216 |
|
160 | 217 | export interface GridControlProps {
|
161 | 218 | grid: GridData
|
|
0 commit comments