From c9d8aabce4acae0d6e35f7e8e38d7f33d368e64b Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 9 Jan 2025 12:40:00 +0400 Subject: [PATCH 1/3] Block Editor: Move state logic inside 'BlockRenameModal' --- .../src/components/block-rename/modal.js | 46 ++++++++++++---- .../components/block-rename/rename-control.js | 54 +------------------ 2 files changed, 37 insertions(+), 63 deletions(-) diff --git a/packages/block-editor/src/components/block-rename/modal.js b/packages/block-editor/src/components/block-rename/modal.js index 2679c8157cc0a2..1e6d016a7d608f 100644 --- a/packages/block-editor/src/components/block-rename/modal.js +++ b/packages/block-editor/src/components/block-rename/modal.js @@ -11,22 +11,41 @@ import { import { __, sprintf } from '@wordpress/i18n'; import { useState } from '@wordpress/element'; import { speak } from '@wordpress/a11y'; +import { useSelect, useDispatch } from '@wordpress/data'; /** * Internal dependencies */ +import { store as blockEditorStore } from '../../store'; +import { useBlockDisplayInformation } from '..'; import isEmptyString from './is-empty-string'; -export default function BlockRenameModal( { - blockName, - originalBlockName, - onClose, - onSave, +export default function BlockRenameModal( { clientId, onClose } ) { + const [ editedBlockName, setEditedBlockName ] = useState( '' ); + + const blockInformation = useBlockDisplayInformation( clientId ); + const { metadata } = useSelect( + ( select ) => { + const { getBlockAttributes } = select( blockEditorStore ); + + return { + metadata: getBlockAttributes( clientId )?.metadata, + }; + }, + [ clientId ] + ); + const { updateBlockAttributes } = useDispatch( blockEditorStore ); + + const blockName = metadata?.name || ''; + const originalBlockName = blockInformation?.title; // Pattern Overrides is a WordPress-only feature but it also uses the Block Binding API. // Ideally this should not be inside the block editor package, but we keep it here for simplicity. - hasOverridesWarning, -} ) { - const [ editedBlockName, setEditedBlockName ] = useState( blockName ); + const hasOverridesWarning = + !! blockName && + !! metadata?.bindings && + Object.values( metadata.bindings ).some( + ( binding ) => binding.source === 'core/pattern-overrides' + ); const nameHasChanged = editedBlockName !== blockName; const nameIsOriginal = editedBlockName === originalBlockName; @@ -37,6 +56,8 @@ export default function BlockRenameModal( { const autoSelectInputText = ( event ) => event.target.select(); const handleSubmit = () => { + const newName = + nameIsOriginal || nameIsEmpty ? undefined : editedBlockName; const message = nameIsOriginal || nameIsEmpty ? sprintf( @@ -52,7 +73,12 @@ export default function BlockRenameModal( { // Must be assertive to immediately announce change. speak( message, 'assertive' ); - onSave( editedBlockName ); + updateBlockAttributes( [ clientId ], { + metadata: { + ...metadata, + name: newName, + }, + } ); // Immediate close avoids ability to hit save multiple times. onClose(); @@ -81,7 +107,7 @@ export default function BlockRenameModal( { { - const { getBlockAttributes } = select( blockEditorStore ); - - const _metadata = getBlockAttributes( clientId )?.metadata; - return { - metadata: _metadata, - }; - }, - [ clientId ] - ); - - const { updateBlockAttributes } = useDispatch( blockEditorStore ); - - const customName = metadata?.name; - const hasPatternOverrides = - !! customName && - !! metadata?.bindings && - Object.values( metadata.bindings ).some( - ( binding ) => binding.source === 'core/pattern-overrides' - ); - - function onChange( newName ) { - updateBlockAttributes( [ clientId ], { - metadata: { - ...metadata, - name: newName, - }, - } ); - } - - const blockInformation = useBlockDisplayInformation( clientId ); - return ( <> { renamingBlock && ( 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 || - isEmptyString( newName ) - ) { - newName = undefined; - } - - onChange( newName ); - } } /> ) } From 7f82629d9a52ebc151f5d56122be0b54fe22654a Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 9 Jan 2025 12:43:42 +0400 Subject: [PATCH 2/3] Update Button disabled props --- packages/block-editor/src/components/block-rename/modal.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/block-editor/src/components/block-rename/modal.js b/packages/block-editor/src/components/block-rename/modal.js index 1e6d016a7d608f..1c038f13f69b4e 100644 --- a/packages/block-editor/src/components/block-rename/modal.js +++ b/packages/block-editor/src/components/block-rename/modal.js @@ -131,7 +131,8 @@ export default function BlockRenameModal( { clientId, onClose } ) {