diff --git a/packages/block-editor/src/components/inner-blocks/index.js b/packages/block-editor/src/components/inner-blocks/index.js index 0ebe7148007566..cd58d71cde07e0 100644 --- a/packages/block-editor/src/components/inner-blocks/index.js +++ b/packages/block-editor/src/components/inner-blocks/index.js @@ -41,6 +41,7 @@ class InnerBlocks extends Component { templateLock, __experimentalBlocks, replaceInnerBlocks, + __unstableMarkNextChangeAsNotPersistent, } = this.props; const { innerBlocks } = block; // Only synchronize innerBlocks with template if innerBlocks are empty or a locking all exists directly on the block. @@ -56,6 +57,7 @@ class InnerBlocks extends Component { // Set controlled blocks value from parent, if any. if ( __experimentalBlocks ) { + __unstableMarkNextChangeAsNotPersistent(); replaceInnerBlocks( __experimentalBlocks ); } } @@ -184,6 +186,7 @@ InnerBlocks = compose( [ withDispatch( ( dispatch, ownProps ) => { const { replaceInnerBlocks, + __unstableMarkNextChangeAsNotPersistent, updateBlockListSettings, } = dispatch( 'core/block-editor' ); const { block, clientId, templateInsertUpdatesSelection = true } = ownProps; @@ -198,6 +201,7 @@ InnerBlocks = compose( [ blocks.length !== 0 ); }, + __unstableMarkNextChangeAsNotPersistent, updateNestedSettings( settings ) { dispatch( updateBlockListSettings( clientId, settings ) ); }, diff --git a/packages/block-editor/src/store/actions.js b/packages/block-editor/src/store/actions.js index 4ab68705d2845e..b6a6552e1f019b 100644 --- a/packages/block-editor/src/store/actions.js +++ b/packages/block-editor/src/store/actions.js @@ -811,6 +811,15 @@ export function __unstableMarkLastChangeAsPersistent() { return { type: 'MARK_LAST_CHANGE_AS_PERSISTENT' }; } +/** + * Returns an action object used in signalling that the next block change should be marked explicitly as not persistent. + * + * @return {Object} Action object. + */ +export function __unstableMarkNextChangeAsNotPersistent() { + return { type: 'MARK_NEXT_CHANGE_AS_NOT_PERSISTENT' }; +} + /** * Returns an action object used in signalling that the last block change is * an automatic change, meaning it was not performed by the user, and can be diff --git a/packages/block-editor/src/store/reducer.js b/packages/block-editor/src/store/reducer.js index 9b770d6c4d0db0..d7b3799a61a0d4 100644 --- a/packages/block-editor/src/store/reducer.js +++ b/packages/block-editor/src/store/reducer.js @@ -350,15 +350,18 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => { */ function withPersistentBlockChange( reducer ) { let lastAction; + let markNextChangeAsNotPersistent = false; return ( state, action ) => { let nextState = reducer( state, action ); - const isExplicitPersistentChange = action.type === 'MARK_LAST_CHANGE_AS_PERSISTENT'; + const isExplicitPersistentChange = action.type === 'MARK_LAST_CHANGE_AS_PERSISTENT' || markNextChangeAsNotPersistent; // Defer to previous state value (or default) unless changing or // explicitly marking as persistent. if ( state === nextState && ! isExplicitPersistentChange ) { + markNextChangeAsNotPersistent = action.type === 'MARK_NEXT_CHANGE_AS_NOT_PERSISTENT'; + const nextIsPersistentChange = get( state, [ 'isPersistentChange' ], true ); if ( state.isPersistentChange === nextIsPersistentChange ) { return state; @@ -372,16 +375,16 @@ function withPersistentBlockChange( reducer ) { nextState = { ...nextState, - isPersistentChange: ( - isExplicitPersistentChange || - ! isUpdatingSameBlockAttribute( action, lastAction ) - ), + isPersistentChange: isExplicitPersistentChange ? + ! markNextChangeAsNotPersistent : + ! isUpdatingSameBlockAttribute( action, lastAction ), }; // In comparing against the previous action, consider only those which // would have qualified as one which would have been ignored or not // have resulted in a changed state. lastAction = action; + markNextChangeAsNotPersistent = action.type === 'MARK_NEXT_CHANGE_AS_NOT_PERSISTENT'; return nextState; };