Skip to content

Commit

Permalink
Spacer: Refactor setAttributes calls to avoid persistent changes du…
Browse files Browse the repository at this point in the history
…ring undo/redo operations
  • Loading branch information
yogeshbhutkar committed Jan 27, 2025
1 parent 152734a commit c8eb021
Showing 1 changed file with 19 additions and 28 deletions.
47 changes: 19 additions & 28 deletions packages/block-library/src/spacer/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,21 +259,28 @@ const SpacerEdit = ( {
};

useEffect( () => {
// To avoid interfering with undo/redo operations any changes in this
// effect must not make history and should be preceded by
// `__unstableMarkNextChangeAsNotPersistent()`.
const setAttributesCovertly = ( nextAttributes ) => {
__unstableMarkNextChangeAsNotPersistent();
setAttributes( nextAttributes );
};

if (
isFlexLayout &&
selfStretch !== 'fill' &&
selfStretch !== 'fit' &&
flexSize === undefined
) {
__unstableMarkNextChangeAsNotPersistent();
if ( inheritedOrientation === 'horizontal' ) {
// If spacer is moving from a vertical container to a horizontal container,
// it might not have width but have height instead.
const newSize =
getCustomValueFromPreset( width, spacingSizes ) ||
getCustomValueFromPreset( height, spacingSizes ) ||
'100px';
setAttributes( {
setAttributesCovertly( {
width: '0px',
style: {
...blockStyle,
Expand All @@ -289,7 +296,7 @@ const SpacerEdit = ( {
getCustomValueFromPreset( height, spacingSizes ) ||
getCustomValueFromPreset( width, spacingSizes ) ||
'100px';
setAttributes( {
setAttributesCovertly( {
height: '0px',
style: {
...blockStyle,
Expand All @@ -305,32 +312,16 @@ const SpacerEdit = ( {
isFlexLayout &&
( selfStretch === 'fill' || selfStretch === 'fit' )
) {
__unstableMarkNextChangeAsNotPersistent();
if ( inheritedOrientation === 'horizontal' ) {
setAttributes( {
width: undefined,
} );
} else {
setAttributes( {
height: undefined,
} );
}
setAttributesCovertly(
inheritedOrientation === 'horizontal'
? { width: undefined }
: { height: undefined }
);
} else if ( ! isFlexLayout && ( selfStretch || flexSize ) ) {
__unstableMarkNextChangeAsNotPersistent();
if ( inheritedOrientation === 'horizontal' ) {
setAttributes( {
width: flexSize,
} );
} else {
setAttributes( {
height: flexSize,
} );
}

// This ensures that the next change is not marked as persistent
// when the user changes the container layout.
__unstableMarkNextChangeAsNotPersistent();
setAttributes( {
setAttributesCovertly( {
...( inheritedOrientation === 'horizontal'
? { width: flexSize }
: { height: flexSize } ),
style: {
...blockStyle,
layout: {
Expand Down

0 comments on commit c8eb021

Please sign in to comment.