This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change default rows for product grid blocks to 3 (#1613)
* Change default rows to 3 * Handle all products rows change via deprecation * Prevent errors if object is undefined * HOC to set default attribute values * Separate default attributes from attribute definitions * Filters to set defaults * Fix deprecation so attributes are not reordered * Move flters to index file and limit which blocks are affected * Fix object assign * Use typeof for undefined checks * Move hoc to filter file so it has context * Fix up comment * Avoid mutating props * Prevent multiple `attributes` by cloning props * Force attributes to be saved to HTML in a specific order, and handle migration * useEffect to set attributes and avoid prop mutation * Switch to class component
- Loading branch information
1 parent
f4d9f7d
commit bd8d9a1
Showing
13 changed files
with
198 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { Component } from '@wordpress/element'; | ||
import { createHigherOrderComponent } from '@wordpress/compose'; | ||
const { getBlockType } = wp.blocks; | ||
const { addFilter } = wp.hooks; | ||
|
||
/** | ||
* withDefaultAttributes HOC for editor.BlockListBlock. | ||
* | ||
* @param object BlockListBlock The BlockListBlock element. | ||
*/ | ||
const withDefaultAttributes = createHigherOrderComponent( | ||
( BlockListBlock ) => { | ||
class WrappedComponent extends Component { | ||
constructor() { | ||
super( ...arguments ); | ||
|
||
const blockType = getBlockType( this.props.block.name ); | ||
const attributes = Object.assign( | ||
{}, | ||
this.props.attributes || {} | ||
); | ||
|
||
if ( | ||
this.props.block.name.startsWith( 'woocommerce/' ) && | ||
typeof blockType.attributes !== 'undefined' && | ||
typeof blockType.defaults !== 'undefined' | ||
) { | ||
Object.keys( blockType.attributes ).map( ( key ) => { | ||
if ( | ||
typeof attributes[ key ] === 'undefined' && | ||
typeof blockType.defaults[ key ] !== 'undefined' | ||
) { | ||
attributes[ key ] = blockType.defaults[ key ]; | ||
} | ||
return key; | ||
} ); | ||
} | ||
|
||
this.attributesWithDefaults = attributes; | ||
} | ||
|
||
componentDidMount() { | ||
const { block, setAttributes } = this.props; | ||
|
||
if ( block.name.startsWith( 'woocommerce/' ) ) { | ||
setAttributes( this.attributesWithDefaults ); | ||
} | ||
} | ||
|
||
render() { | ||
return ( | ||
<BlockListBlock | ||
{ ...this.props } | ||
attributes={ this.attributesWithDefaults } | ||
/> | ||
); | ||
} | ||
} | ||
return WrappedComponent; | ||
}, | ||
'withDefaultAttributes' | ||
); | ||
|
||
/** | ||
* Hook into `editor.BlockListBlock` to set default attributes (if blocks | ||
* define them separately) when a block is inserted. | ||
* | ||
* This is a workaround for Gutenberg which does not save "default" attributes | ||
* to the post, which means if defaults change, all existing blocks change too. | ||
* | ||
* See https://github.com/WordPress/gutenberg/issues/7342 | ||
* | ||
* To use this, the block name needs a `woocommerce/` prefix, and as well | ||
* as defining `attributes` during block registration, you must also declare an | ||
* array called `defaults`. Defaults should be omitted from `attributes`. | ||
*/ | ||
addFilter( | ||
'editor.BlockListBlock', | ||
'woocommerce-blocks/block-list-block', | ||
withDefaultAttributes | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
const { addFilter } = wp.hooks; | ||
|
||
/** | ||
* Adjust attributes on load to set defaults so default attributes get saved. | ||
* | ||
* @param {Object} blockAttributes Original block attributes. | ||
* @param {Object} blockType Block type settings. | ||
* | ||
* @return {Object} Filtered block attributes. | ||
*/ | ||
const setBlockAttributeDefaults = ( blockAttributes, blockType ) => { | ||
if ( blockType.name.startsWith( 'woocommerce/' ) ) { | ||
Object.keys( blockType.attributes ).map( ( key ) => { | ||
if ( | ||
typeof blockAttributes[ key ] === 'undefined' && | ||
typeof blockType.defaults !== 'undefined' && | ||
typeof blockType.defaults[ key ] !== 'undefined' | ||
) { | ||
blockAttributes[ key ] = blockType.defaults[ key ]; | ||
} | ||
return key; | ||
} ); | ||
} | ||
return blockAttributes; | ||
}; | ||
|
||
/** | ||
* Hook into `blocks.getBlockAttributes` to set default attributes (if blocks | ||
* define them separately) when a block is loaded. | ||
* | ||
* This is a workaround for Gutenberg which does not save "default" attributes | ||
* to the post, which means if defaults change, all existing blocks change too. | ||
* | ||
* See https://github.com/WordPress/gutenberg/issues/7342 | ||
* | ||
* To use this, the block name needs a `woocommerce/` prefix, and as well | ||
* as defining `attributes` during block registration, you must also declare an | ||
* array called `defaults`. Defaults should be omitted from `attributes`. | ||
*/ | ||
addFilter( | ||
'blocks.getBlockAttributes', | ||
'woocommerce-blocks/get-block-attributes', | ||
setBlockAttributeDefaults | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters