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

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

Conversation

jorgefilipecosta
Copy link
Member

@jorgefilipecosta 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 force-pushed the update/only-transform-to-blocks-that-can-be-inserted-on-root branch 2 times, most recently from 33a2421 to 2ae5195 Compare June 11, 2018 10:20
@jorgefilipecosta jorgefilipecosta changed the title Allow only to transform to blocks that can be inserted on the root block; Order transforms by frecency; 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 a team June 18, 2018 10:04
@jorgefilipecosta jorgefilipecosta force-pushed the update/only-transform-to-blocks-that-can-be-inserted-on-root branch from 2ae5195 to 794d6ca Compare June 28, 2018 09:38
@jorgefilipecosta
Copy link
Member Author

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

@jorgefilipecosta jorgefilipecosta added this to the 3.2 milestone Jun 28, 2018
@jorgefilipecosta jorgefilipecosta modified the milestones: 3.2, 3.3 Jul 5, 2018
@jorgefilipecosta jorgefilipecosta added [Status] In Progress Tracking issues with work in progress [Type] Bug An existing feature does not function as intended labels Jul 10, 2018
@jorgefilipecosta jorgefilipecosta modified the milestones: 3.3, 3.4 Jul 19, 2018
@pento pento modified the milestones: 3.4, 3.5 Jul 30, 2018
@jorgefilipecosta jorgefilipecosta force-pushed the update/only-transform-to-blocks-that-can-be-inserted-on-root branch from 794d6ca to d750345 Compare August 1, 2018 11:28
@jorgefilipecosta jorgefilipecosta removed the [Status] In Progress Tracking issues with work in progress label Aug 1, 2018
@jorgefilipecosta jorgefilipecosta removed this from the 3.5 milestone Aug 2, 2018
@ZebulanStanphill
Copy link
Member

Any reason why this has not been merged yet?

@karmatosed
Copy link
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.

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

@noisysocks noisysocks left a comment

Choose a reason for hiding this comment

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

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

getPossibleBlockTransformations( blocks ),
( block ) => !! itemsByName[ block.name ]
),
( block ) => -itemsByName[ block.name ].frecency,
Copy link
Member

Choose a reason for hiding this comment

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

We can make it a little more explicit that this is a reverse sort by using orderBy.

const possibleBlockTransformations = orderBy(
	...,
	( block ) => itemsByName[ block.name ].frecency,
	'desc'
);

const sourceBlockName = blocks[ 0 ].name;
const blockType = getBlockType( sourceBlockName );
const hasStyles = blocks.length === 1 && get( blockType, [ 'styles' ], [] ).length !== 0;

if ( ! hasStyles && ( ! allowedBlocks.length || isLocked ) ) {
if ( ! hasStyles && ( ! possibleBlockTransformations.length ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

The inner parens () are no longer necessary here.

if ( ! hasStyles && ! possibleBlockTransformations.length ) {

( clientId ) => !! getTemplateLock( getBlockRootClientId( clientId ) )
),
blocks: getBlocksByClientId( clientIds ),
inserterItems: getInserterItems( rootClientId ),
Copy link
Member

Choose a reason for hiding this comment

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

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.

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

Copy link
Member

Choose a reason for hiding this comment

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

Makes sense 👍 – thanks for explaining!

@jorgefilipecosta jorgefilipecosta force-pushed the update/only-transform-to-blocks-that-can-be-inserted-on-root branch from d750345 to 64a1bfe Compare August 27, 2018 12:31
Copy link
Member

@noisysocks noisysocks left a comment

Choose a reason for hiding this comment

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

Hell yeah 🤙

@@ -23,6 +23,9 @@ import { BlockControls, BlockAlignmentToolbar } from '../components';
* @return {Object} Filtered block settings
*/
export function addAttribute( settings ) {
if ( has( settings, [ 'attributes', 'align' ] ) ) {
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 add a test case for this, and ideally an inline code comment explaining its purpose?

Copy link
Member

Choose a reason for hiding this comment

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

Do we expect settings to always be non-undefined?

  • If yes, we don't need to incur Lodash's extra nullish check on settings.attributes, and instead express this as has( settings.attributes, [ 'align' ] )
  • If no, then we have issues below where we assign a new settings.attributes on what we're arguing to be a potentially undefined settings object

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi @aduth thank you for catching this. This change is in no way related to this PR. I noticed that during the most recent rebase a commit of other branch was added here (I probably missed some command) the commit was removed from here. But I will apply the feedback on the correct place #9338.

@jorgefilipecosta jorgefilipecosta force-pushed the update/only-transform-to-blocks-that-can-be-inserted-on-root branch 2 times, most recently from 8835c93 to 3ec9d09 Compare August 28, 2018 15:04
@jorgefilipecosta jorgefilipecosta merged commit 7462c63 into master Aug 29, 2018
@jorgefilipecosta jorgefilipecosta deleted the update/only-transform-to-blocks-that-can-be-inserted-on-root branch August 29, 2018 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants