Skip to content

Commit

Permalink
Refactor components to hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlende committed Feb 24, 2024
1 parent 5718e9f commit e7b0cf7
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 68 deletions.

This file was deleted.

5 changes: 3 additions & 2 deletions packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import {
} from '../block-edit/context';
import { useTypingObserver } from '../observe-typing';
import { unlock } from '../../lock-unlock';
import MaybeDisableNonMainBlocks from './disable-non-main-blocks';
import useZoomOutBlockEditingMode from './use-zoom-out-block-editing-mode';

export const IntersectionObserver = createContext();
const pendingBlockVisibilityUpdatesPerRegistry = new WeakMap();
Expand Down Expand Up @@ -121,9 +121,10 @@ function Root( { className, ...settings } ) {
settings
);

useZoomOutBlockEditingMode();

return (
<IntersectionObserver.Provider value={ intersectionObserver }>
<MaybeDisableNonMainBlocks />
<div { ...innerBlocksProps } />
{ !! temporarilyEditingAsBlocks && (
<StopEditingAsBlocksOnOutsideSelect
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
*/
import { store as blockEditorStore } from '../../store';
import { useBlockEditContext } from '../block-edit/context';

export default function useZoomOutBlockEditingMode() {
const { clientId: rootClientId = '' } = useBlockEditContext();

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

const { isZoomOutMode, sectionsContainerClientId, sectionsClientIds } =
useSelect( ( select ) => {
const {
__unstableGetEditorMode,
getSectionsContainerClientId,
getClientIdsOfDescendants,
} = select( blockEditorStore );

// TODO: We need a better API as using the post type
// in block editor package is not allowed.
const postType = select( 'core/editor' ).getCurrentPostType();

const _sectionsContainerClientId =
getSectionsContainerClientId( postType );

return {
isZoomOutMode: __unstableGetEditorMode() === 'zoom-out',
sectionsContainerClientId: _sectionsContainerClientId,
sectionsClientIds: getClientIdsOfDescendants(
_sectionsContainerClientId
),
};
}, [] );

useEffect( () => {
if ( isZoomOutMode ) {
setBlockEditingMode( rootClientId, 'disabled' );
setBlockEditingMode( sectionsContainerClientId, 'contentOnly' );
sectionsClientIds.forEach( ( clientId ) => {
setBlockEditingMode( clientId, 'default' );
} );

return () => {
sectionsClientIds.forEach( ( clientId ) => {
unsetBlockEditingMode( clientId );
} );
unsetBlockEditingMode( sectionsContainerClientId );
unsetBlockEditingMode( rootClientId );
};
}
}, [
isZoomOutMode,
rootClientId,
sectionsContainerClientId,
// eslint-disable-next-line react-hooks/exhaustive-deps
...sectionsClientIds,
setBlockEditingMode,
unsetBlockEditingMode,
] );
}

0 comments on commit e7b0cf7

Please sign in to comment.