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

Show other snappable lines during grid ruler resize #6722

Merged
merged 7 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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 @@ -16,7 +16,6 @@ import { assertNever } from '../../../../core/shared/utils'
import { gridContainerIdentifier, gridItemIdentifier } from '../../../editor/store/editor-state'
import { isCSSKeyword } from '../../../inspector/common/css-utils'
import {
controlsForGridPlaceholders,
controlsForGridRulers,
GridResizeControls,
} from '../../controls/grid-controls-for-strategies'
Expand Down Expand Up @@ -82,7 +81,6 @@ export const gridResizeElementRulerStrategy: CanvasStrategyFactory = (
key: `grid-resize-controls-${EP.toString(selectedElement)}`,
show: 'always-visible',
},
controlsForGridPlaceholders(gridItemIdentifier(selectedElement)),
controlsForGridRulers(gridItemIdentifier(selectedElement)),
],
fitness: onlyFitWhenDraggingThisControl(interactionSession, 'GRID_RESIZE_RULER_HANDLE', 1),
Expand Down
185 changes: 139 additions & 46 deletions editor/src/components/canvas/controls/grid-controls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import type { Optic } from '../../../core/shared/optics/optics'
import { optionalMap } from '../../../core/shared/optional-utils'
import { assertNever, NO_OP } from '../../../core/shared/utils'
import { Modifier } from '../../../utils/modifiers'
import { when } from '../../../utils/react-conditionals'
import { unless, when } from '../../../utils/react-conditionals'
import type { UtopiColor } from '../../../uuiui'
import { useColorTheme, UtopiaStyles } from '../../../uuiui'
import { useDispatch } from '../../editor/store/dispatch-context'
Expand All @@ -69,6 +69,7 @@ import {
isGridCSSRepeat,
printCSSNumberWithDefaultUnit,
printGridCSSNumber,
stringifyGridDimension,
} from '../../inspector/common/css-utils'
import CanvasActions from '../canvas-actions'
import type { GridResizeEdge } from '../canvas-strategies/interaction-state'
Expand Down Expand Up @@ -2217,15 +2218,21 @@ const RulerMarkers = React.memo((props: RulerMarkersProps) => {
const [frozenMarkers, setFrozenMarkers] = React.useState<RulerMarkerData | null>(null)
const [showExtraMarkers, setShowExtraMarkers] = React.useState<'row' | 'column' | null>(null)

const gridRect: CanvasRectangle | null = useEditorState(
const gridData = useEditorState(
Substores.metadata,
(store) => {
const originalGrid = findOriginalGrid(store.editor.jsxMetadata, EP.parentPath(props.path))
if (originalGrid == null) {
return null
}

return MetadataUtils.getFrameOrZeroRectInCanvasCoords(originalGrid, store.editor.jsxMetadata)
return {
gridPath: originalGrid,
gridRect: MetadataUtils.getFrameOrZeroRectInCanvasCoords(
originalGrid,
store.editor.jsxMetadata,
),
}
},
'RulerMarkers gridRect',
)
Expand All @@ -2249,10 +2256,12 @@ const RulerMarkers = React.memo((props: RulerMarkersProps) => {
const rulerMarkerData: RulerMarkerData | null = useEditorState(
Substores.metadata,
(store) => {
if (gridRect == null || parentGridCellGlobalFrames == null) {
if (gridData == null || parentGridCellGlobalFrames == null) {
return null
}

const gridRect = gridData.gridRect

const elementMetadata = MetadataUtils.findElementByElementPath(
store.editor.jsxMetadata,
props.path,
Expand Down Expand Up @@ -2463,7 +2472,7 @@ const RulerMarkers = React.memo((props: RulerMarkersProps) => {
[markerMouseUp, props, startResizeInteraction, rulerMarkerData, setForceShowGridPlaceholders],
)

if (rulerMarkerData == null || gridRect == null) {
if (rulerMarkerData == null || gridData == null) {
return null
}

Expand All @@ -2473,7 +2482,7 @@ const RulerMarkers = React.memo((props: RulerMarkersProps) => {
<React.Fragment>
<GridRuler
axis='column'
gridRect={gridRect}
gridRect={gridData.gridRect}
cellFrames={parentGridCellGlobalFrames}
rulerVisible={showExtraMarkers === 'column' ? 'visible' : 'not-visible'}
>
Expand Down Expand Up @@ -2514,7 +2523,7 @@ const RulerMarkers = React.memo((props: RulerMarkersProps) => {

<GridRuler
axis='row'
gridRect={gridRect}
gridRect={gridData.gridRect}
cellFrames={parentGridCellGlobalFrames}
rulerVisible={showExtraMarkers === 'row' ? 'visible' : 'not-visible'}
>
Expand Down Expand Up @@ -2587,8 +2596,8 @@ const RulerMarkers = React.memo((props: RulerMarkersProps) => {
height={rulerMarkerData.cellRect.height + 1}
/>

{/* Snap line during resize */}
<SnapLine
<SnapLines
gridPath={gridData.gridPath}
gridTemplate={rulerMarkerData.parentGrid}
container={rulerMarkerData.gridRect}
edge={resizeControlRef.current?.edge ?? null}
Expand Down Expand Up @@ -2645,9 +2654,10 @@ const ResizeOffsetLine = React.memo(
)
ResizeOffsetLine.displayName = 'ResizeOffsetLine'

const SnapLine = React.memo(
const SnapLines = React.memo(
(props: {
gridTemplate: GridContainerProperties
gridPath: ElementPath
edge: GridResizeEdge | null
container: CanvasRectangle
target: CanvasRectangle
Expand All @@ -2656,6 +2666,8 @@ const SnapLine = React.memo(
}) => {
const colorTheme = useColorTheme()

const gridData = useGridMeasurementHelperData(props.gridPath, 'element')

const targetMarker = React.useMemo(() => {
if (props.edge == null) {
return null
Expand Down Expand Up @@ -2723,49 +2735,130 @@ const SnapLine = React.memo(
? props.container.x
: props.target.x + (props.edge === 'column-end' ? props.target.width : 0)

if (
props.gridTemplate.gridTemplateColumns?.type !== 'DIMENSIONS' ||
props.gridTemplate.gridTemplateRows?.type !== 'DIMENSIONS' ||
gridData == null
) {
return null
}

const columns = props.gridTemplate.gridTemplateColumns.dimensions
const rows = props.gridTemplate.gridTemplateRows.dimensions

// TODO if hasGap === true, then maybe there shouldn't be duplicate lines for the same adjacent rows/cols
const hasGap =
(isColumn
? gridData.gap ?? gridData.columnGap ?? 0
: gridData.gap ?? gridData.rowGap ?? 0) !== 0

return (
<div
style={{
position: 'absolute',
width: isColumn ? 1 : props.container.width * canvasScale,
height: !isColumn ? 1 : props.container.height * canvasScale,
top: top * canvasScale,
left: left * canvasScale,
backgroundColor: colorTheme.gridControlsPink.value,
zoom: 1 / canvasScale,
}}
>
{when(
showLabel,
<div
style={{
position: 'absolute',
top: axis === 'column' ? -labelHeight - RulerMarkerIconSize - 5 : -10,
left: axis === 'row' ? -(labelWidth - RulerMarkerIconSize + 30) : -7,
color: colorTheme.gridControlsPink.value,
fontWeight: 700,
textAlign: axis === 'row' ? 'right' : undefined,
width: labelWidth,
height: labelHeight,
}}
>
<span
<React.Fragment>
{/* The default snap lines for the current edge */}
<div
style={{
position: 'absolute',
left: props.container.x,
top: props.container.y,
width: props.container.width,
height: props.container.height,
border: `1px solid ${colorTheme.grey65.value}`,
display: 'grid',
gridTemplateColumns: isColumn
? columns.map((d) => stringifyGridDimension(d)).join(' ')
: undefined,
gridTemplateRows: !isColumn
? rows.map((d) => stringifyGridDimension(d)).join(' ')
: undefined,
gap: gridData.gap ?? undefined,
rowGap: gridData.rowGap ?? undefined,
columnGap: gridData.columnGap ?? undefined,
padding:
`${gridData.padding.top ?? 0}px ` +
`${gridData.padding.left ?? 0}px ` +
`${gridData.padding.bottom ?? 0}px ` +
`${gridData.padding.right ?? 0}px`,
}}
>
{when(
isColumn,
columns.map((_, index) => {
return (
<div
key={`line-${index}`}
style={{
borderLeft:
index > 0 && hasGap ? `1px solid ${colorTheme.grey65.value}` : undefined,
borderRight:
index < columns.length - 1
? `1px solid ${colorTheme.grey65.value}`
: undefined,
}}
/>
)
}),
)}
{unless(
isColumn,
rows.map((_, index) => {
return (
<div
key={`line-${index}`}
style={{
borderTop:
index > 0 && hasGap ? `1px solid ${colorTheme.grey65.value}` : undefined,
borderBottom:
index < rows.length - 1 ? `1px solid ${colorTheme.grey65.value}` : undefined,
}}
/>
)
}),
)}
</div>
{/* The current snapline */}
<div
style={{
position: 'absolute',
width: isColumn ? 1 : props.container.width * canvasScale,
height: !isColumn ? 1 : props.container.height * canvasScale,
top: top * canvasScale,
left: left * canvasScale,
backgroundColor: colorTheme.gridControlsPink.value,
zoom: 1 / canvasScale,
}}
>
{when(
showLabel,
<div
style={{
backgroundColor: 'white',
padding: '2px 4px',
borderRadius: 2,
fontSize: 11,
position: 'absolute',
top: axis === 'column' ? -labelHeight - RulerMarkerIconSize - 5 : -10,
left: axis === 'row' ? -(labelWidth - RulerMarkerIconSize + 30) : -7,
color: colorTheme.gridControlsPink.value,
fontWeight: 700,
textAlign: axis === 'row' ? 'right' : undefined,
width: labelWidth,
height: labelHeight,
}}
>
{printPin(props.gridTemplate, targetMarker.position, axis)}
</span>
</div>,
)}
</div>
<span
style={{
backgroundColor: 'white',
padding: '2px 4px',
borderRadius: 2,
fontSize: 11,
}}
>
{printPin(props.gridTemplate, targetMarker.position, axis)}
</span>
</div>,
)}
</div>
</React.Fragment>
)
},
)
SnapLine.displayName = 'SnapLine'
SnapLines.displayName = 'SnapLines'

interface GridRulerProps {
axis: 'row' | 'column'
Expand Down
Loading