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

Only show transforms to blocks that can be inserted on the root block; Order transforms by frecency; #7184

Merged
merged 1 commit into from Aug 29, 2018

Conversation

Projects
None yet
6 participants
@jorgefilipecosta
Member

jorgefilipecosta commented Jun 6, 2018

Fixes: #6363
Part of a general polishing to get #6993.
Relates to: #7227

This PR makes sure we can only transform to blocks that can be inserted in the root block.
The transformations are also sorted by frecency (a heuristic used by the inserter) so items transformations should appear in the same order as they appear on the inserter.

Test block: https://gist.github.com/jorgefilipecosta/b958239761a24664685d5efc7ab48fa6
(can be pasted in the browser console)

How has this been tested?

Use a block that sets allowed blocks in the nested area (test block). Verify it is only possible to transform blocks to the allowed blocks (list and quote).
Test both single and multi-block transforms and verify the restriction applies to transforms rendered on the block toolbar and to transforms presented on the side menu.
Verify that if the allowed blocks restriction is applied via PHP filters, the transforms also respect it.
e.g:

add_filter( 'allowed_block_types', function( $allowed_block_types, $post ) {
	if ( $post->post_type === 'post' ) {
	    return $allowed_block_types;
	}
	return [ 'core/paragraph' ];
}, 10, 2 );

Verify the order blocks appear on the transformations list is the same as the order they appear on the inserted.

@jorgefilipecosta jorgefilipecosta self-assigned this Jun 6, 2018

@jorgefilipecosta jorgefilipecosta changed the title from Allow only to transform to blocks that can be inserted on the root block; Order transforms by frecency; to Only show transforms to blocks that can be inserted on the root block; Order transforms by frecency; Jun 12, 2018

@jorgefilipecosta jorgefilipecosta requested a review from WordPress/gutenberg-core Jun 18, 2018

@jorgefilipecosta

This comment has been minimized.

Show comment
Hide comment
@jorgefilipecosta

jorgefilipecosta Jun 28, 2018

Member

This PR was rebased, to contain the last changes to the transforms mechanism. It is ready for a look.

Member

jorgefilipecosta commented Jun 28, 2018

This PR was rebased, to contain the last changes to the transforms mechanism. It is ready for a look.

@ZebulanStanphill

This comment has been minimized.

Show comment
Hide comment
@ZebulanStanphill

ZebulanStanphill Aug 25, 2018

Contributor

Any reason why this has not been merged yet?

Contributor

ZebulanStanphill commented Aug 25, 2018

Any reason why this has not been merged yet?

@karmatosed

This comment has been minimized.

Show comment
Hide comment
@karmatosed

karmatosed Aug 25, 2018

Member

@ZebulanStanphill right now this is waiting for a code review. Let's get it a milestone and see if we can get some 👀 on this. Thanks for championing it.

Member

karmatosed commented Aug 25, 2018

@ZebulanStanphill right now this is waiting for a code review. Let's get it a milestone and see if we can get some 👀 on this. Thanks for championing it.

@karmatosed karmatosed added this to the 3.7 milestone Aug 25, 2018

@noisysocks

This is looking good! Love that we're re-using the logic that's already in the selectors. Thanks @jorgefilipecosta.

Show outdated Hide outdated packages/editor/src/components/block-switcher/index.js
Show outdated Hide outdated packages/editor/src/components/block-switcher/index.js
( clientId ) => !! getTemplateLock( getBlockRootClientId( clientId ) )
),
blocks: getBlocksByClientId( clientIds ),
inserterItems: getInserterItems( rootClientId ),

This comment has been minimized.

@noisysocks

noisysocks Aug 27, 2018

Member

Have you looked into using canInsertBlockType instead of getInserterItems? The latter takes reusable blocks into account which is unnecessary in this case because you can't transform a block into an existing reusable block. It's better that we do as little work as possible to keep the UI feeling responsive.

@noisysocks

noisysocks Aug 27, 2018

Member

Have you looked into using canInsertBlockType instead of getInserterItems? The latter takes reusable blocks into account which is unnecessary in this case because you can't transform a block into an existing reusable block. It's better that we do as little work as possible to keep the UI feeling responsive.

This comment has been minimized.

@jorgefilipecosta

jorgefilipecosta Aug 27, 2018

Member

I have thought about this and it seemed getInserterItems was the best option.
We don't want just to know if a block can be inserted we want to know all the blocks that can be inserted so we can render the UI. We can not do a simple filter using canInsertBlockType inline because that would create a new array reference each time triggering unnecessary rerenders. So the alternative would be to create a new selector that just returns all the allowed blocks without the shared blocks. Besides the available blocks, we also need the frecency ( to sort the items ) and getInserterItems already provides it.
getInserterItems is cached and used in the inserter which is rerendered each time a block changes so I think each time we use the selector here we are just retrieving the value from the cache. If we had a specific selector we would not be able to share the cache.

@jorgefilipecosta

jorgefilipecosta Aug 27, 2018

Member

I have thought about this and it seemed getInserterItems was the best option.
We don't want just to know if a block can be inserted we want to know all the blocks that can be inserted so we can render the UI. We can not do a simple filter using canInsertBlockType inline because that would create a new array reference each time triggering unnecessary rerenders. So the alternative would be to create a new selector that just returns all the allowed blocks without the shared blocks. Besides the available blocks, we also need the frecency ( to sort the items ) and getInserterItems already provides it.
getInserterItems is cached and used in the inserter which is rerendered each time a block changes so I think each time we use the selector here we are just retrieving the value from the cache. If we had a specific selector we would not be able to share the cache.

This comment has been minimized.

@noisysocks

noisysocks Aug 28, 2018

Member

Makes sense 👍 – thanks for explaining!

@noisysocks

noisysocks Aug 28, 2018

Member

Makes sense 👍 – thanks for explaining!

@noisysocks

Hell yeah 🤙

@jorgefilipecosta jorgefilipecosta merged commit 7462c63 into master Aug 29, 2018

0 of 2 checks passed

codecov/project CI failed.
Details
continuous-integration/travis-ci/pr The Travis CI build failed
Details

@jorgefilipecosta jorgefilipecosta deleted the update/only-transform-to-blocks-that-can-be-inserted-on-root branch Aug 29, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment