Skip to content

Commit

Permalink
Try direct insert as a match function.
Browse files Browse the repository at this point in the history
  • Loading branch information
tellthemachines committed Sep 30, 2021
1 parent 3a69217 commit 4e8457a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,20 @@ import { getLayoutType } from '../../layouts';
* the block-editor store, then the store is updated with the new settings which
* came from props.
*
* @param {string} clientId The client ID of the block to update.
* @param {string[]} allowedBlocks An array of block names which are permitted
* in inner blocks.
* @param {?Array} __experimentalDefaultBlock The default block to insert: [ blockName, { blockAttributes } ].
* @param {boolean} __experimentalDirectInsert If a default block should be inserted directly by the
* appender.
* @param {string} [templateLock] The template lock specified for the inner
* blocks component. (e.g. "all")
* @param {boolean} captureToolbars Whether or children toolbars should be shown
* in the inner blocks component rather than on
* the child block.
* @param {string} orientation The direction in which the block
* should face.
* @param {Object} layout The layout object for the block container.
* @param {string} clientId The client ID of the block to update.
* @param {string[]} allowedBlocks An array of block names which are permitted
* in inner blocks.
* @param {?Array} __experimentalDefaultBlock The default block to insert: [ blockName, { blockAttributes } ].
* @param {?Function|boolean} __experimentalDirectInsert If a default block should be inserted directly by the
* appender.
* @param {string} [templateLock] The template lock specified for the inner
* blocks component. (e.g. "all")
* @param {boolean} captureToolbars Whether or children toolbars should be shown
* in the inner blocks component rather than on
* the child block.
* @param {string} orientation The direction in which the block
* should face.
* @param {Object} layout The layout object for the block container.
*/
export default function useNestedSettingsUpdate(
clientId,
Expand Down
12 changes: 10 additions & 2 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1809,12 +1809,20 @@ export const __experimentalGetDirectInsertBlock = createSelector(
state.blockListSettings[ rootClientId ]?.__experimentalDefaultBlock;
const directInsert =
state.blockListSettings[ rootClientId ]?.__experimentalDirectInsert;
if ( ! defaultBlock?.length || ! directInsert ) {
if ( ! defaultBlock || ! directInsert ) {
return;
}
if ( typeof directInsert === 'function' ) {
return directInsert( getBlock( state, rootClientId ) )
? defaultBlock
: null;
}
return defaultBlock;
},
( state, rootClientId ) => [ state.blockListSettings[ rootClientId ] ]
( state, rootClientId ) => [
state.blockListSettings[ rootClientId ],
state.blocks.tree[ rootClientId ],
]
);

const checkAllowListRecursive = ( blocks, allowedBlockTypes ) => {
Expand Down
18 changes: 9 additions & 9 deletions packages/block-library/src/navigation/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ const ALLOWED_BLOCKS = [

const DEFAULT_BLOCK = [ 'core/navigation-link' ];

const DIRECT_INSERT = ( block ) => {
return block.innerBlocks.every(
( { name } ) =>
name === 'core/navigation-link' ||
name === 'core/navigation-submenu'
);
};

const LAYOUT = {
type: 'default',
alignments: [],
Expand Down Expand Up @@ -92,7 +100,6 @@ function Navigation( {
hasExistingNavItems,
isImmediateParentOfSelectedBlock,
isSelected,
onlyLinkInnerBlocks,
updateInnerBlocks,
className,
backgroundColor,
Expand Down Expand Up @@ -168,7 +175,7 @@ function Navigation( {
{
allowedBlocks: ALLOWED_BLOCKS,
__experimentalDefaultBlock: DEFAULT_BLOCK,
__experimentalDirectInsert: onlyLinkInnerBlocks,
__experimentalDirectInsert: DIRECT_INSERT,
orientation: attributes.orientation,
renderAppender: CustomAppender || appender,

Expand Down Expand Up @@ -371,12 +378,6 @@ export default compose( [
selectedBlockId,
] )?.length;

const onlyLinkInnerBlocks = innerBlocks.every(
( block ) =>
block.name === 'core/navigation-link' ||
block.name === 'core/navigation-submenu'
);

return {
isImmediateParentOfSelectedBlock,
selectedBlockHasDescendants,
Expand All @@ -385,7 +386,6 @@ export default compose( [
// This prop is already available but computing it here ensures it's
// fresh compared to isImmediateParentOfSelectedBlock
isSelected: selectedBlockId === clientId,
onlyLinkInnerBlocks,
};
} ),
withDispatch( ( dispatch, { clientId } ) => {
Expand Down

0 comments on commit 4e8457a

Please sign in to comment.