Skip to content

Commit

Permalink
reorganize isPossibleTransformForSource
Browse files Browse the repository at this point in the history
  • Loading branch information
jsnajdr committed Aug 9, 2023
1 parent 74b12d8 commit bd140ba
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions packages/blocks/src/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,45 +154,41 @@ export function cloneBlock( block, mergeAttributes = {}, newInnerBlocks ) {
* @return {boolean} Is the transform possible?
*/
const isPossibleTransformForSource = ( transform, direction, blocks ) => {
// Only consider 'block' type transforms as valid.
if ( transform.type !== 'block' ) {
return false;
}

if ( ! blocks.length ) {
return false;
}

// If multiple blocks are selected, only multi block transforms
// or wildcard transforms are allowed.
const isMultiBlock = blocks.length > 1;
const firstBlockName = blocks[ 0 ].name;
const isValidForMultiBlocks =
isWildcardBlockTransform( transform ) ||
! isMultiBlock ||
transform.isMultiBlock;
if ( ! isValidForMultiBlocks ) {
const isWildcardTransform = isWildcardBlockTransform( transform );
if ( isMultiBlock && ! isWildcardTransform && ! transform.isMultiBlock ) {
return false;
}

// Check non-wildcard transforms to ensure that transform is valid
// for a block selection of multiple blocks of different types.
const firstBlockName = blocks[ 0 ].name;
if (
! isWildcardBlockTransform( transform ) &&
! isWildcardTransform &&
! blocks.every( ( block ) => block.name === firstBlockName )
) {
return false;
}

// Only consider 'block' type transforms as valid.
const isBlockType = transform.type === 'block';
if ( ! isBlockType ) {
return false;
}

// Check if the transform's block name matches the source block (or is a wildcard)
// only if this is a transform 'from'.
const sourceBlock = blocks[ 0 ];
const hasMatchingName =
direction !== 'from' ||
transform.blocks.indexOf( sourceBlock.name ) !== -1 ||
isWildcardBlockTransform( transform );
if ( ! hasMatchingName ) {
if (
direction === 'from' &&
! isWildcardTransform &&
! transform.blocks.includes( sourceBlock.name )
) {
return false;
}

Expand Down

0 comments on commit bd140ba

Please sign in to comment.