Skip to content

Commit

Permalink
fix: query all blocks including innerblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
dkjensen committed Jul 11, 2023
1 parent 63c5982 commit 0ac0a83
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 11 deletions.
31 changes: 26 additions & 5 deletions src/advanced-controls/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,32 @@ const withAdvancedControls = createHigherOrderComponent( ( BlockEdit ) => {
const { name, attributes, setAttributes } = props;

const modals = useSelect( ( select ) => {
return select( blockEditorStore )
.getBlocks()
.filter(
( block ) => block.name === 'cloudcatch/light-modal-block'
);
const data = [];
const blocks = select( blockEditorStore ).getBlocks();

const searchNestedBlocks = ( block ) => {
if ( block?.innerBlocks ) {
block.innerBlocks.forEach( ( innerBlock ) => {
if (
innerBlock.name === 'cloudcatch/light-modal-block'
) {
data.push( innerBlock );
}

searchNestedBlocks( innerBlock );
} );
}
};

blocks.forEach( ( block ) => {
if ( block.name === 'cloudcatch/light-modal-block' ) {
data.push( block );
}

searchNestedBlocks( block );
} );

return data;
} );

const selectedModal = modals.filter(
Expand Down
31 changes: 25 additions & 6 deletions src/sidebar/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,36 @@ import {
CardBody,
__experimentalHeading as Heading,
} from '@wordpress/components';
import { edit, trash } from '@wordpress/icons';
import { edit, search, trash } from '@wordpress/icons';

import { modalIcon as icon } from '../icon';

export default function PluginSidebarTest() {
const modals = useSelect( ( select ) => {
return select( blockEditorStore )
.getBlocks()
.filter(
( block ) => block.name === 'cloudcatch/light-modal-block'
);
const data = [];
const blocks = select( blockEditorStore ).getBlocks();

const searchNestedBlocks = ( block ) => {
if ( block?.innerBlocks ) {
block.innerBlocks.forEach( ( innerBlock ) => {
if ( innerBlock.name === 'cloudcatch/light-modal-block' ) {
data.push( innerBlock );
}

searchNestedBlocks( innerBlock );
} );
}
};

blocks.forEach( ( block ) => {
if ( block.name === 'cloudcatch/light-modal-block' ) {
data.push( block );
}

searchNestedBlocks( block );
} );

return data;
} );

const { selectBlock, removeBlock } = useDispatch( blockEditorStore );
Expand Down

0 comments on commit 0ac0a83

Please sign in to comment.