Skip to content

Commit

Permalink
Migrate editor package isPublishSidebarEnabled to preferences store (
Browse files Browse the repository at this point in the history
…#39707)

* Use preference store for isPublishSidebarEnabled state

* Migrate isPublishSidebarEnabled localstorage value

* Remove preferences reducer from RN code

* Update packae lock

* Update unit tests

* Fix migration of `false` value

* Fix typo
  • Loading branch information
talldan authored Mar 28, 2022
1 parent 7490f12 commit 26c8b71
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 163 deletions.
16 changes: 2 additions & 14 deletions docs/reference-guides/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -968,10 +968,6 @@ _Returns_
Returns whether the pre-publish panel should be shown
or skipped when the user clicks the "publish" button.

_Parameters_

- _state_ `Object`: Global application state.

_Returns_

- `boolean`: Whether the pre-publish panel should be shown or not.
Expand Down Expand Up @@ -1049,11 +1045,7 @@ Action that creates an undo history record.

### disablePublishSidebar

Action that disables the publish sidebar.

_Returns_

- `Object`: Action object
Disables the publish sidebar.

### editPost

Expand All @@ -1067,11 +1059,7 @@ _Parameters_

### enablePublishSidebar

Action that enables the publish sidebar.

_Returns_

- `Object`: Action object
Enable the publish sidebar.

### enterFormattedText

Expand Down
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 25 additions & 12 deletions packages/data/src/plugins/persistence/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,19 @@ export function migrateFeaturePreferencesToPreferencesStore(
}
}

