Skip to content

Commit

Permalink
Spacer Block: Fix broken editing history due to effect (#68869)
Browse files Browse the repository at this point in the history
* `Spacer`: Fix changes being marked as persistent to `undo`

* Spacer: Remove unnecessary line breaks in edit.js

* `Spacer`: Simplify the usage of `__unstableMarkNextChangeAsNotPersistent`

* Refactor: Add comments to clarify the purpose of `__unstableMarkNextChangeAsNotPersistent`

* `Spacer`: Ensure changes are marked as not persistent in multiple conditions

* Spacer: Refactor `setAttributes` calls to avoid persistent changes during undo/redo operations

Co-authored-by: yogeshbhutkar <yogeshbhutkar@git.wordpress.org>
Co-authored-by: stokesman <presstoke@git.wordpress.org>
  • Loading branch information
3 people authored Jan 28, 2025
1 parent 183f671 commit bf32d27
Showing 1 changed file with 24 additions and 22 deletions.
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

1 comment on commit bf32d27

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in bf32d27.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/13016907897
📝 Reported issues:

Please sign in to comment.