Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { noop } from 'lodash';
*/
import {
getBlockMenuDefaultClassName,
getBlockType,
unregisterBlockType,
} from '@wordpress/blocks';
import { withDispatch } from '@wordpress/data';
import { compose } from '@wordpress/compose';
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';

/**
* Internal dependencies
Expand Down Expand Up @@ -59,13 +60,16 @@ function DownloadableBlocksList( {
}

export default compose(
withDispatch( ( dispatch, props ) => {
withDispatch( ( dispatch, props, { select } ) => {
const { installBlock, downloadBlock } = dispatch(
'core/block-directory'
);
const { createErrorNotice, removeNotice } = dispatch( 'core/notices' );
const { removeBlocks } = dispatch( 'core/block-editor' );
const { onSelect } = props;
const { canInsertBlockType, getBlockName } = select(
'core/block-editor'
);
const { onSelect, rootClientId } = props;

return {
downloadAndInstallBlock: ( item ) => {
Expand All @@ -89,6 +93,34 @@ export default compose(
};

const onSuccess = () => {
const parent = rootClientId
? getBlockType( getBlockName( rootClientId ) )
: false;
if ( ! canInsertBlockType( item.name, rootClientId ) ) {
const message = ! parent
? sprintf(
/* translators: %1$s: block to be added. */
__(
'We can’t insert a %1$s block into this post. Select another block.'
),
item.title
)
: sprintf(
/* translators: %1$s: block to be added. %2$s: parent block. */
__(
'We can’t insert a %1$s block into the %2$s. Select another block.'
),
item.title,
parent.title
);
createErrorNotice( message, {
id: INSTALL_ERROR_NOTICE_ID,
} );
unregisterBlockType( item.name );
return;
}

// Insert block.
const createdBlock = onSelect( item );

const onInstallBlockError = () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ function DownloadableBlocksPanel( {
isLoading,
isWaiting,
debouncedSpeak,
rootClientId,
} ) {
if ( ! hasPermission ) {
debouncedSpeak(
Expand Down Expand Up @@ -76,6 +77,7 @@ function DownloadableBlocksPanel( {
items={ downloadableItems }
onSelect={ onSelect }
onHover={ onHover }
rootClientId={ rootClientId }
/>
</Fragment>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,13 @@ function InserterMenuDownloadableBlocksPanel() {

return (
<__experimentalInserterMenuExtension>
{ ( { onSelect, onHover, filterValue, hasItems } ) => {
{ ( {
onSelect,
onHover,
filterValue,
hasItems,
rootClientId,
} ) => {
if ( hasItems || ! filterValue ) {
return null;
}
Expand All @@ -36,6 +42,7 @@ function InserterMenuDownloadableBlocksPanel() {
onHover={ onHover }
filterValue={ debouncedFilterValue }
isWaiting={ filterValue !== debouncedFilterValue }
rootClientId={ rootClientId }
/>
);
} }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ function InserterBlockList( {
onHover,
filterValue,
hasItems,
rootClientId,
} }
>
{ ( fills ) => {
Expand Down