Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 11 additions & 39 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) => {
Expand Down Expand Up @@ -421,63 +420,37 @@ 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;
}
const nextState = reducer( state, action );

if ( explicitPersistent !== undefined ) {
nextIsPersistentChange = explicitPersistent;
return nextIsPersistentChange === nextState.isPersistentChange
? nextState
: {
...nextState,
isPersistentChange: nextIsPersistentChange,
};
}
const wasMarkedAsNotPersistent = markNextChangeAsNotPersistent;
markNextChangeAsNotPersistent =
action.type === 'MARK_NEXT_CHANGE_AS_NOT_PERSISTENT';

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 };
};
}

Expand Down Expand Up @@ -897,7 +870,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 ) ) ) {
Expand Down
39 changes: 39 additions & 0 deletions packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2196,6 +2196,45 @@ describe( 'state', () => {
expect( state.isPersistentChange ).toBe( true );
} );

it( 'should flag only 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 nextState = blocks( original, {
type: 'UPDATE_BLOCK_ATTRIBUTES',
clientIds: [ 'kumquat' ],
attributes: {
updated: true,
},
} );

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', () => {
const original = deepFreeze(
blocks( undefined, {
Expand Down
Loading