Skip to content

Commit

Permalink
Block Library: Make it possible to import individual blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
gziolo committed Jul 8, 2022
1 parent 9d52373 commit b201a0b
Show file tree
Hide file tree
Showing 31 changed files with 98 additions and 78 deletions.
2 changes: 1 addition & 1 deletion packages/block-library/babel-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const fs = require( 'fs' );
/**
* Internal dependencies
*/
const isBlockMetadataExperimental = require( './src/is-block-metadata-experimental' );
const isBlockMetadataExperimental = require( './src/utils/is-block-metadata-experimental' );

/**
* Creates a babel plugin that replaces experimental block imports with
Expand Down
7 changes: 7 additions & 0 deletions packages/block-library/src/archives/init.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* Internal dependencies
*/
import * as block from './';
import initBlock from '../utils/init-block';

export default initBlock( block );
2 changes: 1 addition & 1 deletion packages/block-library/src/audio/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/buttons/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/code/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/columns/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/cover/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/embed/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/file/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/gallery/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/heading/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/image/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
22 changes: 4 additions & 18 deletions packages/block-library/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* WordPress dependencies
*/
import {
registerBlockType,
setDefaultBlockName,
setFreeformContentHandlerName,
setUnregisteredTypeHandlerName,
Expand Down Expand Up @@ -116,21 +115,8 @@ import * as textColumns from './text-columns';
import * as verse from './verse';
import * as video from './video';

import isBlockMetadataExperimental from './is-block-metadata-experimental';

/**
* Function to register an individual block.
*
* @param {Object} block The block to be registered.
*
*/
const registerBlock = ( block ) => {
if ( ! block ) {
return;
}
const { metadata, settings, name } = block;
registerBlockType( { name, ...metadata }, settings );
};
import initBlock from './utils/init-block';
import isBlockMetadataExperimental from './utils/is-block-metadata-experimental';

/**
* Function to get all the block-library blocks in an array
Expand Down Expand Up @@ -268,7 +254,7 @@ export const __experimentalGetCoreBlocks = () =>
export const registerCoreBlocks = (
blocks = __experimentalGetCoreBlocks()
) => {
blocks.forEach( registerBlock );
blocks.forEach( initBlock );

setDefaultBlockName( paragraph.name );
if ( window.wp && window.wp.oldEditor ) {
Expand Down Expand Up @@ -305,6 +291,6 @@ export const __experimentalRegisterExperimentalCoreBlocks = process.env
__experimental === true ||
enabledExperiments.includes( __experimental )
)
.forEach( registerBlock );
.forEach( initBlock );
}
: undefined;
39 changes: 3 additions & 36 deletions packages/block-library/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,8 @@ import * as buttons from './buttons';
import * as socialLink from './social-link';
import * as socialLinks from './social-links';

import { transformationCategory } from './transformationCategories';

const ALLOWED_BLOCKS_GRADIENT_SUPPORT = [ 'core/button' ];
import initBlock from './utils/init-block';
import { transformationCategory } from './utils/transformation-categories';

export const coreBlocks = [
// Common blocks are grouped at the top to prioritize their display
Expand Down Expand Up @@ -118,38 +117,6 @@ export const coreBlocks = [
return accumulator;
}, {} );

/**
* Function to register an individual block.
*
* @param {Object} block The block to be registered.
*
*/
export const registerBlock = ( block ) => {
if ( ! block ) {
return;
}
const { metadata, settings, name } = block;
const { supports } = metadata;

registerBlockType(
{
name,
...metadata,
// Gradients support only available for blocks listed in ALLOWED_BLOCKS_GRADIENT_SUPPORT.
...( ! ALLOWED_BLOCKS_GRADIENT_SUPPORT.includes( name ) &&
supports?.color?.gradients
? {
supports: {
...supports,
color: { ...supports.color, gradients: false },
},
}
: {} ),
},
settings
);
};

/**
* Function to register a block variations e.g. social icons different types.
*
Expand Down Expand Up @@ -277,7 +244,7 @@ export const registerCoreBlocks = ( blocksFlags ) => {
reusableBlock,
search,
embed,
].forEach( registerBlock );
].forEach( initBlock );

registerBlockVariations( socialLink );
setDefaultBlockName( paragraph.name );
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/list/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/media-text/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/more/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/nextpage/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/paragraph/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/pullquote/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/quote/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/separator/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/shortcode/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
20 changes: 20 additions & 0 deletions packages/block-library/src/utils/init-block.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* WordPress dependencies
*/
import { registerBlockType } from '@wordpress/blocks';

/**
* Function to register an individual block.
*
* @param {Object} block The block to be registered.
*
* @return {?WPBlockType} The block, if it has been successfully registered;
* otherwise `undefined`.
*/
export default function initBlock( block ) {
if ( ! block ) {
return;
}
const { metadata, settings, name } = block;
return registerBlockType( { name, ...metadata }, settings );
}
40 changes: 40 additions & 0 deletions packages/block-library/src/utils/init-block.native.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* WordPress dependencies
*/
import { registerBlockType } from '@wordpress/blocks';

const ALLOWED_BLOCKS_GRADIENT_SUPPORT = [ 'core/button' ];

/**
* Function to register an individual block.
*
* @param {Object} block The block to be registered.
*
* @return {?WPBlockType} The block, if it has been successfully registered;
* otherwise `undefined`.
*/
export function initBlock( block ) {
if ( ! block ) {
return;
}
const { metadata, settings, name } = block;
const { supports } = metadata;

return registerBlockType(
{
name,
...metadata,
// Gradients support only available for blocks listed in ALLOWED_BLOCKS_GRADIENT_SUPPORT.
...( ! ALLOWED_BLOCKS_GRADIENT_SUPPORT.includes( name ) &&
supports?.color?.gradients
? {
supports: {
...supports,
color: { ...supports.color, gradients: false },
},
}
: {} ),
},
settings
);
}
2 changes: 1 addition & 1 deletion packages/block-library/src/verse/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/video/transforms.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Internal dependencies
*/
import webTransforms from './transforms.js';
import transformationCategories from '../transformationCategories';
import transformationCategories from '../utils/transformation-categories';

const transforms = {
...webTransforms,
Expand Down

0 comments on commit b201a0b

Please sign in to comment.