-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SiteEditor: Refactor disable non page content blocks (#56103)
Co-authored-by: Kai Hao <kevin830726@gmail.com>
- Loading branch information
1 parent
a042bad
commit 52e9eb2
Showing
1 changed file
with
38 additions
and
33 deletions.
There are no files selected for viewing
71 changes: 38 additions & 33 deletions
71
...es/edit-site/src/components/page-content-focus-manager/disable-non-page-content-blocks.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } />; | ||
} ); | ||
} |
52e9eb2
There was a problem hiding this comment.
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:
/test/e2e/specs/editor/various/writing-flow.spec.js
/test/e2e/specs/editor/various/multi-block-selection.spec.js