From d6b984595b34725e6636d1152913b02117bf54a2 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Fri, 1 May 2026 12:42:08 +0400 Subject: [PATCH 1/3] Block Editor: Remove unused reducer action types Removes dead code in the blocks reducer: `SYNC_DERIVED_BLOCK_ATTRIBUTES` (two fall-through cases into `UPDATE_BLOCK_ATTRIBUTES`) and `SET_EXPLICIT_PERSISTENT` (with its `explicitPersistent` branch in `withPersistentBlockChange`). Neither action is dispatched anywhere in the codebase. --- packages/block-editor/src/store/reducer.js | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/packages/block-editor/src/store/reducer.js b/packages/block-editor/src/store/reducer.js index 8754c4f4890dcc..4011eb8dbb7d2f 100644 --- a/packages/block-editor/src/store/reducer.js +++ b/packages/block-editor/src/store/reducer.js @@ -292,7 +292,6 @@ const withBlockTree = false ); break; - case 'SYNC_DERIVED_BLOCK_ATTRIBUTES': case 'UPDATE_BLOCK_ATTRIBUTES': { newState.tree = new Map( newState.tree ); action.clientIds.forEach( ( clientId ) => { @@ -421,27 +420,11 @@ const withBlockTree = function withPersistentBlockChange( reducer ) { let lastAction; let markNextChangeAsNotPersistent = false; - let explicitPersistent; return ( state, action ) => { let nextState = reducer( state, action ); let nextIsPersistentChange; - if ( action.type === 'SET_EXPLICIT_PERSISTENT' ) { - explicitPersistent = action.isPersistentChange; - nextIsPersistentChange = state.isPersistentChange ?? true; - } - - if ( explicitPersistent !== undefined ) { - nextIsPersistentChange = explicitPersistent; - return nextIsPersistentChange === nextState.isPersistentChange - ? nextState - : { - ...nextState, - isPersistentChange: nextIsPersistentChange, - }; - } - const isExplicitPersistentChange = action.type === 'MARK_LAST_CHANGE_AS_PERSISTENT' || markNextChangeAsNotPersistent; @@ -897,7 +880,6 @@ export const blocks = pipe( return newState; } - case 'SYNC_DERIVED_BLOCK_ATTRIBUTES': case 'UPDATE_BLOCK_ATTRIBUTES': { // Avoid a state change if none of the block IDs are known. if ( action.clientIds.every( ( id ) => ! state.get( id ) ) ) { From 54d68a6a343ed8ba2b13c0c1bab71c8205d88e4c Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Fri, 1 May 2026 12:52:05 +0400 Subject: [PATCH 2/3] Simplify withPersistentBlockChange --- packages/block-editor/src/store/reducer.js | 34 +++++++------------ .../block-editor/src/store/test/reducer.js | 28 +++++++++++++++ 2 files changed, 40 insertions(+), 22 deletions(-) diff --git a/packages/block-editor/src/store/reducer.js b/packages/block-editor/src/store/reducer.js index 4011eb8dbb7d2f..d194ea911a84d8 100644 --- a/packages/block-editor/src/store/reducer.js +++ b/packages/block-editor/src/store/reducer.js @@ -422,45 +422,35 @@ function withPersistentBlockChange( reducer ) { let markNextChangeAsNotPersistent = false; return ( state, action ) => { - let nextState = reducer( state, action ); + const nextState = reducer( state, action ); + + const wasMarkedAsNotPersistent = markNextChangeAsNotPersistent; + markNextChangeAsNotPersistent = + action.type === 'MARK_NEXT_CHANGE_AS_NOT_PERSISTENT'; - let nextIsPersistentChange; const isExplicitPersistentChange = action.type === 'MARK_LAST_CHANGE_AS_PERSISTENT' || - markNextChangeAsNotPersistent; + wasMarkedAsNotPersistent; // 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'; - - nextIsPersistentChange = state?.isPersistentChange ?? true; - if ( state.isPersistentChange === nextIsPersistentChange ) { + if ( state.isPersistentChange !== undefined ) { return state; } - - return { - ...nextState, - isPersistentChange: nextIsPersistentChange, - }; + return { ...nextState, isPersistentChange: true }; } - nextState = { - ...nextState, - isPersistentChange: isExplicitPersistentChange - ? ! markNextChangeAsNotPersistent - : ! isUpdatingSameBlockAttribute( action, lastAction ), - }; + const isPersistentChange = isExplicitPersistentChange + ? ! wasMarkedAsNotPersistent + : ! 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; + return { ...nextState, isPersistentChange }; }; } diff --git a/packages/block-editor/src/store/test/reducer.js b/packages/block-editor/src/store/test/reducer.js index 972c184fbc8fec..32ca5278fb2e7c 100644 --- a/packages/block-editor/src/store/test/reducer.js +++ b/packages/block-editor/src/store/test/reducer.js @@ -2196,6 +2196,34 @@ describe( 'state', () => { expect( state.isPersistentChange ).toBe( true ); } ); + it( 'should flag the next change as not persistent', () => { + let original = deepFreeze( + blocks( undefined, { + type: 'RESET_BLOCKS', + blocks: [ + { + clientId: 'kumquat', + attributes: {}, + innerBlocks: [], + }, + ], + } ) + ); + original = blocks( original, { + type: 'MARK_NEXT_CHANGE_AS_NOT_PERSISTENT', + } ); + + const state = blocks( original, { + type: 'UPDATE_BLOCK_ATTRIBUTES', + clientIds: [ 'kumquat' ], + attributes: { + updated: true, + }, + } ); + + expect( state.isPersistentChange ).toBe( false ); + } ); + it( 'should retain reference for same state, same persistence', () => { const original = deepFreeze( blocks( undefined, { From 7309705c100f87212e60fb9c1b109d8ea2980660 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Wed, 6 May 2026 17:11:34 +0400 Subject: [PATCH 3/3] Update unit test --- packages/block-editor/src/store/test/reducer.js | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/packages/block-editor/src/store/test/reducer.js b/packages/block-editor/src/store/test/reducer.js index 32ca5278fb2e7c..a03c0dce0067c7 100644 --- a/packages/block-editor/src/store/test/reducer.js +++ b/packages/block-editor/src/store/test/reducer.js @@ -2196,7 +2196,7 @@ describe( 'state', () => { expect( state.isPersistentChange ).toBe( true ); } ); - it( 'should flag the next change as not persistent', () => { + it( 'should flag only the next change as not persistent', () => { let original = deepFreeze( blocks( undefined, { type: 'RESET_BLOCKS', @@ -2213,7 +2213,7 @@ describe( 'state', () => { type: 'MARK_NEXT_CHANGE_AS_NOT_PERSISTENT', } ); - const state = blocks( original, { + const nextState = blocks( original, { type: 'UPDATE_BLOCK_ATTRIBUTES', clientIds: [ 'kumquat' ], attributes: { @@ -2221,7 +2221,18 @@ describe( 'state', () => { }, } ); - expect( state.isPersistentChange ).toBe( false ); + expect( nextState.isPersistentChange ).toBe( false ); + + // A subsequent change should revert to persistent. + const subsequentState = blocks( nextState, { + type: 'UPDATE_BLOCK_ATTRIBUTES', + clientIds: [ 'kumquat' ], + attributes: { + other: true, + }, + } ); + + expect( subsequentState.isPersistentChange ).toBe( true ); } ); it( 'should retain reference for same state, same persistence', () => {