-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a "reinstall" button when a block type is not found #22631
Merged
+147
−19
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
167d48e
Block Directory: Add a "reinstall" button when a block type is not found
ryelle d1ad354
Update package-lock.json
ryelle 9c384e8
Filter the Missing block edit component
ryelle 8cabe8c
Remove debug code
ryelle 86cc1e0
Updated warning styles to be more consistent with other patterns like…
MichaelArestad 2413b8c
Run `parse` to get the correct attributes and innerBlocks
ryelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having padding set as an
em
unit could lead to some odd outcomes should the block be broken inside of a parent block. It will inherit its font size. May be better to userem
units.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is an open PR to switch to em units and the block Placeholder state already uses em units. That's the reason for the change. Might as well be consistent.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the heads up....
I wonder if that PR is considering third party blocks. This may get interesting. 🍿
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That PR looks like it was halting for further testing. That said, this will still be more consistent with the placeholder state for other blocks.