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

Improve child blocks API's and UI. #7003

Merged
merged 7 commits into from
May 31, 2018

Conversation

jorgefilipecosta
Copy link
Member

@jorgefilipecosta jorgefilipecosta commented May 29, 2018

Description

This PR extends the blocks API with two new selectors getChildBlocks and hasChildBlocks.
A class is added for inserter items with children and the UI is changed using the new class. Now we have a special design in blocks with children (this design is in progress).
A new area that appears when we are inserting blocks inside blocks with children was created.

*/
export const hasChildBlocks = createSelector(
( state, blockName ) => {
return getChildBlocks( state, blockName ).length > 0;
Copy link
Member

Choose a reason for hiding this comment

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

This selector only wraps another selector. It shouldn’t use another layer of caching.

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch @gziolo, I updated the code!

@jorgefilipecosta jorgefilipecosta force-pushed the add/child-blocks-improvements branch 4 times, most recently from 2fb62dc to cc62ab9 Compare May 29, 2018 17:22
@mtias
Copy link
Member

mtias commented May 29, 2018

Closes #6998.

@gziolo
Copy link
Member

gziolo commented May 29, 2018

One of the unit tests is failing.

@jorgefilipecosta
Copy link
Member Author

Hi @gziolo the test case was fixed.
Thank @mtias for the design updates.

@mtias mtias force-pushed the add/child-blocks-improvements branch from 1339854 to 71239bb Compare May 30, 2018 10:27
@mtias
Copy link
Member

mtias commented May 30, 2018

Pushed some further tweaks to the design:

image

@jasmussen I think the biggest thing missing is addressing the blurry icons :)

@mtias mtias added this to the 3.0 milestone May 30, 2018
@jasmussen
Copy link
Contributor

Nice work. I unblurred the icons :D

@jorgefilipecosta
Copy link
Member Author

I rebased this PR and it seems to be working fine.

<div className="editor-inserter__child-blocks">
<div className="editor-inserter__parent-block-header">
<div className="editor-inserter__parent-block-icon">
<BlockIcon icon={ rootBlockIcon } />
Copy link
Member

Choose a reason for hiding this comment

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

BlockIcon should be rendered only when the icon is set

Copy link
Member Author

Choose a reason for hiding this comment

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

Normally we never do this check because BlockIcon handles it but in this case, we can do it because we have wrapping div for the icon.

@@ -176,6 +184,18 @@ export class InserterMenu extends Component {
/>

<div className="editor-inserter__results">
{ !! childItems.length &&
Copy link
Member

Choose a reason for hiding this comment

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

Can we extract this code to a new component to avoid confusing conditional checks for an icon and title? As far as I understand when there are child blocks then the icon and title must be set, right?

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 if we have childblocks the icon and title are set unless the block did not use a title/item, but that's part of the block responsibility.

Copy link
Member Author

Choose a reason for hiding this comment

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

The child blocks rendering logic was extracted to a separate component.

*
* @return {Array} Array of child block names.
*/
export const getChildBlockNames = createSelector(
Copy link
Member

Choose a reason for hiding this comment

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

This selector seems to be quite important so let’s add unit tests to avoid regressions in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

Unit tests added 👍

const filteredItems = searchItems( items, filterValue );

const childItems = rootChildBlocks ?
Copy link
Member

Choose a reason for hiding this comment

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

rootChildBlocks is always an array, so in JS it’s always truthy.

This makes the search field a bit bigger and nicer, and changes the height to match better the contents.
@jasmussen
Copy link
Contributor

Pushed a little polish.

@@ -0,0 +1,29 @@
/**
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 just be called ChildBlocks

Copy link
Member Author

Choose a reason for hiding this comment

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

Renamed 👍

Copy link
Contributor

@gravityrail gravityrail left a comment

Choose a reason for hiding this comment

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

LGTM! 🎉

@@ -176,6 +182,14 @@ export class InserterMenu extends Component {
/>

<div className="editor-inserter__results">
<ChildBlocks
rootBlockIcon={ rootBlockIcon }
Copy link
Member

Choose a reason for hiding this comment

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

Did you consider to use withSelect directly on ChildBlocks component instead of passing down rootBlockIcon and rootBlockTitle?

import BlockIcon from '../block-icon';

function ChildBlocks( { rootBlockIcon, rootBlockTitle, items, ...props } ) {
if ( ! items || ! items.length ) {
Copy link
Member

Choose a reason for hiding this comment

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

Can we use ifCondition HOC here?

Copy link
Member

@gziolo gziolo left a comment

Choose a reason for hiding this comment

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

Code changes look good, I left 2 more questions, but definitely not blockers. Nice work 👍

* @param {string} blockName Block type name.
*
* @return {Array} Array of child block names.
*/
Copy link
Member

Choose a reason for hiding this comment

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

Not sure a JSDoc parser would detect this as being "attached" to the getChildBlockNames function with the newline between.

Copy link
Member Author

Choose a reason for hiding this comment

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

The line breaks were corrected.

'editor-inserter__item',
getBlockMenuDefaultClassName( item.id ),
{
'editor-inserter__item-with-childs': item.hasChildBlocks,
Copy link
Member

Choose a reason for hiding this comment

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

  • Grammar: "childs" -> "children"
  • This is a modifier, so could be written as one, e.g. has-children (reference)

@@ -1397,6 +1397,7 @@ export const getInserterItems = createSelector(
isDisabled,
utility: calculateUtility( blockType.category, count, isContextual ),
frecency: calculateFrecency( time, count ),
hasChildBlocks: hasChildBlocks( blockType.name ),
Copy link
Member

Choose a reason for hiding this comment

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

How is the cache bust if we proceed to register a block defining blockType.name as a valid parent ?

Copy link
Member Author

Choose a reason for hiding this comment

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

We clean the cache each time state.blockTypes changes, so when we register a block the cache is invalidated.

Copy link
Member

Choose a reason for hiding this comment

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

We clean the cache each time state.blockTypes changes, so when we register a block the cache is invalidated.

But state.blockTypes (from core/blocks store) is not considered when we call getInserterItems (from core/editor store).

Copy link
Member Author

@jorgefilipecosta jorgefilipecosta May 31, 2018

Choose a reason for hiding this comment

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

I understand now the problem, getInserterItems is cached and depends on core/blocks store when core/blocks change the cache may not be updated. I think this is an already existing problem. Before if we register a new block a new item should exist but the cache may not be updated, it is not easy to spot because the cache updates a lot.

Thank you for clarifying this issue I will try to propose a solution to this problem!

@jorgefilipecosta jorgefilipecosta merged commit c93b778 into master May 31, 2018
@jorgefilipecosta jorgefilipecosta deleted the add/child-blocks-improvements branch May 31, 2018 11:16
@jorgefilipecosta
Copy link
Member Author

Thank you @jasmussen and @mtias for the design improvements. Thank you @aduth, @gravityrail, and @gziolo for the reviews!

@jasmussen
Copy link
Contributor

Great work, thank you.

I'm not sure this PR is the cause, but recently I've started seeing duplicate items in the slash command:

screen shot 2018-06-01 at 09 37 11

Simply search for "/image" and you'll see it. Ring any bells?

@aduth
Copy link
Member

aduth commented Jun 6, 2018

@jasmussen Do you still see this on master? I'm not able to reproduce. Can we create a separate issue if it's still present?

@jorgefilipecosta
Copy link
Member Author

Sorry, @jasmussen I missed the comment but like @aduth I'm not being able to reproduce the issue in master or in the last release 3.0.1. Are you still able to reproduce the problem?

@jasmussen
Copy link
Contributor

jasmussen commented Jun 6, 2018

@aduth @jorgefilipecosta Can confirm that I'm not seeing this anymore. I don't know what fixed it, I could swear I saw it for /gallery as soon as yesterday. But I can't repro.

Edit: also seeing it for Cover Image now.

@jasmussen
Copy link
Contributor

@aduth @jorgefilipecosta I now see it in master again. I have zero idea why it reappeared. And I can't reproduce it for all blocks. I now see it for Heading and Gallery:

repeat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants