Skip to content

Commit

Permalink
Block Editor: several little refactors (#57107)
Browse files Browse the repository at this point in the history
* Block Editor: several little refactors

* Remove check for reusable blocks as this tab is not shown in the block editor since this were merged with patterns.

* Flip the no-categories condition for uncategorized

* Tidy up the uncategorized condition

---------

Co-authored-by: Glen Davies <glen.davies@automattic.com>
  • Loading branch information
jsnajdr and glendaviesnz authored Dec 18, 2023
1 parent df9f857 commit aa246d4
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,27 +70,27 @@ export function PatternCategoryPreviews( {
if ( category.name === allPatternsCategory.name ) {
return true;
}

if (
category.name === myPatternsCategory.name &&
pattern.type === PATTERN_TYPES.user
) {
return true;
}
if ( category.name !== 'uncategorized' ) {
return pattern.categories?.includes( category.name );
}

// The uncategorized category should show all the patterns without any category
// or with no available category.
const availablePatternCategories =
pattern.categories?.filter( ( cat ) =>
availableCategories.find(
( availableCategory ) =>
availableCategory.name === cat
)
) ?? [];
if ( category.name === 'uncategorized' ) {
// The uncategorized category should show all the patterns without any category...
if ( ! pattern.categories ) {
return true;
}

// ...or with no available category.
return ! pattern.categories.some( ( catName ) =>
availableCategories.some( ( c ) => c.name === catName )
);
}

return availablePatternCategories.length === 0;
return pattern.categories?.includes( category.name );
} ),
[
allPatterns,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useMemo, useCallback } from '@wordpress/element';
import { useMemo } from '@wordpress/element';
import { _x, _n, sprintf } from '@wordpress/i18n';

import { speak } from '@wordpress/a11y';
Expand All @@ -17,6 +17,16 @@ import {
PATTERN_TYPES,
} from './utils';

function hasRegisteredCategory( pattern, allCategories ) {
if ( ! pattern.categories || ! pattern.categories.length ) {
return false;
}

return pattern.categories.some( ( cat ) =>
allCategories.some( ( category ) => category.name === cat )
);
}

export function usePatternCategories( rootClientId, sourceFilter = 'all' ) {
const [ patterns, allCategories ] = usePatternsState(
undefined,
Expand All @@ -34,19 +44,6 @@ export function usePatternCategories( rootClientId, sourceFilter = 'all' ) {
[ sourceFilter, patterns ]
);

const hasRegisteredCategory = useCallback(
( pattern ) => {
if ( ! pattern.categories || ! pattern.categories.length ) {
return false;
}

return pattern.categories.some( ( cat ) =>
allCategories.some( ( category ) => category.name === cat )
);
},
[ allCategories ]
);

// Remove any empty categories.
const populatedCategories = useMemo( () => {
const categories = allCategories
Expand All @@ -59,7 +56,7 @@ export function usePatternCategories( rootClientId, sourceFilter = 'all' ) {

if (
filteredPatterns.some(
( pattern ) => ! hasRegisteredCategory( pattern )
( pattern ) => ! hasRegisteredCategory( pattern, allCategories )
) &&
! categories.find(
( category ) => category.name === 'uncategorized'
Expand Down Expand Up @@ -95,7 +92,7 @@ export function usePatternCategories( rootClientId, sourceFilter = 'all' ) {
)
);
return categories;
}, [ allCategories, filteredPatterns, hasRegisteredCategory ] );
}, [ allCategories, filteredPatterns ] );

return populatedCategories;
}
19 changes: 4 additions & 15 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,26 +67,18 @@ function InserterMenu(
insertionIndex: __experimentalInsertionIndex,
shouldFocusBlock,
} );
const { showPatterns, inserterItems } = useSelect(
const { showPatterns } = useSelect(
( select ) => {
const { hasAllowedPatterns, getInserterItems } = unlock(
select( blockEditorStore )
);
const { hasAllowedPatterns } = unlock( select( blockEditorStore ) );
return {
showPatterns: hasAllowedPatterns( destinationRootClientId ),
inserterItems: getInserterItems( destinationRootClientId ),
};
},
[ destinationRootClientId ]
);
const hasReusableBlocks = useMemo( () => {
return inserterItems.some(
( { category } ) => category === 'reusable'
);
}, [ inserterItems ] );

const mediaCategories = useMediaCategories( destinationRootClientId );
const showMedia = !! mediaCategories.length;
const showMedia = mediaCategories.length > 0;

const onInsert = useCallback(
( blocks, meta, shouldForceFocusBlock ) => {
Expand Down Expand Up @@ -211,9 +203,7 @@ function InserterMenu(
selectedTab === 'patterns' &&
! delayedFilterValue &&
selectedPatternCategory;
const showAsTabs =
! delayedFilterValue &&
( showPatterns || hasReusableBlocks || showMedia );
const showAsTabs = ! delayedFilterValue && ( showPatterns || showMedia );
const showMediaPanel =
selectedTab === 'media' &&
! delayedFilterValue &&
Expand Down Expand Up @@ -267,7 +257,6 @@ function InserterMenu(
{ showAsTabs && (
<InserterTabs
showPatterns={ showPatterns }
showReusableBlocks={ hasReusableBlocks }
showMedia={ showMedia }
prioritizePatterns={ prioritizePatterns }
onSelect={ handleSetSelectedTab }
Expand Down
23 changes: 7 additions & 16 deletions packages/block-editor/src/components/inserter/tabs.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
/**
* WordPress dependencies
*/
import { useMemo } from '@wordpress/element';
import { privateApis as componentsPrivateApis } from '@wordpress/components';
import { __ } from '@wordpress/i18n';

Expand Down Expand Up @@ -33,23 +32,15 @@ function InserterTabs( {
showPatterns = false,
showMedia = false,
onSelect,
prioritizePatterns,
prioritizePatterns = false,
tabsContents,
} ) {
const tabs = useMemo( () => {
const tempTabs = [];
if ( prioritizePatterns && showPatterns ) {
tempTabs.push( patternsTab );
}
tempTabs.push( blocksTab );
if ( ! prioritizePatterns && showPatterns ) {
tempTabs.push( patternsTab );
}
if ( showMedia ) {
tempTabs.push( mediaTab );
}
return tempTabs;
}, [ prioritizePatterns, showPatterns, showMedia ] );
const tabs = [
prioritizePatterns && showPatterns && patternsTab,
blocksTab,
! prioritizePatterns && showPatterns && patternsTab,
showMedia && mediaTab,
].filter( Boolean );

return (
<div className="block-editor-inserter__tabs">
Expand Down

1 comment on commit aa246d4

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in aa246d4.
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/7246264446
📝 Reported issues:

Please sign in to comment.