diff --git a/docs/data/data-core-editor.md b/docs/data/data-core-editor.md index 1517f5b5317cc..7fcdc848c882f 100644 --- a/docs/data/data-core-editor.md +++ b/docs/data/data-core-editor.md @@ -398,6 +398,19 @@ the client ID. Block name. +### isBlockValid + +Returns whether a block is valid or not. + +*Parameters* + + * state: Editor state. + * clientId: Block client ID. + +*Returns* + +Is Valid. + ### getBlock Returns a block given its client ID. This is a parsed copy of the block, diff --git a/packages/editor/src/components/block-toolbar/index.js b/packages/editor/src/components/block-toolbar/index.js index 511ed1d7da053..e37fbdb2e46aa 100644 --- a/packages/editor/src/components/block-toolbar/index.js +++ b/packages/editor/src/components/block-toolbar/index.js @@ -43,18 +43,19 @@ function BlockToolbar( { blockClientIds, isValid, mode } ) { export default withSelect( ( select ) => { const { - getSelectedBlock, + getSelectedBlockClientId, getBlockMode, getMultiSelectedBlockClientIds, + isBlockValid, } = select( 'core/editor' ); - const block = getSelectedBlock(); - const blockClientIds = block ? - [ block.clientId ] : + const selectedBlockClientId = getSelectedBlockClientId(); + const blockClientIds = selectedBlockClientId ? + [ selectedBlockClientId ] : getMultiSelectedBlockClientIds(); return { blockClientIds, - isValid: block ? block.isValid : null, - mode: block ? getBlockMode( block.clientId ) : null, + isValid: selectedBlockClientId ? isBlockValid( selectedBlockClientId ) : null, + mode: selectedBlockClientId ? getBlockMode( selectedBlockClientId ) : null, }; } )( BlockToolbar ); diff --git a/packages/editor/src/store/selectors.js b/packages/editor/src/store/selectors.js index 619c0e381712a..2efddcf2244b5 100644 --- a/packages/editor/src/store/selectors.js +++ b/packages/editor/src/store/selectors.js @@ -601,6 +601,19 @@ export function getBlockName( state, clientId ) { return block ? block.name : null; } +/** + * Returns whether a block is valid or not. + * + * @param {Object} state Editor state. + * @param {string} clientId Block client ID. + * + * @return {boolean} Is Valid. + */ +export function isBlockValid( state, clientId ) { + const block = state.editor.present.blocks.byClientId[ clientId ]; + return !! block && block.isValid; +} + /** * Returns a block given its client ID. This is a parsed copy of the block, * containing its `blockName`, `clientId`, and current `attributes` state. This