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

Spacer: Fix changes being marked as persistent to undo #68869

Merged
merged 6 commits into from
Jan 28, 2025
46 changes: 24 additions & 22 deletions packages/block-library/src/spacer/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { ResizableBox } from '@wordpress/components';
import { useState, useEffect } from '@wordpress/element';
import { View } from '@wordpress/primitives';
import { useSelect } from '@wordpress/data';
import { useSelect, useDispatch } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -124,6 +124,9 @@ const SpacerEdit = ( {
const onResizeStart = () => toggleSelection( false );
const onResizeStop = () => toggleSelection( true );

const { __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );

const handleOnVerticalResizeStop = ( newHeight ) => {
onResizeStop();

Expand Down Expand Up @@ -256,6 +259,14 @@ 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' &&
Expand All @@ -269,7 +280,7 @@ const SpacerEdit = ( {
getCustomValueFromPreset( width, spacingSizes ) ||
getCustomValueFromPreset( height, spacingSizes ) ||
'100px';
setAttributes( {
setAttributesCovertly( {
width: '0px',
style: {
...blockStyle,
Expand All @@ -285,7 +296,7 @@ const SpacerEdit = ( {
getCustomValueFromPreset( height, spacingSizes ) ||
getCustomValueFromPreset( width, spacingSizes ) ||
'100px';
setAttributes( {
setAttributesCovertly( {
height: '0px',
style: {
...blockStyle,
Expand All @@ -301,26 +312,16 @@ const SpacerEdit = ( {
isFlexLayout &&
( selfStretch === 'fill' || selfStretch === 'fit' )
) {
if ( inheritedOrientation === 'horizontal' ) {
setAttributes( {
width: undefined,
} );
} else {
setAttributes( {
height: undefined,
} );
}
setAttributesCovertly(
inheritedOrientation === 'horizontal'
? { width: undefined }
: { height: undefined }
);
} else if ( ! isFlexLayout && ( selfStretch || flexSize ) ) {
if ( inheritedOrientation === 'horizontal' ) {
setAttributes( {
width: flexSize,
} );
} else {
setAttributes( {
height: flexSize,
} );
}
setAttributes( {
setAttributesCovertly( {
...( inheritedOrientation === 'horizontal'
? { width: flexSize }
: { height: flexSize } ),
style: {
...blockStyle,
layout: {
Expand All @@ -342,6 +343,7 @@ const SpacerEdit = ( {
setAttributes,
spacingSizes,
width,
__unstableMarkNextChangeAsNotPersistent,
] );

return (
Expand Down
Loading