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

Blocks API: Add 'lock' attribute during block registration #35389

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import './compat';
import './align';
import './lock';
import './anchor';
import './custom-class-name';
import './generated-class-name';
Expand Down
34 changes: 0 additions & 34 deletions packages/block-editor/src/hooks/lock.js

This file was deleted.

12 changes: 12 additions & 0 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
isObject,
isPlainObject,
isString,
has,
mapKeys,
omit,
pick,
Expand Down Expand Up @@ -365,6 +366,17 @@ export function registerBlockType( blockNameOrMetadata, settings ) {
return;
}

// Handle `lock` as a "build-in" attribute.
// But allow blocks to specify their own with defaults.
if ( ! has( settings.attributes, [ 'lock', 'type' ] ) ) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed while updating unit tests that there's an expectation for empty attributes if the block doesn't have them.

Should I update the condition and only add the lock attribute if attributes aren't empty?

settings.attributes = {
...settings.attributes,
lock: {
type: 'object',
},
};
}

dispatch( blocksStore ).addBlockTypes( settings );

return settings;
Expand Down
78 changes: 60 additions & 18 deletions packages/blocks/src/api/test/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ describe( 'blocks', () => {
category: 'text',
title: 'block title',
};
const lockAttribute = {
lock: {
type: 'object',
},
};

beforeAll( () => {
// Initialize the block store.
Expand Down Expand Up @@ -121,7 +126,9 @@ describe( 'blocks', () => {
expect( block ).toEqual( {
name: 'my-plugin/fancy-block-4',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
keywords: [],
Expand Down Expand Up @@ -266,7 +273,9 @@ describe( 'blocks', () => {
title: 'block title',
category: 'text',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
keywords: [],
Expand Down Expand Up @@ -298,6 +307,7 @@ describe( 'blocks', () => {
title: 'block title',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {
...lockAttribute,
ok: {
type: 'boolean',
},
Expand Down Expand Up @@ -332,7 +342,9 @@ describe( 'blocks', () => {
save: expect.any( Function ),
title: 'block title',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
keywords: [],
Expand Down Expand Up @@ -363,7 +375,9 @@ describe( 'blocks', () => {
save: expect.any( Function ),
title: 'block title',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {
fontSize: 'fontSize',
},
Expand Down Expand Up @@ -400,7 +414,9 @@ describe( 'blocks', () => {
title: 'block title',
category: 'widgets',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
keywords: [],
Expand Down Expand Up @@ -467,7 +483,9 @@ describe( 'blocks', () => {
</svg>
),
},
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
keywords: [],
Expand Down Expand Up @@ -497,7 +515,9 @@ describe( 'blocks', () => {
icon: {
src: 'foo',
},
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
keywords: [],
Expand Down Expand Up @@ -541,7 +561,9 @@ describe( 'blocks', () => {
icon: {
src: MyTestIcon,
},
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
keywords: [],
Expand Down Expand Up @@ -599,7 +621,9 @@ describe( 'blocks', () => {
</svg>
),
},
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
keywords: [],
Expand All @@ -624,7 +648,9 @@ describe( 'blocks', () => {
category: 'text',
title: 'block title',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
keywords: [],
Expand Down Expand Up @@ -816,7 +842,9 @@ describe( 'blocks', () => {
src: 'palmtree',
},
keywords: [],
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
supports: {},
Expand Down Expand Up @@ -882,7 +910,9 @@ describe( 'blocks', () => {
src: 'palmtree',
},
keywords: [ 'i18n (translated)', 'metadata (translated)' ],
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
supports: {},
Expand Down Expand Up @@ -946,7 +976,9 @@ describe( 'blocks', () => {
category: 'text',
title: 'block title',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
keywords: [],
Expand All @@ -963,7 +995,9 @@ describe( 'blocks', () => {
category: 'text',
title: 'block title',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
keywords: [],
Expand Down Expand Up @@ -1041,7 +1075,9 @@ describe( 'blocks', () => {
category: 'text',
title: 'block title',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
keywords: [],
Expand All @@ -1065,7 +1101,9 @@ describe( 'blocks', () => {
category: 'text',
title: 'block title',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
keywords: [],
Expand Down Expand Up @@ -1096,7 +1134,9 @@ describe( 'blocks', () => {
category: 'text',
title: 'block title',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
keywords: [],
Expand All @@ -1111,7 +1151,9 @@ describe( 'blocks', () => {
category: 'text',
title: 'block title',
icon: { src: BLOCK_ICON_DEFAULT },
attributes: {},
attributes: {
...lockAttribute,
},
providesContext: {},
usesContext: [],
keywords: [],
Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/api/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,10 +359,10 @@ describe( '__experimentalGetBlockAttributesNamesByRole', () => {
'core/test-block-3',
].forEach( unregisterBlockType );
} );
it( 'should return empty array if block has no attributes', () => {
it( 'should return lock attribute block has no other attributes', () => {
expect(
__experimentalGetBlockAttributesNamesByRole( 'core/test-block-3' )
).toEqual( [] );
).toEqual( [ 'lock' ] );
} );
it( 'should return all attribute names if no role is provided', () => {
expect(
Expand Down