Skip to content

Commit

Permalink
Allow a block to disable being converted into a reusable block; Fix: …
Browse files Browse the repository at this point in the history
…Column block (#11550)
  • Loading branch information
jorgefilipecosta authored Nov 7, 2018
1 parent f23242b commit 7c63d10
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 17 deletions.
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 );

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

Expand Down

0 comments on commit 7c63d10

Please sign in to comment.