From 96cd10a82b288eb17a6ceddd0e90d316eb1493da Mon Sep 17 00:00:00 2001 From: Miguel Fonseca <150562+mcsf@users.noreply.github.com> Date: Fri, 23 Jun 2023 11:27:00 +0100 Subject: [PATCH 1/4] Block removal prompt: let consumers pass their own rules Following up on #51145, this untangles `edit-site` from `block-editor` by removing the hard-coded set of rules `blockTypePromptMessages` from the generic `BlockRemovalWarningModal` component. Rules are now to be passed to that component by whichever block editor is using it. Names and comments have been updated accordingly and improved. --- .../block-removal-warning-modal/index.js | 40 +++++-------- .../block-editor/src/store/private-actions.js | 57 +++++++++++-------- .../src/store/private-selectors.js | 4 +- packages/block-editor/src/store/reducer.js | 24 +++++--- .../block-editor/src/store/test/actions.js | 6 +- .../edit-site/src/components/editor/index.js | 13 ++++- 6 files changed, 79 insertions(+), 65 deletions(-) diff --git a/packages/block-editor/src/components/block-removal-warning-modal/index.js b/packages/block-editor/src/components/block-removal-warning-modal/index.js index 2e16d2834d2ce3..ee3e6b51e5e10c 100644 --- a/packages/block-editor/src/components/block-removal-warning-modal/index.js +++ b/packages/block-editor/src/components/block-removal-warning-modal/index.js @@ -16,38 +16,26 @@ import { __, _n } from '@wordpress/i18n'; import { store as blockEditorStore } from '../../store'; import { unlock } from '../../lock-unlock'; -// In certain editing contexts, we'd like to prevent accidental removal of -// important blocks. For example, in the site editor, the Query Loop block is -// deemed important. In such cases, we'll ask the user for confirmation that -// they intended to remove such block(s). -// -// @see https://github.com/WordPress/gutenberg/pull/51145 -export const blockTypePromptMessages = { - 'core/query': __( 'Query Loop displays a list of posts or pages.' ), - 'core/post-content': __( - 'Post Content displays the content of a post or page.' - ), -}; - -export function BlockRemovalWarningModal() { +export function BlockRemovalWarningModal( { rules } ) { const { clientIds, selectPrevious, blockNamesForPrompt } = useSelect( ( select ) => unlock( select( blockEditorStore ) ).getRemovalPromptData() ); const { - clearRemovalPrompt, - toggleRemovalPromptSupport, + clearBlockRemovalPrompt, + setBlockRemovalRules, privateRemoveBlocks, } = unlock( useDispatch( blockEditorStore ) ); - // Signalling the removal prompt is in place. + // Load block removal rules, simultaneously signalling that the block + // removal prompt is in place. useEffect( () => { - toggleRemovalPromptSupport( true ); + setBlockRemovalRules( rules ); return () => { - toggleRemovalPromptSupport( false ); + setBlockRemovalRules(); }; - }, [ toggleRemovalPromptSupport ] ); + }, [ rules, setBlockRemovalRules ] ); if ( ! blockNamesForPrompt ) { return; @@ -55,22 +43,20 @@ export function BlockRemovalWarningModal() { const onConfirmRemoval = () => { privateRemoveBlocks( clientIds, selectPrevious, /* force */ true ); - clearRemovalPrompt(); + clearBlockRemovalPrompt(); }; return ( { blockNamesForPrompt.length === 1 ? ( -

{ blockTypePromptMessages[ blockNamesForPrompt[ 0 ] ] }

+

{ rules[ blockNamesForPrompt[ 0 ] ] }

) : ( ) } @@ -82,7 +68,7 @@ export function BlockRemovalWarningModal() { ) }

-