/**
* Migrates an individual item inside the `preferences` object for a store.
*
* @param {Object} persistence The persistence interface.
* @param {Object} migrate An options object that contains details of the migration.
* @param {string} migrate.from The name of the store to migrate from.
* @param {string} migrate.scope The scope in the preferences store to migrate to.
* @param {string} key The key in the preferences object to migrate.
* @param {?Function} convert A function that converts preferences from one format to another.
*/
export function migrateIndividualPreferenceToPreferencesStore(
persistence,
sourceStoreName,
{ from: sourceStoreName, scope },
key,
convert = identity
) {
Expand All @@ -311,14 +321,12 @@ export function migrateIndividualPreferenceToPreferencesStore(
const sourcePreference = state[ sourceStoreName ]?.preferences?.[ key ];

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

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

// There's existing data at the target, so don't overwrite it, exit early.
if ( targetPreference ) {
Expand All @@ -327,7 +335,7 @@ export function migrateIndividualPreferenceToPreferencesStore(

const otherScopes = state[ preferencesStoreName ]?.preferences;
const otherPreferences =
state[ preferencesStoreName ]?.preferences?.[ sourceStoreName ];
state[ preferencesStoreName ]?.preferences?.[ scope ];

// Pass an object with the key and value as this allows the convert
// function to convert to a data structure that has different keys.
Expand All @@ -336,7 +344,7 @@ export function migrateIndividualPreferenceToPreferencesStore(
persistence.set( preferencesStoreName, {
preferences: {
...otherScopes,
[ sourceStoreName ]: {
[ scope ]: {
...otherPreferences,
...convertedPreferences,
},
Expand Down Expand Up @@ -602,28 +610,33 @@ persistencePlugin.__unstableMigrate = ( pluginOptions ) => {
// Other ad-hoc preferences.
migrateIndividualPreferenceToPreferencesStore(
persistence,
'core/edit-post',
{ from: 'core/edit-post', scope: 'core/edit-post' },
'hiddenBlockTypes'
);
migrateIndividualPreferenceToPreferencesStore(
persistence,
'core/edit-post',
{ from: 'core/edit-post', scope: 'core/edit-post' },
'editorMode'
);
migrateIndividualPreferenceToPreferencesStore(
persistence,
'core/edit-post',
{ from: 'core/edit-post', scope: 'core/edit-post' },
'preferredStyleVariations'
);
migrateIndividualPreferenceToPreferencesStore(
persistence,
'core/edit-post',
{ from: 'core/edit-post', scope: 'core/edit-post' },
'panels',
convertEditPostPanels
);
migrateIndividualPreferenceToPreferencesStore(
persistence,
'core/edit-site',
{ from: 'core/editor', scope: 'core/edit-post' },
'isPublishSidebarEnabled'
);
migrateIndividualPreferenceToPreferencesStore(
persistence,
{ from: 'core/edit-site', scope: 'core/edit-site' },
'editorMode'
);
migrateInterfaceEnableItemsToPreferencesStore( persistence );
Expand Down
72 changes: 69 additions & 3 deletions packages/data/src/plugins/persistence/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ describe( 'migrateIndividualPreferenceToPreferencesStore', () => {

migrateIndividualPreferenceToPreferencesStore(
persistenceInterface,
'core/test',
{ from: 'core/test', scope: 'core/test' },
'myPreference'
);

Expand Down Expand Up @@ -743,7 +743,7 @@ describe( 'migrateIndividualPreferenceToPreferencesStore', () => {

migrateIndividualPreferenceToPreferencesStore(
persistenceInterface,
'core/test',
{ from: 'core/test', scope: 'core/test' },
'myPreference'
);

Expand Down Expand Up @@ -771,6 +771,41 @@ describe( 'migrateIndividualPreferenceToPreferencesStore', () => {
} );
} );

it( 'supports moving data to a scope that is differently named to the source store', () => {
const persistenceInterface = createPersistenceInterface( {
storageKey: 'test-username',
} );

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

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

migrateIndividualPreferenceToPreferencesStore(
persistenceInterface,
{ from: 'core/source', scope: 'core/destination' },
'myPreference'
);

expect( persistenceInterface.get() ).toEqual( {
'core/preferences': {
preferences: {
'core/destination': {
myPreference: '123',
},
},
},
'core/source': {
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',
Expand All @@ -792,7 +827,7 @@ describe( 'migrateIndividualPreferenceToPreferencesStore', () => {

migrateIndividualPreferenceToPreferencesStore(
persistenceInterface,
'core/test',
{ from: 'core/test', scope: 'core/test' },
'myPreference'
);

Expand All @@ -811,6 +846,37 @@ describe( 'migrateIndividualPreferenceToPreferencesStore', () => {
},
} );
} );

it( 'migrates preferences that have a `false` value', () => {
const persistenceInterface = createPersistenceInterface( {
storageKey: 'test-username',
} );

persistenceInterface.set( 'core/test', {
preferences: {
myFalsePreference: false,
},
} );

migrateIndividualPreferenceToPreferencesStore(
persistenceInterface,
{ from: 'core/test', scope: 'core/test' },
'myFalsePreference'
);

expect( persistenceInterface.get() ).toEqual( {
'core/preferences': {
preferences: {
'core/test': {
myFalsePreference: false,
},
},
},
'core/test': {
preferences: {},
},
} );
} );
} );

describe( 'migrateThirdPartyFeaturePreferencesToPreferencesStore', () => {
Expand Down
1 change: 1 addition & 0 deletions packages/edit-post/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ export function initializeEditor(
fullscreenMode: true,
hiddenBlockTypes: [],
inactivePanels: [],
isPublishSidebarEnabled: true,
openPanels: [ 'post-status' ],
preferredStyleVariations: {},
showBlockBreadcrumbs: true,
Expand Down
1 change: 1 addition & 0 deletions packages/edit-post/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export function initializeEditor( id, postType, postId ) {
fullscreenMode: true,
hiddenBlockTypes: [],
inactivePanels: [],
isPublishSidebarEnabled: true,
openPanels: [ 'post-status' ],
preferredStyleVariations: {},
welcomeGuide: true,
Expand Down
1 change: 1 addition & 0 deletions packages/editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@wordpress/keycodes": "file:../keycodes",
"@wordpress/media-utils": "file:../media-utils",
"@wordpress/notices": "file:../notices",
"@wordpress/preferences": "file:../preferences",
"@wordpress/reusable-blocks": "file:../reusable-blocks",
"@wordpress/rich-text": "file:../rich-text",
"@wordpress/server-side-render": "file:../server-side-render",
Expand Down
29 changes: 13 additions & 16 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
import { store as noticesStore } from '@wordpress/notices';
import { store as coreStore } from '@wordpress/core-data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { store as preferencesStore } from '@wordpress/preferences';

/**
* Internal dependencies
Expand Down Expand Up @@ -320,26 +321,22 @@ export function updatePostLock( lock ) {
}

/**
* Action that enables the publish sidebar.
*
* @return {Object} Action object
* Enable the publish sidebar.
*/
export function enablePublishSidebar() {
return {
type: 'ENABLE_PUBLISH_SIDEBAR',
};
}
export const enablePublishSidebar = () => ( { registry } ) => {
registry
.dispatch( preferencesStore )
.set( 'core/edit-post', 'isPublishSidebarEnabled', true );
};

/**
* Action that disables the publish sidebar.
*
* @return {Object} Action object
* Disables the publish sidebar.
*/
export function disablePublishSidebar() {
return {
type: 'DISABLE_PUBLISH_SIDEBAR',
};
}
export const disablePublishSidebar = () => ( { registry } ) => {
registry
.dispatch( preferencesStore )
.set( 'core/edit-post', 'isPublishSidebarEnabled', false );
};

/**
* Action that locks post saving.
Expand Down
5 changes: 0 additions & 5 deletions packages/editor/src/store/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
*/
import { SETTINGS_DEFAULTS } from '@wordpress/block-editor';

export const PREFERENCES_DEFAULTS = {
insertUsage: {}, // Should be kept for backward compatibility, see: https://github.com/WordPress/gutenberg/issues/14580.
isPublishSidebarEnabled: true,
};

/**
* The default post editor settings.
*
Expand Down
10 changes: 2 additions & 8 deletions packages/editor/src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { createReduxStore, registerStore } from '@wordpress/data';
import { createReduxStore, register } from '@wordpress/data';

/**
* Internal dependencies
Expand Down Expand Up @@ -33,12 +33,6 @@ export const storeConfig = {
*/
export const store = createReduxStore( STORE_NAME, {
...storeConfig,
persist: [ 'preferences' ],
} );

// Once we build a more generic persistence plugin that works across types of stores
// we'd be able to replace this with a register call.
registerStore( STORE_NAME, {
...storeConfig,
persist: [ 'preferences' ],
} );
register( store );
Loading

0 comments on commit 26c8b71

Please sign in to comment.