Skip to content

Commit

Permalink
Prioritize patterns in the main inserter
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed May 27, 2022
1 parent 02b87d7 commit be88a13
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
10 changes: 2 additions & 8 deletions packages/block-editor/src/components/inserter/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ class Inserter extends Component {
clientId={ clientId }
isAppender={ isAppender }
showInserterHelpPanel={ showInserterHelpPanel }
prioritizePatterns={ prioritizePatterns }
/>
);
}
Expand Down Expand Up @@ -213,8 +214,6 @@ export default compose( [
hasInserterItems,
__experimentalGetAllowedBlocks,
__experimentalGetDirectInsertBlock,
getBlockIndex,
getBlockCount,
getSettings,
} = select( blockEditorStore );

Expand All @@ -229,8 +228,6 @@ export default compose( [
rootClientId
);

const index = getBlockIndex( clientId );
const blockCount = getBlockCount();
const settings = getSettings();

const hasSingleBlockType =
Expand All @@ -252,10 +249,7 @@ export default compose( [
directInsertBlock,
rootClientId,
prioritizePatterns:
settings.__experimentalPreferPatternsOnRoot &&
! rootClientId &&
index > 0 &&
( index < blockCount || blockCount === 0 ),
settings.__experimentalPreferPatternsOnRoot && ! rootClientId,
};
} ),
withDispatch( ( dispatch, ownProps, { select } ) => {
Expand Down
17 changes: 12 additions & 5 deletions packages/block-editor/src/components/inserter/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ function InserterLibrary(
},
ref
) {
const destinationRootClientId = useSelect(
const { destinationRootClientId, prioritizePatterns } = useSelect(
( select ) => {
const { getBlockRootClientId } = select( blockEditorStore );

return (
rootClientId || getBlockRootClientId( clientId ) || undefined
const { getBlockRootClientId, getSettings } = select(
blockEditorStore
);

const _rootClientId =
rootClientId || getBlockRootClientId( clientId ) || undefined;
return {
destinationRootClientId: _rootClientId,
prioritizePatterns: getSettings()
.__experimentalPreferPatternsOnRoot,
};
},
[ clientId, rootClientId ]
);
Expand All @@ -51,6 +57,7 @@ function InserterLibrary(
__experimentalInsertionIndex={ __experimentalInsertionIndex }
__experimentalFilterValue={ __experimentalFilterValue }
shouldFocusBlock={ shouldFocusBlock }
prioritizePatterns={ prioritizePatterns }
ref={ ref }
/>
);
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ function InserterMenu(
showMostUsedBlocks,
__experimentalFilterValue = '',
shouldFocusBlock = true,
prioritizePatterns,
},
ref
) {
Expand Down Expand Up @@ -220,6 +221,7 @@ function InserterMenu(
<InserterTabs
showPatterns={ showPatterns }
showReusableBlocks={ hasReusableBlocks }
prioritizePatterns={ prioritizePatterns }
>
{ getCurrentTab }
</InserterTabs>
Expand Down
12 changes: 8 additions & 4 deletions packages/block-editor/src/components/inserter/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ function InserterTabs( {
showPatterns = false,
showReusableBlocks = false,
onSelect,
prioritizePatterns,
} ) {
const tabs = useMemo( () => {
const tempTabs = [ blocksTab ];

if ( showPatterns ) {
const tempTabs = [];
if ( prioritizePatterns && showPatterns ) {
tempTabs.push( patternsTab );
}
tempTabs.push( blocksTab );
if ( ! prioritizePatterns && showPatterns ) {
tempTabs.push( patternsTab );
}

if ( showReusableBlocks ) {
tempTabs.push( reusableBlocksTab );
}

return tempTabs;
}, [
prioritizePatterns,
blocksTab,
showPatterns,
patternsTab,
Expand Down

0 comments on commit be88a13

Please sign in to comment.