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
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { usePrevious } from '@wordpress/compose';
*/
import { store as blockEditorStore } from '../../store';
import { GridRect } from './utils';
import { setImmutably } from '../../utils/object';

export function useGridLayoutSync( { clientId: gridClientId } ) {
const { gridLayout, blockOrder, selectedBlockLayout } = useSelect(
Expand All @@ -26,7 +27,8 @@ export function useGridLayoutSync( { clientId: gridClientId } ) {
[ gridClientId ]
);

const { getBlockAttributes } = useSelect( blockEditorStore );
const { getBlockAttributes, getBlockRootClientId } =
useSelect( blockEditorStore );
const { updateBlockAttributes, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );

Expand All @@ -40,6 +42,7 @@ export function useGridLayoutSync( { clientId: gridClientId } ) {
const previousIsManualPlacement = usePrevious(
gridLayout.isManualPlacement
);
const previousBlockOrder = usePrevious( blockOrder );

useEffect( () => {
const updates = {};
Expand Down Expand Up @@ -123,6 +126,46 @@ export function useGridLayoutSync( { clientId: gridClientId } ) {
},
};
}

// Unset grid layout attributes for blocks removed from the grid.
previousBlockOrder?.forEach( ( clientId ) => {
talldan marked this conversation as resolved.
Show resolved Hide resolved
if ( ! blockOrder.includes( clientId ) ) {
const rootClientId = getBlockRootClientId( clientId );

// Block was removed from the editor, so nothing to do.
if ( rootClientId === null ) {
return;
}

// Check if the block is being moved to another grid.
// If so, do nothing and let the new grid parent handle
// the attributes.
const rootAttributes = getBlockAttributes( rootClientId );
if ( rootAttributes?.layout?.type === 'grid' ) {
return;
}

const attributes = getBlockAttributes( clientId );
const {
columnStart,
rowStart,
columnSpan,
rowSpan,
...layout
} = attributes.style?.layout ?? {};

if ( columnStart || rowStart || columnSpan || rowSpan ) {
const hasEmptyLayoutAttribute =
Object.keys( layout ).length === 0;

updates[ clientId ] = setImmutably(
attributes,
[ 'style', 'layout' ],
hasEmptyLayoutAttribute ? undefined : layout
);
}
}
} );
} else {
// Remove all of the columnStart and rowStart values
// when switching from manual to auto mode,
Expand All @@ -133,12 +176,14 @@ export function useGridLayoutSync( { clientId: gridClientId } ) {
attributes.style?.layout ?? {};
// Only update attributes if columnStart or rowStart are set.
if ( columnStart || rowStart ) {
updates[ clientId ] = {
style: {
...attributes.style,
layout,
},
};
const hasEmptyLayoutAttribute =
Object.keys( layout ).length === 0;

updates[ clientId ] = setImmutably(
attributes,
[ 'style', 'layout' ],
hasEmptyLayoutAttribute ? undefined : layout
);
}
}
}
Expand Down Expand Up @@ -166,12 +211,14 @@ export function useGridLayoutSync( { clientId: gridClientId } ) {
// Actual deps to sync:
gridClientId,
gridLayout,
previousBlockOrder,
blockOrder,
previouslySelectedBlockRect,
previousIsManualPlacement,
// These won't change, but the linter thinks they might:
__unstableMarkNextChangeAsNotPersistent,
getBlockAttributes,
getBlockRootClientId,
updateBlockAttributes,
] );
}
Expand Down
Loading