-
Notifications
You must be signed in to change notification settings - Fork 172
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
Convert to grid #6097
Merged
Merged
Convert to grid #6097
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1858a3a
first stab at convert to grid strategy
bkrmendy 0ab1d6e
grid conversion too
bkrmendy 6595df3
reinstate the layout system selector
bkrmendy f87493b
Merge branch 'master' into feature/convert-to-grid
ruggi 1e610b5
Update editor/src/components/inspector/inspector-strategies/inspector…
bkrmendy 02c015c
An icon for the "Do Nothing" strategy (#6112)
lankaukk 2968526
remove positioning from children
bkrmendy a9e3b52
add grid/flex dropdown
bkrmendy c1b2a9d
remove stray comma
bkrmendy 4c269fb
fix + button positioning
bkrmendy 63cf9b3
test ids
bkrmendy e9c412b
grid shortcut
ruggi 8255cf1
fix flex tests
bkrmendy fe1303c
grid test
bkrmendy ae33713
Merge branch 'master' of https://github.com/concrete-utopia/utopia in…
bkrmendy 83a7e54
popup positioning
bkrmendy bfba9e5
removeGridConvertToAbsolute
bkrmendy 96412e6
naming
bkrmendy 0875c97
Update editor/src/components/common/shared-strategies/convert-strateg…
bkrmendy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
editor/src/components/common/shared-strategies/convert-strategies-common.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import type { ElementPath } from 'utopia-shared/src/types' | ||
import { MetadataUtils } from '../../../core/model/element-metadata-utils' | ||
import type { ElementPathTrees } from '../../../core/shared/element-path-tree' | ||
import type { ElementInstanceMetadataMap } from '../../../core/shared/element-template' | ||
import { | ||
isElementNonDOMElement, | ||
replaceNonDOMElementPathsWithTheirChildrenRecursive, | ||
} from '../../canvas/canvas-strategies/strategies/fragment-like-helpers' | ||
import type { AllElementProps } from '../../editor/store/editor-state' | ||
|
||
export type FlexDirectionRowColumn = 'row' | 'column' // a limited subset as we never guess row-reverse or column-reverse | ||
export type FlexAlignItems = 'center' | 'flex-end' | ||
|
||
export function getChildrenPathsForContainer( | ||
metadata: ElementInstanceMetadataMap, | ||
elementPathTree: ElementPathTrees, | ||
path: ElementPath, | ||
allElementProps: AllElementProps, | ||
) { | ||
return MetadataUtils.getChildrenPathsOrdered(metadata, elementPathTree, path).flatMap((child) => | ||
isElementNonDOMElement(metadata, allElementProps, elementPathTree, child) | ||
? replaceNonDOMElementPathsWithTheirChildrenRecursive( | ||
metadata, | ||
allElementProps, | ||
elementPathTree, | ||
[child], | ||
) | ||
: child, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
editor/src/components/common/shared-strategies/convert-to-grid-strategy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import type { ElementPath } from 'utopia-shared/src/types' | ||
import type { ElementPathTrees } from '../../../core/shared/element-path-tree' | ||
import type { ElementInstanceMetadataMap } from '../../../core/shared/element-template' | ||
import type { AllElementProps } from '../../editor/store/editor-state' | ||
import type { CanvasCommand } from '../../canvas/commands/commands' | ||
import { | ||
flexContainerProps, | ||
gridContainerProps, | ||
nukeAllAbsolutePositioningPropsCommands, | ||
prunePropsCommands, | ||
sizeToVisualDimensions, | ||
} from '../../inspector/inspector-common' | ||
import { getChildrenPathsForContainer } from './convert-strategies-common' | ||
import type { CanvasFrameAndTarget } from '../../canvas/canvas-types' | ||
import { MetadataUtils } from '../../../core/model/element-metadata-utils' | ||
import { setProperty } from '../../canvas/commands/set-property-command' | ||
import * as PP from '../../../core/shared/property-path' | ||
|
||
function guessLayoutInfoAlongAxis( | ||
children: Array<CanvasFrameAndTarget>, | ||
sortFn: (a: CanvasFrameAndTarget, b: CanvasFrameAndTarget) => number, | ||
comesAfter: (a: CanvasFrameAndTarget, b: CanvasFrameAndTarget) => boolean, | ||
gapBetween: (a: CanvasFrameAndTarget, b: CanvasFrameAndTarget) => number, | ||
): { nChildren: number; averageGap: number } { | ||
if (children.length === 0) { | ||
return { nChildren: 0, averageGap: 0 } | ||
} | ||
|
||
const sortedChildren = children.sort(sortFn) | ||
let childrenAlongAxis = 1 | ||
let gaps: number[] = [] | ||
let currentChild = sortedChildren[0] | ||
for (const child of sortedChildren.slice(1)) { | ||
if (comesAfter(currentChild, child)) { | ||
childrenAlongAxis += 1 | ||
gaps.push(gapBetween(currentChild, child)) | ||
currentChild = child | ||
} | ||
} | ||
|
||
const averageGap = | ||
gaps.length === 0 ? 0 : Math.floor(gaps.reduce((a, b) => a + b, 0) / gaps.length) | ||
|
||
return { | ||
nChildren: childrenAlongAxis, | ||
averageGap: averageGap, | ||
} | ||
} | ||
|
||
function guessMatchingGridSetup(children: Array<CanvasFrameAndTarget>): { | ||
gap: number | ||
numberOfColumns: number | ||
numberOfRows: number | ||
} { | ||
const horizontalData = guessLayoutInfoAlongAxis( | ||
children, | ||
(a, b) => a.frame.x - b.frame.x, | ||
bkrmendy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
(a, b) => a.frame.x + a.frame.width <= b.frame.x, | ||
(a, b) => b.frame.x - (a.frame.x + a.frame.width), | ||
) | ||
const verticalData = guessLayoutInfoAlongAxis( | ||
children, | ||
(a, b) => a.frame.y - b.frame.y, | ||
(a, b) => a.frame.y + a.frame.height <= b.frame.y, | ||
(a, b) => b.frame.y - (a.frame.y + a.frame.height), | ||
) | ||
|
||
return { | ||
gap: (horizontalData.averageGap + verticalData.averageGap) / 2, | ||
numberOfColumns: horizontalData.nChildren, | ||
numberOfRows: verticalData.nChildren, | ||
} | ||
} | ||
|
||
export function convertLayoutToGridCommands( | ||
metadata: ElementInstanceMetadataMap, | ||
elementPathTree: ElementPathTrees, | ||
elementPaths: Array<ElementPath>, | ||
allElementProps: AllElementProps, | ||
): Array<CanvasCommand> { | ||
return elementPaths.flatMap((elementPath) => { | ||
const childrenPaths = getChildrenPathsForContainer( | ||
metadata, | ||
elementPathTree, | ||
elementPath, | ||
allElementProps, | ||
) | ||
const childFrames: Array<CanvasFrameAndTarget> = childrenPaths.map((child) => ({ | ||
target: child, | ||
frame: MetadataUtils.getFrameOrZeroRectInCanvasCoords(child, metadata), | ||
})) | ||
|
||
const { gap, numberOfColumns, numberOfRows } = guessMatchingGridSetup(childFrames) | ||
|
||
return [ | ||
ruggi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
...prunePropsCommands(flexContainerProps, elementPath), | ||
...prunePropsCommands(gridContainerProps, elementPath), | ||
...childrenPaths.flatMap((child) => [ | ||
...nukeAllAbsolutePositioningPropsCommands(child), | ||
...sizeToVisualDimensions(metadata, elementPathTree, child), | ||
]), | ||
setProperty('always', elementPath, PP.create('style', 'display'), 'grid'), | ||
setProperty('always', elementPath, PP.create('style', 'gap'), gap), | ||
setProperty( | ||
'always', | ||
elementPath, | ||
PP.create('style', 'gridTemplateColumns'), | ||
Array(numberOfColumns).fill('1fr').join(' '), | ||
), | ||
setProperty( | ||
'always', | ||
elementPath, | ||
PP.create('style', 'gridTemplateRows'), | ||
Array(numberOfRows).fill('1fr').join(' '), | ||
), | ||
] | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should also have a logic branch so that it makes sure that the combined number of resulting cells is
>=
than the number of children. For example if you have N children resulting from duplication, all having the exact same coordinates, the resulting grid template would be incorrect (e.g. 1x1 instead of 1xN)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussed on a side channel, I'll implement this on a separate PR because that way we'll have a holistic overview of the convert-to-grid strategy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
keeping this unresolved so we can find it quickly later