Skip to content
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

Enable Block Renaming support for (almost) all blocks #54426

Merged
merged 16 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -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~~
- **Supports:** ~~customClassName~~, ~~html~~, ~~inserter~~, ~~renaming~~
- **Attributes:** ref

## Button
Expand Down Expand Up @@ -459,7 +459,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~~
- **Supports:** align (full, wide), ariaLabel, inserter, interactivity, layout (allowSizingOnChildren, default, ~~allowInheriting~~, ~~allowSwitching~~, ~~allowVerticalAlignment~~), spacing (blockGap, units), typography (fontSize, lineHeight), ~~html~~, ~~renaming~~
- **Attributes:** __unstableLocation, backgroundColor, customBackgroundColor, customOverlayBackgroundColor, customOverlayTextColor, customTextColor, hasIcon, icon, maxNestingLevel, openSubmenusOnClick, overlayBackgroundColor, overlayMenu, overlayTextColor, ref, rgbBackgroundColor, rgbTextColor, showSubmenuIcon, templateLock, textColor

## Custom Link
Expand Down Expand Up @@ -526,7 +526,7 @@ Show a block pattern. ([Source](https://github.com/WordPress/gutenberg/tree/trun

- **Name:** core/pattern
- **Category:** theme
- **Supports:** ~~html~~, ~~inserter~~
- **Supports:** ~~html~~, ~~inserter~~, ~~renaming~~
- **Attributes:** slug

## Author
Expand Down Expand Up @@ -907,7 +907,7 @@ Edit the different global regions of your site, like the header, footer, sidebar

- **Name:** core/template-part
- **Category:** theme
- **Supports:** align, ~~html~~, ~~reusable~~
- **Supports:** align, ~~html~~, ~~renaming~~, ~~reusable~~
- **Attributes:** area, slug, tagName, theme

## Term Description
Expand Down
22 changes: 22 additions & 0 deletions lib/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -466,3 +466,25 @@ 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 ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically @ntsekouras this is where it is being promoted to non-experimental. This doesn't actually change much other than it's no longer in the experimental directory.

Copy link
Contributor

@ntsekouras ntsekouras Sep 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think having the metadata support is okay and has been discussed in many other issues, so it could be helpful. Should we add it in a different php file under block-supports folder though?

Additionally I believe we should update the support docs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeh I was wondering that. However this is no longer a block supports is it? It's just a feature of every block.

If so then maybe it should be somewhere else in a more fundamental place lower down in the code?

Copy link
Contributor Author

@getdave getdave Sep 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ntsekouras I wonder whether infact it would be better to register the block metadata attribure as a global attribute available to every block such as seems to have been done for lock.

https://github.com/WordPress/wordpress-develop/blob/a80165b78f2367f92e2eff679c4e035d1c5bdc7c/src/wp-includes/class-wp-block-type.php#L248-L250

Maybe @Mamaduka can shed some light on the best approach here? 🙏

Remember we are discussing the metadata attribute and not the block support for blockRenaming. I say this because it's a confusing PR in that regard 😅

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Global attributes seems a good place for this, yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will handle in #55194

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ockham It looks like you backported this to Core in https://core.trac.wordpress.org/changeset/57068.

I was going to say we can remove this code but would that cause backwards compat with versions of WP Core which don't have the backported code but are running the Plugin?

If so it seems to be gated code:

if ( ! array_key_exists( 'metadata', $args['attributes'] ) ) {

...so we should probably move to a compat/wordpress-* directory? What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@getdave I think that makes sense! compat/wordpress-6.5/ sounds like the right place for this 👍

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been looking everywhere for this comment 😅

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// 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' );
21 changes: 0 additions & 21 deletions lib/experimental/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,6 @@ 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' );
Expand Down
12 changes: 2 additions & 10 deletions packages/block-editor/src/hooks/block-rename-ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { createHigherOrderComponent, useInstanceId } from '@wordpress/compose';
import { addFilter } from '@wordpress/hooks';
import { __, sprintf } from '@wordpress/i18n';
import { getBlockSupport } from '@wordpress/blocks';
import { hasBlockSupport } from '@wordpress/blocks';
import {
MenuItem,
__experimentalHStack as HStack,
Expand Down Expand Up @@ -191,15 +191,7 @@ export const withBlockRenameControl = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const { clientId, name, attributes, setAttributes } = props;

const metaDataSupport = getBlockSupport(
name,
'__experimentalMetadata',
false
);

const supportsBlockNaming = !! (
true === metaDataSupport || metaDataSupport?.name
);
const supportsBlockNaming = hasBlockSupport( name, 'renaming', true );

return (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import { hasBlockMetadataSupport } from './metadata';
import { hasBlockSupport } from '@wordpress/blocks';

/**
* Filters registered block settings, adding an `__experimentalLabel` callback if one does not already exist.
Expand All @@ -20,10 +17,10 @@ export function addLabelCallback( settings ) {
return settings;
}

const supportsBlockNaming = hasBlockMetadataSupport(
const supportsBlockNaming = hasBlockSupport(
settings,
'name',
false // default value
'renaming',
true // default value
);

// Check whether block metadata is supported before using it.
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
44 changes: 6 additions & 38 deletions packages/block-editor/src/hooks/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,8 @@
* 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`.
*
Expand All @@ -29,39 +18,18 @@ export function addMetaAttribute( blockTypeSettings ) {
return blockTypeSettings;
}

const supportsBlockNaming = hasBlockMetadataSupport(
blockTypeSettings,
'name'
);

if ( supportsBlockNaming ) {
blockTypeSettings.attributes = {
...blockTypeSettings.attributes,
[ META_ATTRIBUTE_NAME ]: {
type: 'object',
},
};
}
Comment on lines -32 to -44
Copy link
Contributor Author

@getdave getdave Sep 22, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change enables metadata support adds the metadata attribute for to all blocks.

blockTypeSettings.attributes = {
...blockTypeSettings.attributes,
[ META_ATTRIBUTE_NAME ]: {
type: 'object',
},
};
getdave marked this conversation as resolved.
Show resolved Hide resolved

return blockTypeSettings;
}

export function addSaveProps( extraProps, blockType, attributes ) {
if ( hasBlockMetadataSupport( blockType ) ) {
extraProps[ META_ATTRIBUTE_NAME ] = attributes[ META_ATTRIBUTE_NAME ];
}

return extraProps;
}

Comment on lines -49 to -56
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needed to be removed to allow tests to pass otherwise an additional <div> was added to output of all blocks.

All functionality and tests appears to work without it. I think it was mistakenly included in the original PR.

addFilter(
'blocks.registerBlockType',
'core/metadata/addMetaAttribute',
addMetaAttribute
);

addFilter(
'blocks.getSaveContent.extraProps',
'core/metadata/save-props',
addSaveProps
);
3 changes: 2 additions & 1 deletion packages/block-library/src/block/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"supports": {
"customClassName": false,
"html": false,
"inserter": false
"inserter": false,
"renaming": false
}
}
1 change: 0 additions & 1 deletion packages/block-library/src/group/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
"__experimentalOnEnter": true,
"__experimentalOnMerge": true,
"__experimentalSettings": true,
"__experimentalMetadata": true,
"align": [ "wide", "full" ],
"anchor": true,
"ariaLabel": true,
Expand Down
6 changes: 4 additions & 2 deletions packages/block-library/src/heading/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ 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' && content ) {
return content;
if ( context === 'list-view' && ( customName || content ) ) {
return attributes?.metadata?.name || content;
}

if ( context === 'accessibility' ) {
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@
}
}
},
"interactivity": true
"interactivity": true,
"renaming": false
},
"viewScript": "file:./view.min.js",
"editorStyle": "wp-block-navigation-editor",
Expand Down
10 changes: 10 additions & 0 deletions packages/block-library/src/paragraph/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ export const settings = {
},
},
__experimentalLabel( attributes, { context } ) {
const customName = attributes?.metadata?.name;

if ( context === 'list-view' && customName ) {
getdave marked this conversation as resolved.
Show resolved Hide resolved
return customName;
}

if ( context === 'accessibility' ) {
if ( customName ) {
return customName;
}

const { content } = attributes;
return ! content || content.length === 0 ? __( 'Empty' ) : content;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/pattern/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"description": "Show a block pattern.",
"supports": {
"html": false,
"inserter": false
"inserter": false,
"renaming": false
},
"textdomain": "default",
"attributes": {
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/template-part/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
"supports": {
"align": true,
"html": false,
"reusable": false
"reusable": false,
"renaming": false
},
"editorStyle": "wp-block-template-part-editor"
}
57 changes: 57 additions & 0 deletions test/e2e/specs/editor/various/block-renaming.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,27 @@ 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', () => {
Expand Down Expand Up @@ -219,5 +240,41 @@ 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();
} );
} );
} );
Loading