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

Refactor embed block to single block with block variations #24090

Merged
merged 48 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
7dc62e7
add embed variations + blocks.json
ntsekouras Jul 20, 2020
3f9c058
remove core-embeds
ntsekouras Jul 20, 2020
c9071fa
minor refactorings
ntsekouras Jul 20, 2020
878db03
add block attributes
ntsekouras Jul 20, 2020
ad8c2a6
add deprecated + save files
ntsekouras Jul 20, 2020
300e310
remove settings file and change utils
ntsekouras Jul 20, 2020
0b07b31
index + edit draft
ntsekouras Jul 20, 2020
128bccc
add responsive false to unknown embeds
ntsekouras Jul 20, 2020
e90de91
add save function
ntsekouras Jul 20, 2020
1d32d56
get className from attributes
ntsekouras Jul 20, 2020
0235c38
use `useSelect` and `useDispatch` hooks
ntsekouras Jul 21, 2020
4108e66
remove common prop from attributes
ntsekouras Jul 21, 2020
87b0ae1
minor changes
ntsekouras Jul 21, 2020
4a76985
convert supports to scope in variations
ntsekouras Jul 21, 2020
8811425
Inline the handleIncomingPreview effect
mcsf Jul 21, 2020
16e9d6f
add getEmbedInfoByProvider unit tests
ntsekouras Jul 21, 2020
5b6eb42
add getEmbedInfoByProvider jsdoc
ntsekouras Jul 22, 2020
66d0f86
fix variable typo
ntsekouras Jul 22, 2020
deb0428
small util refactorings
ntsekouras Jul 22, 2020
c7fb1dc
change utils
ntsekouras Jul 22, 2020
1b3d8b1
change findMoreSuitableBlock tests
ntsekouras Jul 23, 2020
f15b79a
use state variable for mergedAttributes
ntsekouras Jul 23, 2020
4533cde
small changes
ntsekouras Jul 23, 2020
8075a4a
change responsive default attribute
ntsekouras Jul 23, 2020
566c34e
change default value of responsive
ntsekouras Jul 23, 2020
302bb3d
change getEmbedInfoByProvider to just search variations
ntsekouras Jul 23, 2020
b916a2b
fix block test
ntsekouras Jul 23, 2020
89c6f3e
add check for registered default embed block + tests
ntsekouras Jul 27, 2020
803cca5
add support for old embed blocks
ntsekouras Jul 27, 2020
0af7734
support deprecated transforms in parser
ntsekouras Jul 27, 2020
3c688a9
support deprecated transforms in parser
ntsekouras Jul 27, 2020
956dd64
fix full-content tests
ntsekouras Jul 27, 2020
1b0d6a3
revert previewAttributes handling
ntsekouras Jul 28, 2020
8f48b3a
fix e2e tests
ntsekouras Jul 28, 2020
c3da25a
fix unit tests
ntsekouras Jul 28, 2020
bfee404
change all references to old core-embed block
ntsekouras Jul 28, 2020
5df70de
add snapshots to e2e tests
ntsekouras Jul 28, 2020
dbe4db7
enhance WP embed block matching with WP_EMBED_TYPE
ntsekouras Jul 28, 2020
a7b57ca
remove snapshot with publish post
ntsekouras Jul 28, 2020
0ee5c3a
add responsive attribute to demo vimeo block
ntsekouras Jul 29, 2020
29ed83e
fix typo
ntsekouras Jul 29, 2020
edaa6b7
add responsive attribute in old embed blocks in parser
ntsekouras Jul 29, 2020
5ebb4a5
try to limit BlockTypesList with prop
ntsekouras Aug 3, 2020
575469a
review feedback + tests
ntsekouras Aug 5, 2020
f008ffc
review feedback
ntsekouras Aug 10, 2020
38fdf1a
indentation change to tabs
ntsekouras Aug 10, 2020
6c5b5bc
fix e2e snapshots after adding the `wp-block-embed-{provider}` class
ntsekouras Aug 10, 2020
0819a14
minor refactor
ntsekouras Aug 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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