From dfef9cef28fc9a38f4c76c1cc4ddfc1aede7ee89 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Thu, 10 Mar 2022 12:52:39 +0800 Subject: [PATCH 1/4] Migrate post editor preferred style variations to preferences store --- .../data/data-core-edit-post.md | 4 -- .../data/src/plugins/persistence/index.js | 5 +++ packages/edit-post/src/index.js | 1 + packages/edit-post/src/index.native.js | 1 + packages/edit-post/src/store/actions.js | 45 +++++++++++++++---- packages/edit-post/src/store/defaults.js | 2 - packages/edit-post/src/store/reducer.js | 19 +------- packages/edit-post/src/store/selectors.js | 6 ++- 8 files changed, 49 insertions(+), 34 deletions(-) diff --git a/docs/reference-guides/data/data-core-edit-post.md b/docs/reference-guides/data/data-core-edit-post.md index f11b3d65ab387d..97c063fcdceb51 100644 --- a/docs/reference-guides/data/data-core-edit-post.md +++ b/docs/reference-guides/data/data-core-edit-post.md @@ -551,8 +551,4 @@ _Parameters_ - _blockName_ `string`: Name of the block. - _blockStyle_ `?string`: Name of the style that should be auto applied. If undefined, the "auto apply" setting of the block is removed. -_Returns_ - -- `Object`: Action object. - diff --git a/packages/data/src/plugins/persistence/index.js b/packages/data/src/plugins/persistence/index.js index a5b722bab07fbb..5b19d5035a8a85 100644 --- a/packages/data/src/plugins/persistence/index.js +++ b/packages/data/src/plugins/persistence/index.js @@ -416,6 +416,11 @@ persistencePlugin.__unstableMigrate = ( pluginOptions ) => { 'core/edit-post', 'editorMode' ); + migrateIndividualPreferenceToPreferencesStore( + persistence, + 'core/edit-post', + 'preferredStyleVariations' + ); migrateFeaturePreferencesToPreferencesStore( persistence, 'core/edit-site' diff --git a/packages/edit-post/src/index.js b/packages/edit-post/src/index.js index 05c3fa1fe4c3b0..513915b2edd29c 100644 --- a/packages/edit-post/src/index.js +++ b/packages/edit-post/src/index.js @@ -111,6 +111,7 @@ export function initializeEditor( fixedToolbar: false, fullscreenMode: true, hiddenBlockTypes: [], + preferredStyleVariations: {}, showBlockBreadcrumbs: true, showIconLabels: false, themeStyles: true, diff --git a/packages/edit-post/src/index.native.js b/packages/edit-post/src/index.native.js index d2d10efbde478b..0316597aa8c394 100644 --- a/packages/edit-post/src/index.native.js +++ b/packages/edit-post/src/index.native.js @@ -26,6 +26,7 @@ export function initializeEditor( id, postType, postId ) { fixedToolbar: false, fullscreenMode: true, hiddenBlockTypes: [], + preferredStyleVariations: {}, welcomeGuide: true, } ); diff --git a/packages/edit-post/src/store/actions.js b/packages/edit-post/src/store/actions.js index ad353afff6b200..17dc121e286596 100644 --- a/packages/edit-post/src/store/actions.js +++ b/packages/edit-post/src/store/actions.js @@ -192,16 +192,43 @@ export const togglePinnedPluginItem = ( pluginName ) => ( { registry } ) => { * * @param {string} blockName Name of the block. * @param {?string} blockStyle Name of the style that should be auto applied. If undefined, the "auto apply" setting of the block is removed. - * - * @return {Object} Action object. */ -export function updatePreferredStyleVariations( blockName, blockStyle ) { - return { - type: 'UPDATE_PREFERRED_STYLE_VARIATIONS', - blockName, - blockStyle, - }; -} +export const updatePreferredStyleVariations = ( blockName, blockStyle ) => ( { + registry, +} ) => { + if ( ! blockName ) { + return; + } + + const existingVarations = + registry + .select( preferencesStore ) + .get( 'core/edit-post', 'preferredStyleVariations' ) ?? {}; + + // When the blockStyle is ommitted, remove the block's preferred variation. + if ( ! blockStyle ) { + const updatedVariations = { + ...existingVarations, + }; + + delete updatedVariations[ blockName ]; + + registry + .dispatch( preferencesStore ) + .set( + 'core/edit-post', + 'preferredStyleVariations', + updatedVariations + ); + } + + registry + .dispatch( preferencesStore ) + .set( 'core/edit-post', 'preferredStyleVariations', { + ...existingVarations, + [ blockName ]: blockStyle, + } ); +}; /** * Update the provided block types to be visible. diff --git a/packages/edit-post/src/store/defaults.js b/packages/edit-post/src/store/defaults.js index 774f7b6caacf22..4f234264051613 100644 --- a/packages/edit-post/src/store/defaults.js +++ b/packages/edit-post/src/store/defaults.js @@ -4,6 +4,4 @@ export const PREFERENCES_DEFAULTS = { opened: true, }, }, - hiddenBlockTypes: [], - preferredStyleVariations: {}, }; diff --git a/packages/edit-post/src/store/reducer.js b/packages/edit-post/src/store/reducer.js index 200f9c06c1a623..ee4d4f47e5270b 100644 --- a/packages/edit-post/src/store/reducer.js +++ b/packages/edit-post/src/store/reducer.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { flow, get, includes, omit } from 'lodash'; +import { flow, get, includes } from 'lodash'; /** * WordPress dependencies @@ -78,23 +78,6 @@ export const preferences = flow( [ return state; }, - preferredStyleVariations( state, action ) { - switch ( action.type ) { - case 'UPDATE_PREFERRED_STYLE_VARIATIONS': { - if ( ! action.blockName ) { - return state; - } - if ( ! action.blockStyle ) { - return omit( state, [ action.blockName ] ); - } - return { - ...state, - [ action.blockName ]: action.blockStyle, - }; - } - } - return state; - }, } ); /** diff --git a/packages/edit-post/src/store/selectors.js b/packages/edit-post/src/store/selectors.js index fc81507328ec7f..19dfaf0c3a5e39 100644 --- a/packages/edit-post/src/store/selectors.js +++ b/packages/edit-post/src/store/selectors.js @@ -91,7 +91,11 @@ export const getActiveGeneralSidebarName = createRegistrySelector( // The current list of preference keys that have been migrated to the // preferences package. -const MIGRATED_KEYS = [ 'hiddenBlockTypes', 'editorMode' ]; +const MIGRATED_KEYS = [ + 'hiddenBlockTypes', + 'editorMode', + 'preferredStyleVariations', +]; /** * Returns the preferences (these preferences are persisted locally). From 181479b82af6dad054886280ecd6ce6a80ec9954 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Thu, 10 Mar 2022 14:29:26 +0800 Subject: [PATCH 2/4] Add missing unit tests --- packages/edit-post/src/store/test/actions.js | 49 ++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/packages/edit-post/src/store/test/actions.js b/packages/edit-post/src/store/test/actions.js index e8114e830a5a2a..5547f2aa8a3310 100644 --- a/packages/edit-post/src/store/test/actions.js +++ b/packages/edit-post/src/store/test/actions.js @@ -287,4 +287,53 @@ describe( 'actions', () => { } ); } ); } ); + + describe( 'updatePreferredStyleVariations', () => { + it( 'sets a preferred style variation for a block when a style name is passed', () => { + registry + .dispatch( 'core/edit-post' ) + .updatePreferredStyleVariations( 'core/paragraph', 'fancy' ); + registry + .dispatch( 'core/edit-post' ) + .updatePreferredStyleVariations( 'core/quote', 'posh' ); + + expect( + registry + .select( editPostStore ) + .getPreference( 'preferredStyleVariations' ) + ).toEqual( { + 'core/paragraph': 'fancy', + 'core/quote': 'posh', + } ); + } ); + + it( 'removes a preferred style variation for a block when a style name is omitted', () => { + registry + .dispatch( 'core/edit-post' ) + .updatePreferredStyleVariations( 'core/paragraph', 'fancy' ); + registry + .dispatch( 'core/edit-post' ) + .updatePreferredStyleVariations( 'core/quote', 'posh' ); + expect( + registry + .select( editPostStore ) + .getPreference( 'preferredStyleVariations' ) + ).toEqual( { + 'core/paragraph': 'fancy', + 'core/quote': 'posh', + } ); + + registry + .dispatch( 'core/edit-post' ) + .updatePreferredStyleVariations( 'core/paragraph' ); + + expect( + registry + .select( editPostStore ) + .getPreference( 'preferredStyleVariations' ) + ).toEqual( { + 'core/quote': 'posh', + } ); + } ); + } ); } ); From 97296f6e744dba6f8898de1256cd18fabc05f991 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Thu, 10 Mar 2022 14:36:56 +0800 Subject: [PATCH 3/4] Avoid double dispatch --- packages/edit-post/src/store/actions.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/packages/edit-post/src/store/actions.js b/packages/edit-post/src/store/actions.js index 17dc121e286596..636b18217f26ad 100644 --- a/packages/edit-post/src/store/actions.js +++ b/packages/edit-post/src/store/actions.js @@ -220,14 +220,15 @@ export const updatePreferredStyleVariations = ( blockName, blockStyle ) => ( { 'preferredStyleVariations', updatedVariations ); + } else { + // Else add the variation. + registry + .dispatch( preferencesStore ) + .set( 'core/edit-post', 'preferredStyleVariations', { + ...existingVarations, + [ blockName ]: blockStyle, + } ); } - - registry - .dispatch( preferencesStore ) - .set( 'core/edit-post', 'preferredStyleVariations', { - ...existingVarations, - [ blockName ]: blockStyle, - } ); }; /** From c43c4614c594023193b1fb70bb573b5d4a1637b6 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Thu, 10 Mar 2022 14:44:12 +0800 Subject: [PATCH 4/4] Fix typo --- packages/edit-post/src/store/actions.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/edit-post/src/store/actions.js b/packages/edit-post/src/store/actions.js index 636b18217f26ad..ae9f54d48bc225 100644 --- a/packages/edit-post/src/store/actions.js +++ b/packages/edit-post/src/store/actions.js @@ -200,15 +200,15 @@ export const updatePreferredStyleVariations = ( blockName, blockStyle ) => ( { return; } - const existingVarations = + const existingVariations = registry .select( preferencesStore ) .get( 'core/edit-post', 'preferredStyleVariations' ) ?? {}; - // When the blockStyle is ommitted, remove the block's preferred variation. + // When the blockStyle is omitted, remove the block's preferred variation. if ( ! blockStyle ) { const updatedVariations = { - ...existingVarations, + ...existingVariations, }; delete updatedVariations[ blockName ]; @@ -225,7 +225,7 @@ export const updatePreferredStyleVariations = ( blockName, blockStyle ) => ( { registry .dispatch( preferencesStore ) .set( 'core/edit-post', 'preferredStyleVariations', { - ...existingVarations, + ...existingVariations, [ blockName ]: blockStyle, } ); }