Skip to content

Commit

Permalink
Fix content only lock behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Oct 22, 2024
1 parent d5fa686 commit baf4093
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 27 deletions.
6 changes: 0 additions & 6 deletions packages/block-editor/src/components/inner-blocks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,11 @@ function UncontrolledInnerBlocks( props ) {
layout,
name,
blockType,
parentLock,
defaultLayout,
} = props;

useNestedSettingsUpdate(
clientId,
parentLock,
allowedBlocks,
prioritizedInserterBlocks,
defaultBlock,
Expand Down Expand Up @@ -196,7 +194,6 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
const {
getBlockName,
isZoomOut,
getTemplateLock,
getBlockRootClientId,
getBlockEditingMode,
getBlockSettings,
Expand Down Expand Up @@ -233,7 +230,6 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
),
name: blockName,
blockType: getBlockType( blockName ),
parentLock: getTemplateLock( parentClientId ),
parentClientId,
isDropZoneDisabled: _isDropZoneDisabled,
defaultLayout,
Expand All @@ -245,7 +241,6 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
__experimentalCaptureToolbars,
name,
blockType,
parentLock,
parentClientId,
isDropZoneDisabled,
defaultLayout,
Expand All @@ -272,7 +267,6 @@ export function useInnerBlocksProps( props = {}, options = {} ) {
layout,
name,
blockType,
parentLock,
defaultLayout,
...options,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,18 @@ function UncontrolledInnerBlocks( props ) {

const context = useBlockContext( clientId );

const { nestingLevel, parentLock } = useSelect(
const { nestingLevel } = useSelect(
( select ) => {
const { getBlockParents, getTemplateLock, getBlockRootClientId } =
select( blockEditorStore );
const { getBlockParents } = select( blockEditorStore );
return {
nestingLevel: getBlockParents( clientId )?.length,
parentLock: getTemplateLock( getBlockRootClientId( clientId ) ),
};
},
[ clientId ]
);

useNestedSettingsUpdate(
clientId,
parentLock,
allowedBlocks,
prioritizedInserterBlocks,
defaultBlock,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ function useShallowMemo( value ) {
* came from props.
*
* @param {string} clientId The client ID of the block to update.
* @param {string} parentLock
* @param {string[]} allowedBlocks An array of block names which are permitted
* in inner blocks.
* @param {string[]} prioritizedInserterBlocks Block names and/or block variations to be prioritized in the inserter, in the format {blockName}/{variationName}.
Expand All @@ -63,7 +62,6 @@ function useShallowMemo( value ) {
*/
export default function useNestedSettingsUpdate(
clientId,
parentLock,
allowedBlocks,
prioritizedInserterBlocks,
defaultBlock,
Expand All @@ -90,16 +88,11 @@ export default function useNestedSettingsUpdate(
prioritizedInserterBlocks
);

const _templateLock =
templateLock === undefined || parentLock === 'contentOnly'
? parentLock
: templateLock;

useLayoutEffect( () => {
const newSettings = {
allowedBlocks: _allowedBlocks,
prioritizedInserterBlocks: _prioritizedInserterBlocks,
templateLock: _templateLock,
templateLock,
};

// These values are not defined for RN, so only include them if they
Expand Down Expand Up @@ -176,7 +169,7 @@ export default function useNestedSettingsUpdate(
clientId,
_allowedBlocks,
_prioritizedInserterBlocks,
_templateLock,
templateLock,
defaultBlock,
directInsert,
__experimentalDefaultBlock,
Expand Down
5 changes: 4 additions & 1 deletion packages/block-editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
getClientIdsWithDescendants,
getBlockRootClientId,
__unstableGetEditorMode,
getBlockListSettings,
} from './selectors';
import {
checkAllowListRecursive,
Expand Down Expand Up @@ -522,7 +523,9 @@ export function isSectionBlock( state, clientId ) {
const sectionClientIds = getBlockOrder( state, sectionRootClientId );
return (
getBlockName( state, clientId ) === 'core/block' ||
getTemplateLock( state, clientId ) === 'contentOnly' ||
// This is different than getTemplateLock
// because children of sections are not sections automatically.
getBlockListSettings( state, clientId )?.templateLock ||
( ( __unstableGetEditorMode( state ) === 'navigation' ||
isZoomOut( state ) ) &&
sectionClientIds.includes( clientId ) )
Expand Down
14 changes: 12 additions & 2 deletions packages/block-editor/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -1572,7 +1572,17 @@ export function getTemplateLock( state, rootClientId ) {
return state.settings.templateLock ?? false;
}

return getBlockListSettings( state, rootClientId )?.templateLock ?? false;
const currentLock = getBlockListSettings(
state,
rootClientId
)?.templateLock;
if ( currentLock !== undefined ) {
return currentLock;
}
return getTemplateLock(
state,
getBlockRootClientId( state, rootClientId )
);
}

/**
Expand Down Expand Up @@ -2948,7 +2958,7 @@ export function __unstableHasActiveBlockOverlayActive( state, clientId ) {
return true;
}

// In navigation mode, the block overlay is active when the block is not
// For sections, the block overlay is active when the block is not
// selected (and doesn't contain a selected child). The same behavior is
// also enabled in all modes for blocks that have controlled children
// (reusable block, template part, navigation), unless explicitly disabled
Expand Down
33 changes: 29 additions & 4 deletions packages/block-editor/src/store/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -3830,28 +3830,45 @@ describe( 'selectors', () => {

it( 'should return false if the specified clientId was not found', () => {
const state = {
settings: { templateLock: 'all' },
settings: {},
blockListSettings: {
chicken: {
templateLock: 'insert',
},
ribs: {},
},
blocks: {
parents: new Map(
Object.entries( {
chicken: '',
ribs: '',
} )
),
},
};

expect( getTemplateLock( state, 'ribs' ) ).toBe( false );
} );

it( 'should return false if template lock was not set on the specified block', () => {
it( 'should return the parent lock if the specified clientId was not found', () => {
const state = {
settings: { templateLock: 'all' },
blockListSettings: {
chicken: {
test: 'tes1t',
templateLock: 'insert',
},
},
blocks: {
parents: new Map(
Object.entries( {
chicken: '',
ribs: '',
} )
),
},
};

expect( getTemplateLock( state, 'chicken' ) ).toBe( false );
expect( getTemplateLock( state, 'ribs' ) ).toBe( 'all' );
} );

it( 'should return the template lock for the specified clientId', () => {
Expand All @@ -3862,6 +3879,14 @@ describe( 'selectors', () => {
templateLock: 'insert',
},
},
blocks: {
parents: new Map(
Object.entries( {
chicken: '',
ribs: '',
} )
),
},
};

expect( getTemplateLock( state, 'chicken' ) ).toBe( 'insert' );
Expand Down
16 changes: 16 additions & 0 deletions test/e2e/specs/editor/various/content-only-lock.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ test.describe( 'Content-only lock', () => {
<!-- /wp:group -->` );

await pageUtils.pressKeys( 'secondary+M' );

// First click selects the section.
await editor.canvas
.locator( 'role=document[name="Block: Group"i]' )
.click();

// Second click selects the content.
await editor.canvas
.locator( 'role=document[name="Block: Paragraph"i]' )
.click();
Expand All @@ -50,9 +57,18 @@ test.describe( 'Content-only lock', () => {
<!-- /wp:group -->` );

await pageUtils.pressKeys( 'secondary+M' );

// First click selects the section.
await editor.canvas
.locator( 'role=document[name="Block: Group"i]' )
.first()
.click();

// Second click selects the content.
await editor.canvas
.locator( 'role=document[name="Block: Paragraph"i]' )
.click();

await page.keyboard.type( ' WP' );
await expect.poll( editor.getBlocks ).toMatchObject( [
{
Expand Down

0 comments on commit baf4093

Please sign in to comment.