From b201a0b9db923408d655ef17c1ca01ed9e49d1ff Mon Sep 17 00:00:00 2001 From: Grzegorz Ziolkowski Date: Fri, 8 Jul 2022 09:11:35 +0200 Subject: [PATCH] Block Library: Make it possible to import individual blocks --- packages/block-library/babel-plugin.js | 2 +- packages/block-library/src/archives/init.js | 7 ++++ .../src/audio/transforms.native.js | 2 +- .../src/buttons/transforms.native.js | 2 +- .../src/code/transforms.native.js | 2 +- .../src/columns/transforms.native.js | 2 +- .../src/cover/transforms.native.js | 2 +- .../src/embed/transforms.native.js | 2 +- .../src/file/transforms.native.js | 2 +- .../src/gallery/transforms.native.js | 2 +- .../src/heading/transforms.native.js | 2 +- .../src/image/transforms.native.js | 2 +- packages/block-library/src/index.js | 22 ++-------- packages/block-library/src/index.native.js | 39 ++---------------- .../src/list/transforms.native.js | 2 +- .../src/media-text/transforms.native.js | 2 +- .../src/more/transforms.native.js | 2 +- .../src/nextpage/transforms.native.js | 2 +- .../src/paragraph/transforms.native.js | 2 +- .../src/preformatted/transforms.native.js | 2 +- .../src/pullquote/transforms.native.js | 2 +- .../src/quote/transforms.native.js | 2 +- .../src/separator/transforms.native.js | 2 +- .../src/shortcode/transforms.native.js | 2 +- .../src/text-columns/transforms.native.js | 2 +- .../block-library/src/utils/init-block.js | 20 ++++++++++ .../src/utils/init-block.native.js | 40 +++++++++++++++++++ .../is-block-metadata-experimental.js | 0 .../transformation-categories.native.js} | 0 .../src/verse/transforms.native.js | 2 +- .../src/video/transforms.native.js | 2 +- 31 files changed, 98 insertions(+), 78 deletions(-) create mode 100644 packages/block-library/src/archives/init.js create mode 100644 packages/block-library/src/utils/init-block.js create mode 100644 packages/block-library/src/utils/init-block.native.js rename packages/block-library/src/{ => utils}/is-block-metadata-experimental.js (100%) rename packages/block-library/src/{transformationCategories.native.js => utils/transformation-categories.native.js} (100%) diff --git a/packages/block-library/babel-plugin.js b/packages/block-library/babel-plugin.js index ed688777169f3..1fba03a39a35a 100644 --- a/packages/block-library/babel-plugin.js +++ b/packages/block-library/babel-plugin.js @@ -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 diff --git a/packages/block-library/src/archives/init.js b/packages/block-library/src/archives/init.js new file mode 100644 index 0000000000000..c1fb9560b48ee --- /dev/null +++ b/packages/block-library/src/archives/init.js @@ -0,0 +1,7 @@ +/** + * Internal dependencies + */ +import * as block from './'; +import initBlock from '../utils/init-block'; + +export default initBlock( block ); diff --git a/packages/block-library/src/audio/transforms.native.js b/packages/block-library/src/audio/transforms.native.js index 839cdfe3c9381..a9bf418541e88 100644 --- a/packages/block-library/src/audio/transforms.native.js +++ b/packages/block-library/src/audio/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/buttons/transforms.native.js b/packages/block-library/src/buttons/transforms.native.js index 0816b765fee29..c497af10c918f 100644 --- a/packages/block-library/src/buttons/transforms.native.js +++ b/packages/block-library/src/buttons/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/code/transforms.native.js b/packages/block-library/src/code/transforms.native.js index 21b6405a15030..25ce6b9b40c43 100644 --- a/packages/block-library/src/code/transforms.native.js +++ b/packages/block-library/src/code/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/columns/transforms.native.js b/packages/block-library/src/columns/transforms.native.js index 032a346425894..cea16e6fd62a2 100644 --- a/packages/block-library/src/columns/transforms.native.js +++ b/packages/block-library/src/columns/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/cover/transforms.native.js b/packages/block-library/src/cover/transforms.native.js index 839cdfe3c9381..a9bf418541e88 100644 --- a/packages/block-library/src/cover/transforms.native.js +++ b/packages/block-library/src/cover/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/embed/transforms.native.js b/packages/block-library/src/embed/transforms.native.js index 839cdfe3c9381..a9bf418541e88 100644 --- a/packages/block-library/src/embed/transforms.native.js +++ b/packages/block-library/src/embed/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/file/transforms.native.js b/packages/block-library/src/file/transforms.native.js index 839cdfe3c9381..a9bf418541e88 100644 --- a/packages/block-library/src/file/transforms.native.js +++ b/packages/block-library/src/file/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/gallery/transforms.native.js b/packages/block-library/src/gallery/transforms.native.js index 839cdfe3c9381..a9bf418541e88 100644 --- a/packages/block-library/src/gallery/transforms.native.js +++ b/packages/block-library/src/gallery/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/heading/transforms.native.js b/packages/block-library/src/heading/transforms.native.js index 21b6405a15030..25ce6b9b40c43 100644 --- a/packages/block-library/src/heading/transforms.native.js +++ b/packages/block-library/src/heading/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/image/transforms.native.js b/packages/block-library/src/image/transforms.native.js index 839cdfe3c9381..a9bf418541e88 100644 --- a/packages/block-library/src/image/transforms.native.js +++ b/packages/block-library/src/image/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/index.js b/packages/block-library/src/index.js index ff72f0b90d101..28ce41bd86d06 100644 --- a/packages/block-library/src/index.js +++ b/packages/block-library/src/index.js @@ -2,7 +2,6 @@ * WordPress dependencies */ import { - registerBlockType, setDefaultBlockName, setFreeformContentHandlerName, setUnregisteredTypeHandlerName, @@ -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 @@ -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 ) { @@ -305,6 +291,6 @@ export const __experimentalRegisterExperimentalCoreBlocks = process.env __experimental === true || enabledExperiments.includes( __experimental ) ) - .forEach( registerBlock ); + .forEach( initBlock ); } : undefined; diff --git a/packages/block-library/src/index.native.js b/packages/block-library/src/index.native.js index 984e5c290135d..ea455bf406f18 100644 --- a/packages/block-library/src/index.native.js +++ b/packages/block-library/src/index.native.js @@ -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 @@ -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. * @@ -277,7 +244,7 @@ export const registerCoreBlocks = ( blocksFlags ) => { reusableBlock, search, embed, - ].forEach( registerBlock ); + ].forEach( initBlock ); registerBlockVariations( socialLink ); setDefaultBlockName( paragraph.name ); diff --git a/packages/block-library/src/list/transforms.native.js b/packages/block-library/src/list/transforms.native.js index 21b6405a15030..25ce6b9b40c43 100644 --- a/packages/block-library/src/list/transforms.native.js +++ b/packages/block-library/src/list/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/media-text/transforms.native.js b/packages/block-library/src/media-text/transforms.native.js index 839cdfe3c9381..a9bf418541e88 100644 --- a/packages/block-library/src/media-text/transforms.native.js +++ b/packages/block-library/src/media-text/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/more/transforms.native.js b/packages/block-library/src/more/transforms.native.js index 0816b765fee29..c497af10c918f 100644 --- a/packages/block-library/src/more/transforms.native.js +++ b/packages/block-library/src/more/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/nextpage/transforms.native.js b/packages/block-library/src/nextpage/transforms.native.js index 0816b765fee29..c497af10c918f 100644 --- a/packages/block-library/src/nextpage/transforms.native.js +++ b/packages/block-library/src/nextpage/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/paragraph/transforms.native.js b/packages/block-library/src/paragraph/transforms.native.js index 21b6405a15030..25ce6b9b40c43 100644 --- a/packages/block-library/src/paragraph/transforms.native.js +++ b/packages/block-library/src/paragraph/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/preformatted/transforms.native.js b/packages/block-library/src/preformatted/transforms.native.js index 21b6405a15030..25ce6b9b40c43 100644 --- a/packages/block-library/src/preformatted/transforms.native.js +++ b/packages/block-library/src/preformatted/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/pullquote/transforms.native.js b/packages/block-library/src/pullquote/transforms.native.js index 21b6405a15030..25ce6b9b40c43 100644 --- a/packages/block-library/src/pullquote/transforms.native.js +++ b/packages/block-library/src/pullquote/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/quote/transforms.native.js b/packages/block-library/src/quote/transforms.native.js index 21b6405a15030..25ce6b9b40c43 100644 --- a/packages/block-library/src/quote/transforms.native.js +++ b/packages/block-library/src/quote/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/separator/transforms.native.js b/packages/block-library/src/separator/transforms.native.js index 0816b765fee29..c497af10c918f 100644 --- a/packages/block-library/src/separator/transforms.native.js +++ b/packages/block-library/src/separator/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/shortcode/transforms.native.js b/packages/block-library/src/shortcode/transforms.native.js index 21b6405a15030..25ce6b9b40c43 100644 --- a/packages/block-library/src/shortcode/transforms.native.js +++ b/packages/block-library/src/shortcode/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/text-columns/transforms.native.js b/packages/block-library/src/text-columns/transforms.native.js index 032a346425894..cea16e6fd62a2 100644 --- a/packages/block-library/src/text-columns/transforms.native.js +++ b/packages/block-library/src/text-columns/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/utils/init-block.js b/packages/block-library/src/utils/init-block.js new file mode 100644 index 0000000000000..fb48f422a07cf --- /dev/null +++ b/packages/block-library/src/utils/init-block.js @@ -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 ); +} diff --git a/packages/block-library/src/utils/init-block.native.js b/packages/block-library/src/utils/init-block.native.js new file mode 100644 index 0000000000000..c69155175948d --- /dev/null +++ b/packages/block-library/src/utils/init-block.native.js @@ -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 + ); +} diff --git a/packages/block-library/src/is-block-metadata-experimental.js b/packages/block-library/src/utils/is-block-metadata-experimental.js similarity index 100% rename from packages/block-library/src/is-block-metadata-experimental.js rename to packages/block-library/src/utils/is-block-metadata-experimental.js diff --git a/packages/block-library/src/transformationCategories.native.js b/packages/block-library/src/utils/transformation-categories.native.js similarity index 100% rename from packages/block-library/src/transformationCategories.native.js rename to packages/block-library/src/utils/transformation-categories.native.js diff --git a/packages/block-library/src/verse/transforms.native.js b/packages/block-library/src/verse/transforms.native.js index 21b6405a15030..25ce6b9b40c43 100644 --- a/packages/block-library/src/verse/transforms.native.js +++ b/packages/block-library/src/verse/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms, diff --git a/packages/block-library/src/video/transforms.native.js b/packages/block-library/src/video/transforms.native.js index 839cdfe3c9381..a9bf418541e88 100644 --- a/packages/block-library/src/video/transforms.native.js +++ b/packages/block-library/src/video/transforms.native.js @@ -2,7 +2,7 @@ * Internal dependencies */ import webTransforms from './transforms.js'; -import transformationCategories from '../transformationCategories'; +import transformationCategories from '../utils/transformation-categories'; const transforms = { ...webTransforms,