-
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 hook to validate useOnce
blocks
#5031
Merged
Merged
Changes from all commits
Commits
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
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ | |
* Internal dependencies | ||
*/ | ||
import './more-menu'; | ||
import './validate-use-once'; |
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,123 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { find } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createBlock, getBlockType } from '@wordpress/blocks'; | ||
import { Button } from '@wordpress/components'; | ||
import { withSelect, withDispatch } from '@wordpress/data'; | ||
import { Warning } from '@wordpress/editor'; | ||
import { compose, getWrapperDisplayName } from '@wordpress/element'; | ||
import { addFilter } from '@wordpress/hooks'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
const enhance = compose( | ||
/* | ||
* For blocks whose block type defines `useOnce`, provides the wrapped | ||
* component with `originalBlockUid` -- a reference to the first block of | ||
* the same type in the content -- if and only if that "original" block is | ||
* not the current one. Thus, an inexisting `originalBlockUid` prop signals | ||
* that the block is valid. | ||
* | ||
* @param {Component} WrappedBlockEdit A filtered BlockEdit instance. | ||
* @return {Component} Enhanced component with merged state | ||
* data props. | ||
*/ | ||
withSelect( ( select, block ) => { | ||
const blocks = select( 'core/editor' ).getBlocks(); | ||
const { useOnce } = getBlockType( block.name ); | ||
|
||
// For block types with no `useOnce` restriction, there is no "original | ||
// block" to be found in the content, as the block itself is valid. | ||
if ( ! useOnce ) { | ||
return {}; | ||
} | ||
|
||
// Otherwise, only pass `originalBlockUid` if it refers to a different | ||
// block from the current one. | ||
const firstOfSameType = find( blocks, ( { name } ) => block.name === name ); | ||
const isInvalid = firstOfSameType && firstOfSameType.uid !== block.id; | ||
return { | ||
originalBlockUid: isInvalid && firstOfSameType.uid, | ||
}; | ||
} ), | ||
withDispatch( ( dispatch, { originalBlockUid } ) => ( { | ||
selectFirst: () => dispatch( 'core/editor' ).selectBlock( originalBlockUid ), | ||
} ) ), | ||
); | ||
|
||
function withUseOnceValidation( BlockEdit ) { | ||
const WrappedBlockEdit = ( { | ||
originalBlockUid, | ||
selectFirst, | ||
...props | ||
} ) => { | ||
if ( ! originalBlockUid ) { | ||
return <BlockEdit { ...props } />; | ||
} | ||
|
||
const blockType = getBlockType( props.name ); | ||
const outboundType = getOutboundType( blockType ); | ||
|
||
return [ | ||
<div key="invalid-preview" style={ { minHeight: '100px' } }> | ||
<BlockEdit key="block-edit" { ...props } /> | ||
</div>, | ||
<Warning key="use-once-warning"> | ||
<p> | ||
<strong>{ blockType.title }: </strong> | ||
{ __( 'This block may not be used more than once.' ) }</p> | ||
<p> | ||
<Button isLarge onClick={ | ||
selectFirst | ||
}>{ __( 'Find original' ) }</Button> | ||
<Button isLarge onClick={ | ||
() => props.onReplace( [] ) | ||
}>{ __( 'Remove' ) }</Button> | ||
{ outboundType && | ||
<Button isLarge onClick={ () => props.onReplace( | ||
createBlock( outboundType.name, props.attributes ) | ||
) }> | ||
{ __( 'Transform into:' ) }{ ' ' } | ||
{ outboundType.title } | ||
</Button> | ||
} | ||
</p> | ||
</Warning>, | ||
]; | ||
}; | ||
|
||
WrappedBlockEdit.displayName = getWrapperDisplayName( BlockEdit, 'useOnceValidation' ); | ||
|
||
return enhance( WrappedBlockEdit ); | ||
} | ||
|
||
/** | ||
* Given a base block type, returns the default block type to which to offer | ||
* transforms. | ||
* | ||
* @param {Object} blockType Base block type. | ||
* @return {?Object} The chosen default block type. | ||
*/ | ||
function getOutboundType( blockType ) { | ||
// Grab the first outbound transform | ||
const { to = [] } = blockType.transforms || {}; | ||
const transform = find( to, ( { type, blocks } ) => | ||
type === 'block' && blocks.length === 1 // What about when .length > 1? | ||
); | ||
|
||
if ( ! transform ) { | ||
return null; | ||
} | ||
|
||
return getBlockType( transform.blocks[ 0 ] ); | ||
} | ||
|
||
addFilter( | ||
'blocks.BlockEdit', | ||
'core/validation/useOnce', | ||
withUseOnceValidation | ||
); |
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.
Is this actually doing anything? Where is
block.id
assigned for this? Is this a typo forblock.uid
?(Asking as I seek to upgrade all references of
uid
toclientId
)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.
It's passed from
BlockListBlock
toBlockEdit
, but it's a synonym ofclientId
.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.
gutenberg/editor/components/block-list/block.js
Lines 633 to 644 in 07dbbb0
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.
Another reason #7990 was desperately needed 😅