Skip to content

Commit

Permalink
Refactor the BlockToolbar compoonent to avoid getSelectedBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Nov 14, 2018
1 parent 3c8eb8e commit aa48d43
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
13 changes: 13 additions & 0 deletions docs/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 7 additions & 6 deletions packages/editor/src/components/block-toolbar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
13 changes: 13 additions & 0 deletions packages/editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit aa48d43

Please sign in to comment.