-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Show warning on critical block removal #51145
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 25 commits
2d11a45
c1912d9
405c26b
79ba8ad
15959b5
ce307ad
e5ddee6
df68c00
042063a
fb4c79d
d3a12c8
c9952f1
33afda6
7482ba3
a5cce0a
b26efc4
0e88e1a
29f99c1
bb0cd7c
c27ab49
65df9cf
02df539
d2b6a9f
5d1c3e2
e180954
23f034d
6a0a806
628207a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { useEffect } from '@wordpress/element'; | ||
| import { useDispatch, useSelect } from '@wordpress/data'; | ||
| import { | ||
| Modal, | ||
| Button, | ||
| __experimentalHStack as HStack, | ||
| } from '@wordpress/components'; | ||
| import { __, _n } from '@wordpress/i18n'; | ||
|
|
||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| 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() { | ||
| const { clientIds, selectPrevious, blockNamesForPrompt } = useSelect( | ||
| ( select ) => | ||
| unlock( select( blockEditorStore ) ).getRemovalPromptData() | ||
| ); | ||
|
|
||
| const { | ||
| clearRemovalPrompt, | ||
| toggleRemovalPromptSupport, | ||
| privateRemoveBlocks, | ||
| } = unlock( useDispatch( blockEditorStore ) ); | ||
|
|
||
| // Signalling the removal prompt is in place. | ||
| useEffect( () => { | ||
| toggleRemovalPromptSupport( true ); | ||
| return () => { | ||
| toggleRemovalPromptSupport( false ); | ||
| }; | ||
| }, [ toggleRemovalPromptSupport ] ); | ||
|
|
||
| if ( ! blockNamesForPrompt ) { | ||
| return; | ||
| } | ||
|
|
||
| const onConfirmRemoval = () => { | ||
| privateRemoveBlocks( clientIds, selectPrevious, /* force */ true ); | ||
| clearRemovalPrompt(); | ||
| }; | ||
|
|
||
| return ( | ||
| <Modal | ||
| title={ __( 'Are you sure?' ) } | ||
| onRequestClose={ clearRemovalPrompt } | ||
| > | ||
| { blockNamesForPrompt.length === 1 ? ( | ||
| <p>{ blockTypePromptMessages[ blockNamesForPrompt[ 0 ] ] }</p> | ||
| ) : ( | ||
| <ul style={ { listStyleType: 'disc', paddingLeft: '1rem' } }> | ||
| { blockNamesForPrompt.map( ( name ) => ( | ||
| <li key={ name }> | ||
| { blockTypePromptMessages[ name ] } | ||
| </li> | ||
| ) ) } | ||
| </ul> | ||
| ) } | ||
| <p> | ||
| { _n( | ||
| 'Removing this block is not advised.', | ||
| 'Removing these blocks is not advised.', | ||
| blockNamesForPrompt.length | ||
| ) } | ||
| </p> | ||
| <HStack justify="right"> | ||
| <Button variant="tertiary" onClick={ clearRemovalPrompt }> | ||
| { __( 'Cancel' ) } | ||
| </Button> | ||
| <Button variant="primary" onClick={ onConfirmRemoval }> | ||
| { __( 'Delete' ) } | ||
| </Button> | ||
| </HStack> | ||
| </Modal> | ||
| ); | ||
|
Comment on lines
57
to
93
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a blocker, but just noting that this is pretty similar to the There are possibly some differences (the title?), so it could be a later refactor.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, good observation. |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now this entangles
block-editorwithedit-site, since these are rules that only apply to the site editor. I don't want to hold up this PR any longer, but a future improvement could be to let the consumer (the component renderingBlockRemovalWarningModal) provide their own rules. Maybe:As you can tell, I'm undecided on the terminology: are these messages? Rules? Warnings?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
-> #51841