Skip to content

Commit

Permalink
[Patterns]: Add a flag to hide patterns from UI (#36108)
Browse files Browse the repository at this point in the history
* [Patterns]: Add a flag to hide patterns from UI

* rename flag
  • Loading branch information
ntsekouras authored Nov 1, 2021
1 parent f2df34c commit d21da6a
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 3 deletions.
8 changes: 5 additions & 3 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1901,9 +1901,11 @@ const getAllAllowedPatterns = createSelector(
( state ) => {
const patterns = state.settings.__experimentalBlockPatterns;
const { allowedBlockTypes } = getSettings( state );
const parsedPatterns = patterns.map( ( { name } ) =>
__experimentalGetParsedPattern( state, name )
);
const parsedPatterns = patterns
.filter( ( { inserter = true } ) => !! inserter )
.map( ( { name } ) =>
__experimentalGetParsedPattern( state, name )
);
const allowedPatterns = parsedPatterns.filter( ( { blocks } ) =>
checkAllowListRecursive( blocks, allowedBlockTypes )
);
Expand Down
79 changes: 79 additions & 0 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const {
getLowestCommonAncestorWithSelectedBlock,
__experimentalGetActiveBlockIdByBlockNames: getActiveBlockIdByBlockNames,
__experimentalGetAllowedPatterns,
__experimentalGetParsedPattern,
__experimentalGetPatternsByBlockTypes,
__unstableGetClientIdWithClientIdsTree,
__unstableGetClientIdsTree,
Expand Down Expand Up @@ -3330,6 +3331,13 @@ describe( 'selectors', () => {
content:
'<!-- wp:test-block-b --><!-- /wp:test-block-b -->',
},
{
name: 'pattern-c',
title: 'pattern hidden from UI',
inserter: false,
content:
'<!-- wp:test-block-a --><!-- /wp:test-block-a -->',
},
],
},
};
Expand All @@ -3349,6 +3357,77 @@ describe( 'selectors', () => {
__experimentalGetAllowedPatterns( state, 'block2' )
).toHaveLength( 0 );
} );
it( 'should return empty array if only patterns hidden from UI exist', () => {
expect(
__experimentalGetAllowedPatterns( {
blocks: { byClientId: {} },
blockListSettings: {},
settings: {
__experimentalBlockPatterns: [
{
name: 'pattern-c',
title: 'pattern hidden from UI',
inserter: false,
content:
'<!-- wp:test-block-a --><!-- /wp:test-block-a -->',
},
],
},
} )
).toHaveLength( 0 );
} );
} );
describe( '__experimentalGetParsedPattern', () => {
const state = {
settings: {
__experimentalBlockPatterns: [
{
name: 'pattern-a',
title: 'pattern with a',
content: `<!-- wp:test-block-a --><!-- /wp:test-block-a -->`,
},
{
name: 'pattern-hidden-from-ui',
title: 'pattern hidden from UI',
inserter: false,
content:
'<!-- wp:test-block-a --><!-- /wp:test-block-a --><!-- wp:test-block-b --><!-- /wp:test-block-b -->',
},
],
},
};
it( 'should return proper results when pattern does not exist', () => {
expect(
__experimentalGetParsedPattern( state, 'not there' )
).toBeNull();
} );
it( 'should return existing pattern properly parsed', () => {
const { name, blocks } = __experimentalGetParsedPattern(
state,
'pattern-a'
);
expect( name ).toEqual( 'pattern-a' );
expect( blocks ).toHaveLength( 1 );
expect( blocks[ 0 ] ).toEqual(
expect.objectContaining( {
name: 'core/test-block-a',
} )
);
} );
it( 'should return hidden from UI pattern when requested', () => {
const { name, blocks, inserter } = __experimentalGetParsedPattern(
state,
'pattern-hidden-from-ui'
);
expect( name ).toEqual( 'pattern-hidden-from-ui' );
expect( inserter ).toBeFalsy();
expect( blocks ).toHaveLength( 2 );
expect( blocks[ 0 ] ).toEqual(
expect.objectContaining( {
name: 'core/test-block-a',
} )
);
} );
} );
describe( '__experimentalGetPatternsByBlockTypes', () => {
const state = {
Expand Down

0 comments on commit d21da6a

Please sign in to comment.