Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Block Editor: several little refactors #57107

Merged
merged 4 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
)
) ?? [];
Copy link
Contributor

@glendaviesnz glendaviesnz Dec 18, 2023

Choose a reason for hiding this comment

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

With this change it is now showing all patterns under the Uncategorized category for me, but I ran out of time to work out why. I can do some more testing tomorrow, and have a look to see how we could make this code path a bit clearer as to what is happening if you don't have time to look.

To test if you add a new synced pattern with a category it will not display under uncategorized in trunk, but will on this branch.

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
Loading