-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Block Directory: Add an "Install" button when a block type is not fou…
…nd (#22631) * Update package-lock.json * Filter the Missing block edit component * Remove debug code * Updated warning styles to be more consistent with other patterns like the block placeholder state * Removed font size declaration - was inconsistent with other patterns particularly because of the use of `em` units for padding * changed padding on the warning and changed margin on the warning message * changed the side the padding was on for warning buttons * Run `parse` to get the correct attributes and innerBlocks Co-authored-by: Michael Arestad <marestad@gmail.com>
- Loading branch information
1 parent
2da8da9
commit d62d455
Showing
7 changed files
with
147 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
66 changes: 66 additions & 0 deletions
66
packages/block-directory/src/plugins/filter-missing/index.js
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,66 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { Button } from '@wordpress/components'; | ||
import { RawHTML } from '@wordpress/element'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { Warning } from '@wordpress/block-editor'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import InstallButton from './install-button'; | ||
|
||
const filterMissing = ( OriginalComponent ) => ( props ) => { | ||
const { originalName, originalUndelimitedContent } = props.attributes; | ||
const { block, hasPermission } = useSelect( | ||
( select ) => { | ||
const { getDownloadableBlocks } = select( 'core/block-directory' ); | ||
const blocks = getDownloadableBlocks( | ||
'block:' + originalName | ||
).filter( ( { name } ) => originalName === name ); | ||
return { | ||
hasPermission: select( 'core' ).canUser( | ||
'read', | ||
'block-directory/search' | ||
), | ||
block: blocks.length && blocks[ 0 ], | ||
}; | ||
}, | ||
[ originalName ] | ||
); | ||
|
||
if ( ! hasPermission || ! block ) { | ||
return <OriginalComponent { ...props } />; | ||
} | ||
|
||
const actions = [ | ||
<InstallButton | ||
key="install" | ||
block={ block } | ||
attributes={ props.attributes } | ||
clientId={ props.clientId } | ||
/>, | ||
<Button key="convert" onClick={ props.convertToHTML } isLink> | ||
{ __( 'Keep as HTML' ) } | ||
</Button>, | ||
]; | ||
|
||
return ( | ||
<> | ||
<Warning actions={ actions }> | ||
{ sprintf( | ||
/* translators: %s: block name */ | ||
__( | ||
'Your site doesn’t include support for the %s block. You can try installing the block, convert it to a Custom HTML block, or remove it entirely.' | ||
), | ||
block.title || originalName | ||
) } | ||
</Warning> | ||
<RawHTML>{ originalUndelimitedContent }</RawHTML> | ||
</> | ||
); | ||
}; | ||
|
||
export default filterMissing; |
49 changes: 49 additions & 0 deletions
49
packages/block-directory/src/plugins/filter-missing/install-button.js
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,49 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
import { Button } from '@wordpress/components'; | ||
import { createBlock, getBlockType, parse } from '@wordpress/blocks'; | ||
import { useSelect, useDispatch } from '@wordpress/data'; | ||
|
||
export default function InstallButton( { attributes, block, clientId } ) { | ||
const isInstallingBlock = useSelect( ( select ) => | ||
select( 'core/block-directory' ).isInstalling( block.id ) | ||
); | ||
const { installBlockType } = useDispatch( 'core/block-directory' ); | ||
const { replaceBlock } = useDispatch( 'core/block-editor' ); | ||
|
||
return ( | ||
<Button | ||
onClick={ () => | ||
installBlockType( block ).then( ( success ) => { | ||
if ( success ) { | ||
const blockType = getBlockType( block.name ); | ||
const [ originalBlock ] = parse( | ||
attributes.originalContent | ||
); | ||
if ( originalBlock ) { | ||
replaceBlock( | ||
clientId, | ||
createBlock( | ||
blockType.name, | ||
originalBlock.attributes, | ||
originalBlock.innerBlocks | ||
) | ||
); | ||
} | ||
} | ||
} ) | ||
} | ||
disabled={ isInstallingBlock } | ||
isBusy={ isInstallingBlock } | ||
isPrimary | ||
> | ||
{ sprintf( | ||
/* translators: %s: block name */ | ||
__( 'Install %s' ), | ||
block.title | ||
) } | ||
</Button> | ||
); | ||
} |
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