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

Block API: Register alias blocks as standalone block types on the client #61279

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/blocks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@wordpress/shortcode": "file:../shortcode",
"change-case": "^4.1.2",
"colord": "^2.7.0",
"deepmerge": "^4.3.0",
"fast-deep-equal": "^3.1.3",
"hpq": "^1.3.0",
"is-plain-object": "^5.0.0",
Expand Down
79 changes: 78 additions & 1 deletion packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import { select, dispatch } from '@wordpress/data';
import { _x } from '@wordpress/i18n';

/**
* External dependencies
*/
import deepmerge from 'deepmerge';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -727,7 +732,11 @@ export const registerBlockVariation = ( blockName, variation ) => {
console.warn( 'Variation names must be unique strings.' );
}

dispatch( blocksStore ).addBlockVariations( blockName, variation );
if ( variation.supports.alias ) {
registerBlockAlias( blockName, variation );
} else {
dispatch( blocksStore ).addBlockVariations( blockName, variation );
}
};

/**
Expand Down Expand Up @@ -758,3 +767,71 @@ export const registerBlockVariation = ( blockName, variation ) => {
export const unregisterBlockVariation = ( blockName, variationName ) => {
dispatch( blocksStore ).removeBlockVariations( blockName, variationName );
};

/**
* Registers a new block alias for the given block type.
*
* For more information on block variations see
* [the official documentation ](https://developer.wordpress.org/block-editor/reference-guides/block-api/block-variations/).
*
* @param {string} blockName Name of the block (example: “core/columns”).
* @param {Object} alias Object describing a block variation.
*
* @example
* ```js
* import { __ } from '@wordpress/i18n';
* import { registerBlockAlias } from '@wordpress/blocks';
* import { Button } from '@wordpress/components';
*
* const ExampleComponent = () => {
* return (
* <Button
* onClick={ () => {
* registerBlockAlias( 'core/embed', {
* name: 'custom',
* title: __( 'My Custom Embed' ),
* attributes: { providerNameSlug: 'custom' },
* } );
* } }
* >
* __( 'Add a custom variation for core/embed' ) }
* </Button>
* );
* };
* ```
*/
const registerBlockAlias = ( blockName, alias ) => {
const canonicalBlock = select( blocksStore ).getBlockType( blockName );

if ( ! canonicalBlock ) {
console.warn( `Block "${ blockName }" does not exist.` );
return;
}

Object.keys( alias.attributes || {} ).forEach( ( key ) => {
const aliasAttributeObject = alias.attributes[ key ];
const attributeObject = canonicalBlock.attributes[ key ];

if ( ! attributeObject ) {
console.warn(
`Attribute "${ key }" does not exist in the original block.`
);
return;
}

if ( aliasAttributeObject.type === attributeObject.type ) {
alias.attributes[ key ] = {
...aliasAttributeObject,
};
} else {
alias.attributes[ key ] = { ...attributeObject };
}
} );

const mergedBlockSettings = deepmerge( canonicalBlock, alias );

registerBlockType( alias.name, {
...mergedBlockSettings,
canonicalName: blockName,
} );
};