Skip to content
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

Merged
merged 3 commits into from
Oct 29, 2018
Merged

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented Oct 22, 2018

Description

Closes #5227.

Based on proposal from @matias:

We have several functions that take arguments like blockType, attributes, innerHtml, but the order is not always consistent.

Since this is an often repeated pattern, we should consolidate usage.

Examples:

isValidBlock — innerHTML, blockType, attributes.
getBlockAttributes — blockType, innerHTML, attributes.
getAttributesFromDeprecatedVersion — blockType, innerHTML, attributes.
createBlock — name, attributes, innerBlocks.
getSaveContent — blockType, attributes.

I did the following refactorings:

  • isValidBlock( innerHTML, blockType, attributes ) renamed to isBlockContentValid( blockType, attributes, innerHTML ).
  • createBlock( name, blockAttributes = {}, innerBlocks = [] ) became createBlock( name, attributes = {}, innerBlocks = [] ).
  • Added default value to the last param in 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 now getCommentAttributes( blockType, attributes ) - this is internal function.
  • getAttributesFromDeprecatedVersion from the list above no longer exists - no action required.
  • Did some code cleanup whenever I discovered something suspicious.

It still differs from @aduth shared which I wanted to follow initially:

isValidBlock( name, attributes, innerHTML )
getBlockAttributes( name, attributes, innerHTML )
getAttributesFromDeprecatedVersion( name, attributes, innerHTML )
createBlock( name, attributes, innerBlocks )
getSaveContent( name, attributes, innerBlocks )

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 both string and object 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:

  • My code is tested.
  • My code follows the WordPress code style.
  • My code follows the accessibility standards.
  • My code has proper inline documentation.

@gziolo gziolo requested review from aduth and mtias October 22, 2018 10:22
@gziolo gziolo self-assigned this Oct 22, 2018
@gziolo gziolo requested a review from a team October 22, 2018 10:25
@gziolo gziolo force-pushed the update/block-api-cleanup branch from 92bc4c6 to 529d985 Compare October 22, 2018 10:31
@ellatrix
Copy link
Member

I generally prefer using an object with named arguments if there is more than one argument. E.g. for createBlock it would be:

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.

@gziolo gziolo added this to the 4.2 milestone Oct 22, 2018
@gziolo gziolo added [Type] Code Quality Issues or PRs that relate to code quality [Feature] Block API API that allows to express the block paradigm. labels Oct 22, 2018
@aduth
Copy link
Member

aduth commented Oct 22, 2018

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:

isValidBlock — innerHTML, blockType, attributes
getAttributesFromDeprecatedVersion — blockType, innerHTML, attributes

On the point of name vs. type as an argument; I'd actually be okay with allowing the convenience here of specifying one or the other, as long as it's supported consistently across all functions. Maybe we need an internal function to normalize?

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 generally prefer using an object with named arguments if there is more than one argument.

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 createElement.

@gziolo gziolo force-pushed the update/block-api-cleanup branch from 529d985 to f7f7af6 Compare October 29, 2018 16:39

### New feature

- `isBlockContentValid` function has been added ([#10891](https://github.com/WordPress/gutenberg/pull/10891)).
Copy link
Member

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.

Copy link
Member Author

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.
Copy link
Member

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?

Copy link
Member Author

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 ).

Copy link
Member Author

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.


### Deprecation

- `isValidBlock` function has been deprecated ([#10891](https://github.com/WordPress/gutenberg/pull/10891)). Use `isBlockContentValid` instead.
Copy link
Member

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',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4.4 now?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, true.

@gziolo gziolo force-pushed the update/block-api-cleanup branch from 0c92289 to 670d647 Compare October 29, 2018 18:34
@gziolo gziolo merged commit 0e2fa13 into master Oct 29, 2018
@gziolo gziolo deleted the update/block-api-cleanup branch October 29, 2018 19:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Block API API that allows to express the block paradigm. [Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants