Skip to content

Commit

Permalink
Modify the block attributes during its deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
pierlon committed Jul 24, 2020
1 parent 4bc1439 commit 6687931
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions assets/src/block-editor/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,11 @@ export const addAMPAttributes = ( settings, name ) => {
if ( ! settings.attributes ) {
settings.attributes = {};
}
// The following attributes have to default to `false` so that the gallery block can be updated successfully if
// it has deprecated props.
settings.attributes.ampCarousel = {
type: 'boolean',
default: ! select( 'amp/block-editor' ).hasThemeSupport(), // @todo We could just default this to false now even in Reader mode since block styles are loaded.
default: false,
};
settings.attributes.ampLightbox = {
type: 'boolean',
Expand Down Expand Up @@ -175,10 +177,11 @@ export const addAMPAttributes = ( settings, name ) => {
* Remove AMP deprecated props from any blocks which may contain them.
*
* @param {Object} settings Block settings.
* @param {string} name Block name.
*
* @return {Object} Modified block settings.
*/
export const removeDeprecatedAmpProps = ( settings ) => {
export const removeDeprecatedAmpProps = ( settings, name ) => {
const maybeBlockHasDeprecatedProp = Object.keys( mappedAttributes )
.some( ( attribute ) => Object.keys( settings.attributes ).includes( attribute ) );

Expand All @@ -205,6 +208,11 @@ export const removeDeprecatedAmpProps = ( settings ) => {
return element;
}

// Once we know the block will be updated successfully, we can now modify the block's attributes.
if ( 'core/gallery' === name ) {
settings.attributes.ampCarousel.default = ! select( 'amp/block-editor' ).hasThemeSupport(); // @todo We could just default this to false now even in Reader mode since block styles are loaded.
}

// Re-render the block with deprecated props.
const props = { ...element.props, ...deprecatedProps };
element = cloneElement( element, props );
Expand Down

0 comments on commit 6687931

Please sign in to comment.