Skip to content

Commit

Permalink
Updating the BlockEditorProvider settings prop should reset the store…
Browse files Browse the repository at this point in the history
…'s settings entirely (#51904)

Co-authored-by: Dave Smith <[email protected]>
  • Loading branch information
youknowriad and getdave authored Jun 27, 2023
1 parent 33fc395 commit 31d691d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 14 deletions.
11 changes: 9 additions & 2 deletions packages/block-editor/src/components/provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,16 @@ export const ExperimentalBlockEditorProvider = withRegistryProvider(
...settings,
__internalIsInitialized: true,
},
stripExperimentalSettings
{
stripExperimentalSettings,
reset: true,
}
);
}, [ settings ] );
}, [
settings,
stripExperimentalSettings,
__experimentalUpdateSettings,
] );

// Syncs the entity provider with changes in the block-editor store.
useBlockSync( props );
Expand Down
4 changes: 3 additions & 1 deletion packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1390,7 +1390,9 @@ export function updateBlockListSettings( clientId, settings ) {
* @return {Object} Action object
*/
export function updateSettings( settings ) {
return __experimentalUpdateSettings( settings, true );
return __experimentalUpdateSettings( settings, {
stripExperimentalSettings: true,
} );
}

/**
Expand Down
9 changes: 6 additions & 3 deletions packages/block-editor/src/store/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,15 @@ const privateSettings = [
* Action that updates the block editor settings and
* conditionally preserves the experimental ones.
*
* @param {Object} settings Updated settings
* @param {boolean} stripExperimentalSettings Whether to strip experimental settings.
* @param {Object} settings Updated settings
* @param {Object} options Options object.
* @param {boolean} options.stripExperimentalSettings Whether to strip experimental settings.
* @param {boolean} options.reset Whether to reset the settings.
* @return {Object} Action object
*/
export function __experimentalUpdateSettings(
settings,
stripExperimentalSettings = false
{ stripExperimentalSettings = false, reset = false } = {}
) {
let cleanSettings = settings;
// There are no plugins in the mobile apps, so there is no
Expand All @@ -50,6 +52,7 @@ export function __experimentalUpdateSettings(
return {
type: 'UPDATE_SETTINGS',
settings: cleanSettings,
reset,
};
}

Expand Down
6 changes: 6 additions & 0 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,12 @@ export function template( state = { isValid: true }, action ) {
export function settings( state = SETTINGS_DEFAULTS, action ) {
switch ( action.type ) {
case 'UPDATE_SETTINGS':
if ( action.reset ) {
return {
...SETTINGS_DEFAULTS,
...action.settings,
};
}
return {
...state,
...action.settings,
Expand Down
56 changes: 56 additions & 0 deletions packages/block-editor/src/store/test/private-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
showBlockInterface,
setBlockEditingMode,
unsetBlockEditingMode,
__experimentalUpdateSettings,
} from '../private-actions';

describe( 'private actions', () => {
Expand Down Expand Up @@ -50,4 +51,59 @@ describe( 'private actions', () => {
} );
} );
} );

describe( '__experimentalUpdateSettings', () => {
const experimentalSettings = {
inserterMediaCategories: 'foo',
blockInspectorAnimation: 'bar',
};

const stableSettings = {
foo: 'foo',
bar: 'bar',
baz: 'baz',
};

const settings = {
...experimentalSettings,
...stableSettings,
};

it( 'should dispatch provided settings by default', () => {
expect( __experimentalUpdateSettings( settings ) ).toEqual( {
type: 'UPDATE_SETTINGS',
settings,
reset: false,
} );
} );

it( 'should dispatch provided settings with reset flag when `reset` argument is truthy', () => {
expect(
__experimentalUpdateSettings( settings, {
stripExperimentalSettings: false,
reset: true,
} )
).toEqual( {
type: 'UPDATE_SETTINGS',
settings,
reset: true,
} );
} );

it( 'should strip experimental settings from a given settings object when `stripExperimentalSettings` argument is truthy', () => {
expect(
__experimentalUpdateSettings( settings, {
stripExperimentalSettings: true,
} )
).toEqual( {
type: 'UPDATE_SETTINGS',
settings: {
foo: 'foo',
bar: 'bar',
baz: 'baz',
},
reset: false,
} );
} );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,6 @@ export default function useSiteEditorSettings( templateType ) {
inserterMediaCategories,
__experimentalBlockPatterns: blockPatterns,
__experimentalBlockPatternCategories: blockPatternCategories,
// Temporary fix for bug in Block Editor Provider:
// see: https://github.com/WordPress/gutenberg/issues/51489.
// Some Site Editor entities (e.g. `wp_navigation`) may utilise
// template locking in their settings. Therefore this must be
// explicitly "unset" to avoid the template locking UI remaining
// active for all entities.
templateLock: false,
template: false,
};
}, [ storedSettings, blockPatterns, blockPatternCategories ] );
}

0 comments on commit 31d691d

Please sign in to comment.