From 7c63d1095606ac069a288116f4ece054730ae30b Mon Sep 17 00:00:00 2001 From: Jorge Date: Wed, 7 Nov 2018 12:01:30 +0000 Subject: [PATCH] Allow a block to disable being converted into a reusable block; Fix: Column block (#11550) --- docs/block-api.md | 7 +++++ packages/block-library/src/classic/index.js | 3 ++ packages/block-library/src/columns/column.js | 1 + packages/block-library/src/missing/index.js | 1 + .../reusable-block-convert-button.js | 29 ++++++++----------- 5 files changed, 24 insertions(+), 17 deletions(-) diff --git a/docs/block-api.md b/docs/block-api.md index a3df73ffcb80e6..01b2295b46de0b 100644 --- a/docs/block-api.md +++ b/docs/block-api.md @@ -504,6 +504,13 @@ inserter: false, multiple: false, ``` +- `reusable` (default `true`): A block may want to disable the ability of being converted into a reusable block. +By default all blocks can be converted to a reusable block. If supports reusable is set to false, the option to convert the block into a reusable block will not appear. + +```js +// Don't allow the block to be converted into a reusable block. +reusable: false, +``` ## Edit and Save The `edit` and `save` functions define the editor interface with which a user would interact, and the markup to be serialized back when a post is saved. They are the heart of how a block operates, so they are [covered separately](../docs/block-api/block-edit-save.md). diff --git a/packages/block-library/src/classic/index.js b/packages/block-library/src/classic/index.js index e30d7fa75eada5..bcc4f0a388010b 100644 --- a/packages/block-library/src/classic/index.js +++ b/packages/block-library/src/classic/index.js @@ -31,6 +31,9 @@ export const settings = { supports: { className: false, customClassName: false, + // Hide 'Add to Reusable Blocks' on Classic blocks. Showing it causes a + // confusing UX, because of its similarity to the 'Convert to Blocks' button. + reusable: false, }, edit, diff --git a/packages/block-library/src/columns/column.js b/packages/block-library/src/columns/column.js index 89e22f1601d2c3..a8f37dd7df7ece 100644 --- a/packages/block-library/src/columns/column.js +++ b/packages/block-library/src/columns/column.js @@ -20,6 +20,7 @@ export const settings = { supports: { inserter: false, + reusable: false, }, edit() { diff --git a/packages/block-library/src/missing/index.js b/packages/block-library/src/missing/index.js index 1ef9391b9264c0..82de41b1d6eff5 100644 --- a/packages/block-library/src/missing/index.js +++ b/packages/block-library/src/missing/index.js @@ -66,6 +66,7 @@ export const settings = { customClassName: false, inserter: false, html: false, + reusable: false, }, attributes: { diff --git a/packages/editor/src/components/block-settings-menu/reusable-block-convert-button.js b/packages/editor/src/components/block-settings-menu/reusable-block-convert-button.js index 84530fb22bbef3..6d39538aa5e5ab 100644 --- a/packages/editor/src/components/block-settings-menu/reusable-block-convert-button.js +++ b/packages/editor/src/components/block-settings-menu/reusable-block-convert-button.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { noop, every, map } from 'lodash'; +import { noop, every } from 'lodash'; /** * WordPress dependencies @@ -9,7 +9,7 @@ import { noop, every, map } from 'lodash'; import { Fragment } from '@wordpress/element'; import { MenuItem } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; -import { isReusableBlock } from '@wordpress/blocks'; +import { hasBlockSupport, isReusableBlock } from '@wordpress/blocks'; import { withSelect, withDispatch } from '@wordpress/data'; import { compose } from '@wordpress/compose'; @@ -50,31 +50,26 @@ export function ReusableBlockConvertButton( { export default compose( [ withSelect( ( select, { clientIds } ) => { const { - getBlock, + getBlocksByClientId, canInsertBlockType, __experimentalGetReusableBlock: getReusableBlock, } = select( 'core/editor' ); - const { - getFreeformFallbackBlockName, - getUnregisteredFallbackBlockName, - } = select( 'core/blocks' ); - const blocks = map( clientIds, ( clientId ) => getBlock( clientId ) ); + const blocks = getBlocksByClientId( clientIds ); const isVisible = ( - // Guard against the case where a regular block has *just* been converted to a - // reusable block and doesn't yet exist in the editor store. - every( blocks, ( block ) => !! block ) && - // Hide 'Add to Reusable Blocks' when Reusable Blocks are disabled, i.e. when // core/block is not in the allowed_block_types filter. canInsertBlockType( 'core/block' ) && - // Hide 'Add to Reusable Blocks' on Classic blocks. Showing it causes a - // confusing UX, because of its similarity to the 'Convert to Blocks' button. - ( blocks.length !== 1 || ( - blocks[ 0 ].name !== getFreeformFallbackBlockName() && - blocks[ 0 ].name !== getUnregisteredFallbackBlockName() + every( blocks, ( block ) => ( + // Guard against the case where a regular block has *just* been converted to a + // reusable block and doesn't yet exist in the editor store. + !! block && + // Only show the option to covert to reusable blocks on valid blocks. + block.isValid && + // Make sure the block supports being converted into a reusable block (by default that is the case). + hasBlockSupport( block.name, 'reusable', true ) ) ) );