-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d23898a
commit eb576e9
Showing
5 changed files
with
128 additions
and
124 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { PATTERN_TYPES } from '../components/inserter/block-patterns-tab/utils'; | ||
|
||
const EMPTY_ARRAY = []; | ||
|
||
export function getUserPatterns( state ) { | ||
const userPatterns = | ||
state?.settings?.__experimentalReusableBlocks ?? EMPTY_ARRAY; | ||
const userPatternCategories = | ||
state?.settings?.__experimentalUserPatternCategories ?? []; | ||
const categories = new Map(); | ||
userPatternCategories.forEach( ( userCategory ) => | ||
categories.set( userCategory.id, userCategory ) | ||
); | ||
return userPatterns.map( ( userPattern ) => { | ||
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 ) | ||
? categories.get( catId ).slug | ||
: catId | ||
), | ||
content: userPattern.content.raw, | ||
syncStatus: userPattern.wp_pattern_sync_status, | ||
}; | ||
} ); | ||
} | ||
|
||
export const checkAllowList = ( list, item, defaultResult = null ) => { | ||
if ( typeof list === 'boolean' ) { | ||
return list; | ||
} | ||
if ( Array.isArray( list ) ) { | ||
// TODO: when there is a canonical way to detect that we are editing a post | ||
// the following check should be changed to something like: | ||
// if ( list.includes( 'core/post-content' ) && getEditorMode() === 'post-content' && item === null ) | ||
if ( list.includes( 'core/post-content' ) && item === null ) { | ||
return true; | ||
} | ||
return list.includes( item ); | ||
} | ||
return defaultResult; | ||
}; | ||
|
||
export const checkAllowListRecursive = ( blocks, allowedBlockTypes ) => { | ||
if ( typeof allowedBlockTypes === 'boolean' ) { | ||
return allowedBlockTypes; | ||
} | ||
|
||
const blocksQueue = [ ...blocks ]; | ||
while ( blocksQueue.length > 0 ) { | ||
const block = blocksQueue.shift(); | ||
|
||
const isAllowed = checkAllowList( | ||
allowedBlockTypes, | ||
block.name || block.blockName, | ||
true | ||
); | ||
if ( ! isAllowed ) { | ||
return false; | ||
} | ||
|
||
block.innerBlocks?.forEach( ( innerBlock ) => { | ||
blocksQueue.push( innerBlock ); | ||
} ); | ||
} | ||
|
||
return true; | ||
}; |