Skip to content

Commit

Permalink
Template lock: batch block disabling (#60934)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <[email protected]>
Co-authored-by: noisysocks <[email protected]>
  • Loading branch information
3 people authored Apr 22, 2024
1 parent 2485128 commit b99607d
Showing 1 changed file with 23 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect, useRegistry } from '@wordpress/data';
import { store as blockEditorStore } from '@wordpress/block-editor';
import { useEffect } from '@wordpress/element';
import { applyFilters } from '@wordpress/hooks';
Expand All @@ -18,6 +18,8 @@ const CONTENT_ONLY_BLOCKS = applyFilters( 'editor.postContentBlockTypes', [
* page content to be edited.
*/
export default function DisableNonPageContentBlocks() {
// Note that there are two separate subscription because the result for each
// returns a new array.
const contentOnlyIds = useSelect( ( select ) => {
const { getBlocksByName, getBlockParents, getBlockName } =
select( blockEditorStore );
Expand All @@ -33,41 +35,41 @@ export default function DisableNonPageContentBlocks() {
} )
);
}, [] );

const disabledIds = useSelect( ( select ) => {
const { getBlocksByName, getBlockOrder } = select( blockEditorStore );
return getBlocksByName( [ 'core/template-part' ] ).flatMap(
( clientId ) => getBlockOrder( clientId )
);
}, [] );

const { setBlockEditingMode, unsetBlockEditingMode } =
useDispatch( blockEditorStore );
const registry = useRegistry();

useEffect( () => {
setBlockEditingMode( '', 'disabled' );
for ( const clientId of contentOnlyIds ) {
setBlockEditingMode( clientId, 'contentOnly' );
}
for ( const clientId of disabledIds ) {
setBlockEditingMode( clientId, 'disabled' );
}
const { setBlockEditingMode, unsetBlockEditingMode } =
registry.dispatch( blockEditorStore );

return () => {
unsetBlockEditingMode( '' );
registry.batch( () => {
setBlockEditingMode( '', 'disabled' );
for ( const clientId of contentOnlyIds ) {
unsetBlockEditingMode( clientId );
setBlockEditingMode( clientId, 'contentOnly' );
}
for ( const clientId of disabledIds ) {
unsetBlockEditingMode( clientId );
setBlockEditingMode( clientId, 'disabled' );
}
} );

return () => {
registry.batch( () => {
unsetBlockEditingMode( '' );
for ( const clientId of contentOnlyIds ) {
unsetBlockEditingMode( clientId );
}
for ( const clientId of disabledIds ) {
unsetBlockEditingMode( clientId );
}
} );
};
}, [
contentOnlyIds,
disabledIds,
setBlockEditingMode,
unsetBlockEditingMode,
] );
}, [ contentOnlyIds, disabledIds, registry ] );

return null;
}

0 comments on commit b99607d

Please sign in to comment.