Skip to content

Commit

Permalink
Nav offcanvas disable direct insertion (#46898)
Browse files Browse the repository at this point in the history
* Add new shouldDirectInsert prop to enabling disabling of direct insertion

* Disable direct insertion in Offcanvas
  • Loading branch information
getdave authored Jan 5, 2023
1 parent d9f13a8 commit 339aa2c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 41 deletions.
87 changes: 46 additions & 41 deletions packages/block-editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,48 +220,53 @@ class Inserter extends Component {
}

export default compose( [
withSelect( ( select, { clientId, rootClientId } ) => {
const {
getBlockRootClientId,
hasInserterItems,
__experimentalGetAllowedBlocks,
__experimentalGetDirectInsertBlock,
getSettings,
} = select( blockEditorStore );

const { getBlockVariations } = select( blocksStore );

rootClientId =
rootClientId || getBlockRootClientId( clientId ) || undefined;

const allowedBlocks = __experimentalGetAllowedBlocks( rootClientId );

const directInsertBlock =
__experimentalGetDirectInsertBlock( rootClientId );

const settings = getSettings();

const hasSingleBlockType =
allowedBlocks?.length === 1 &&
getBlockVariations( allowedBlocks[ 0 ].name, 'inserter' )
?.length === 0;

let allowedBlockType = false;
if ( hasSingleBlockType ) {
allowedBlockType = allowedBlocks[ 0 ];
withSelect(
( select, { clientId, rootClientId, shouldDirectInsert = true } ) => {
const {
getBlockRootClientId,
hasInserterItems,
__experimentalGetAllowedBlocks,
__experimentalGetDirectInsertBlock,
getSettings,
} = select( blockEditorStore );

const { getBlockVariations } = select( blocksStore );

rootClientId =
rootClientId || getBlockRootClientId( clientId ) || undefined;

const allowedBlocks =
__experimentalGetAllowedBlocks( rootClientId );

const directInsertBlock =
shouldDirectInsert &&
__experimentalGetDirectInsertBlock( rootClientId );

const settings = getSettings();

const hasSingleBlockType =
allowedBlocks?.length === 1 &&
getBlockVariations( allowedBlocks[ 0 ].name, 'inserter' )
?.length === 0;

let allowedBlockType = false;
if ( hasSingleBlockType ) {
allowedBlockType = allowedBlocks[ 0 ];
}

return {
hasItems: hasInserterItems( rootClientId ),
hasSingleBlockType,
blockTitle: allowedBlockType ? allowedBlockType.title : '',
allowedBlockType,
directInsertBlock,
rootClientId,
prioritizePatterns:
settings.__experimentalPreferPatternsOnRoot &&
! rootClientId,
};
}

return {
hasItems: hasInserterItems( rootClientId ),
hasSingleBlockType,
blockTitle: allowedBlockType ? allowedBlockType.title : '',
allowedBlockType,
directInsertBlock,
rootClientId,
prioritizePatterns:
settings.__experimentalPreferPatternsOnRoot && ! rootClientId,
};
} ),
),
withDispatch( ( dispatch, ownProps, { select } ) => {
return {
insertOnlyAllowedBlock() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const Appender = forwardRef( ( props, ref ) => {
position="bottom right"
isAppender={ true }
selectBlockOnInsert={ false }
shouldDirectInsert={ false }
__experimentalIsQuick
{ ...props }
/>
Expand Down

1 comment on commit 339aa2c

@github-actions
Copy link

@github-actions github-actions bot commented on 339aa2c Jan 5, 2023

Choose a reason for hiding this comment

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

Flaky tests detected in 339aa2c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3846175713
📝 Reported issues:

Please sign in to comment.