Skip to content

Commit

Permalink
Add template lock attribute to column and group.
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Oct 15, 2020
1 parent 935d70a commit 0abfa0c
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 3 deletions.
3 changes: 3 additions & 0 deletions packages/block-library/src/column/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
},
"width": {
"type": "string"
},
"templateLock": {
"type": "string"
}
},
"supports": {
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/column/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { useSelect, useDispatch } from '@wordpress/data';
import { __ } from '@wordpress/i18n';

function ColumnEdit( {
attributes: { verticalAlignment, width },
attributes: { verticalAlignment, width, templateLock = false },
setAttributes,
clientId,
} ) {
Expand Down Expand Up @@ -60,7 +60,7 @@ function ColumnEdit( {
style: width ? { flexBasis: width } : undefined,
} );
const innerBlocksProps = useInnerBlocksProps( blockProps, {
templateLock: false,
templateLock,
renderAppender: hasChildBlocks
? undefined
: InnerBlocks.ButtonBlockAppender,
Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/group/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"tagName": {
"type": "string",
"default": "div"
},
"templateLock": {
"type": "string"
}
},
"supports": {
Expand Down
3 changes: 2 additions & 1 deletion packages/block-library/src/group/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@ function GroupEdit( { attributes, clientId } ) {
[ clientId ]
);
const blockProps = useBlockProps();
const { tagName: TagName = 'div' } = attributes;
const { tagName: TagName = 'div', templateLock } = attributes;
const innerBlocksProps = useInnerBlocksProps(
{
className: 'wp-block-group__inner-container',
},
{
templateLock,
renderAppender: hasInnerBlocks
? undefined
: InnerBlocks.ButtonBlockAppender,
Expand Down
76 changes: 76 additions & 0 deletions packages/e2e-tests/plugins/cpt-locking.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,82 @@ function gutenberg_test_cpt_locking() {
'template_lock' => false,
)
);
register_post_type(
'ul-post-ul-group',
array(
'public' => true,
'label' => 'Locked Post Unlocked group',
'show_in_rest' => true,
'template' => array(
array(
'core/group',
array(
'templateLock' => false,
),
array(
array( 'core/quote' ),
array(
'core/paragraph',
array(
'placeholder' => 'Add a description343',
),
),
),
),
),
'template_lock' => 'all',
)
);
register_post_type(
'l-post-l-group',
array(
'public' => true,
'label' => 'Locked Post Locked group',
'show_in_rest' => true,
'template' => array(
array(
'core/group',
array(
'templateLock' => 'all',
),
array(
array( 'core/quote' ),
array(
'core/paragraph',
array(
'placeholder' => 'Add a description',
),
),
),
),
),
'template_lock' => 'all',
)
);
register_post_type(
'l-post-i-group',
array(
'public' => true,
'label' => 'Locked Post Inherited group',
'show_in_rest' => true,
'template' => array(
array(
'core/group',
array(),
array(
array( 'core/quote' ),
array(
'core/paragraph',
array(
'placeholder' => 'Add a description',
),
),
),
),
),
'template_lock' => 'all',
)
);
}

add_action( 'init', 'gutenberg_test_cpt_locking' );
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ exports[`cpt locking template_lock all should not error when deleting the cotent
<!-- /wp:columns -->"
`;
exports[`cpt locking template_lock all unlocked group should allow blocks to be moved 1`] = `
"<!-- wp:group {\\"templateLock\\":false} -->
<div class=\\"wp-block-group\\"><div class=\\"wp-block-group__inner-container\\"><!-- wp:paragraph {\\"placeholder\\":\\"Add a description343\\"} -->
<p>p1</p>
<!-- /wp:paragraph -->
<!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p></blockquote>
<!-- /wp:quote --></div></div>
<!-- /wp:group -->"
`;
exports[`cpt locking template_lock all unlocked group should allow blocks to be removed 1`] = `
"<!-- wp:group {\\"templateLock\\":false} -->
<div class=\\"wp-block-group\\"><div class=\\"wp-block-group__inner-container\\"><!-- wp:quote -->
<blockquote class=\\"wp-block-quote\\"><p></p></blockquote>
<!-- /wp:quote --></div></div>
<!-- /wp:group -->"
`;
exports[`cpt locking template_lock false should allow blocks to be inserted 1`] = `
"<!-- wp:image -->
<figure class=\\"wp-block-image\\"><img alt=\\"\\"/></figure>
Expand Down
63 changes: 63 additions & 0 deletions packages/e2e-tests/specs/editor/plugins/cpt-locking.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,67 @@ describe( 'cpt locking', () => {

it( 'should allow blocks to be moved', shouldAllowBlocksToBeMoved );
} );

describe( 'template_lock all unlocked group', () => {
beforeEach( async () => {
await createNewPost( {
postType: 'ul-post-ul-group',
} );
} );

it( 'should allow blocks to be removed', async () => {
await page.type(
'.block-editor-rich-text__editable[data-type="core/paragraph"]',
'p1'
);
await clickBlockToolbarButton( 'More options' );
const [ removeBlock ] = await page.$x(
'//button[contains(text(), "Remove block")]'
);
await removeBlock.click();
expect( await getEditedPostContent() ).toMatchSnapshot();
} );

it( 'should allow blocks to be moved', shouldAllowBlocksToBeMoved );
} );

describe( 'template_lock all locked group', () => {
beforeEach( async () => {
await createNewPost( {
postType: 'l-post-l-group',
} );
} );

it(
'should not allow blocks to be removed',
shouldNotAllowBlocksToBeRemoved
);

it( 'should not allow blocks to be moved', async () => {
await page.click(
'.block-editor-rich-text__editable[data-type="core/paragraph"]'
);
expect( await page.$( 'button[aria-label="Move up"]' ) ).toBeNull();
} );
} );

describe( 'template_lock all inherited group', () => {
beforeEach( async () => {
await createNewPost( {
postType: 'l-post-i-group',
} );
} );

it(
'should not allow blocks to be removed',
shouldNotAllowBlocksToBeRemoved
);

it( 'should not allow blocks to be moved', async () => {
await page.click(
'.block-editor-rich-text__editable[data-type="core/paragraph"]'
);
expect( await page.$( 'button[aria-label="Move up"]' ) ).toBeNull();
} );
} );
} );

0 comments on commit 0abfa0c

Please sign in to comment.