Skip to content

Commit

Permalink
Retain previous ignoredHookedBlocks
Browse files Browse the repository at this point in the history
  • Loading branch information
ockham committed Feb 8, 2024
1 parent dfae816 commit c257a66
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/blocks/src/api/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,23 @@ export function synchronizeBlocksWithTemplate( blocks = [], template ) {
];

if ( ignoredHookedBlocks.length ) {
const { metadata, ...otherAttributes } = blockAttributes;
const { metadata = {}, ...otherAttributes } = blockAttributes;
const {
ignoredHookedBlocks: ignoredHookedBlocksFromTemplate = [],
...otherMetadata
} = metadata;

const newIgnoredHookedBlocks = [
...new Set( [
...ignoredHookedBlocks,
...ignoredHookedBlocksFromTemplate,
] ),
];

blockAttributes = {
metadata: {
ignoredHookedBlocks,
...metadata,
ignoredHookedBlocks: newIgnoredHookedBlocks,
...otherMetadata,
},
...otherAttributes,
};
Expand Down
42 changes: 42 additions & 0 deletions packages/blocks/src/api/test/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,48 @@ describe( 'templates', () => {
] );
} );

it( 'retains previously set ignoredHookedBlocks metadata', () => {
registerBlockType( 'core/hooked-block', {
attributes: {},
save: noop,
category: 'text',
title: 'hooked block',
blockHooks: { 'core/test-block': 'after' },
} );

const template = [
[
'core/test-block',
{
metadata: {
ignoredHookedBlocks: [ 'core/other-hooked-block' ],
},
},
],
[ 'core/test-block-2' ],
[ 'core/test-block-2' ],
];
const blockList = [];

expect(
synchronizeBlocksWithTemplate( blockList, template )
).toMatchObject( [
{
name: 'core/test-block',
attributes: {
metadata: {
ignoredHookedBlocks: [
'core/hooked-block',
'core/other-hooked-block',
],
},
},
},
{ name: 'core/test-block-2' },
{ name: 'core/test-block-2' },
] );
} );

it( 'should create nested blocks', () => {
const template = [
[ 'core/test-block', {}, [ [ 'core/test-block-2' ] ] ],
Expand Down

0 comments on commit c257a66

Please sign in to comment.