-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #194 from phpbits/add/group-transform
Add option to Transform Group Block to Cover Block
- Loading branch information
Showing
5 changed files
with
161 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
<?php return array('dependencies' => array('lodash', 'react', 'wp-element', 'wp-polyfill', 'wp-wordcount'), 'version' => 'd5c66d5eded401dc90269bb36926f20f'); | ||
<?php return array('dependencies' => array('lodash', 'react', 'wp-element', 'wp-polyfill', 'wp-wordcount'), 'version' => '3f58ab9034899090d86de6e6258b7f2f'); |
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,60 @@ | ||
/** | ||
* WordPress Dependencies | ||
*/ | ||
const { addFilter } = wp.hooks; | ||
const { createBlock } = wp.blocks; | ||
|
||
const allowedBlocks = [ 'core/cover' ]; | ||
|
||
/** | ||
* Filters registered block settings, extending attributes with anchor using ID | ||
* of the first node. | ||
* | ||
* @param {Object} settings Original block settings. | ||
* | ||
* @return {Object} Filtered block settings. | ||
*/ | ||
function addTransforms( settings ) { | ||
if ( typeof settings.transforms !== 'undefined' !== 'undefined' && allowedBlocks.includes( settings.name ) ) { | ||
const transforms = settings.transforms; | ||
|
||
if ( typeof transforms.from === 'undefined' ) { | ||
transforms.from = []; | ||
} | ||
|
||
const convertToCovert = { | ||
type: 'block', | ||
blocks: [ 'core/group' ], | ||
__experimentalConvert( block ) { | ||
if ( typeof block.attributes.backgroundColor !== 'undefined' ) { | ||
block.attributes.overlayColor = block.attributes.backgroundColor; | ||
} | ||
if ( typeof block.attributes.customBackgroundColor !== 'undefined' ) { | ||
block.attributes.customOverlayColor = block.attributes.customBackgroundColor; | ||
} | ||
|
||
// Failing to create new block references causes the original blocks | ||
// to be replaced in the switchToBlockType call thereby meaning they | ||
// are removed both from their original location and within the | ||
// new cover block. | ||
const groupInnerBlocks = block.innerBlocks.map( ( innerBlock ) => { | ||
return createBlock( innerBlock.name, innerBlock.attributes, innerBlock.innerBlocks ); | ||
} ); | ||
|
||
return createBlock( 'core/cover', block.attributes, groupInnerBlocks ); | ||
}, | ||
}; | ||
|
||
transforms.from.push( convertToCovert ); | ||
|
||
settings.transforms = transforms; | ||
} | ||
|
||
return settings; | ||
} | ||
|
||
addFilter( | ||
'blocks.registerBlockType', | ||
'editorskit/transform/group', | ||
addTransforms | ||
); |
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,4 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import './group'; |