-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add grid visualization * Show grid visualization when child is selected * Allow dragging to set column and row span * Don't need this ref * Just disable left/top resizing for now * Clean up CSS * Add shift=false to popovers * Accommodate variably sized columns/rows by using grid-template to calculate span * BlockPopover: Use ResizeObserver to match size of covered block * Fix error due to undefined selectedElement * Update GridVisualizer when grid or its children resize * Add experimental flag * Fix formatting * Go away spaces * BlockPopover: Remove __unstableRefreshSize prop and improve how __unstableCoverTarget works * BlockPopover: Remove __unstableCoverTarget in favour of BlockPopoverCover * Use BlockPopoverCover Co-authored-by: noisysocks <noisysocks@git.wordpress.org> Co-authored-by: hanneslsm <hanneslsm@git.wordpress.org> Co-authored-by: tellthemachines <isabel_brison@git.wordpress.org> Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
- Loading branch information
1 parent
671fbf9
commit ba535da
Showing
13 changed files
with
299 additions
and
2 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
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
100 changes: 100 additions & 0 deletions
100
packages/block-editor/src/components/grid-visualizer/grid-item-resizer.js
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,100 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { ResizableBox } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { __unstableUseBlockElement as useBlockElement } from '../block-list/use-block-props/use-block-refs'; | ||
import BlockPopoverCover from '../block-popover/cover'; | ||
import { getComputedCSS } from './utils'; | ||
|
||
export function GridItemResizer( { clientId, onChange } ) { | ||
const blockElement = useBlockElement( clientId ); | ||
if ( ! blockElement ) { | ||
return null; | ||
} | ||
return ( | ||
<BlockPopoverCover | ||
className="block-editor-grid-item-resizer" | ||
clientId={ clientId } | ||
__unstablePopoverSlot="block-toolbar" | ||
> | ||
<ResizableBox | ||
className="block-editor-grid-item-resizer__box" | ||
size={ { | ||
width: '100%', | ||
height: '100%', | ||
} } | ||
enable={ { | ||
bottom: true, | ||
bottomLeft: false, | ||
bottomRight: false, | ||
left: false, | ||
right: true, | ||
top: false, | ||
topLeft: false, | ||
topRight: false, | ||
} } | ||
onResizeStop={ ( event, direction, boxElement ) => { | ||
const gridElement = blockElement.parentElement; | ||
const columnGap = parseFloat( | ||
getComputedCSS( gridElement, 'column-gap' ) | ||
); | ||
const rowGap = parseFloat( | ||
getComputedCSS( gridElement, 'row-gap' ) | ||
); | ||
const gridColumnLines = getGridLines( | ||
getComputedCSS( gridElement, 'grid-template-columns' ), | ||
columnGap | ||
); | ||
const gridRowLines = getGridLines( | ||
getComputedCSS( gridElement, 'grid-template-rows' ), | ||
rowGap | ||
); | ||
const columnStart = getClosestLine( | ||
gridColumnLines, | ||
blockElement.offsetLeft | ||
); | ||
const rowStart = getClosestLine( | ||
gridRowLines, | ||
blockElement.offsetTop | ||
); | ||
const columnEnd = getClosestLine( | ||
gridColumnLines, | ||
blockElement.offsetLeft + boxElement.offsetWidth | ||
); | ||
const rowEnd = getClosestLine( | ||
gridRowLines, | ||
blockElement.offsetTop + boxElement.offsetHeight | ||
); | ||
onChange( { | ||
columnSpan: Math.max( columnEnd - columnStart, 1 ), | ||
rowSpan: Math.max( rowEnd - rowStart, 1 ), | ||
} ); | ||
} } | ||
/> | ||
</BlockPopoverCover> | ||
); | ||
} | ||
|
||
function getGridLines( template, gap ) { | ||
const lines = [ 0 ]; | ||
for ( const size of template.split( ' ' ) ) { | ||
const line = parseFloat( size ); | ||
lines.push( lines[ lines.length - 1 ] + line + gap ); | ||
} | ||
return lines; | ||
} | ||
|
||
function getClosestLine( lines, position ) { | ||
return lines.reduce( | ||
( closest, line, index ) => | ||
Math.abs( line - position ) < | ||
Math.abs( lines[ closest ] - position ) | ||
? index | ||
: closest, | ||
0 | ||
); | ||
} |
81 changes: 81 additions & 0 deletions
81
packages/block-editor/src/components/grid-visualizer/grid-visualizer.js
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,81 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState, useEffect } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { __unstableUseBlockElement as useBlockElement } from '../block-list/use-block-props/use-block-refs'; | ||
import BlockPopoverCover from '../block-popover/cover'; | ||
import { getComputedCSS } from './utils'; | ||
|
||
export function GridVisualizer( { clientId } ) { | ||
const blockElement = useBlockElement( clientId ); | ||
if ( ! blockElement ) { | ||
return null; | ||
} | ||
return ( | ||
<BlockPopoverCover | ||
className="block-editor-grid-visualizer" | ||
clientId={ clientId } | ||
__unstablePopoverSlot="block-toolbar" | ||
> | ||
<GridVisualizerGrid blockElement={ blockElement } /> | ||
</BlockPopoverCover> | ||
); | ||
} | ||
|
||
function GridVisualizerGrid( { blockElement } ) { | ||
const [ gridInfo, setGridInfo ] = useState( () => | ||
getGridInfo( blockElement ) | ||
); | ||
useEffect( () => { | ||
const observers = []; | ||
for ( const element of [ blockElement, ...blockElement.children ] ) { | ||
const observer = new window.ResizeObserver( () => { | ||
setGridInfo( getGridInfo( blockElement ) ); | ||
} ); | ||
observer.observe( element ); | ||
observers.push( observer ); | ||
} | ||
return () => { | ||
for ( const observer of observers ) { | ||
observer.disconnect(); | ||
} | ||
}; | ||
}, [ blockElement ] ); | ||
return ( | ||
<div | ||
className="block-editor-grid-visualizer__grid" | ||
style={ gridInfo.style } | ||
> | ||
{ Array.from( { length: gridInfo.numItems }, ( _, i ) => ( | ||
<div key={ i } className="block-editor-grid-visualizer__item" /> | ||
) ) } | ||
</div> | ||
); | ||
} | ||
|
||
function getGridInfo( blockElement ) { | ||
const gridTemplateColumns = getComputedCSS( | ||
blockElement, | ||
'grid-template-columns' | ||
); | ||
const gridTemplateRows = getComputedCSS( | ||
blockElement, | ||
'grid-template-rows' | ||
); | ||
const numColumns = gridTemplateColumns.split( ' ' ).length; | ||
const numRows = gridTemplateRows.split( ' ' ).length; | ||
const numItems = numColumns * numRows; | ||
return { | ||
numItems, | ||
style: { | ||
gridTemplateColumns, | ||
gridTemplateRows, | ||
gap: getComputedCSS( blockElement, 'gap' ), | ||
padding: getComputedCSS( blockElement, 'padding' ), | ||
}, | ||
}; | ||
} |
2 changes: 2 additions & 0 deletions
2
packages/block-editor/src/components/grid-visualizer/index.js
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,2 @@ | ||
export { GridVisualizer } from './grid-visualizer'; | ||
export { GridItemResizer } from './grid-item-resizer'; |
33 changes: 33 additions & 0 deletions
33
packages/block-editor/src/components/grid-visualizer/style.scss
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,33 @@ | ||
// TODO: Specificity hacks to get rid of all these darn !importants. | ||
|
||
.block-editor-grid-visualizer { | ||
z-index: z-index(".block-editor-grid-visualizer") !important; | ||
} | ||
|
||
.block-editor-grid-visualizer .components-popover__content * { | ||
pointer-events: none !important; | ||
} | ||
|
||
.block-editor-grid-visualizer__grid { | ||
display: grid; | ||
} | ||
|
||
.block-editor-grid-visualizer__item { | ||
border: $border-width dashed $gray-300; | ||
} | ||
|
||
.block-editor-grid-item-resizer { | ||
z-index: z-index(".block-editor-grid-visualizer") !important; | ||
} | ||
|
||
.block-editor-grid-item-resizer .components-popover__content * { | ||
pointer-events: none !important; | ||
} | ||
|
||
.block-editor-grid-item-resizer__box { | ||
border: $border-width solid var(--wp-admin-theme-color); | ||
|
||
.components-resizable-box__handle { | ||
pointer-events: all !important; | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/block-editor/src/components/grid-visualizer/utils.js
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,5 @@ | ||
export function getComputedCSS( element, property ) { | ||
return element.ownerDocument.defaultView | ||
.getComputedStyle( element ) | ||
.getPropertyValue( property ); | ||
} |
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.