Skip to content

Commit

Permalink
SiteEditor: Refactor disable non page content blocks (#56103)
Browse files Browse the repository at this point in the history
Co-authored-by: Kai Hao <kevin830726@gmail.com>
  • Loading branch information
youknowriad and kevin940726 authored Nov 15, 2023
1 parent a042bad commit 52e9eb2
Showing 1 changed file with 38 additions and 33 deletions.
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 } />;
} );
}

1 comment on commit 52e9eb2

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 52e9eb2.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/6874064759
📝 Reported issues:

Please sign in to comment.