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 block type function argument #11490

Merged
merged 7 commits into from
Nov 9, 2018

Conversation

gziolo
Copy link
Member

@gziolo gziolo commented Nov 5, 2018

Description

Closes #5227.

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?

We should address this before 5.0 lands. This won't be a breaking change but it should simplify using the following API methods at least:

  • getBlockAttributes
  • getSaveContent
  • getSaveElement

This PR adds new internal helper method normalizeBlockType which makes use of the following methods more convenient for all plugin developers:

  • getBlockAttributes
  • getSaveContent
  • getSaveElement
  • isValidBlockContent

It's going to be possible to pass either block's name as string or block type object as an object as the first param for all of the aforementioned methods. In the majority of cases passing string removes some boilerplate code.

How has this been tested?

All unit and e2e tests still pass.

Types of changes

Refactoring and code enhancement.

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 added [Feature] Block API API that allows to express the block paradigm. [Type] Code Quality Issues or PRs that relate to code quality labels Nov 5, 2018
@gziolo gziolo added this to the 4.3 milestone Nov 5, 2018
@gziolo gziolo self-assigned this Nov 5, 2018
@gziolo gziolo requested review from mtias, aduth and a team November 5, 2018 11:51
@gziolo gziolo changed the title Update/block api normalize Block API: Normalize block type function argument Nov 5, 2018
@aduth
Copy link
Member

aduth commented Nov 5, 2018

Thoughts on?

  • getBlockTransforms( direction, blockName ) -> getBlockTransforms( direction, blockTypeOrName ) (Public API)
    • My opinion: I'd honestly like to see this one changed to reverse its argument getBlockTransforms( blockTypeOrName, direction ) to be more cohesive with other APIs
  • getBlockDefaultClassName( blockName ) -> getBlockDefaultClassName( blockTypeOrName ) (Public API)
    • My opinion: Should probably be converted.
  • getBlockMenuDefaultClassName( blockName ) -> getBlockMenuDefaultClassName( blockTypeOrName ) (Public API)
    • My opinion: Should probably be converted.
  • getCommentDelimitedContent( rawBlockName, attributes, content ) -> getCommentDelimitedContent( blockNameOrType, attributes, content )
    • My opinion: Should be left as-is. Internal to serializer, and functions purpose is to compose the raw pieces.

return getBlockType( blockTypeOrName );
}

return blockTypeOrName;
Copy link
Member

Choose a reason for hiding this comment

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

A general question (not a blocker!) about our public APIs: Should we be validating their inputs more strictly and e.g. throwing an error if a number or boolean is passed here?

My fear is that we may lose some flexibility to change APIs in the future if there are popular plugins out there that are using our APIs incorrectly.

Copy link
Member Author

@gziolo gziolo Nov 7, 2018

Choose a reason for hiding this comment

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

Should we be validating their inputs more strictly and e.g. throwing an error if a number or boolean is passed here?

As bad as it sounds, wouldn't it be a breaking change? :)

In overall, this is something we should pay more attention to in the 2nd phase of the project.

Copy link
Member

Choose a reason for hiding this comment

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

My 2¢ : While laudable, as a general pattern it's hard to anticipate all nonsense inputs, and in setting expectations of tolerance while being unable to deliver (or deliver consistently), we're doing more a disservice than we're helping.

@gziolo
Copy link
Member Author

gziolo commented Nov 7, 2018

I added change for:

  • getBlockTransforms( direction, blockName ) -> getBlockTransforms( direction, blockTypeOrName ) (Public API)

See 896e4b9.

My opinion: I'd honestly like to see this one changed to reverse its argument getBlockTransforms( blockTypeOrName, direction ) to be more cohesive with other APIs

The thing is that blockName is optional in this function. It's often called without blockName which means take all of them.

getBlockDefaultClassName and getBlockMenuDefaultClassName operate only on the name of the block and they use hooks internally which also propagate only class name. I'm not convinced we need to enrich them with block type object. I would skip it.

getCommentDelimitedContent - it's an internal function. We can change it later if we want. However, it feels like it should stay as is because it operates only on a string. We might want to rename it to blockName and update usage of blockName inside the function's body to processedBlockName to make it read better.

// When retrieving transforms for all block types, recurse into self.
if ( blockName === undefined ) {
if ( blockTypeOrName === undefined ) {
return flatMap(
getBlockTypes(),
( { name } ) => getBlockTransforms( direction, name )
Copy link
Member

Choose a reason for hiding this comment

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

This could be partial( getBlockTransforms, direction ). I dunno that I'd care to change it though (given partial's questionable future)

packages/blocks/src/api/parser.js Outdated Show resolved Hide resolved
@gziolo gziolo merged commit a3785cd into master Nov 9, 2018
@gziolo gziolo deleted the update/block-api-normalize branch November 9, 2018 08:11
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