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

Fix DisableNonPageContentBlocks #59297

Merged
merged 2 commits into from
Feb 27, 2024
Merged
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
5 changes: 0 additions & 5 deletions packages/editor/src/components/provider/constants.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,52 @@
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import {
useBlockEditingMode,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import { PAGE_CONTENT_BLOCK_TYPES } from './constants';
const PAGE_CONTENT_BLOCKS = [
'core/post-title',
'core/post-featured-image',
'core/post-content',
];

function useDisableNonPageContentBlocks() {
const contentIds = useSelect( ( select ) => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a question for my own understanding: it looks like this is safely not creating a new reference too frequently (i.e. it's used in the useEffect's dependencies and doesn't cause any issues). I'm used to the idea of trying to avoid .filter() calls within a useSelect because we then wind up with a new reference. In this case, is the reason that it's safe that contentIds is the value returned by the callback here, rather than destructuring a value within an object?

Copy link
Member

Choose a reason for hiding this comment

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

Correct, this is a "hack"; mapSelect returns an array of clientId strings, which can correctly pass shallow equality check.

I'm used to the idea of trying to avoid .filter() calls within a useSelect because we then wind up with a new reference.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, gotcha. Thanks for confirming!

Copy link
Member Author

Choose a reason for hiding this comment

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

Huh, I didn't know it was a "hack" 😀 I just prefer not returning multiple things from useSelect where possible as I think it's easier to read.

There's a warning if you do it wrong so I just let the computer do the thinking about this stuff.

console.warn(
`The 'useSelect' hook returns different values when called with the same state and parameters. This can lead to unnecessary rerenders.`
);

Copy link
Member

Choose a reason for hiding this comment

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

I mean “hack” because it’s not obvious why it’s okay to use filter in current situation if you’re not familiar with useSelect internals.

const { getBlocksByName, getBlockParents, getBlockName } =
select( blockEditorStore );
return getBlocksByName( PAGE_CONTENT_BLOCKS ).filter( ( clientId ) =>
getBlockParents( clientId ).every( ( parentClientId ) => {
const parentBlockName = getBlockName( parentClientId );
return (
parentBlockName !== 'core/query' &&
! PAGE_CONTENT_BLOCKS.includes( parentBlockName )
);
} )
);
}, [] );

function DisableBlock( { clientId } ) {
const isDescendentOfQueryLoop = useSelect(
( select ) => {
const { getBlockParentsByBlockName } = select( blockEditorStore );
return (
getBlockParentsByBlockName( clientId, 'core/query' ).length !==
0
);
},
[ clientId ]
);
const mode = isDescendentOfQueryLoop ? undefined : 'contentOnly';
const { setBlockEditingMode, unsetBlockEditingMode } =
useDispatch( blockEditorStore );

useEffect( () => {
if ( mode ) {
setBlockEditingMode( clientId, mode );
return () => {
unsetBlockEditingMode( clientId );
};
setBlockEditingMode( '', 'disabled' ); // Disable editing at the root level.

for ( const contentId of contentIds ) {
setBlockEditingMode( contentId, 'contentOnly' ); // Re-enable each content block.
}
}, [ clientId, mode, setBlockEditingMode, unsetBlockEditingMode ] );
return () => {
unsetBlockEditingMode( '' );
for ( const contentId of contentIds ) {
unsetBlockEditingMode( contentId );
}
};
}, [ contentIds, setBlockEditingMode, unsetBlockEditingMode ] );
}

/**
* Component that when rendered, makes it so that the site editor allows only
* page content to be edited.
*/
export default function DisableNonPageContentBlocks() {
useBlockEditingMode( 'disabled' );
const clientIds = useSelect( ( select ) => {
return select( blockEditorStore ).getBlocksByName(
PAGE_CONTENT_BLOCK_TYPES
);
}, [] );

return clientIds.map( ( clientId ) => {
return <DisableBlock key={ clientId } clientId={ clientId } />;
} );
useDisableNonPageContentBlocks();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/**
* External dependencies
*/
import { render } from '@testing-library/react';

/**
* WordPress dependencies
*/
import { createRegistry, RegistryProvider } from '@wordpress/data';

/**
* Internal dependencies
*/
import DisableNonPageContentBlocks from '../disable-non-page-content-blocks';

describe( 'DisableNonPageContentBlocks', () => {
it( 'disables page content blocks', () => {
const testBlocks = {
0: 'core/template-part',
/**/ '00': 'core/site-title',
/**/ '01': 'core/navigation',
1: 'core/group',
/**/ 10: 'core/post-title',
/**/ 11: 'core/post-featured-image',
/**/ 12: 'core/post-content',
/**/ /**/ 120: 'core/paragraph',
/**/ /**/ 121: 'core/post-featured-image',
2: 'core/query',
/**/ 20: 'core/post-title',
/**/ 21: 'core/post-featured-image',
/**/ 22: 'core/post-content',
3: 'core/template-part',
/**/ 30: 'core/paragraph',
};

const setBlockEditingMode = jest.fn( () => ( {
type: 'SET_BLOCK_EDITING_MODE',
} ) );
const unsetBlockEditingMode = jest.fn( () => ( {
type: 'UNSET_BLOCK_EDITING_MODE',
} ) );

const registry = createRegistry( {
'core/block-editor': {
reducer: () => {},
selectors: {
getBlocksByName( state, blockNames ) {
return Object.keys( testBlocks ).filter( ( clientId ) =>
blockNames.includes( testBlocks[ clientId ] )
);
},
getBlockParents( state, clientId ) {
return clientId.slice( 0, -1 ).split( '' );
},
getBlockName( state, clientId ) {
return testBlocks[ clientId ];
},
},
actions: {
setBlockEditingMode,
unsetBlockEditingMode,
},
},
} );

const { unmount } = render(
<RegistryProvider value={ registry }>
<DisableNonPageContentBlocks />
</RegistryProvider>
);

expect( setBlockEditingMode.mock.calls ).toEqual( [
[ '', 'disabled' ], // root
[ '10', 'contentOnly' ], // post-title
[ '11', 'contentOnly' ], // post-featured-image
[ '12', 'contentOnly' ], // post-content
// NOT the post-featured-image nested within post-content
// NOT any of the content blocks within query
] );

unmount();

expect( unsetBlockEditingMode.mock.calls ).toEqual( [
[ '' ], // root
[ '10' ], // post-title
[ '11' ], // post-featured-image
[ '12' ], // post-content
] );
} );
} );
Loading