Skip to content

Commit

Permalink
Show invisible grid controls on all grids during interactions
Browse files Browse the repository at this point in the history
  • Loading branch information
gbalint committed Oct 10, 2024
1 parent 647fb23 commit d1f7ae8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import {
import { mapDropNulls } from '../../../../core/shared/array-utils'
import { assertNever } from '../../../../core/shared/utils'
import { showGridControls } from '../../commands/show-grid-controls-command'
import { memoize } from '../../../../core/shared/memoize'

export function runGridRearrangeMove(
targetElement: ElementPath,
Expand Down Expand Up @@ -813,3 +814,11 @@ function getOriginalElementGridConfiguration(
mouseCellPosInOriginalElement,
}
}

export const getAllGrids = memoize(getAllGridsInner, { maxSize: 1 })

function getAllGridsInner(jsxMetadata: ElementInstanceMetadataMap): Array<ElementPath> {
return Object.keys(jsxMetadata)
.filter((key) => MetadataUtils.isGridLayoutedContainer(jsxMetadata[key]))
.map(EP.fromString)
}
67 changes: 38 additions & 29 deletions editor/src/components/canvas/controls/grid-controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ import {
pointsEqual,
scaleRect,
windowPoint,
zeroRectangle,
zeroRectIfNullOrInfinity,
} from '../../../core/shared/math-utils'
import {
Expand Down Expand Up @@ -89,7 +88,6 @@ import { windowToCanvasCoordinates } from '../dom-lookup'
import type { Axis } from '../gap-utils'
import { useCanvasAnimation } from '../ui-jsx-canvas-renderer/animation-context'
import { CanvasOffsetWrapper } from './canvas-offset-wrapper'
import { CanvasLabel } from './select-mode/controls-common'
import { useMaybeHighlightElement } from './select-mode/select-mode-hooks'
import type { GridCellCoordinates } from '../canvas-strategies/strategies/grid-cell-bounds'
import { gridCellTargetId } from '../canvas-strategies/strategies/grid-cell-bounds'
Expand Down Expand Up @@ -662,9 +660,10 @@ export const GridControlsKey = (gridPath: ElementPath) => `grid-controls-${EP.to

export interface GridControlProps {
grid: GridData
visible: 'visible' | 'hidden'
}

export const GridControl = React.memo<GridControlProps>(({ grid }) => {
export const GridControl = React.memo<GridControlProps>(({ grid, visible }) => {
const dispatch = useDispatch()
const controls = useAnimationControls()
const colorTheme = useColorTheme()
Expand Down Expand Up @@ -916,6 +915,7 @@ export const GridControl = React.memo<GridControlProps>(({ grid }) => {
grid.padding == null
? 0
: `${grid.padding.top}px ${grid.padding.right}px ${grid.padding.bottom}px ${grid.padding.left}px`,
opacity: visible === 'visible' ? 1 : 0,
}

// Gap needs to be set only if the other two are not present or we'll have rendering issues
Expand Down Expand Up @@ -1126,38 +1126,47 @@ GridControl.displayName = 'GridControl'

export interface GridControlsProps {
targets: ElementPath[]
visible: 'visible' | 'hidden'
}

export const GridControls = controlForStrategyMemoized<GridControlsProps>(({ targets }) => {
const targetRootCell = useEditorState(
Substores.canvas,
(store) => store.editor.canvas.controls.gridControlData?.rootCell ?? null,
'GridControls targetRootCell',
)
export const GridControls = controlForStrategyMemoized<GridControlsProps>(
({ targets, visible }) => {
const targetRootCell = useEditorState(
Substores.canvas,
(store) => store.editor.canvas.controls.gridControlData?.rootCell ?? null,
'GridControls targetRootCell',
)

const hoveredGrids = useEditorState(
Substores.canvas,
(store) => stripNulls([store.editor.canvas.controls.gridControlData?.grid]),
'GridControls hoveredGrids',
)
const hoveredGrids = useEditorState(
Substores.canvas,
(store) => stripNulls([store.editor.canvas.controls.gridControlData?.grid]),
'GridControls hoveredGrids',
)

const grids = useGridData(uniqBy([...targets, ...hoveredGrids], (a, b) => EP.pathsEqual(a, b)))
const grids = useGridData(uniqBy([...targets, ...hoveredGrids], (a, b) => EP.pathsEqual(a, b)))

if (grids.length === 0) {
return null
}
if (grids.length === 0) {
return null
}

return (
<div id={'grid-controls'}>
<CanvasOffsetWrapper>
{grids.map((grid) => {
return <GridControl key={`grid-control-${EP.toString(grid.elementPath)}`} grid={grid} />
})}
<AbsoluteDistanceIndicators targetRootCell={targetRootCell} />
</CanvasOffsetWrapper>
</div>
)
})
return (
<div id={'grid-controls'}>
<CanvasOffsetWrapper>
{grids.map((grid) => {
return (
<GridControl
key={`grid-control-${EP.toString(grid.elementPath)}`}
grid={grid}
visible={visible ?? 'visible'}
/>
)
})}
<AbsoluteDistanceIndicators targetRootCell={targetRootCell} />
</CanvasOffsetWrapper>
</div>
)
},
)

const MIN_INDICATORS_DISTANCE = 32 // px

Expand Down
12 changes: 12 additions & 0 deletions editor/src/components/canvas/controls/new-canvas-controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ import { NO_OP } from '../../../core/shared/utils'
import { useIsMyProject } from '../../editor/store/collaborative-editing'
import { MultiplayerWrapper } from '../../../utils/multiplayer-wrapper'
import { MultiplayerPresence } from '../multiplayer-presence'
import { GridControls } from './grid-controls'
import { getAllGrids } from '../canvas-strategies/strategies/grid-helpers'

export const CanvasControlsContainerID = 'new-canvas-controls-container'

Expand Down Expand Up @@ -313,6 +315,7 @@ const NewCanvasControlsInner = (props: NewCanvasControlsInnerProps) => {
autoFocusedPaths,
filePathMappings,
propertyControlsInfo,
grids,
} = useEditorState(
Substores.fullStore,
(store) => {
Expand All @@ -333,6 +336,7 @@ const NewCanvasControlsInner = (props: NewCanvasControlsInnerProps) => {
autoFocusedPaths: store.derived.autoFocusedPaths,
filePathMappings: store.derived.filePathMappings,
propertyControlsInfo: store.editor.propertyControlsInfo,
grids: isDragInteractionActive(store.editor) ? getAllGrids(store.editor.jsxMetadata) : [],
}
},
'NewCanvasControlsInner',
Expand Down Expand Up @@ -596,6 +600,14 @@ const NewCanvasControlsInner = (props: NewCanvasControlsInnerProps) => {
isSelectOrInsertMode(editorMode) &&
!EP.multiplePathsAllWithTheSameUID(localSelectedViews),
<>
{when(
dragInteractionActive,
<RenderControlMemoized
key={'grids'}
control={GridControls.control as React.FC<{}>}
propsForControl={{ targets: grids, visible: 'hidden' }}
/>,
)}
{strategyControls.map((c) => (
<RenderControlMemoized
key={c.key}
Expand Down

0 comments on commit d1f7ae8

Please sign in to comment.