Skip to content
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

Improve user experience with blocks editor when a block is not registered #37646

Merged
merged 9 commits into from
Jan 6, 2022
13 changes: 12 additions & 1 deletion packages/blocks/src/api/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,25 @@ export function synchronizeBlocksWithTemplate( blocks = [], template ) {
attributes
);

const [
let [
blockName,
blockAttributes,
] = convertLegacyBlockNameAndAttributes(
name,
normalizedAttributes
);

// If a Block is undefined at this point, use the core/missing block as
// a placeholder for a better user experience.
if ( undefined === getBlockType( blockName ) ) {
blockAttributes = {
originalName: name,
originalContent: '',
originalUndelimitedContent: '',
};
blockName = 'core/missing';
}

return createBlock(
blockName,
blockAttributes,
Expand Down
19 changes: 19 additions & 0 deletions packages/blocks/src/api/test/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ describe( 'templates', () => {
category: 'text',
title: 'test block',
} );

registerBlockType( 'core/missing', {
attributes: {},
save: noop,
category: 'text',
title: 'missing block',
} );
} );

describe( 'doBlocksMatchTemplate', () => {
Expand Down Expand Up @@ -201,5 +208,17 @@ describe( 'templates', () => {
synchronizeBlocksWithTemplate( blockList, template )
).toEqual( [ block1 ] );
} );

it( 'should replace unregistered blocks from template with core/missing block', () => {
const template = [
[ 'core/test-block' ],
[ 'core/test-block-2' ],
[ 'core/test-faker' ],
];

expect(
synchronizeBlocksWithTemplate( [], template )[ 2 ].name
).toEqual( 'core/missing' );
} );
} );
} );