-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
base: trunk
Are you sure you want to change the base?
Spacer
: Fix changes being marked as persistent to undo
#68869
Conversation
…hangeAsNotPersistent`
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Hi, @stokesman, can you please review the PR when you get a moment? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for tackling this one! The changes make sense. I’ve tested and can no longer reproduce the issue.
I have one specific suggestion I’d like to see done and I’ll leave a separate comment fort that. Alternatively, I’ll include here a larger suggested diff that incorporates it but also changes the code style to be less verbose. Please apply it only if you’d agree it’s a clear improvement. I’m fine with keeping the changes more minimal.
Reduce some verbosity and add a general comment about making changes non-persistent
diff --git a/packages/block-library/src/spacer/edit.js b/packages/block-library/src/spacer/edit.js
index cd21870f0c..c11201861a 100644
--- a/packages/block-library/src/spacer/edit.js
+++ b/packages/block-library/src/spacer/edit.js
@@ -259,13 +259,18 @@ const SpacerEdit = ( {
};
useEffect( () => {
+ // To avoid interfering with undo/redo operations any changes in this
+ // effect must not make history.
+ 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.
@@ -273,7 +278,7 @@ const SpacerEdit = ( {
getCustomValueFromPreset( width, spacingSizes ) ||
getCustomValueFromPreset( height, spacingSizes ) ||
'100px';
- setAttributes( {
+ setAttributesCovertly( {
width: '0px',
style: {
...blockStyle,
@@ -289,7 +294,7 @@ const SpacerEdit = ( {
getCustomValueFromPreset( height, spacingSizes ) ||
getCustomValueFromPreset( width, spacingSizes ) ||
'100px';
- setAttributes( {
+ setAttributesCovertly( {
height: '0px',
style: {
...blockStyle,
@@ -305,32 +310,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: {
// This ensures that the next change is not marked as persistent | ||
// when the user changes the container layout. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given there are multiple places in the effect this comment is applicable I think it’d be better to remove this and instead put a more generalized version at the start of the effect. Something like:
// To avoid interfering with undo/redo operations any changes in this
// effect must not make history and should be preceded by
// `__unstableMarkNextChangeAsNotPersistent()`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the review, @stokesman, I've incorporated the suggested changes in the latest commit.
…ring undo/redo operations
While testing this PR, I discovered a bug in the Spacer block. When a flex container's orientation is vertical and the Spacer block's Screen.Recording.2025-01-27.at.10.38.58.AM.movIt's best to tackle this issue separately. |
What, Why and How?
Fixes: #68863
Currently, inserting or moving a
Spacer block
into a Row or Stack block disrupts theundo
functionality. Similarly, moving aSpacer block
out of a Row or Stack block also breaks theundo
behavior.Also, Converting a
Row block
containing aSpacer block
into aGroup block
caused undo to fail.Testing Instructions
Step 1: Navigate to the
post edit
page.Step 2: Verify the undo functionality with
Spacer
works for the following cases:Flex Containers
. ( i.e.Row
&Stack
without settingselfStretch
tofit
orfill
).Flex Containers
withselfStretch
to be eitherfit
orfill
.Non-Flex Container
withselfStretch
to be eitherfit
orfill
.Spacer
intoRow/Stack
.Spacer
out ofRow/Stack
.Spacer
inserted into aRow/Stack
, convert it toGroup
and verify undo.Screencast
Screen.Recording.2025-01-24.at.3.32.05.PM.mov