Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grid: Unset the rowStart and columnStart attributes when a block is removed from a manual layout #64186

Merged
merged 13 commits into from
Aug 6, 2024
Merged
Prev Previous commit
Next Next commit
Remove helper function in favour of setImmutably
  • Loading branch information
talldan committed Aug 5, 2024
commit 5463f2d7eecce393da23a01ac6aaaa969c2aa41e
76 changes: 22 additions & 54 deletions packages/block-editor/src/components/grid/use-grid-layout-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,7 @@ import { usePrevious } from '@wordpress/compose';
*/
import { store as blockEditorStore } from '../../store';
import { GridRect } from './utils';

/**
* Returns an attributes 'update' object for unsetting columnStart and rowStart
* `styles.layout` attributes.
*
* Will return `undefined` when the attributes are already not set indicating
* there's nothing to do.
*
* @param {Object} attributes The current block attributes.
* @param {boolean} unsetSpanAttributes Whether rowSpan and columnSpans should
* also be unset.
*
* @return {undefined|Object} The attribute updates or `undefined` when the
* attribute are already not set.
*/
function unsetGridPositionAttributes(
attributes,
unsetSpanAttributes = false
) {
let layout;
let hasUpdate;

if ( unsetSpanAttributes ) {
const { columnStart, rowStart, columnSpan, rowSpan, ...restLayout } =
attributes.style?.layout ?? {};
layout = restLayout;
hasUpdate = !! columnStart || rowStart || columnSpan || rowSpan;
} else {
const { columnStart, rowStart, ...restLayout } =
attributes.style?.layout ?? {};
layout = restLayout;
hasUpdate = !! columnStart || rowStart;
}

if ( hasUpdate ) {
if ( ! Object.keys( layout ).length ) {
return { style: { ...attributes.style, layout: undefined } };
}
return {
style: { ...attributes.style, layout },
};
}
}
import { setImmutably } from '../../utils/object';

export function useGridLayoutSync( { clientId: gridClientId } ) {
const { gridLayout, blockOrder, selectedBlockLayout } = useSelect(
Expand Down Expand Up @@ -173,14 +131,18 @@ export function useGridLayoutSync( { clientId: gridClientId } ) {
previousBlockOrder?.forEach( ( clientId ) => {
talldan marked this conversation as resolved.
Show resolved Hide resolved
if ( ! blockOrder.includes( clientId ) ) {
const attributes = getBlockAttributes( clientId );
const unsetSpanAttributes = true;
const update = unsetGridPositionAttributes(
attributes,
unsetSpanAttributes
const pathsToUnset = [
[ 'styles', 'layout', 'columnStart' ],
[ 'styles', 'layout', 'rowStart' ],
[ 'styles', 'layout', 'columnSpan' ],
[ 'styles', 'layout', 'rowSpan' ],
];
const updatedAttributes = pathsToUnset.reduce(
( attrs, path ) =>
setImmutably( attrs, path, undefined ),
attributes
);
if ( update ) {
updates[ clientId ] = update;
}
updates[ clientId ] = updatedAttributes;
}
} );
} else {
Expand All @@ -189,10 +151,16 @@ export function useGridLayoutSync( { clientId: gridClientId } ) {
if ( previousIsManualPlacement === true ) {
for ( const clientId of blockOrder ) {
const attributes = getBlockAttributes( clientId );
const update = unsetGridPositionAttributes( attributes );
if ( update ) {
updates[ clientId ] = update;
}
const pathsToUnset = [
[ 'styles', 'layout', 'columnStart' ],
[ 'styles', 'layout', 'rowStart' ],
];
const updatedAttributes = pathsToUnset.reduce(
( attrs, path ) =>
setImmutably( attrs, path, undefined ),
attributes
);
updates[ clientId ] = updatedAttributes;
}
}

Expand Down