Skip to content

Commit

Permalink
Use specific selector for excluding controlled innerblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
noahtallen committed Apr 21, 2020
1 parent f4c1467 commit 47d88ec
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,16 @@ _Returns_
Returns all block objects for the current post being edited as an array in
the order they appear in the post.

Does not include InnerBlocks of blocks which control their own InnerBlocks.

Note: It's important to memoize this selector to avoid return a new instance
on each call

_Parameters_

- _state_ `Object`: Editor state.
- _rootClientId_ `?string`: Optional root client ID of block list.
- _withControlledInnerBlocks_ `?boolean`: If true, include inner blocks that are controlled by their parent.

_Returns_

Expand Down Expand Up @@ -307,6 +310,10 @@ _Returns_

- `?string`: Client ID of block selection start.

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

Undocumented declaration.

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

Returns an array containing the clientIds of all descendants
Expand Down
9 changes: 2 additions & 7 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,7 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => {
do {
result.push( current );
current = state.parents[ current ];
// We do not want to include the parent clientID if its inner
// blocks are controlled so that its cache key stay the same.
} while ( current && ! state.controlledInnerBlocks[ current ] );
} while ( current );
return result;
}, [] );
};
Expand All @@ -267,10 +265,7 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => {
const updatedBlockUids = keys( flattenBlocks( action.blocks ) );

// Only include the rootClientId if its inner blocks are uncontrolled.
if (
action.rootClientId &&
! state.controlledInnerBlocks[ action.rootClientId ]
) {
if ( action.rootClientId ) {
updatedBlockUids.push( action.rootClientId );
}
newState.cache = {
Expand Down
43 changes: 36 additions & 7 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ export function isBlockValid( state, clientId ) {
* @return {Object?} Block attributes.
*/
export function getBlockAttributes( state, clientId ) {
if ( ! state.blocks ) {
return null;
}
const block = state.blocks.byClientId[ clientId ];
if ( ! block ) {
return null;
Expand All @@ -137,8 +140,8 @@ export function getBlockAttributes( state, clientId ) {
* is not the block's registration settings, which must be retrieved from the
* blocks module registration store.
*
* @param {Object} state Editor state.
* @param {string} clientId Block client ID.
* @param {Object} state Editor state.
* @param {string} clientId Block client ID.
*
* @return {Object} Parsed block object.
*/
Expand All @@ -152,9 +155,7 @@ export const getBlock = createSelector(
return {
...block,
attributes: getBlockAttributes( state, clientId ),
innerBlocks: areInnerBlocksControlled( state, clientId )
? EMPTY_ARRAY
: getBlocks( state, clientId ),
innerBlocks: getBlocks( state, clientId ),
};
},
( state, clientId ) => [
Expand All @@ -167,6 +168,28 @@ export const getBlock = createSelector(
]
);

export const getBlockWithoutControlledInnerBlocks = createSelector(
( state, clientId ) => {
const block = state.blocks.byClientId[ clientId ];
if ( ! block ) {
return null;
}

return {
...block,
attributes: getBlockAttributes( state, clientId ),
innerBlocks: areInnerBlocksControlled( state, clientId )
? EMPTY_ARRAY
: getBlocks( state, clientId, false ),
};
},
( state, clientId ) => [
state.blocks.controlledInnerBlocks[ clientId ]
? getBlockAttributes( clientId )
: state.blocks.cache[ clientId ],
]
);

export const __unstableGetBlockWithoutInnerBlocks = createSelector(
( state, clientId ) => {
const block = state.blocks.byClientId[ clientId ];
Expand All @@ -189,18 +212,24 @@ export const __unstableGetBlockWithoutInnerBlocks = createSelector(
* Returns all block objects for the current post being edited as an array in
* the order they appear in the post.
*
* Does not include InnerBlocks of blocks which control their own InnerBlocks.
*
* Note: It's important to memoize this selector to avoid return a new instance
* on each call
*
* @param {Object} state Editor state.
* @param {?string} rootClientId Optional root client ID of block list.
* @param {?boolean} withControlledInnerBlocks If true, include inner blocks that
* are controlled by their parent.
*
* @return {Object[]} Post blocks.
*/
export const getBlocks = createSelector(
( state, rootClientId ) => {
( state, rootClientId, withControlledInnerBlocks = true ) => {
return map( getBlockOrder( state, rootClientId ), ( clientId ) =>
getBlock( state, clientId )
withControlledInnerBlocks
? getBlock( state, clientId )
: getBlockWithoutControlledInnerBlocks( state, clientId )
);
},
( state ) => [
Expand Down

0 comments on commit 47d88ec

Please sign in to comment.