From c63e0befaff1cdeb17307da38a720dc3be40f524 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Tue, 10 Oct 2023 17:06:22 +0100 Subject: [PATCH] Revert "Enable Block Renaming support for (almost) all blocks (#54426)" This reverts commit 7aa5a913473c223402549a9060aafac652153965. --- docs/reference-guides/core-blocks.md | 8 +-- lib/blocks.php | 22 ------- lib/experimental/blocks.php | 21 +++++++ .../block-editor/src/hooks/block-rename-ui.js | 12 +++- packages/block-editor/src/hooks/index.js | 2 +- .../{block-renaming.js => metadata-name.js} | 11 ++-- packages/block-editor/src/hooks/metadata.js | 44 ++++++++++++-- packages/block-library/src/block/block.json | 3 +- packages/block-library/src/group/block.json | 1 + packages/block-library/src/heading/index.js | 6 +- .../block-library/src/navigation/block.json | 3 +- packages/block-library/src/paragraph/index.js | 10 ---- packages/block-library/src/pattern/block.json | 3 +- .../src/template-part/block.json | 3 +- .../editor/various/block-renaming.spec.js | 57 ------------------- 15 files changed, 88 insertions(+), 118 deletions(-) rename packages/block-editor/src/hooks/{block-renaming.js => metadata-name.js} (84%) diff --git a/docs/reference-guides/core-blocks.md b/docs/reference-guides/core-blocks.md index 68b4349197a8b..3c48206c55a23 100644 --- a/docs/reference-guides/core-blocks.md +++ b/docs/reference-guides/core-blocks.md @@ -41,7 +41,7 @@ Create and save content to reuse across your site. Update the pattern, and the c - **Name:** core/block - **Category:** reusable -- **Supports:** ~~customClassName~~, ~~html~~, ~~inserter~~, ~~renaming~~ +- **Supports:** ~~customClassName~~, ~~html~~, ~~inserter~~ - **Attributes:** ref ## Button @@ -463,7 +463,7 @@ A collection of blocks that allow visitors to get around your site. ([Source](ht - **Name:** core/navigation - **Category:** theme -- **Supports:** align (full, wide), ariaLabel, inserter, interactivity, layout (allowSizingOnChildren, default, ~~allowInheriting~~, ~~allowSwitching~~, ~~allowVerticalAlignment~~), spacing (blockGap, units), typography (fontSize, lineHeight), ~~html~~, ~~renaming~~ +- **Supports:** align (full, wide), ariaLabel, inserter, interactivity, layout (allowSizingOnChildren, default, ~~allowInheriting~~, ~~allowSwitching~~, ~~allowVerticalAlignment~~), spacing (blockGap, units), typography (fontSize, lineHeight), ~~html~~ - **Attributes:** __unstableLocation, backgroundColor, customBackgroundColor, customOverlayBackgroundColor, customOverlayTextColor, customTextColor, hasIcon, icon, maxNestingLevel, openSubmenusOnClick, overlayBackgroundColor, overlayMenu, overlayTextColor, ref, rgbBackgroundColor, rgbTextColor, showSubmenuIcon, templateLock, textColor ## Custom Link @@ -530,7 +530,7 @@ Show a block pattern. ([Source](https://github.com/WordPress/gutenberg/tree/trun - **Name:** core/pattern - **Category:** theme -- **Supports:** ~~html~~, ~~inserter~~, ~~renaming~~ +- **Supports:** ~~html~~, ~~inserter~~ - **Attributes:** slug ## Author @@ -911,7 +911,7 @@ Edit the different global regions of your site, like the header, footer, sidebar - **Name:** core/template-part - **Category:** theme -- **Supports:** align, ~~html~~, ~~renaming~~, ~~reusable~~ +- **Supports:** align, ~~html~~, ~~reusable~~ - **Attributes:** area, slug, tagName, theme ## Term Description diff --git a/lib/blocks.php b/lib/blocks.php index 39ac57d8b6d09..1794762b010db 100644 --- a/lib/blocks.php +++ b/lib/blocks.php @@ -466,25 +466,3 @@ function gutenberg_should_render_lightbox( $block ) { } add_filter( 'render_block_data', 'gutenberg_should_render_lightbox', 15, 1 ); - -/** - * Registers the metadata block attribute for all block types. - * - * @param array $args Array of arguments for registering a block type. - * @return array $args - */ -function gutenberg_register_metadata_attribute( $args ) { - // Setup attributes if needed. - if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) { - $args['attributes'] = array(); - } - - if ( ! array_key_exists( 'metadata', $args['attributes'] ) ) { - $args['attributes']['metadata'] = array( - 'type' => 'object', - ); - } - - return $args; -} -add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' ); diff --git a/lib/experimental/blocks.php b/lib/experimental/blocks.php index f9f2412ae5120..2102834dc6a15 100644 --- a/lib/experimental/blocks.php +++ b/lib/experimental/blocks.php @@ -79,6 +79,27 @@ function wp_enqueue_block_view_script( $block_name, $args ) { } +/** + * Registers the metadata block attribute for block types. + * + * @param array $args Array of arguments for registering a block type. + * @return array $args + */ +function gutenberg_register_metadata_attribute( $args ) { + // Setup attributes if needed. + if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) { + $args['attributes'] = array(); + } + + if ( ! array_key_exists( 'metadata', $args['attributes'] ) ) { + $args['attributes']['metadata'] = array( + 'type' => 'object', + ); + } + + return $args; +} +add_filter( 'register_block_type_args', 'gutenberg_register_metadata_attribute' ); $gutenberg_experiments = get_option( 'gutenberg-experiments' ); diff --git a/packages/block-editor/src/hooks/block-rename-ui.js b/packages/block-editor/src/hooks/block-rename-ui.js index 6a98dcf2e2fad..9025bfee61983 100644 --- a/packages/block-editor/src/hooks/block-rename-ui.js +++ b/packages/block-editor/src/hooks/block-rename-ui.js @@ -4,7 +4,7 @@ import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose'; import { addFilter } from '@wordpress/hooks'; import { __, sprintf } from '@wordpress/i18n'; -import { hasBlockSupport } from '@wordpress/blocks'; +import { getBlockSupport } from '@wordpress/blocks'; import { MenuItem, __experimentalHStack as HStack, @@ -191,7 +191,15 @@ export const withBlockRenameControl = createHigherOrderComponent( ( BlockEdit ) => ( props ) => { const { clientId, name, attributes, setAttributes } = props; - const supportsBlockNaming = hasBlockSupport( name, 'renaming', true ); + const metaDataSupport = getBlockSupport( + name, + '__experimentalMetadata', + false + ); + + const supportsBlockNaming = !! ( + true === metaDataSupport || metaDataSupport?.name + ); return ( <> diff --git a/packages/block-editor/src/hooks/index.js b/packages/block-editor/src/hooks/index.js index 730f0defe0a63..8ae5c1dbe3a7e 100644 --- a/packages/block-editor/src/hooks/index.js +++ b/packages/block-editor/src/hooks/index.js @@ -19,9 +19,9 @@ import './position'; import './layout'; import './content-lock-ui'; import './metadata'; +import './metadata-name'; import './custom-fields'; import './block-hooks'; -import './block-renaming'; import './block-rename-ui'; export { useCustomSides } from './dimensions'; diff --git a/packages/block-editor/src/hooks/block-renaming.js b/packages/block-editor/src/hooks/metadata-name.js similarity index 84% rename from packages/block-editor/src/hooks/block-renaming.js rename to packages/block-editor/src/hooks/metadata-name.js index 5db06d1a652d4..6eecb0ce3667c 100644 --- a/packages/block-editor/src/hooks/block-renaming.js +++ b/packages/block-editor/src/hooks/metadata-name.js @@ -2,7 +2,10 @@ * WordPress dependencies */ import { addFilter } from '@wordpress/hooks'; -import { hasBlockSupport } from '@wordpress/blocks'; +/** + * Internal dependencies + */ +import { hasBlockMetadataSupport } from './metadata'; /** * Filters registered block settings, adding an `__experimentalLabel` callback if one does not already exist. @@ -17,10 +20,10 @@ export function addLabelCallback( settings ) { return settings; } - const supportsBlockNaming = hasBlockSupport( + const supportsBlockNaming = hasBlockMetadataSupport( settings, - 'renaming', - true // default value + 'name', + false // default value ); // Check whether block metadata is supported before using it. diff --git a/packages/block-editor/src/hooks/metadata.js b/packages/block-editor/src/hooks/metadata.js index 8b938f1348f1c..918f4f80ee9c2 100644 --- a/packages/block-editor/src/hooks/metadata.js +++ b/packages/block-editor/src/hooks/metadata.js @@ -2,8 +2,19 @@ * WordPress dependencies */ import { addFilter } from '@wordpress/hooks'; +import { getBlockSupport } from '@wordpress/blocks'; + const META_ATTRIBUTE_NAME = 'metadata'; +export function hasBlockMetadataSupport( blockType, feature = '' ) { + // Only core blocks are allowed to use __experimentalMetadata until the fetaure is stablised. + if ( ! blockType.name.startsWith( 'core/' ) ) { + return false; + } + const support = getBlockSupport( blockType, '__experimentalMetadata' ); + return !! ( true === support || support?.[ feature ] ); +} + /** * Filters registered block settings, extending attributes to include `metadata`. * @@ -18,18 +29,39 @@ export function addMetaAttribute( blockTypeSettings ) { return blockTypeSettings; } - blockTypeSettings.attributes = { - ...blockTypeSettings.attributes, - [ META_ATTRIBUTE_NAME ]: { - type: 'object', - }, - }; + const supportsBlockNaming = hasBlockMetadataSupport( + blockTypeSettings, + 'name' + ); + + if ( supportsBlockNaming ) { + blockTypeSettings.attributes = { + ...blockTypeSettings.attributes, + [ META_ATTRIBUTE_NAME ]: { + type: 'object', + }, + }; + } return blockTypeSettings; } +export function addSaveProps( extraProps, blockType, attributes ) { + if ( hasBlockMetadataSupport( blockType ) ) { + extraProps[ META_ATTRIBUTE_NAME ] = attributes[ META_ATTRIBUTE_NAME ]; + } + + return extraProps; +} + addFilter( 'blocks.registerBlockType', 'core/metadata/addMetaAttribute', addMetaAttribute ); + +addFilter( + 'blocks.getSaveContent.extraProps', + 'core/metadata/save-props', + addSaveProps +); diff --git a/packages/block-library/src/block/block.json b/packages/block-library/src/block/block.json index aeccdbfc1051d..4cb53960725d2 100644 --- a/packages/block-library/src/block/block.json +++ b/packages/block-library/src/block/block.json @@ -15,7 +15,6 @@ "supports": { "customClassName": false, "html": false, - "inserter": false, - "renaming": false + "inserter": false } } diff --git a/packages/block-library/src/group/block.json b/packages/block-library/src/group/block.json index 92bbc1b0d1135..4b89d86539117 100644 --- a/packages/block-library/src/group/block.json +++ b/packages/block-library/src/group/block.json @@ -24,6 +24,7 @@ "__experimentalOnEnter": true, "__experimentalOnMerge": true, "__experimentalSettings": true, + "__experimentalMetadata": true, "align": [ "wide", "full" ], "anchor": true, "ariaLabel": true, diff --git a/packages/block-library/src/heading/index.js b/packages/block-library/src/heading/index.js index 3752ca70bc714..4ff1203df33fc 100644 --- a/packages/block-library/src/heading/index.js +++ b/packages/block-library/src/heading/index.js @@ -29,12 +29,10 @@ export const settings = { __experimentalLabel( attributes, { context } ) { const { content, level } = attributes; - const customName = attributes?.metadata?.name; - // In the list view, use the block's content as the label. // If the content is empty, fall back to the default label. - if ( context === 'list-view' && ( customName || content ) ) { - return attributes?.metadata?.name || content; + if ( context === 'list-view' && content ) { + return content; } if ( context === 'accessibility' ) { diff --git a/packages/block-library/src/navigation/block.json b/packages/block-library/src/navigation/block.json index 9ec919ae38d1f..cb5ca4fec1b90 100644 --- a/packages/block-library/src/navigation/block.json +++ b/packages/block-library/src/navigation/block.json @@ -133,8 +133,7 @@ } } }, - "interactivity": true, - "renaming": false + "interactivity": true }, "viewScript": "file:./view.min.js", "editorStyle": "wp-block-navigation-editor", diff --git a/packages/block-library/src/paragraph/index.js b/packages/block-library/src/paragraph/index.js index 715fb35ec05ab..bceff88136707 100644 --- a/packages/block-library/src/paragraph/index.js +++ b/packages/block-library/src/paragraph/index.js @@ -28,17 +28,7 @@ export const settings = { }, }, __experimentalLabel( attributes, { context } ) { - const customName = attributes?.metadata?.name; - - if ( context === 'list-view' && customName ) { - return customName; - } - if ( context === 'accessibility' ) { - if ( customName ) { - return customName; - } - const { content } = attributes; return ! content || content.length === 0 ? __( 'Empty' ) : content; } diff --git a/packages/block-library/src/pattern/block.json b/packages/block-library/src/pattern/block.json index da02f7b72747e..e9a85a9b2f84f 100644 --- a/packages/block-library/src/pattern/block.json +++ b/packages/block-library/src/pattern/block.json @@ -7,8 +7,7 @@ "description": "Show a block pattern.", "supports": { "html": false, - "inserter": false, - "renaming": false + "inserter": false }, "textdomain": "default", "attributes": { diff --git a/packages/block-library/src/template-part/block.json b/packages/block-library/src/template-part/block.json index 3b0946718bcb9..9fe431150ae39 100644 --- a/packages/block-library/src/template-part/block.json +++ b/packages/block-library/src/template-part/block.json @@ -23,8 +23,7 @@ "supports": { "align": true, "html": false, - "reusable": false, - "renaming": false + "reusable": false }, "editorStyle": "wp-block-template-part-editor" } diff --git a/test/e2e/specs/editor/various/block-renaming.spec.js b/test/e2e/specs/editor/various/block-renaming.spec.js index 4150be64bd33d..1c8a958b23fd4 100644 --- a/test/e2e/specs/editor/various/block-renaming.spec.js +++ b/test/e2e/specs/editor/various/block-renaming.spec.js @@ -145,27 +145,6 @@ test.describe( 'Block Renaming', () => { }, ] ); } ); - - test( 'does not allow renaming of blocks that do not support renaming', async ( { - // use `core/template-part` as the block - editor, - page, - } ) => { - await editor.insertBlock( { - name: 'core/navigation', - } ); - - // Opens the block options menu and check there is not a `Rename` option - await editor.clickBlockToolbarButton( 'Options' ); - // - - const renameMenuItem = page.getByRole( 'menuitem', { - name: 'Rename', - } ); - - // TODO: assert that the locator didn't find a DOM node at all. - await expect( renameMenuItem ).toBeHidden(); - } ); } ); test.describe( 'Block inspector renaming', () => { @@ -240,41 +219,5 @@ test.describe( 'Block Renaming', () => { }, ] ); } ); - - test( 'does not allow renaming of blocks that do not support renaming', async ( { - editor, - page, - } ) => { - await editor.insertBlock( { - name: 'core/navigation', - } ); - - await editor.openDocumentSettingsSidebar(); - - const settingsTab = page - .getByRole( 'region', { - name: 'Editor settings', - } ) - .getByRole( 'tab', { name: 'Settings' } ); - - await settingsTab.click(); - - const advancedPanelToggle = page - .getByRole( 'region', { - name: 'Editor settings', - } ) - .getByRole( 'button', { - name: 'Advanced', - expanded: false, - } ); - - await advancedPanelToggle.click(); - - const nameInput = page.getByRole( 'textbox', { - name: 'Block name', - } ); - - await expect( nameInput ).toBeHidden(); - } ); } ); } );