Skip to content

Commit

Permalink
Grid Visualizer: Improve observation logic (WordPress#68230)
Browse files Browse the repository at this point in the history
* Grid Visualizer: Improve observation logic

* Add `class` to `attributeFilter`

Co-authored-by: t-hamano <[email protected]>
Co-authored-by: talldan <[email protected]>
  • Loading branch information
3 people authored Dec 23, 2024
1 parent b73c95d commit 813c24b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
11 changes: 11 additions & 0 deletions packages/block-editor/src/components/grid/grid-visualizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ const GridVisualizerGrid = forwardRef(
observer.observe( element );
observers.push( observer );
}

const mutationObserver = new window.MutationObserver( () => {
setGridInfo( getGridInfo( gridElement ) );
} );
mutationObserver.observe( gridElement, {
attributeFilter: [ 'style', 'class' ],
childList: true,
subtree: true,
} );
observers.push( mutationObserver );

return () => {
for ( const observer of observers ) {
observer.disconnect();
Expand Down
20 changes: 19 additions & 1 deletion packages/block-editor/src/components/grid/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ export function getGridInfo( gridElement ) {
gridElement,
'grid-template-rows'
);
const borderTopWidth = getComputedCSS( gridElement, 'border-top-width' );
const borderRightWidth = getComputedCSS(
gridElement,
'border-right-width'
);
const borderBottomWidth = getComputedCSS(
gridElement,
'border-bottom-width'
);
const borderLeftWidth = getComputedCSS( gridElement, 'border-left-width' );
const paddingTop = getComputedCSS( gridElement, 'padding-top' );
const paddingRight = getComputedCSS( gridElement, 'padding-right' );
const paddingBottom = getComputedCSS( gridElement, 'padding-bottom' );
const paddingLeft = getComputedCSS( gridElement, 'padding-left' );

const numColumns = gridTemplateColumns.split( ' ' ).length;
const numRows = gridTemplateRows.split( ' ' ).length;
const numItems = numColumns * numRows;
Expand All @@ -172,7 +187,10 @@ export function getGridInfo( gridElement ) {
gridTemplateColumns,
gridTemplateRows,
gap: getComputedCSS( gridElement, 'gap' ),
padding: getComputedCSS( gridElement, 'padding' ),
paddingTop: `calc(${ paddingTop } + ${ borderTopWidth })`,
paddingRight: `calc(${ paddingRight } + ${ borderRightWidth })`,
paddingBottom: `calc(${ paddingBottom } + ${ borderBottomWidth })`,
paddingLeft: `calc(${ paddingLeft } + ${ borderLeftWidth })`,
},
};
}

0 comments on commit 813c24b

Please sign in to comment.