diff --git a/docs/designers-developers/developers/data/data-core-block-editor.md b/docs/designers-developers/developers/data/data-core-block-editor.md index 591e5322cf35b..5664148643014 100644 --- a/docs/designers-developers/developers/data/data-core-block-editor.md +++ b/docs/designers-developers/developers/data/data-core-block-editor.md @@ -826,6 +826,10 @@ _Returns_ Returns an action object used in signalling that the block selection is cleared. +_Parameters_ + +- _type_ `Object`: Object specifying if the clearing is destructive or not, default { destructive: false }. + _Returns_ - `Object`: Action object. diff --git a/packages/block-editor/src/store/actions.js b/packages/block-editor/src/store/actions.js index 758d1f047cf7d..bd18f39fff12e 100644 --- a/packages/block-editor/src/store/actions.js +++ b/packages/block-editor/src/store/actions.js @@ -195,9 +195,17 @@ export function multiSelect( start, end ) { /** * Returns an action object used in signalling that the block selection is cleared. * + * @param {Object} type Object specifying if the clearing is destructive or not, default + * { destructive: false }. + * * @return {Object} Action object. */ -export function clearSelectedBlock() { +export function clearSelectedBlock( type = { destructive: false } ) { + if ( type.destructive === true ) { + return { + type: 'DESTRUCTIVELY_CLEAR_SELECTED_BLOCK', + }; + } return { type: 'CLEAR_SELECTED_BLOCK', }; diff --git a/packages/block-editor/src/store/reducer.js b/packages/block-editor/src/store/reducer.js index a09a5b8a423ba..3bf5b24022907 100644 --- a/packages/block-editor/src/store/reducer.js +++ b/packages/block-editor/src/store/reducer.js @@ -717,6 +717,8 @@ const BLOCK_SELECTION_INITIAL_STATE = { */ export function blockSelection( state = BLOCK_SELECTION_INITIAL_STATE, action ) { switch ( action.type ) { + case 'DESTRUCTIVELY_CLEAR_SELECTED_BLOCK': + return BLOCK_SELECTION_INITIAL_STATE; case 'CLEAR_SELECTED_BLOCK': if ( isEqual( state, BLOCK_SELECTION_INITIAL_STATE ) ) { return BLOCK_SELECTION_INITIAL_STATE; diff --git a/packages/edit-post/src/store/effects.js b/packages/edit-post/src/store/effects.js index 54bfe07fbd67b..2b747431250a8 100644 --- a/packages/edit-post/src/store/effects.js +++ b/packages/edit-post/src/store/effects.js @@ -109,7 +109,7 @@ const effects = { SWITCH_MODE( action ) { // Unselect blocks when we switch to the code editor. if ( action.mode !== 'visual' ) { - dispatch( 'core/block-editor' ).clearSelectedBlock(); + dispatch( 'core/block-editor' ).clearSelectedBlock( { destructive: true } ); } const message = action.mode === 'visual' ? __( 'Visual editor selected' ) : __( 'Code editor selected' );