-
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
Block API: Normalize function arguments #10891
Conversation
92bc4c6
to
529d985
Compare
I generally prefer using an object with named arguments if there is more than one argument. E.g. for createBlock( {
name,
attributes = {},
innerBlocks = [],
} ) The benefits are that the arguments are clearly named, the order doesn't matter, and you can easily omit optional arguments. |
My main concern is consistency in argument ordering of the existing functions, and in how they can be called. These are flagrantly inconsistent from other functions, for example:
On the point of The ability to provide as an argument the type object implies that one might be able to call it a definition which is not actually registered. We should determine whether that's something we want, and if it even works at all.
I think there are pros and cons. I don't know that this is something we really want to change at this point though. For posterity's sake, the original implementation may have been partly inspired by React's interface for |
529d985
to
f7f7af6
Compare
packages/blocks/CHANGELOG.md
Outdated
|
||
### New feature | ||
|
||
- `isBlockContentValid` function has been added ([#10891](https://github.com/WordPress/gutenberg/pull/10891)). |
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.
Nit: We have some precedent of the adjective coming first in boolean "is" functions, e.g. isReusableBlock
, so for consistency's sake, isValidBlockContent
might be the more consistent ordering.
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.
I see also isValidElement
, let's rename. 👍
@@ -174,10 +174,10 @@ export function unregisterBlockType( name ) { | |||
/** | |||
* Assigns name of block for handling non-block content. | |||
* | |||
* @param {string} name Block name. |
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.
Why should we update this, but not unregisterBlockType( name )
-> unregisterBlockType( blockName )
. What's the motivation for this change?
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.
I will revert. It's obvious here what the name is. Other examples where I changed bring more clarity as the function name isn't as specific. e.g.: setFreeformContentHandlerName( blockName )
.
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.
I didn't understand your question properly, but I think I answered it anyway :)
It's obvious here what the
name
is.
name
is also used in registerBlockType
as the first param - so I wanted to have it consistent. In general I expanded to blockName
in cases where the funcion name doesn't say anything about the block.
packages/blocks/CHANGELOG.md
Outdated
|
||
### Deprecation | ||
|
||
- `isValidBlock` function has been deprecated ([#10891](https://github.com/WordPress/gutenberg/pull/10891)). Use `isBlockContentValid` instead. |
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.
Maybe a hint to the changed argument order?
export function isValidBlock( innerHTML, blockType, attributes ) { | ||
deprecated( 'isValidBlock', { | ||
plugin: 'Gutenberg', | ||
version: '4.3', |
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.
4.4 now?
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.
Yes, true.
0c92289
to
670d647
Compare
Description
Closes #5227.
Based on proposal from @matias:
I did the following refactorings:
isValidBlock( innerHTML, blockType, attributes )
renamed toisBlockContentValid( blockType, attributes, innerHTML )
.createBlock( name, blockAttributes = {}, innerBlocks = [] )
becamecreateBlock( name, attributes = {}, innerBlocks = [] )
.getBlockAttributes( blockType, innerHTML, attributes = {} )
- this one is tricky as it often is used with only 2 params. We should discuss if it really makes sense to swap two last params.getCommentAttributes( allAttributes, blockType )
is nowgetCommentAttributes( blockType, attributes )
- this is internal function.getAttributesFromDeprecatedVersion
from the list above no longer exists - no action required.It still differs from @aduth shared which I wanted to follow initially:
However after some time spent exploring the codebase, I find the fact that we use
blockType
very convinient in the context of unit tests. Should we allow using bothstring
andobject
instead to leave the codebase unchanged but offer some more flexibility?How has this been tested?
npm run test
&npm run test-e2e
Types of changes
New deprecation is introduced.
Refactoring.
Checklist: