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

Refactor the BlockToolbar compoonent to avoid getSelectedBlock #11843

Merged
merged 1 commit into from
Nov 14, 2018
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
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
Copy link
Member

Choose a reason for hiding this comment

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

There was some discussion at #10891 (comment) concerning the naming with regards to placement of adjectives. I think it's particularly relevant here only because we already have precedent with blocks, and with block validation specification, i.e. wp.blocks.isValidBlockContent.

Copy link
Member

Choose a reason for hiding this comment

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

On second thought, there seems to be subtle distinction between what's being validated, where the language here might be more appropriate for a known identity, vs. in the block API case of considering some transient inputs.

¯\_(ツ)_/¯


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