Skip to content

Commit

Permalink
Migrate hidden block types (block manager data) to new preferences pa…
Browse files Browse the repository at this point in the history
…ckages (#39132)

* Use preferences store for hiddenBlockTypes

* Add unit tests

* Remove reducer

* Add merged prefs to `getPreferences`

* Add white space to test file

* Add tests for hiding and showing block types

* Add getHiddenBlockTypes selector

* Add tests for remaining preferences actions

* Refine documentation
  • Loading branch information
talldan authored Mar 3, 2022
1 parent 84090e4 commit b6c79fe
Show file tree
Hide file tree
Showing 13 changed files with 457 additions and 90 deletions.
22 changes: 10 additions & 12 deletions docs/reference-guides/data/data-core-edit-post.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ _Returns_

- `string`: Editing mode.

### getHiddenBlockTypes

Returns an array of blocks that are hidden.

_Returns_

- `Array`: A list of the hidden block types

### getMetaBoxesPerLocation

Returns the list of all the available meta boxes for a given location.
Expand Down Expand Up @@ -352,17 +360,12 @@ _Returns_

### hideBlockTypes

Returns an action object used in signalling that block types by the given
name(s) should be hidden.
Update the provided block types to be hidden.

_Parameters_

- _blockNames_ `string[]`: Names of block types to hide.

_Returns_

- `Object`: Action object.

### initializeMetaBoxes

Initializes WordPress `postboxes` script and the logic for saving meta boxes.
Expand Down Expand Up @@ -477,17 +480,12 @@ _Returns_

### showBlockTypes

Returns an action object used in signalling that block types by the given
name(s) should be shown.
Update the provided block types to be visible.

_Parameters_

- _blockNames_ `string[]`: Names of block types to show.

_Returns_

- `Object`: Action object.

### switchEditorMode

Triggers an action used to switch editor mode.
Expand Down
53 changes: 53 additions & 0 deletions packages/data/src/plugins/persistence/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,54 @@ export function migrateFeaturePreferencesToPreferencesStore(
}
}

export function migrateIndividualPreferenceToPreferencesStore(
persistence,
sourceStoreName,
key
) {
const preferencesStoreName = 'core/preferences';
const state = persistence.get();
const sourcePreference = state[ sourceStoreName ]?.preferences?.[ key ];

// There's nothing to migrate, exit early.
if ( ! sourcePreference ) {
return;
}

const targetPreference =
state[ preferencesStoreName ]?.preferences?.[ sourceStoreName ]?.[
key
];

// There's existing data at the target, so don't overwrite it, exit early.
if ( targetPreference ) {
return;
}

const allPreferences = state[ preferencesStoreName ]?.preferences;
const targetPreferences =
state[ preferencesStoreName ]?.preferences?.[ sourceStoreName ];

persistence.set( preferencesStoreName, {
preferences: {
...allPreferences,
[ sourceStoreName ]: {
...targetPreferences,
[ key ]: sourcePreference,
},
},
} );

// Remove migrated feature preferences from the source.
const allSourcePreferences = state[ sourceStoreName ]?.preferences;
persistence.set( sourceStoreName, {
preferences: {
...allSourcePreferences,
[ key ]: undefined,
},
} );
}

