-
-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Editor - Resizing elements inside page rows (#645)
* Fixes and remove unneeded stylings in recent visual editor update * Enable dragging row child edges (WIP) (broken) * Small refactor * Use context instead of state * Attempt to resize columns in rows * Add logic for right edge dragging * Enable resizing in DOM * Normalize column sizes (will extract this logic later) * Remove unneeded stop propagation * Drag & drop improvements * Drag preview + fix resizing result * Fix resizing preview sizes + show 12-column grid while dragging * Disallow resizing from left of first and right of last element in a row * Add minimum resize width and snapping to grid * Fix resizing positions + show resize cursor while resizing * Fix some function names and types * Fix element selection after resize * Fix deleting elements by clicking * Fix resizing on add + delete + edge cases with automatic column behavior * Fix deleting with Backspace * Set row column sizes prop in runtime - remove prop control * Change OverlayGrid file location * Simplicy some logic + separate resizing logic in its own hook * Add box around Image component * Increase and move horizontal resize drag areas to make resizing more intuitive * Extract some components to their own file * Move pageViw utils to geometry utils Co-authored-by: Jan Potoms <[email protected]>
- Loading branch information
1 parent
464ffc9
commit 2d06db6
Showing
14 changed files
with
1,085 additions
and
426 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
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
198 changes: 198 additions & 0 deletions
198
packages/toolpad-app/src/toolpad/AppEditor/PageEditor/NodeDropArea.tsx
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,198 @@ | ||
import * as React from 'react'; | ||
import clsx from 'clsx'; | ||
import { styled } from '@mui/material'; | ||
import { NodeInfo } from '../../../types'; | ||
import * as appDom from '../../../appDom'; | ||
import { | ||
absolutePositionCss, | ||
isHorizontalFlow, | ||
isVerticalFlow, | ||
Rectangle, | ||
} from '../../../utils/geometry'; | ||
import { | ||
DropZone, | ||
DROP_ZONE_BOTTOM, | ||
DROP_ZONE_CENTER, | ||
DROP_ZONE_LEFT, | ||
DROP_ZONE_RIGHT, | ||
DROP_ZONE_TOP, | ||
} from './PageEditorProvider'; | ||
|
||
const dropAreaHighlightClasses = { | ||
highlightedTop: 'DropArea_HighlightedTop', | ||
highlightedRight: 'DropArea_HighlightedRight', | ||
highlightedBottom: 'DropArea_HighlightedBottom', | ||
highlightedLeft: 'DropArea_HighlightedLeft', | ||
highlightedCenter: 'DropArea_HighlightedCenter', | ||
}; | ||
|
||
function getHighlightedZoneOverlayClass( | ||
highlightedZone: DropZone, | ||
): typeof dropAreaHighlightClasses[keyof typeof dropAreaHighlightClasses] | null { | ||
switch (highlightedZone) { | ||
case DROP_ZONE_TOP: | ||
return dropAreaHighlightClasses.highlightedTop; | ||
case DROP_ZONE_RIGHT: | ||
return dropAreaHighlightClasses.highlightedRight; | ||
case DROP_ZONE_BOTTOM: | ||
return dropAreaHighlightClasses.highlightedBottom; | ||
case DROP_ZONE_LEFT: | ||
return dropAreaHighlightClasses.highlightedLeft; | ||
case DROP_ZONE_CENTER: | ||
return dropAreaHighlightClasses.highlightedCenter; | ||
default: | ||
return null; | ||
} | ||
} | ||
|
||
const StyledNodeDropArea = styled('div', { | ||
shouldForwardProp: (prop) => prop !== 'highlightRelativeRect', | ||
})<{ | ||
highlightRelativeRect?: Partial<Rectangle>; | ||
}>(({ highlightRelativeRect = {} }) => { | ||
const { | ||
x: highlightRelativeX = 0, | ||
y: highlightRelativeY = 0, | ||
width: highlightWidth = '100%', | ||
height: highlightHeight = '100%', | ||
} = highlightRelativeRect; | ||
|
||
return { | ||
pointerEvents: 'none', | ||
position: 'absolute', | ||
[`&.${dropAreaHighlightClasses.highlightedTop}`]: { | ||
'&:after': { | ||
backgroundColor: '#44EB2D', | ||
content: "''", | ||
position: 'absolute', | ||
height: 4, | ||
width: highlightWidth, | ||
top: -2, | ||
left: highlightRelativeX, | ||
}, | ||
}, | ||
[`&.${dropAreaHighlightClasses.highlightedRight}`]: { | ||
'&:after': { | ||
backgroundColor: '#44EB2D', | ||
content: "''", | ||
position: 'absolute', | ||
height: highlightHeight, | ||
width: 4, | ||
top: highlightRelativeY, | ||
right: -2, | ||
}, | ||
}, | ||
[`&.${dropAreaHighlightClasses.highlightedBottom}`]: { | ||
'&:after': { | ||
backgroundColor: '#44EB2D', | ||
content: "''", | ||
position: 'absolute', | ||
height: 4, | ||
width: highlightWidth, | ||
bottom: -2, | ||
left: highlightRelativeX, | ||
}, | ||
}, | ||
[`&.${dropAreaHighlightClasses.highlightedLeft}`]: { | ||
'&:after': { | ||
backgroundColor: '#44EB2D', | ||
content: "''", | ||
position: 'absolute', | ||
height: highlightHeight, | ||
width: 4, | ||
left: -2, | ||
top: highlightRelativeY, | ||
}, | ||
}, | ||
[`&.${dropAreaHighlightClasses.highlightedCenter}`]: { | ||
border: '4px solid #44EB2D', | ||
}, | ||
}; | ||
}); | ||
|
||
const EmptySlot = styled('div')({ | ||
alignItems: 'center', | ||
border: '1px dashed green', | ||
color: 'green', | ||
display: 'flex', | ||
fontSize: 20, | ||
justifyContent: 'center', | ||
position: 'absolute', | ||
opacity: 0.75, | ||
}); | ||
|
||
interface NodeDropAreaProps { | ||
node: appDom.ElementNode | appDom.PageNode; | ||
parentInfo: NodeInfo | null; | ||
layoutRect: Rectangle; | ||
dropAreaRect: Rectangle; | ||
slotRect?: Rectangle; | ||
highlightedZone?: DropZone | null; | ||
isEmptySlot: boolean; | ||
isPageChild: boolean; | ||
} | ||
|
||
export default function NodeDropArea({ | ||
node, | ||
highlightedZone, | ||
parentInfo, | ||
layoutRect, | ||
slotRect, | ||
dropAreaRect, | ||
isEmptySlot, | ||
isPageChild, | ||
}: NodeDropAreaProps) { | ||
const highlightedZoneOverlayClass = | ||
highlightedZone && getHighlightedZoneOverlayClass(highlightedZone); | ||
|
||
const nodeParentProp = node.parentProp; | ||
|
||
const parentSlots = parentInfo?.slots; | ||
const parentSlot = parentSlots && nodeParentProp && parentSlots[nodeParentProp]; | ||
|
||
const parentRect = parentInfo?.rect; | ||
|
||
const isHorizontalContainerChild = parentSlot | ||
? isHorizontalFlow(parentSlot.flowDirection) | ||
: false; | ||
const isVerticalContainerChild = parentSlot ? isVerticalFlow(parentSlot.flowDirection) : false; | ||
|
||
const highlightHeight = | ||
isHorizontalContainerChild && parentRect ? parentRect.height : layoutRect.height; | ||
const highlightWidth = | ||
!isPageChild && isVerticalContainerChild && parentRect ? parentRect.width : layoutRect.width; | ||
|
||
const highlightRelativeX = | ||
(!isPageChild && isVerticalContainerChild && parentRect ? parentRect.x : layoutRect.x) - | ||
dropAreaRect.x; | ||
const highlightRelativeY = | ||
(isHorizontalContainerChild && parentRect ? parentRect.y : layoutRect.y) - dropAreaRect.y; | ||
|
||
const isHighlightingCenter = highlightedZone === DROP_ZONE_CENTER; | ||
|
||
const highlightRect = isHighlightingCenter && isEmptySlot && slotRect ? slotRect : dropAreaRect; | ||
|
||
return ( | ||
<React.Fragment> | ||
<StyledNodeDropArea | ||
style={absolutePositionCss(highlightRect)} | ||
className={clsx( | ||
highlightedZoneOverlayClass | ||
? { | ||
[highlightedZoneOverlayClass]: !isHighlightingCenter || isEmptySlot, | ||
} | ||
: {}, | ||
)} | ||
highlightRelativeRect={{ | ||
x: highlightRelativeX, | ||
y: highlightRelativeY, | ||
width: highlightWidth, | ||
height: highlightHeight, | ||
}} | ||
/> | ||
{isEmptySlot && slotRect ? ( | ||
<EmptySlot style={absolutePositionCss(slotRect)}>+</EmptySlot> | ||
) : null} | ||
</React.Fragment> | ||
); | ||
} |
Oops, something went wrong.