-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
https://github.com/user-attachments/assets/7627f89e-c87b-4246-9ac5-1f70ca4e254d ## Problem The x shortcut isn't aware of grids ## Fix Special case grid elements in the x shortcut. ### How it works If an element is a grid child and it's absolute positioned (for example after a reparent or drawing), pressing x removes the `position: absolute`, absolute positioning prop, and any explicit width/height props on the element, and sets `gridRow`/`gridColumn`/`gridRowStart`/gridRowEnd`/`gridColumnStart`/gridColumnEnd` in way that snaps the element into the grid cells it occupies. When an element is a non-position-absolute grid child, and x is pressed, `position: absolute` is set, the element is sized to its visual dimensions with `width`/`height`, and `top: 0` / `left: 0` is added. Making it possible to absolute resize the element after this is left to a follow-up PR. ### Details - Due to a circular dependency, a lot of grid cell related code was factored out into `grid-cell-utils.ts`
- Loading branch information
Showing
12 changed files
with
541 additions
and
142 deletions.
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
139 changes: 139 additions & 0 deletions
139
editor/src/components/canvas/canvas-strategies/strategies/grid-cell-bounds.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,139 @@ | ||
import type { ElementPath } from 'utopia-shared/src/types' | ||
import type { ElementInstanceMetadata } from '../../../../core/shared/element-template' | ||
import type { CanvasVector, WindowPoint, WindowRectangle } from '../../../../core/shared/math-utils' | ||
import { | ||
isInfinityRectangle, | ||
offsetPoint, | ||
rectContainsPoint, | ||
windowPoint, | ||
windowRectangle, | ||
} from '../../../../core/shared/math-utils' | ||
import { canvasPointToWindowPoint } from '../../dom-lookup' | ||
import * as EP from '../../../../core/shared/element-path' | ||
|
||
export type GridCellCoordinates = { row: number; column: number } | ||
|
||
export function gridCellCoordinates(row: number, column: number): GridCellCoordinates { | ||
return { row: row, column: column } | ||
} | ||
|
||
const gridCellTargetIdPrefix = 'grid-cell-target-' | ||
|
||
export function gridCellTargetId( | ||
gridElementPath: ElementPath, | ||
row: number, | ||
column: number, | ||
): string { | ||
return gridCellTargetIdPrefix + `${EP.toString(gridElementPath)}-${row}-${column}` | ||
} | ||
|
||
export function getGridCellUnderMouse(mousePoint: WindowPoint) { | ||
return getGridCellAtPoint(mousePoint, false) | ||
} | ||
|
||
export function getGridCellUnderMouseRecursive(mousePoint: WindowPoint) { | ||
return getGridCellAtPoint(mousePoint, true) | ||
} | ||
|
||
function isGridCellTargetId(id: string): boolean { | ||
return id.startsWith(gridCellTargetIdPrefix) | ||
} | ||
|
||
export function getGridCellAtPoint( | ||
point: WindowPoint, | ||
duplicating: boolean, | ||
): { id: string; coordinates: GridCellCoordinates; cellWindowRectangle: WindowRectangle } | null { | ||
function maybeRecursivelyFindCellAtPoint( | ||
elements: Element[], | ||
): { element: Element; cellWindowRectangle: WindowRectangle } | null { | ||
// If this used during duplication, the canvas controls will be in the way and we need to traverse the children too. | ||
for (const element of elements) { | ||
if (isGridCellTargetId(element.id)) { | ||
const domRect = element.getBoundingClientRect() | ||
const windowRect = windowRectangle(domRect) | ||
if (rectContainsPoint(windowRect, point)) { | ||
return { element: element, cellWindowRectangle: windowRect } | ||
} | ||
} | ||
|
||
if (duplicating) { | ||
const child = maybeRecursivelyFindCellAtPoint(Array.from(element.children)) | ||
if (child != null) { | ||
return child | ||
} | ||
} | ||
} | ||
|
||
return null | ||
} | ||
|
||
const cellUnderMouse = maybeRecursivelyFindCellAtPoint( | ||
document.elementsFromPoint(point.x, point.y), | ||
) | ||
if (cellUnderMouse == null) { | ||
return null | ||
} | ||
|
||
const { element, cellWindowRectangle } = cellUnderMouse | ||
const row = element.getAttribute('data-grid-row') | ||
const column = element.getAttribute('data-grid-column') | ||
|
||
return { | ||
id: element.id, | ||
cellWindowRectangle: cellWindowRectangle, | ||
coordinates: gridCellCoordinates( | ||
row == null ? 0 : parseInt(row), | ||
column == null ? 0 : parseInt(column), | ||
), | ||
} | ||
} | ||
|
||
const GRID_BOUNDS_TOLERANCE = 5 // px | ||
|
||
export function getGridCellBoundsFromCanvas( | ||
cell: ElementInstanceMetadata, | ||
canvasScale: number, | ||
canvasOffset: CanvasVector, | ||
) { | ||
const cellFrame = cell.globalFrame | ||
if (cellFrame == null || isInfinityRectangle(cellFrame)) { | ||
return null | ||
} | ||
|
||
const canvasFrameWidth = cellFrame.width * canvasScale | ||
const canvasFrameHeight = cellFrame.height * canvasScale | ||
|
||
const cellOriginPoint = offsetPoint( | ||
canvasPointToWindowPoint(cellFrame, canvasScale, canvasOffset), | ||
windowPoint({ x: GRID_BOUNDS_TOLERANCE, y: GRID_BOUNDS_TOLERANCE }), | ||
) | ||
const cellOrigin = getGridCellAtPoint(cellOriginPoint, true) | ||
if (cellOrigin == null) { | ||
return null | ||
} | ||
|
||
const cellEndPoint = offsetPoint( | ||
cellOriginPoint, | ||
windowPoint({ | ||
x: canvasFrameWidth - GRID_BOUNDS_TOLERANCE, | ||
y: canvasFrameHeight - GRID_BOUNDS_TOLERANCE, | ||
}), | ||
) | ||
const cellEnd = getGridCellAtPoint(cellEndPoint, true) | ||
if (cellEnd == null) { | ||
return null | ||
} | ||
|
||
const cellOriginCoords = cellOrigin.coordinates | ||
const cellEndCoords = cellEnd.coordinates | ||
|
||
const cellWidth = cellEndCoords.column - cellOriginCoords.column + 1 | ||
const cellHeight = cellEndCoords.row - cellOriginCoords.row + 1 | ||
|
||
return { | ||
column: cellOriginCoords.column, | ||
row: cellOriginCoords.row, | ||
width: cellWidth, | ||
height: cellHeight, | ||
} | ||
} |
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
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
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
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.