Skip to content

Commit

Permalink
Properly clone blocks with the Add Card and Duplicate Card controls i…
Browse files Browse the repository at this point in the history
…n supernova blocks - #404
  • Loading branch information
razwan committed Jul 6, 2022
1 parent 402dc7c commit 825fab2
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { __ } from "@wordpress/i18n";
import { createBlock } from "@wordpress/blocks";
import { cloneBlock } from "@wordpress/blocks";
import { useDispatch, useSelect } from "@wordpress/data";
import { useCallback } from "@wordpress/element";
import { BlockControls } from "@wordpress/block-editor";
Expand All @@ -10,6 +10,7 @@ import { useInnerBlocks } from "@novablocks/block-editor";
const SupernovaItemBlockControls = ( props ) => {
const { clientId, attributes } = props;
const { replaceInnerBlocks } = useDispatch( 'core/block-editor' );
const block = useSelect( select => select( 'core/block-editor' ).getBlock( clientId ), [ clientId ] );

const parentClientId = useSelect( select => {
const parents = select( 'core/block-editor' ).getBlockParents( clientId ).slice();
Expand All @@ -27,7 +28,7 @@ const SupernovaItemBlockControls = ( props ) => {
const addNewCard = useCallback( () => {
const newInnerBlocks = parentInnerBlocks.slice();
const index = newInnerBlocks.findIndex( block => block.clientId === clientId );
const newBlock = createBlock( 'novablocks/supernova-item', attributes, innerBlocks );
const newBlock = cloneBlock( block );
newInnerBlocks.splice( index + 1, 0, newBlock );
replaceInnerBlocks( parentClientId, newInnerBlocks );
}, [ clientId, innerBlocks, parentClientId, attributes ] );
Expand Down
8 changes: 3 additions & 5 deletions packages/block-library/src/blocks/supernova/block-controls.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { __ } from '@wordpress/i18n';
import { createBlock } from '@wordpress/blocks';
import { cloneBlock, createBlock } from '@wordpress/blocks';
import { Fragment, useCallback, useMemo } from '@wordpress/element';
import { addCard, gallery, plus } from '@wordpress/icons';

Expand Down Expand Up @@ -49,7 +49,7 @@ const QueryControls = ( props ) => {

return (
<BlockControls>
<ToolbarGroup label={ __( 'Altceva', '__plugin_txtd' ) }>
<ToolbarGroup label={ __( 'Cards', '__plugin_txtd' ) }>
<ToolbarButton onClick={ () => { setAttributes( { postsToShow: postsToShow + 1 } ); } }>
{ __( 'Add Cards', '__plugin_txtd' ) }
</ToolbarButton>
Expand Down Expand Up @@ -96,9 +96,7 @@ const InnerBlocksControls = ( props ) => {
const addNewCard = useCallback( () => {
const newInnerBlocks = innerBlocks.slice();
const lastBlock = innerBlocks[ innerBlocks.length - 1 ];
const newBlockAttributes = lastBlock.attributes;
const newBlockInnerBlocks = lastBlock.innerBlocks;
const newBlock = createBlock( 'novablocks/supernova-item', newBlockAttributes, newBlockInnerBlocks );
const newBlock = cloneBlock( lastBlock );
newInnerBlocks.push( newBlock );
replaceInnerBlocks( clientId, newInnerBlocks );
}, [ innerBlocks, postsToShow ] );
Expand Down

0 comments on commit 825fab2

Please sign in to comment.