-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 missing custom patterns in patterns explorer #51889
Merged
talldan
merged 6 commits into
fix/custom-patterns-error
from
fix/custom-patterns-missing-in-explorer
Jun 26, 2023
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f76948d
Add custom patterns to pattern explorer
glendaviesnz 11bf435
show custom patterns in the patterns explorer dialog
glendaviesnz fba73ad
remove changes from 51877
glendaviesnz 6f3bea4
Fix up use of async lists
glendaviesnz 7c94907
remove a bit of code duplication by adding a new hook
glendaviesnz 720d30f
add 51877 fix back to make testing easier
glendaviesnz File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,6 @@ import { | |
Button, | ||
} from '@wordpress/components'; | ||
import { Icon, chevronRight, chevronLeft } from '@wordpress/icons'; | ||
import { parse } from '@wordpress/blocks'; | ||
import { focus } from '@wordpress/dom'; | ||
|
||
/** | ||
|
@@ -28,7 +27,7 @@ import usePatternsState from './hooks/use-patterns-state'; | |
import BlockPatternList from '../block-patterns-list'; | ||
import PatternsExplorerModal from './block-patterns-explorer/explorer'; | ||
import MobileTabNavigation from './mobile-tab-navigation'; | ||
import useBlockTypesState from './hooks/use-block-types-state'; | ||
import useUnsyncedPatterns from './hooks/use-unsynced-patterns'; | ||
|
||
const noop = () => {}; | ||
|
||
|
@@ -51,18 +50,8 @@ function usePatternsCategories( rootClientId ) { | |
rootClientId | ||
); | ||
|
||
const [ unsyncedPatterns ] = useBlockTypesState( | ||
rootClientId, | ||
undefined, | ||
'unsynced' | ||
); | ||
const filteredUnsyncedPatterns = useUnsyncedPatterns( rootClientId ); | ||
|
||
const filteredUnsyncedPatterns = useMemo( () => { | ||
return unsyncedPatterns.filter( | ||
( { category: unsyncedPatternCategory } ) => | ||
unsyncedPatternCategory === 'reusable' | ||
); | ||
}, [ unsyncedPatterns ] ); | ||
const hasRegisteredCategory = useCallback( | ||
( pattern ) => { | ||
if ( ! pattern.categories || ! pattern.categories.length ) { | ||
|
@@ -169,24 +158,11 @@ export function BlockPatternsCategoryPanel( { | |
onInsert, | ||
rootClientId | ||
); | ||
const [ unsyncedPatterns ] = useBlockTypesState( | ||
const filteredUnsyncedPatterns = useUnsyncedPatterns( | ||
rootClientId, | ||
onInsert, | ||
'unsynced' | ||
true | ||
); | ||
const filteredUnsyncedPatterns = useMemo( () => { | ||
return unsyncedPatterns | ||
.filter( | ||
( { category: unsyncedPatternCategory } ) => | ||
unsyncedPatternCategory === 'reusable' | ||
) | ||
.map( ( syncedPattern ) => ( { | ||
...syncedPattern, | ||
blocks: parse( syncedPattern.content, { | ||
__unstableSkipMigrationLogs: true, | ||
} ), | ||
} ) ); | ||
}, [ unsyncedPatterns ] ); | ||
|
||
const availableCategories = usePatternsCategories( rootClientId ); | ||
const currentCategoryPatterns = useMemo( | ||
|
@@ -214,7 +190,14 @@ export function BlockPatternsCategoryPanel( { | |
category.name === 'reusable' | ||
? filteredUnsyncedPatterns | ||
: currentCategoryPatterns; | ||
const currentShownPatterns = useAsyncList( patterns ); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did have a single unsynced list here but that caused a lot of problems with list placeholders being left in place when switching between unsynched and other categories |
||
const unsyncedPatternsList = useAsyncList( filteredUnsyncedPatterns ); | ||
const categoryPatternsList = useAsyncList( currentCategoryPatterns ); | ||
|
||
const currentShownPatterns = | ||
category.name === 'reusable' | ||
? unsyncedPatternsList | ||
: categoryPatternsList; | ||
|
||
// Hide block pattern preview on unmount. | ||
useEffect( () => () => onHover( null ), [] ); | ||
|
38 changes: 38 additions & 0 deletions
38
packages/block-editor/src/components/inserter/hooks/use-unsynced-patterns.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useMemo } from '@wordpress/element'; | ||
import { parse } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import useBlockTypesState from '../hooks/use-block-types-state'; | ||
|
||
export default function useUnsyncedPatterns( | ||
rootClientId, | ||
onInsert, | ||
withBlocks | ||
) { | ||
const [ unsyncedPatterns ] = useBlockTypesState( | ||
rootClientId, | ||
onInsert, | ||
'unsynced' | ||
); | ||
const filteredUnsyncedPatterns = useMemo( () => { | ||
return unsyncedPatterns | ||
.filter( | ||
( { category: unsyncedPatternCategory } ) => | ||
unsyncedPatternCategory === 'reusable' | ||
) | ||
.map( ( syncedPattern ) => ( { | ||
...syncedPattern, | ||
blocks: withBlocks | ||
? parse( syncedPattern.content, { | ||
__unstableSkipMigrationLogs: true, | ||
} ) | ||
: undefined, | ||
} ) ); | ||
}, [ unsyncedPatterns, withBlocks ] ); | ||
return filteredUnsyncedPatterns; | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes in this file are copied from #51877 to make testing easier.