From 7a2ad9f1d70d234b2dc9b5b5f3adb040503d27cc Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 22 Nov 2023 12:10:11 +1300 Subject: [PATCH 1/4] Add a type to identify user patterns instead of relying on id field --- .../components/block-patterns-list/index.js | 20 ++++++++++--------- packages/block-editor/src/store/selectors.js | 1 + 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/block-editor/src/components/block-patterns-list/index.js b/packages/block-editor/src/components/block-patterns-list/index.js index bebeb1f44184c..7c6afc04fd519 100644 --- a/packages/block-editor/src/components/block-patterns-list/index.js +++ b/packages/block-editor/src/components/block-patterns-list/index.js @@ -82,7 +82,8 @@ function BlockPattern( { 'block-editor-block-patterns-list__item', { 'block-editor-block-patterns-list__list-item-synced': - pattern.id && ! pattern.syncStatus, + pattern.type === 'user' && + ! pattern.syncStatus, } ) } onClick={ () => { @@ -107,14 +108,15 @@ function BlockPattern( { /> - { pattern.id && ! pattern.syncStatus && ( -
- -
- ) } + { pattern.type === 'user' && + ! pattern.syncStatus && ( +
+ +
+ ) } { ( ! showTooltip || pattern.id ) && (
{ pattern.title } diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 548ad71664b5e..6338162a49db9 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -2287,6 +2287,7 @@ function getUserPatterns( state ) { return { name: `core/block/${ userPattern.id }`, id: userPattern.id, + type: 'user', title: userPattern.title.raw, categories: userPattern.wp_pattern_category.map( ( catId ) => categories && categories.get( catId ) From 2f68bb573c81cc9ece546d7a4793c67e2f4a6ee3 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 22 Nov 2023 12:34:46 +1300 Subject: [PATCH 2/4] Use existing constant instead of string --- .../src/components/block-patterns-list/index.js | 12 ++++++++---- .../inserter/block-patterns-explorer/pattern-list.js | 6 +++++- .../block-patterns-tab/pattern-category-previews.js | 6 +++++- .../block-patterns-tab/use-pattern-categories.js | 7 ++++++- .../components/inserter/block-patterns-tab/utils.js | 8 +++++--- .../components/inserter/hooks/use-patterns-state.js | 4 +++- packages/block-editor/src/store/selectors.js | 3 ++- 7 files changed, 34 insertions(+), 12 deletions(-) diff --git a/packages/block-editor/src/components/block-patterns-list/index.js b/packages/block-editor/src/components/block-patterns-list/index.js index 7c6afc04fd519..daf07fa5c7fc0 100644 --- a/packages/block-editor/src/components/block-patterns-list/index.js +++ b/packages/block-editor/src/components/block-patterns-list/index.js @@ -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 ) { @@ -71,7 +72,9 @@ function BlockPattern( { } } > - { pattern.type === 'user' && + { pattern.type === PATTERN_TYPES.user && ! pattern.syncStatus && (
) } - { ( ! showTooltip || pattern.id ) && ( + { ( ! showTooltip || + pattern.type === PATTERN_TYPES.user ) && (
{ pattern.title }
diff --git a/packages/block-editor/src/components/inserter/block-patterns-explorer/pattern-list.js b/packages/block-editor/src/components/inserter/block-patterns-explorer/pattern-list.js index 7cd2320a4fd1f..bf2867be5cdf3 100644 --- a/packages/block-editor/src/components/inserter/block-patterns-explorer/pattern-list.js +++ b/packages/block-editor/src/components/inserter/block-patterns-explorer/pattern-list.js @@ -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'; @@ -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' ) { diff --git a/packages/block-editor/src/components/inserter/block-patterns-tab/pattern-category-previews.js b/packages/block-editor/src/components/inserter/block-patterns-tab/pattern-category-previews.js index 2fef53cfa2a19..76864a6a00ccc 100644 --- a/packages/block-editor/src/components/inserter/block-patterns-tab/pattern-category-previews.js +++ b/packages/block-editor/src/components/inserter/block-patterns-tab/pattern-category-previews.js @@ -30,6 +30,7 @@ import { isPatternFiltered, allPatternsCategory, myPatternsCategory, + PATTERN_TYPES, } from './utils'; const noop = () => {}; @@ -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' ) { diff --git a/packages/block-editor/src/components/inserter/block-patterns-tab/use-pattern-categories.js b/packages/block-editor/src/components/inserter/block-patterns-tab/use-pattern-categories.js index 9c7a7a32a60c0..12e885954f4bf 100644 --- a/packages/block-editor/src/components/inserter/block-patterns-tab/use-pattern-categories.js +++ b/packages/block-editor/src/components/inserter/block-patterns-tab/use-pattern-categories.js @@ -14,6 +14,7 @@ import { isPatternFiltered, allPatternsCategory, myPatternsCategory, + PATTERN_TYPES, } from './utils'; export function usePatternCategories( rootClientId, sourceFilter = 'all' ) { @@ -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 ) { diff --git a/packages/block-editor/src/components/inserter/block-patterns-tab/utils.js b/packages/block-editor/src/components/inserter/block-patterns-tab/utils.js index 9f222c6a2f93c..439955eca92b3 100644 --- a/packages/block-editor/src/components/inserter/block-patterns-tab/utils.js +++ b/packages/block-editor/src/components/inserter/block-patterns-tab/utils.js @@ -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; } diff --git a/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js b/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js index 1924187e04179..576768c76abca 100644 --- a/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js +++ b/packages/block-editor/src/components/inserter/hooks/use-patterns-state.js @@ -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. @@ -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( diff --git a/packages/block-editor/src/store/selectors.js b/packages/block-editor/src/store/selectors.js index 6338162a49db9..c0441cd3b3755 100644 --- a/packages/block-editor/src/store/selectors.js +++ b/packages/block-editor/src/store/selectors.js @@ -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. @@ -2287,7 +2288,7 @@ function getUserPatterns( state ) { return { name: `core/block/${ userPattern.id }`, id: userPattern.id, - type: 'user', + type: PATTERN_TYPES.user, title: userPattern.title.raw, categories: userPattern.wp_pattern_category.map( ( catId ) => categories && categories.get( catId ) From f512fd3582e1ff159b522266e46da999eef25184 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 22 Nov 2023 13:18:00 +1300 Subject: [PATCH 3/4] Also update site editor uses of .id to identify user patterns --- .../edit-site/src/components/page-patterns/use-patterns.js | 2 +- packages/patterns/src/components/duplicate-pattern-modal.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/packages/edit-site/src/components/page-patterns/use-patterns.js b/packages/edit-site/src/components/page-patterns/use-patterns.js index 9cb6c8b998c41..be5992bd9b4ef 100644 --- a/packages/edit-site/src/components/page-patterns/use-patterns.js +++ b/packages/edit-site/src/components/page-patterns/use-patterns.js @@ -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; } ); diff --git a/packages/patterns/src/components/duplicate-pattern-modal.js b/packages/patterns/src/components/duplicate-pattern-modal.js index baf977a7b27e2..d8b75b420c6e7 100644 --- a/packages/patterns/src/components/duplicate-pattern-modal.js +++ b/packages/patterns/src/components/duplicate-pattern-modal.js @@ -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 ) From 8fce536b391ed9828281b3991ebf69ce446e9759 Mon Sep 17 00:00:00 2001 From: Glen Davies Date: Wed, 22 Nov 2023 13:34:41 +1300 Subject: [PATCH 4/4] Another instance --- .../patterns/src/components/duplicate-pattern-modal.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/patterns/src/components/duplicate-pattern-modal.js b/packages/patterns/src/components/duplicate-pattern-modal.js index d8b75b420c6e7..a62e7306dc90e 100644 --- a/packages/patterns/src/components/duplicate-pattern-modal.js +++ b/packages/patterns/src/components/duplicate-pattern-modal.js @@ -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)' ),