/**
* Move the 'features' object in local storage from the sourceStoreName to the
* interface store.
Expand Down Expand Up @@ -358,6 +406,11 @@ persistencePlugin.__unstableMigrate = ( pluginOptions ) => {
persistence,
'core/edit-post'
);
migrateIndividualPreferenceToPreferencesStore(
persistence,
'core/edit-post',
'hiddenBlockTypes'
);
};

export default persistencePlugin;
130 changes: 130 additions & 0 deletions packages/data/src/plugins/persistence/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import plugin, {
withLazySameState,
migrateFeaturePreferencesToInterfaceStore,
migrateFeaturePreferencesToPreferencesStore,
migrateIndividualPreferenceToPreferencesStore,
} from '../';
import objectStorage from '../storage/object';
import { createRegistry } from '../../../';
Expand Down Expand Up @@ -767,4 +768,133 @@ describe( 'migrateFeaturePreferencesToInterfaceStore', () => {
} );
} );
} );

describe( 'migrateIndividualPreferenceToPreferencesStore', () => {
it( 'migrates an individual preference from the source to the preferences store', () => {
const persistenceInterface = createPersistenceInterface( {
storageKey: 'test-username',
} );

const initialState = {
preferences: {
myPreference: '123',
},
};

persistenceInterface.set( 'core/test', initialState );

migrateIndividualPreferenceToPreferencesStore(
persistenceInterface,
'core/test',
'myPreference'
);

expect( persistenceInterface.get() ).toEqual( {
'core/preferences': {
preferences: {
'core/test': {
myPreference: '123',
},
},
},
'core/test': {
preferences: {
myPreference: undefined,
},
},
} );
} );

it( 'does not overwrite other preferences in the preferences store', () => {
const persistenceInterface = createPersistenceInterface( {
storageKey: 'test-username',
} );

const initialState = {
preferences: {
myPreference: '123',
},
};

persistenceInterface.set( 'core/test', initialState );
persistenceInterface.set( 'core/preferences', {
preferences: {
'core/other-store': {
preferenceA: 1,
preferenceB: 2,
},
'core/test': {
unrelatedPreference: 'unrelated-value',
},
},
} );

migrateIndividualPreferenceToPreferencesStore(
persistenceInterface,
'core/test',
'myPreference'
);

expect( persistenceInterface.get() ).toEqual( {
'core/preferences': {
preferences: {
'core/other-store': {
preferenceA: 1,
preferenceB: 2,
},
'core/test': {
unrelatedPreference: 'unrelated-value',
myPreference: '123',
},
},
},
'core/test': {
preferences: {
myPreference: undefined,
},
},
} );
} );

it( 'does not migrate data if there is already a matching preference key at the target', () => {
const persistenceInterface = createPersistenceInterface( {
storageKey: 'test-username',
} );

persistenceInterface.set( 'core/test', {
preferences: {
myPreference: '123',
},
} );

persistenceInterface.set( 'core/preferences', {
preferences: {
'core/test': {
myPreference: 'already-set',
},
},
} );

migrateIndividualPreferenceToPreferencesStore(
persistenceInterface,
'core/test',
'myPreference'
);

expect( persistenceInterface.get() ).toEqual( {
'core/preferences': {
preferences: {
'core/test': {
myPreference: 'already-set',
},
},
},
'core/test': {
preferences: {
myPreference: '123',
},
},
} );
} );
} );
} );
4 changes: 2 additions & 2 deletions packages/edit-post/src/components/block-manager/category.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ function BlockManagerCategory( { title, blockTypes } ) {
const { defaultAllowedBlockTypes, hiddenBlockTypes } = useSelect(
( select ) => {
const { getEditorSettings } = select( editorStore );
const { getPreference } = select( editPostStore );
const { getHiddenBlockTypes } = select( editPostStore );
return {
defaultAllowedBlockTypes: getEditorSettings()
.defaultAllowedBlockTypes,
hiddenBlockTypes: getPreference( 'hiddenBlockTypes' ),
hiddenBlockTypes: getHiddenBlockTypes(),
};
},
[]
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-post/src/components/block-manager/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ export default withSelect( ( select ) => {
hasBlockSupport,
isMatchingSearchTerm,
} = select( blocksStore );
const { getPreference } = select( editPostStore );
const hiddenBlockTypes = getPreference( 'hiddenBlockTypes' );
const { getHiddenBlockTypes } = select( editPostStore );
const hiddenBlockTypes = getHiddenBlockTypes();
const numberOfHiddenBlocks =
isArray( hiddenBlockTypes ) && hiddenBlockTypes.length;

Expand Down
3 changes: 2 additions & 1 deletion packages/edit-post/src/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ function Editor( {
__experimentalGetPreviewDeviceType,
isEditingTemplate,
getEditedPostTemplate,
getHiddenBlockTypes,
} = select( editPostStore );
const { getEntityRecord, getPostType, getEntityRecords } = select(
coreStore
Expand Down Expand Up @@ -89,7 +90,7 @@ function Editor( {
preferredStyleVariations: getPreference(
'preferredStyleVariations'
),
hiddenBlockTypes: getPreference( 'hiddenBlockTypes' ),
hiddenBlockTypes: getHiddenBlockTypes(),
blockTypes: getBlockTypes(),
__experimentalLocalAutosaveInterval: getPreference(
'localAutosaveInterval'
Expand Down
4 changes: 2 additions & 2 deletions packages/edit-post/src/editor.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ export default compose( [
const {
isFeatureActive,
getEditorMode,
getPreference,
__experimentalGetPreviewDeviceType,
getHiddenBlockTypes,
} = select( editPostStore );
const { getBlockTypes } = select( blocksStore );

Expand All @@ -198,7 +198,7 @@ export default compose( [
__experimentalGetPreviewDeviceType() !== 'Desktop',
focusMode: isFeatureActive( 'focusMode' ),
mode: getEditorMode(),
hiddenBlockTypes: getPreference( 'hiddenBlockTypes' ),
hiddenBlockTypes: getHiddenBlockTypes(),
blockTypes: getBlockTypes(),
};
} ),
Expand Down
5 changes: 3 additions & 2 deletions packages/edit-post/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ export function initializeEditor(

dispatch( preferencesStore ).setDefaults( 'core/edit-post', {
fixedToolbar: false,
welcomeGuide: true,
fullscreenMode: true,
hiddenBlockTypes: [],
showBlockBreadcrumbs: true,
showIconLabels: false,
themeStyles: true,
showBlockBreadcrumbs: true,
welcomeGuide: true,
welcomeGuideTemplate: true,
} );

Expand Down
Loading

0 comments on commit b6c79fe

Please sign in to comment.