Skip to content

Commit

Permalink
Revise logic around directInsert
Browse files Browse the repository at this point in the history
  • Loading branch information
stokesman committed Sep 14, 2023
1 parent bc4163b commit 2c74a13
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 49 deletions.
98 changes: 50 additions & 48 deletions packages/block-editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,16 +195,14 @@ class PrivateInserter extends Component {
const {
position,
hasSingleBlockType,
directInsert,
directInsertBlock,
insertOnlyAllowedBlock,
__experimentalIsQuick: isQuick,
onSelectOrClose,
} = this.props;

if (
hasSingleBlockType ||
( directInsertBlock && hasSingleBlockType )
) {
if ( hasSingleBlockType || ( directInsertBlock && directInsert ) ) {
return this.renderToggle( { onToggle: insertOnlyAllowedBlock } );
}

Expand All @@ -228,51 +226,55 @@ class PrivateInserter extends Component {
}

export const ComposedPrivateInserter = compose( [
withSelect(
( select, { clientId, rootClientId, shouldDirectInsert = true } ) => {
const {
getBlockRootClientId,
hasInserterItems,
getAllowedBlocks,
getDirectInsertBlock,
getSettings,
} = select( blockEditorStore );

const { getBlockVariations } = select( blocksStore );

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

const allowedBlocks = getAllowedBlocks( rootClientId );

const directInsertBlock =
shouldDirectInsert && getDirectInsertBlock( 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,
};
withSelect( ( select, { clientId, rootClientId, shouldDirectInsert } ) => {
const {
getBlock,
getBlockRootClientId,
hasInserterItems,
getAllowedBlocks,
getSettings,
getBlockListSettings,
} = select( blockEditorStore );

const { getBlockVariations } = select( blocksStore );

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

const allowedBlocks = getAllowedBlocks( rootClientId );

const { defaultBlock, directInsert } =
getBlockListSettings( rootClientId ) ?? {};

const resolvedDirectInsert =
shouldDirectInsert ?? typeof directInsert === 'function'
? directInsert( getBlock( rootClientId ) )
: directInsert;

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,
directInsert: resolvedDirectInsert,
directInsertBlock: defaultBlock,
rootClientId,
prioritizePatterns:
settings.__experimentalPreferPatternsOnRoot && ! rootClientId,
};
} ),
withDispatch( ( dispatch, ownProps, { select } ) => {
return {
insertOnlyAllowedBlock() {
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2229,7 +2229,7 @@ export const getDirectInsertBlock = createSelector(
state.blockListSettings[ rootClientId ]?.defaultBlock;
const directInsert =
state.blockListSettings[ rootClientId ]?.directInsert;
if ( ! defaultBlock || directInsert === false ) {
if ( ! defaultBlock || ! directInsert ) {
return;
}
if ( typeof directInsert === 'function' ) {
Expand Down

0 comments on commit 2c74a13

Please sign in to comment.