-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Fix query block duplicate group block rename controls for WP 6.4 #55604
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
Merged
scruffian
merged 13 commits into
wp/6.4
from
fix/query-block-duplicate-group-block-rename-controls
Oct 26, 2023
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
c8c44cf
Initial migration away from hooks
getdave aa44e93
Reimplement outside the hook
getdave 0b7c36d
Remove the hook UI
getdave 03bfef0
Extract to dedicated files
getdave bde882c
Name all the things
getdave 87677b6
Remove reference to redundant hook
getdave c7d3de0
Port CSS styling
getdave 9b0c20b
Remove redundant file
getdave ddd878b
Reinstate Inspector Controls renaming
getdave eff631d
Inline util func and rename
getdave d30ba81
Reinstate empty string util
getdave 1b596f6
Remove Todo comment
getdave bee71b2
Remove non-existing file ref
getdave File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Extract to dedicated files
- Loading branch information
commit 03bfef06b84bbc3dd2de446120abbaf28955d163
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
packages/block-editor/src/components/block-rename/block-rename-control.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { MenuItem } from '@wordpress/components'; | ||
| import { useSelect, useDispatch } from '@wordpress/data'; | ||
| import { __ } from '@wordpress/i18n'; | ||
| import { useState } from '@wordpress/element'; | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { store as blockEditorStore } from '../../store'; | ||
| import { useBlockDisplayInformation } from '..'; | ||
| import { emptyString } from './empty-string'; | ||
| import { RenameModal } from './modal'; | ||
|
|
||
| export function BlockRenameControl( { clientId } ) { | ||
| const [ renamingBlock, setRenamingBlock ] = useState( false ); | ||
|
|
||
| const { metadata } = useSelect( | ||
| ( select ) => { | ||
| const { getBlockAttributes } = select( blockEditorStore ); | ||
|
|
||
| const _metadata = getBlockAttributes( clientId )?.metadata; | ||
| return { | ||
| metadata: _metadata, | ||
| }; | ||
| }, | ||
| [ clientId ] | ||
| ); | ||
|
|
||
| const { updateBlockAttributes } = useDispatch( blockEditorStore ); | ||
|
|
||
| const customName = metadata?.name; | ||
|
|
||
| function onChange( newName ) { | ||
| updateBlockAttributes( [ clientId ], { | ||
| metadata: { | ||
| ...( metadata && metadata ), | ||
| name: newName, | ||
| }, | ||
| } ); | ||
| } | ||
|
|
||
| const blockInformation = useBlockDisplayInformation( clientId ); | ||
|
|
||
| return ( | ||
| <> | ||
| <MenuItem | ||
| onClick={ () => { | ||
| setRenamingBlock( true ); | ||
| } } | ||
| aria-expanded={ renamingBlock } | ||
| aria-haspopup="dialog" | ||
| > | ||
| { __( 'Rename' ) } | ||
| </MenuItem> | ||
| { renamingBlock && ( | ||
| <RenameModal | ||
| blockName={ customName || '' } | ||
| originalBlockName={ blockInformation?.title } | ||
| onClose={ () => setRenamingBlock( false ) } | ||
| onSave={ ( newName ) => { | ||
| // If the new value is the block's original name (e.g. `Group`) | ||
| // or it is an empty string then assume the intent is to reset | ||
| // the value. Therefore reset the metadata. | ||
| if ( | ||
| newName === blockInformation?.title || | ||
| emptyString( newName ) | ||
| ) { | ||
| newName = undefined; | ||
| } | ||
|
|
||
| onChange( newName ); | ||
| } } | ||
| /> | ||
| ) } | ||
| </> | ||
| ); | ||
| } |
1 change: 1 addition & 0 deletions
1
packages/block-editor/src/components/block-rename/empty-string.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const emptyString = ( testString ) => testString?.trim()?.length === 0; | ||
114 changes: 114 additions & 0 deletions
114
packages/block-editor/src/components/block-rename/modal.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { | ||
| __experimentalHStack as HStack, | ||
| __experimentalVStack as VStack, | ||
| Button, | ||
| TextControl, | ||
| Modal, | ||
| } from '@wordpress/components'; | ||
| import { useInstanceId } from '@wordpress/compose'; | ||
| import { __, sprintf } from '@wordpress/i18n'; | ||
| import { useState } from '@wordpress/element'; | ||
| import { speak } from '@wordpress/a11y'; | ||
| /** | ||
| * Internal dependencies | ||
| */ | ||
| import { emptyString } from './empty-string'; | ||
|
|
||
| export function RenameModal( { | ||
| blockName, | ||
| originalBlockName, | ||
| onClose, | ||
| onSave, | ||
| } ) { | ||
| const [ editedBlockName, setEditedBlockName ] = useState( blockName ); | ||
|
|
||
| const nameHasChanged = editedBlockName !== blockName; | ||
| const nameIsOriginal = editedBlockName === originalBlockName; | ||
| const nameIsEmpty = emptyString( editedBlockName ); | ||
|
|
||
| const isNameValid = nameHasChanged || nameIsOriginal; | ||
|
|
||
| const autoSelectInputText = ( event ) => event.target.select(); | ||
|
|
||
| const dialogDescription = useInstanceId( | ||
| RenameModal, | ||
| `block-editor-rename-modal__description` | ||
| ); | ||
|
|
||
| const handleSubmit = () => { | ||
| const message = | ||
| nameIsOriginal || nameIsEmpty | ||
| ? sprintf( | ||
| /* translators: %s: new name/label for the block */ | ||
| __( 'Block name reset to: "%s".' ), | ||
| editedBlockName | ||
| ) | ||
| : sprintf( | ||
| /* translators: %s: new name/label for the block */ | ||
| __( 'Block name changed to: "%s".' ), | ||
| editedBlockName | ||
| ); | ||
|
|
||
| // Must be assertive to immediately announce change. | ||
| speak( message, 'assertive' ); | ||
| onSave( editedBlockName ); | ||
|
|
||
| // Immediate close avoids ability to hit save multiple times. | ||
| onClose(); | ||
| }; | ||
|
|
||
| return ( | ||
| <Modal | ||
| title={ __( 'Rename' ) } | ||
| onRequestClose={ onClose } | ||
| overlayClassName="block-editor-block-rename-modal" | ||
| aria={ { | ||
| describedby: dialogDescription, | ||
| } } | ||
| focusOnMount="firstContentElement" | ||
| > | ||
| <p id={ dialogDescription }> | ||
| { __( 'Enter a custom name for this block.' ) } | ||
| </p> | ||
| <form | ||
| onSubmit={ ( e ) => { | ||
| e.preventDefault(); | ||
|
|
||
| if ( ! isNameValid ) { | ||
| return; | ||
| } | ||
|
|
||
| handleSubmit(); | ||
| } } | ||
| > | ||
| <VStack spacing="3"> | ||
| <TextControl | ||
| __nextHasNoMarginBottom | ||
| value={ editedBlockName } | ||
| label={ __( 'Block name' ) } | ||
| hideLabelFromVision={ true } | ||
| placeholder={ originalBlockName } | ||
| onChange={ setEditedBlockName } | ||
| onFocus={ autoSelectInputText } | ||
| /> | ||
| <HStack justify="right"> | ||
| <Button variant="tertiary" onClick={ onClose }> | ||
| { __( 'Cancel' ) } | ||
| </Button> | ||
|
|
||
| <Button | ||
| aria-disabled={ ! isNameValid } | ||
| variant="primary" | ||
| type="submit" | ||
| > | ||
| { __( 'Save' ) } | ||
| </Button> | ||
| </HStack> | ||
| </VStack> | ||
| </form> | ||
| </Modal> | ||
| ); | ||
| } |
20 changes: 20 additions & 0 deletions
20
packages/block-editor/src/components/block-rename/use-block-rename.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** | ||
| * WordPress dependencies | ||
| */ | ||
| import { getBlockSupport } from '@wordpress/blocks'; | ||
|
|
||
| export function useBlockRename( name ) { | ||
| const metaDataSupport = getBlockSupport( | ||
| name, | ||
| '__experimentalMetadata', | ||
| false | ||
| ); | ||
|
|
||
| const supportsBlockNaming = !! ( | ||
| true === metaDataSupport || metaDataSupport?.name | ||
| ); | ||
|
|
||
| return { | ||
| canRename: supportsBlockNaming, | ||
| }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.