Skip to content

Commit

Permalink
Quote v2: allow to unwrap a quote
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix authored and oandregal committed Mar 24, 2022
1 parent 0562350 commit bb4a8f1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
20 changes: 17 additions & 3 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
import { Platform } from '@wordpress/element';
import { applyFilters } from '@wordpress/hooks';
import { symbol } from '@wordpress/icons';
import { __ } from '@wordpress/i18n';

/**
* A block selection object.
Expand Down Expand Up @@ -1767,6 +1768,7 @@ export const getInserterItems = createSelector(
*/
export const getBlockTransformItems = createSelector(
( state, blocks, rootClientId = null ) => {
const [ sourceBlock ] = blocks;
const buildBlockTypeTransformItem = buildBlockTypeItem( state, {
buildScope: 'transform',
} );
Expand All @@ -1780,20 +1782,32 @@ export const getBlockTransformItems = createSelector(
blockTypeTransformItems,
( { name } ) => name
);

// Consider unwraping the highest priority.
itemsByName[ '*' ] = {
frecency: +Infinity,
id: '*',
isDisabled: false,
name: '*',
title: __( 'Unwrap' ),
icon: itemsByName[ sourceBlock.name ]?.icon,
};

const possibleTransforms = getPossibleBlockTransformations(
blocks
).reduce( ( accumulator, block ) => {
if ( itemsByName[ block?.name ] ) {
if ( block === '*' ) {
accumulator.push( itemsByName[ '*' ] );
} else if ( itemsByName[ block?.name ] ) {
accumulator.push( itemsByName[ block.name ] );
}
return accumulator;
}, [] );
const possibleBlockTransformations = orderBy(
return orderBy(
possibleTransforms,
( block ) => itemsByName[ block.name ].frecency,
'desc'
);
return possibleBlockTransformations;
},
( state, rootClientId ) => [
state.blockListSettings[ rootClientId ],
Expand Down
13 changes: 13 additions & 0 deletions packages/block-library/src/quote/v2/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,19 @@ const transforms = {
} );
},
},
{
type: 'block',
blocks: [ '*' ],
transform: ( { attribution }, innerBlocks ) =>
attribution
? [
...innerBlocks,
createBlock( 'core/paragraph', {
content: attribution,
} ),
]
: innerBlocks,
},
{
type: 'block',
blocks: [ 'core/group' ],
Expand Down
11 changes: 6 additions & 5 deletions packages/blocks/src/api/factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,9 @@ const getBlockTypesForPossibleToTransforms = ( blocks ) => {
);

// Map block names to block types.
return blockNames.map( ( name ) => getBlockType( name ) );
return blockNames.map( ( name ) =>
name === '*' ? name : getBlockType( name )
);
};

/**
Expand Down Expand Up @@ -541,10 +543,9 @@ export function switchToBlockType( blocks, name ) {
return null;
}

const hasSwitchedBlock = some(
transformationResults,
( result ) => result.name === name
);
const hasSwitchedBlock =
name === '*' ||
some( transformationResults, ( result ) => result.name === name );

// Ensure that at least one block object returned by the transformation has
// the expected "destination" block type.
Expand Down

0 comments on commit bb4a8f1

Please sign in to comment.