diff --git a/blocks/README.md b/blocks/README.md index f7d740219955b..dd6660706a840 100644 --- a/blocks/README.md +++ b/blocks/README.md @@ -129,6 +129,7 @@ Registers a new block provided a unique slug and an object defining its behavior - `save( attributes: Object ): WPElement` - Returns an element describing the markup of a block to be saved in the published content. This function is called before save and when switching to an editor's HTML view. - `controls: string[]` - Slugs for controls to be made available to block. See also: [`wp.blocks.registerControl`](#wpblocksregistercontrol-slug-string-settings-object-) - `encodeAttributes( attributes: Object ): Object` - Called when save markup is generated, this function allows you to control which attributes are to be encoded in the block comment metadata. By default, all attribute values not defined in the block's `attributes` property are serialized to the comment metadata. If defined, this function should return the subset of attributes to encode, or `null` to bypass default behavior. +- `isVisible: boolean` - Whether the block is to be made available as an option in the block inserter overlay. Defaults to `true`. ### `wp.blocks.registerControl( slug: string, settings: Object )` diff --git a/blocks/index.js b/blocks/index.js index db210c40e3db3..d104892bc2bf8 100644 --- a/blocks/index.js +++ b/blocks/index.js @@ -111,3 +111,12 @@ export function getBlockAttributes( blockNode, blockSettings ) { export function getBlocks() { return Object.values( blocks ); } + +/** + * Returns all public registered blocks. + * + * @return {Array} Block settings + */ +export function getVisibleBlocks() { + return getBlocks().filter( ( block ) => false !== block.isVisible ); +} diff --git a/blocks/test/index.js b/blocks/test/index.js index e115850fa2cc6..a16f7e8d83c41 100644 --- a/blocks/test/index.js +++ b/blocks/test/index.js @@ -176,4 +176,21 @@ describe( 'blocks', () => { ] ); } ); } ); + + describe( 'getVisibleBlocks()', () => { + it( 'should return an empty array at first', () => { + expect( blocks.getVisibleBlocks() ).to.eql( [] ); + } ); + + it( 'should return all registered public blocks', () => { + blocks.registerBlock( 'core/visible-block', { isVisible: true } ); + blocks.registerBlock( 'core/inferred-visible-block' ); + blocks.registerBlock( 'core/non-visible-block', { isVisible: false } ); + + expect( blocks.getVisibleBlocks().map( ( block ) => block.slug ) ).eql( [ + 'core/visible-block', + 'core/inferred-visible-block' + ] ); + } ); + } ); } ); diff --git a/editor/blocks/generic-block/index.js b/editor/blocks/generic-block/index.js index fbfadd5203a86..820eb69a19bbd 100644 --- a/editor/blocks/generic-block/index.js +++ b/editor/blocks/generic-block/index.js @@ -1,6 +1,8 @@ const { html } = wp.blocks.query; wp.blocks.registerBlock( 'wp/generic', { + isVisible: false, + attributes: { html: html() }, diff --git a/editor/inserter/index.js b/editor/inserter/index.js index 11113cc592a31..7a62980ce67a1 100644 --- a/editor/inserter/index.js +++ b/editor/inserter/index.js @@ -1,5 +1,5 @@ const Inserter = () => { - const blocks = wp.blocks.getBlocks(); + const blocks = wp.blocks.getVisibleBlocks(); return (