-
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
Conversation
Adding that the current take is pretty opinionated in that it requires the user to take action. My first stab included a Ignore option. I’m not sure where I stand on honoring this contract between block author and block user. It could be argued that offering transforms + conversion to HTML is a way to let the user keep their content and not restrict them too much. |
I like your approach of giving the user options instead of locking them down with "this is wrong". That's a good UX. And the transformations sound logical, so for me this is a good solution to #4932. |
0e0a75a
to
375dfc1
Compare
375dfc1
to
902f982
Compare
Looks good to me. |
I'm going to merge this. Noting that more of Gutenberg's core validation process could be moved to a hook like this. Until then, I want this atom merged so it can serve as yet another example of usage of hooks. |
@mcsf Ah, noticed a bug with this: when adding the subheading block more than once, it shows the "you cannot use this block more than once" as expected. BUT, you can then transform that block into a paragraph, and it still stays locked, saying "Paragraph can not be used more than once" 😄 |
@hedgefield: interesting. I can't repro, though. Could you perhaps provide a screencast? |
Ah, good catch. I can repro. Yep, you can make an issue, thanks! |
👍 #5471 |
// 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; |
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 for block.uid
?
(Asking as I seek to upgrade all references of uid
to clientId
)
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
to BlockEdit
, but it's a synonym of clientId
.
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
<BlockEdit | |
name={ blockName } | |
isSelected={ isSelected } | |
attributes={ block.attributes } | |
setAttributes={ this.setAttributes } | |
insertBlocksAfter={ isLocked ? undefined : this.insertBlocksAfter } | |
onReplace={ isLocked ? undefined : onReplace } | |
mergeBlocks={ isLocked ? undefined : this.mergeBlocks } | |
id={ block.uid } | |
isSelectionEnabled={ this.props.isSelectionEnabled } | |
toggleSelection={ this.props.toggleSelection } | |
/> |
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 😅
Description
Fixes #4932.
This pull request is one answer to handling scenarios in which multiple instances of a block are found in a post even though the block's type specifies it should only be used once (
useOnce
). The following example should be useful:Edit: The Edit as HTML button has been removed for now, and the More block doesn't offer any transformation (offering "Next Page" was merely conceptual). Instead, a Find original button was added that selects (and moves into view) the one block in the content that is valid for the same block type.
Caveat
blocks.BlockEdit
) and can be revisited.<!--nextpage-->
and may be stitching together a post and realize they have multiple break points that they were representing with<!--more-->
. Creatingblocks/library/next-page
and adding a transform from More would solve this. Edit: more on Pagination below.How Has This Been Tested?
Screenshots (jpeg or gifs if applicable):
Types of changes
Enhancement; extension.
Checklist: