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

Patterns: Fix issue with template in replace template screen #56407

Merged
merged 4 commits into from
Nov 22, 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
28 changes: 17 additions & 11 deletions packages/block-editor/src/components/block-patterns-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import { Icon, symbol } from '@wordpress/icons';
import BlockPreview from '../block-preview';
import InserterDraggableBlocks from '../inserter-draggable-blocks';
import BlockPatternsPaging from '../block-patterns-paging';
import { PATTERN_TYPES } from '../inserter/block-patterns-tab/utils';

const WithToolTip = ( { showTooltip, title, children } ) => {
if ( showTooltip ) {
Expand Down Expand Up @@ -71,7 +72,9 @@ function BlockPattern( {
} }
>
<WithToolTip
showTooltip={ showTooltip && ! pattern.id }
Copy link
Contributor Author

@glendaviesnz glendaviesnz Nov 22, 2023

Choose a reason for hiding this comment

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

The existing bug really only needed the change to pattern.type in this file, but I thought it was better to update all the uses of the pattern.id check now as the issue may appear in other areas if we start mixing patterns and templates more in the future.

showTooltip={
showTooltip && ! pattern.type !== PATTERN_TYPES.user
}
title={ pattern.title }
>
<CompositeItem
Expand All @@ -82,7 +85,8 @@ function BlockPattern( {
'block-editor-block-patterns-list__item',
{
'block-editor-block-patterns-list__list-item-synced':
pattern.id && ! pattern.syncStatus,
pattern.type === PATTERN_TYPES.user &&
! pattern.syncStatus,
}
) }
onClick={ () => {
Expand All @@ -107,15 +111,17 @@ function BlockPattern( {
/>

<HStack className="block-editor-patterns__pattern-details">
{ pattern.id && ! pattern.syncStatus && (
<div className="block-editor-patterns__pattern-icon-wrapper">
<Icon
className="block-editor-patterns__pattern-icon"
icon={ symbol }
/>
</div>
) }
{ ( ! showTooltip || pattern.id ) && (
{ pattern.type === PATTERN_TYPES.user &&
! pattern.syncStatus && (
<div className="block-editor-patterns__pattern-icon-wrapper">
<Icon
className="block-editor-patterns__pattern-icon"
icon={ symbol }
/>
</div>
) }
{ ( ! showTooltip ||
pattern.type === PATTERN_TYPES.user ) && (
<div className="block-editor-block-patterns-list__item-title">
{ pattern.title }
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { searchItems } from '../search-items';
import BlockPatternsPaging from '../../block-patterns-paging';
import usePatternsPaging from '../hooks/use-patterns-paging';
import {
PATTERN_TYPES,
allPatternsCategory,
myPatternsCategory,
} from '../block-patterns-tab/utils';
Expand Down Expand Up @@ -70,7 +71,10 @@ function PatternList( { searchValue, selectedCategory, patternCategories } ) {
if ( selectedCategory === allPatternsCategory.name ) {
return true;
}
if ( selectedCategory === myPatternsCategory.name && pattern.id ) {
if (
selectedCategory === myPatternsCategory.name &&
pattern.type === PATTERN_TYPES.user
) {
return true;
}
if ( selectedCategory === 'uncategorized' ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
isPatternFiltered,
allPatternsCategory,
myPatternsCategory,
PATTERN_TYPES,
} from './utils';

const noop = () => {};
Expand Down Expand Up @@ -69,7 +70,10 @@ export function PatternCategoryPreviews( {
if ( category.name === allPatternsCategory.name ) {
return true;
}
if ( category.name === myPatternsCategory.name && pattern.id ) {
if (
category.name === myPatternsCategory.name &&
pattern.type === PATTERN_TYPES.user
) {
return true;
}
if ( category.name !== 'uncategorized' ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
isPatternFiltered,
allPatternsCategory,
myPatternsCategory,
PATTERN_TYPES,
} from './utils';

export function usePatternCategories( rootClientId, sourceFilter = 'all' ) {
Expand Down Expand Up @@ -69,7 +70,11 @@ export function usePatternCategories( rootClientId, sourceFilter = 'all' ) {
label: _x( 'Uncategorized' ),
} );
}
if ( filteredPatterns.some( ( pattern ) => pattern.id ) ) {
if (
filteredPatterns.some(
( pattern ) => pattern.type === PATTERN_TYPES.user
)
) {
categories.unshift( myPatternsCategory );
}
if ( filteredPatterns.length > 0 ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,11 @@ export function isPatternFiltered( pattern, sourceFilter, syncFilter ) {
return true;
}

// If user source selected, filter out theme patterns. Any pattern without
// an id wasn't created by a user.
if ( sourceFilter === PATTERN_TYPES.user && ! pattern.id ) {
// If user source selected, filter out theme patterns.
if (
sourceFilter === PATTERN_TYPES.user &&
pattern.type !== PATTERN_TYPES.user
) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { store as noticesStore } from '@wordpress/notices';
* Internal dependencies
*/
import { store as blockEditorStore } from '../../../store';
import { PATTERN_TYPES } from '../block-patterns-tab/utils';

/**
* Retrieves the block patterns inserter state.
Expand Down Expand Up @@ -57,7 +58,8 @@ const usePatternsState = ( onInsert, rootClientId ) => {
const onClickPattern = useCallback(
( pattern, blocks ) => {
const patternBlocks =
pattern.id && pattern.syncStatus !== 'unsynced'
pattern.type === PATTERN_TYPES.user &&
pattern.syncStatus !== 'unsynced'
? [ createBlock( 'core/block', { ref: pattern.id } ) ]
: blocks;
onInsert(
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { createRegistrySelector } from '@wordpress/data';
* Internal dependencies
*/
import { orderBy } from '../utils/sorting';
import { PATTERN_TYPES } from '../components/inserter/block-patterns-tab/utils';

/**
* A block selection object.
Expand Down Expand Up @@ -2287,6 +2288,7 @@ function getUserPatterns( state ) {
return {
name: `core/block/${ userPattern.id }`,
id: userPattern.id,
type: PATTERN_TYPES.user,
title: userPattern.title.raw,
categories: userPattern.wp_pattern_category.map( ( catId ) =>
categories && categories.get( catId )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ const selectPatterns = createSelector(
// User patterns can have their sync statuses checked directly
// Non-user patterns are all unsynced for the time being.
patterns = patterns.filter( ( pattern ) => {
return pattern.id
return pattern.type === PATTERN_TYPES.user
? pattern.syncStatus === syncStatus
: syncStatus === PATTERN_SYNC_TYPES.unsynced;
} );
Expand Down
13 changes: 7 additions & 6 deletions packages/patterns/src/components/duplicate-pattern-modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import { store as noticesStore } from '@wordpress/notices';
* Internal dependencies
*/
import CreatePatternModal from './create-pattern-modal';
import { PATTERN_SYNC_TYPES } from '../constants';
import { PATTERN_SYNC_TYPES, PATTERN_TYPES } from '../constants';

function getTermLabels( pattern, categories ) {
// Theme patterns don't have an id and rely on core pattern categories.
if ( ! pattern.id ) {
// Theme patterns rely on core pattern categories.
if ( pattern.type !== PATTERN_TYPES.user ) {
return categories.core
?.filter( ( category ) =>
pattern.categories.includes( category.name )
Expand Down Expand Up @@ -52,9 +52,10 @@ export default function DuplicatePatternModal( {
const duplicatedProps = {
content: pattern.content,
defaultCategories: getTermLabels( pattern, categories ),
defaultSyncType: ! pattern.id // Theme patterns don't have an ID.
? PATTERN_SYNC_TYPES.unsynced
: pattern.wp_pattern_sync_status || PATTERN_SYNC_TYPES.full,
defaultSyncType:
pattern.type !== PATTERN_TYPES.user // Theme patterns are unsynced by default.
? PATTERN_SYNC_TYPES.unsynced
: pattern.wp_pattern_sync_status || PATTERN_SYNC_TYPES.full,
defaultTitle: sprintf(
/* translators: %s: Existing pattern title */
__( '%s (Copy)' ),
Expand Down
Loading