Skip to content

Commit

Permalink
NavigationLink: fix getting Navigation parent block (#20032)
Browse files Browse the repository at this point in the history
* block-editor: add getBLockParentsByBlockName()

* docs: generate selectors doc

* navigation: get the proper parent block
  • Loading branch information
retrofox authored Feb 4, 2020
1 parent 77bd274 commit aae6d1b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,23 @@ _Returns_

- `Array`: ClientIDs of the parent blocks.

<a name="getBlockParentsByBlockName" href="#getBlockParentsByBlockName">#</a> **getBlockParentsByBlockName**

Given a block client ID and a block name,
returns the list of all its parents from top to bottom,
filtered by the given name.

_Parameters_

- _state_ `Object`: Editor state.
- _clientId_ `string`: Block from which to find root client ID.
- _blockName_ `string`: Block name to filter.
- _ascending_ `boolean`: Order results from bottom to top (true) or top to bottom (false).

_Returns_

- `Array`: ClientIDs of the parent blocks.

<a name="getBlockRootClientId" href="#getBlockRootClientId">#</a> **getBlockRootClientId**

Given a block client ID, returns the root block from which the block is
Expand Down
31 changes: 31 additions & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,37 @@ export const getBlockParents = createSelector(
( state ) => [ state.blocks.parents ]
);

/**
* Given a block client ID and a block name,
* returns the list of all its parents from top to bottom,
* filtered by the given name.
*
* @param {Object} state Editor state.
* @param {string} clientId Block from which to find root client ID.
* @param {string} blockName Block name to filter.
* @param {boolean} ascending Order results from bottom to top (true) or top to bottom (false).
*
* @return {Array} ClientIDs of the parent blocks.
*/
export const getBlockParentsByBlockName = (
state,
clientId,
blockName,
ascending = false
) => {
const parents = getBlockParents( state, clientId, ascending );
return map(
filter(
map( parents, ( id ) => ( {
id,
name: getBlockName( state, id ),
} ) ),
{ name: blockName }
),
( { id } ) => id
);
};

/**
* Given a block client ID, returns the root of the hierarchy from which the block is nested, return the block itself for root level blocks.
*
Expand Down
8 changes: 5 additions & 3 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { escape, unescape } from 'lodash';
import { escape, unescape, head } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -308,12 +308,14 @@ export default compose( [
withSelect( ( select, ownProps ) => {
const {
getBlockAttributes,
getBlockParents,
getClientIdsOfDescendants,
hasSelectedInnerBlock,
getBlockParentsByBlockName,
} = select( 'core/block-editor' );
const { clientId } = ownProps;
const rootBlock = getBlockParents( clientId )[ 0 ];
const rootBlock = head(
getBlockParentsByBlockName( clientId, 'core/navigation' )
);
const navigationBlockAttributes = getBlockAttributes( rootBlock );
const hasDescendants = !! getClientIdsOfDescendants( [ clientId ] )
.length;
Expand Down

0 comments on commit aae6d1b

Please sign in to comment.