Skip to content

Commit

Permalink
Refactor embed block to single block with block variations (#24090)
Browse files Browse the repository at this point in the history
* add embed variations + blocks.json

* remove core-embeds

* minor refactorings

* add block attributes

* add deprecated + save files

* remove settings file and change utils

* index + edit draft

* add responsive false to unknown embeds

* add save function

* get className from attributes

* use `useSelect` and `useDispatch` hooks

* remove common prop from attributes

* minor changes

* convert supports to scope in variations

* Inline the handleIncomingPreview effect

* add getEmbedInfoByProvider unit tests

* add getEmbedInfoByProvider jsdoc

* fix variable typo

Co-authored-by: Miguel Fonseca <[email protected]>

* small util refactorings

* change utils

* change findMoreSuitableBlock tests

* use state variable for mergedAttributes

* small changes

* change responsive default attribute

* change default value of responsive

Co-authored-by: Miguel Fonseca <[email protected]>

* change getEmbedInfoByProvider to just search variations

* fix block test

* add check for registered default embed block + tests

* add support for old embed blocks

* support deprecated transforms in parser

* support deprecated transforms in parser

* fix full-content tests

* revert previewAttributes handling

react lifecycle doesn't play well with the previous change - using megedAttributes as a local state var

* fix e2e tests

* fix unit tests

* change all references to old core-embed block

* add snapshots to e2e tests

* enhance WP embed block matching with WP_EMBED_TYPE

* remove snapshot with publish post

* add responsive attribute to demo vimeo block

* fix typo

* add responsive attribute in old embed blocks in parser

* try to limit BlockTypesList with prop

* review feedback + tests

* review feedback

* indentation change to tabs

* fix e2e snapshots after adding the `wp-block-embed-{provider}` class

* minor refactor

Co-authored-by: Miguel Fonseca <[email protected]>
  • Loading branch information
ntsekouras and mcsf committed Aug 10, 2020
1 parent 665a260 commit 0259002
Show file tree
Hide file tree
Showing 36 changed files with 1,236 additions and 1,060 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ function BlockTypesList( {
onHover = () => {},
children,
label,
limit,
} ) {
const composite = useCompositeState();
const normalizedItems = includeVariationsInInserterItems( items );
const normalizedItems = includeVariationsInInserterItems( items, limit );
const orderId = normalizedItems.reduce(
( acc, item ) => acc + '--' + item.id,
''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export function BlockTypesTab( {
onSelect={ onSelectItem }
onHover={ onHover }
label={ _x( 'Most used', 'blocks' ) }
limit={ MAX_SUGGESTED_ITEMS }
/>
</InserterPanel>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ function QuickInserterList( {
onSelect={ onSelectBlockType }
onHover={ onHover }
label={ __( 'Blocks' ) }
limit={ SHOWN_BLOCK_TYPES }
/>
</InserterPanel>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ export const paragraphItem = {
keywords: [ 'random' ],
};

export const withSingleVariationItem = {
id: 'core/embed',
name: 'core/embed',
description: 'core description',
initialAttributes: {},
category: 'embed',
variations: [
{
name: 'youtube',
title: 'YouTube',
description: 'youtube description',
},
],
};

export const withVariationsItem = {
id: 'core/block-with-variations',
name: 'core/block-with-variations',
Expand Down Expand Up @@ -81,8 +96,8 @@ export const moreItem = {
};

export const youtubeItem = {
id: 'core-embed/youtube',
name: 'core-embed/youtube',
id: 'core/embed',
name: 'core/embed',
initialAttributes: {},
title: 'YouTube',
category: 'embed',
Expand Down
55 changes: 55 additions & 0 deletions packages/block-editor/src/components/inserter/test/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* Internal dependencies
*/
import {
moreItem,
paragraphItem,
someOtherItem,
withVariationsItem,
withSingleVariationItem,
} from './fixtures';
import { includeVariationsInInserterItems } from '../utils';

describe( 'inserter utils', () => {
describe( 'includeVariationsInInserterItems', () => {
it( 'should return items if limit is equal to items length', () => {
const items = [ moreItem, paragraphItem, someOtherItem ];
const res = includeVariationsInInserterItems( items, 3 );
expect( res ).toEqual( items );
} );
it( 'should slice items if items are more than provided limit', () => {
const items = [
moreItem,
paragraphItem,
someOtherItem,
withVariationsItem,
];
const res = includeVariationsInInserterItems( items, 2 );
expect( res ).toEqual( [ moreItem, paragraphItem ] );
} );
it( 'should return proper result if no limit provided and block variations do NOT exist', () => {
const items = [ moreItem, paragraphItem, someOtherItem ];
const res = includeVariationsInInserterItems( items );
expect( res ).toEqual( items );
} );
it( 'should return proper result if no limit provided and block variations exist', () => {
const items = [
moreItem,
paragraphItem,
someOtherItem,
withSingleVariationItem,
];
const expected = [
...items,
{
...withSingleVariationItem,
id: 'core/embed-youtube',
title: 'YouTube',
description: 'youtube description',
},
];
const res = includeVariationsInInserterItems( items );
expect( res ).toEqual( expected );
} );
} );
} );
53 changes: 32 additions & 21 deletions packages/block-editor/src/components/inserter/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
/**
* Return a function to be used to tranform a block variation to an inserter item
*
* @param {Object} item Denormalized inserter item
* @return {Function} Function to transform a block variation to inserter item
*/
const getItemFromVariation = ( item ) => ( variation ) => ( {
...item,
id: `${ item.id }-${ variation.name }`,
icon: variation.icon || item.icon,
title: variation.title || item.title,
description: variation.description || item.description,
// If `example` is explicitly undefined for the variation, the preview will not be shown.
example: variation.hasOwnProperty( 'example' )
? variation.example
: item.example,
initialAttributes: {
...item.initialAttributes,
...variation.attributes,
},
innerBlocks: variation.innerBlocks,
} );

/**
* Normalizes an inserter block types list and includes variations as separate items.
*
* @param {Array} items Denormalized inserter items
* @param {number} [limit=Infinity] Limit returned results by a given threshold.
* @return {Array} Normalized inserter items.
*/
export function includeVariationsInInserterItems( items ) {
export function includeVariationsInInserterItems( items, limit = Infinity ) {
if ( items.length >= limit ) {
// No need to iterate for variations
return items.slice( 0, limit );
}
// Show all available blocks with variations
return items.reduce( ( result, item ) => {
const { variations = [] } = item;
const hasDefaultVariation = variations.some(
Expand All @@ -18,26 +47,8 @@ export function includeVariationsInInserterItems( items ) {
}

if ( variations.length ) {
result = result.concat(
variations.map( ( variation ) => {
return {
...item,
id: `${ item.id }-${ variation.name }`,
icon: variation.icon || item.icon,
title: variation.title || item.title,
description: variation.description || item.description,
// If `example` is explicitly undefined for the variation, the preview will not be shown.
example: variation.hasOwnProperty( 'example' )
? variation.example
: item.example,
initialAttributes: {
...item.initialAttributes,
...variation.attributes,
},
innerBlocks: variation.innerBlocks,
};
} )
);
const variationMapper = getItemFromVariation( item );
result.push( ...variations.map( variationMapper ) );
}

return result;
Expand Down
16 changes: 8 additions & 8 deletions packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2404,29 +2404,29 @@ describe( 'state', () => {
blocks: [
{
clientId: 'bacon',
name: 'core-embed/twitter',
name: 'core/embed',
},
],
time: 123456,
} );

expect( state ).toEqual( {
insertUsage: {
'core-embed/twitter': {
'core/embed': {
time: 123456,
count: 1,
insert: { name: 'core-embed/twitter' },
insert: { name: 'core/embed' },
},
},
} );

const twoRecentBlocks = preferences(
deepFreeze( {
insertUsage: {
'core-embed/twitter': {
'core/embed': {
time: 123456,
count: 1,
insert: { name: 'core-embed/twitter' },
insert: { name: 'core/embed' },
},
},
} ),
Expand All @@ -2435,7 +2435,7 @@ describe( 'state', () => {
blocks: [
{
clientId: 'eggs',
name: 'core-embed/twitter',
name: 'core/embed',
},
{
clientId: 'bacon',
Expand All @@ -2449,10 +2449,10 @@ describe( 'state', () => {

expect( twoRecentBlocks ).toEqual( {
insertUsage: {
'core-embed/twitter': {
'core/embed': {
time: 123457,
count: 2,
insert: { name: 'core-embed/twitter' },
insert: { name: 'core/embed' },
},
'core/block/123': {
time: 123457,
Expand Down
37 changes: 37 additions & 0 deletions packages/block-library/src/embed/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
{
"name": "core/embed",
"category": "embed",
"attributes": {
"url": {
"type": "string"
},
"caption": {
"type": "string",
"source": "html",
"selector": "figcaption"
},
"type": {
"type": "string"
},
"providerNameSlug": {
"type": "string"
},
"allowResponsive": {
"type": "boolean",
"default": true
},
"responsive": {
"type": "boolean",
"default": false
},
"previewable": {
"type": "boolean",
"default": true
}
},
"supports": {
"align": true,
"reusable": false,
"html": false
}
}
3 changes: 1 addition & 2 deletions packages/block-library/src/embed/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ export const ASPECT_RATIOS = [
{ ratio: '0.50', className: 'wp-embed-aspect-1-2' },
];

export const DEFAULT_EMBED_BLOCK = 'core/embed';
export const WORDPRESS_EMBED_BLOCK = 'core-embed/wordpress';
export const WP_EMBED_TYPE = 'wp-embed';
Loading

0 comments on commit 0259002

Please sign in to comment.