From 862d71975d72abd942c5528e142662168c7e0e7a Mon Sep 17 00:00:00 2001 From: Miguel Fonseca <150562+mcsf@users.noreply.github.com> Date: Fri, 30 Jun 2023 14:50:43 +0100 Subject: [PATCH] Block removal prompt: let consumers pass their own rules (#51841) * 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. * Site editor: Add e2e test for block removal prompt --- .../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 +++- .../specs/site-editor/block-removal.spec.js | 67 +++++++++++++++++++ 7 files changed, 146 insertions(+), 65 deletions(-) create mode 100644 test/e2e/specs/site-editor/block-removal.spec.js 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 2ed65481f68959..08f3deccb5ae08 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 { __ } 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 ] ] }

) : ( ) } @@ -80,7 +66,7 @@ export function BlockRemovalWarningModal() { : __( 'Removing this block is not advised.' ) }

-