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

SiteEditor: Refactor disable non page content blocks #56103

Merged
merged 2 commits into from
Nov 15, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -1,52 +1,57 @@
/**
* WordPress dependencies
*/
import { createHigherOrderComponent } from '@wordpress/compose';
import { addFilter, removeFilter } from '@wordpress/hooks';
import { useBlockEditingMode } from '@wordpress/block-editor';
import { useSelect, useDispatch } from '@wordpress/data';
import {
useBlockEditingMode,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import { PAGE_CONTENT_BLOCK_TYPES } from '../../utils/constants';

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 );
};
}
}, [ clientId, mode, setBlockEditingMode, unsetBlockEditingMode ] );
}

/**
* Component that when rendered, makes it so that the site editor allows only
* page content to be edited.
*/
export default function DisableNonPageContentBlocks() {
useDisableNonPageContentBlocks();
return null;
}

/**
* Disables non-content blocks using the `useBlockEditingMode` hook.
*/
export function useDisableNonPageContentBlocks() {
useBlockEditingMode( 'disabled' );
useEffect( () => {
addFilter(
'editor.BlockEdit',
'core/edit-site/disable-non-content-blocks',
withDisableNonPageContentBlocks
const clientIds = useSelect( ( select ) => {
const { __experimentalGetGlobalBlocksByName } =
select( blockEditorStore );
return __experimentalGetGlobalBlocksByName(
Object.keys( PAGE_CONTENT_BLOCK_TYPES )
);
return () =>
removeFilter(
'editor.BlockEdit',
'core/edit-site/disable-non-content-blocks'
);
}, [] );
}

const withDisableNonPageContentBlocks = createHigherOrderComponent(
( BlockEdit ) => ( props ) => {
const isDescendentOfQueryLoop = props.context.queryId !== undefined;
const isPageContent =
PAGE_CONTENT_BLOCK_TYPES[ props.name ] && ! isDescendentOfQueryLoop;
const mode = isPageContent ? 'contentOnly' : undefined;
useBlockEditingMode( mode );
return <BlockEdit { ...props } />;
},
'withDisableNonPageContentBlocks'
);
return clientIds.map( ( clientId ) => {
return <DisableBlock key={ clientId } clientId={ clientId } />;
} );
Comment on lines +54 to +56
Copy link
Contributor

Choose a reason for hiding this comment

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

Does it matter that this will perform a new map on each render (returning a new array), or is that mitigated by the use of key within <DisableBlock />?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It doesn't matter. When React has an array of children, it compares one by one using arrays. Memoising the array is not expected.

Copy link
Contributor

Choose a reason for hiding this comment

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

Great, thanks for confirming!

}
Loading