forked from WordPress/gutenberg
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
85 changed files
with
1,625 additions
and
828 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
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
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' ), | ||
}, | ||
}; | ||
} |
Oops, something went wrong.