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

Allow a block to disable being converted into a reusable block; Fix: Column block #11550

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions docs/block-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/classic/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/columns/column.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const settings = {

supports: {
inserter: false,
reusable: false,
},

edit() {
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/missing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export const settings = {
customClassName: false,
inserter: false,
html: false,
reusable: false,
},

attributes: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* External dependencies
*/
import { noop, every, map } from 'lodash';
import { noop, every } from 'lodash';

/**
* WordPress dependencies
*/
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';

Expand Down Expand Up @@ -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 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


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() &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that we formalize this on the blocks API rather than ad hoc exemptions 👍

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 )
) )
);

Expand Down