Skip to content

Commit

Permalink
rehabilitate maybeFlexGapData
Browse files Browse the repository at this point in the history
  • Loading branch information
bkrmendy committed Oct 10, 2024
1 parent b31a2aa commit 5146ef8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import type { FlexGapData } from '../../gap-utils'
import {
cursorFromFlexDirection,
dragDeltaForOrientation,
getFlexGapData,
maybeFlexGapData,
recurseIntoChildrenOfMapOrFragment,
} from '../../gap-utils'
import type { CanvasStrategyFactory } from '../canvas-strategies'
Expand Down Expand Up @@ -95,7 +95,7 @@ export const setFlexGapStrategy: CanvasStrategyFactory = (
return null
}

const flexGap = getFlexGapData(
const flexGap = maybeFlexGapData(
canvasState.styleInfoReader(selectedElement),
MetadataUtils.findElementByElementPath(canvasState.startingMetadata, selectedElement),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import type { FlexGapData } from '../../gap-utils'
import {
cursorFromFlexDirection,
gapControlBoundsFromMetadata,
getFlexGapData,
maybeFlexGapData,
recurseIntoChildrenOfMapOrFragment,
} from '../../gap-utils'
import { CanvasOffsetWrapper } from '../canvas-offset-wrapper'
Expand Down Expand Up @@ -136,7 +136,7 @@ export const FlexGapControl = controlForStrategyMemoized<FlexGapControlProps>((p
const flexGapFromEditor = useEditorState(
Substores.fullStore,
(store) =>
getFlexGapData(
maybeFlexGapData(
getActivePlugin(store.editor).styleInfoFactory({
projectContents: store.editor.projectContents,
metadata: metadata,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CanvasOffsetWrapper } from '../canvas-offset-wrapper'
import type { FlexGapData, PathWithBounds } from '../../gap-utils'
import {
gapControlBoundsFromMetadata,
getFlexGapData,
maybeFlexGapData,
recurseIntoChildrenOfMapOrFragment,
} from '../../gap-utils'
import type { ElementPath } from 'utopia-shared/src/types'
Expand Down Expand Up @@ -40,7 +40,7 @@ export const SubduedFlexGapControl = React.memo<SubduedFlexGapControlProps>((pro
const flexGap = useEditorState(
Substores.fullStore,
(store) =>
getFlexGapData(
maybeFlexGapData(
getActivePlugin(store.editor).styleInfoFactory({
projectContents: store.editor.projectContents,
metadata: store.editor.jsxMetadata,
Expand Down
28 changes: 20 additions & 8 deletions editor/src/components/canvas/gap-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -332,23 +332,35 @@ export interface FlexGapData {
direction: FlexDirection
}

export function getFlexGapData(
export function maybeFlexGapData(
info: StyleInfo | null,
instance: ElementInstanceMetadata | null,
element: ElementInstanceMetadata | null,
): FlexGapData | null {
if (instance == null || info == null) {
if (
element == null ||
element.specialSizeMeasurements.display !== 'flex' ||
isLeft(element.element) ||
!isJSXElement(element.element.value) ||
info == null
) {
return null
}

const gap = info.gap?.value
const renderedValuePx = instance.specialSizeMeasurements.gap
if (gap == null || renderedValuePx == null) {
if (element.specialSizeMeasurements.justifyContent?.startsWith('space')) {
return null
}

const gap = element.specialSizeMeasurements.gap ?? 0
const gapFromReader = info.gap?.value

const flexDirection = element.specialSizeMeasurements.flexDirection ?? 'row'

return {
value: cssNumberWithRenderedValue(gap, renderedValuePx),
direction: info.flexDirection?.value ?? 'row',
value: {
renderedValuePx: gap,
value: gapFromReader ?? null,
},
direction: flexDirection,
}
}

Expand Down

0 comments on commit 5146ef8

Please sign in to comment.