Skip to content

Commit

Permalink
Make private with symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed Dec 10, 2023
1 parent 952096b commit 69c9d34
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 30 deletions.
13 changes: 8 additions & 5 deletions packages/block-editor/src/components/block-controls/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@
* Internal dependencies
*/
import groups from './groups';
import { useBlockEditContext } from '../block-edit/context';
import {
useBlockEditContext,
mayDisplayControlsKey,
mayDisplayParentControlsKey,
} from '../block-edit/context';

export default function useBlockControlsFill( group, shareWithChildBlocks ) {
const { mayDisplayControls, mayDisplayParentControls } =
useBlockEditContext();
if ( mayDisplayControls ) {
const context = useBlockEditContext();
if ( context[ mayDisplayControlsKey ] ) {
return groups[ group ]?.Fill;
}
if ( mayDisplayParentControls && shareWithChildBlocks ) {
if ( context[ mayDisplayParentControlsKey ] && shareWithChildBlocks ) {
return groups.parent.Fill;
}
return null;
Expand Down
3 changes: 3 additions & 0 deletions packages/block-editor/src/components/block-edit/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
*/
import { createContext, useContext } from '@wordpress/element';

export const mayDisplayControlsKey = Symbol( 'mayDisplayControls' );
export const mayDisplayParentControlsKey = Symbol( 'mayDisplayParentControls' );

export const DEFAULT_BLOCK_EDIT_CONTEXT = {
name: '',
isSelected: false,
Expand Down
38 changes: 27 additions & 11 deletions packages/block-editor/src/components/block-edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ import { hasBlockSupport } from '@wordpress/blocks';
* Internal dependencies
*/
import Edit from './edit';
import { BlockEditContextProvider, useBlockEditContext } from './context';
import {
BlockEditContextProvider,
useBlockEditContext,
mayDisplayControlsKey,
mayDisplayParentControlsKey,
} from './context';

/**
* The `useBlockEditContext` hook provides information about the block this hook is being used in.
Expand Down Expand Up @@ -38,21 +43,32 @@ export default function BlockEdit( {
const layoutSupport =
hasBlockSupport( name, 'layout', false ) ||
hasBlockSupport( name, '__experimentalLayout', false );
const context = {
name,
isSelected,
clientId,
layout: layoutSupport ? layout : null,
__unstableLayoutClassNames,
mayDisplayControls,
mayDisplayParentControls,
};
return (
<BlockEditContextProvider
// It is important to return the same object if props haven't
// changed to avoid unnecessary rerenders.
// See https://reactjs.org/docs/context.html#caveats.
value={ useMemo( () => context, Object.values( context ) ) }
value={ useMemo(
() => ( {
name,
isSelected,
clientId,
layout: layoutSupport ? layout : null,
__unstableLayoutClassNames,
[ mayDisplayControlsKey ]: mayDisplayControls,
[ mayDisplayParentControlsKey ]: mayDisplayParentControls,
} ),
[
name,
isSelected,
clientId,
layoutSupport,
layout,
__unstableLayoutClassNames,
mayDisplayControls,
mayDisplayParentControls,
]
) }
>
<Edit { ...props } />
</BlockEditContextProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@ import { privateApis as componentsPrivateApis } from '@wordpress/components';
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import { useBlockEditContext } from '../block-edit/context';
import {
useBlockEditContext,
mayDisplayControlsKey,
} from '../block-edit/context';

const { createPrivateSlotFill } = unlock( componentsPrivateApis );
const { Fill, Slot } = createPrivateSlotFill( 'BlockInformation' );

const BlockInfo = ( props ) => {
const { mayDisplayControls } = useBlockEditContext();
if ( ! mayDisplayControls ) {
const context = useBlockEditContext();
if ( ! context[ mayDisplayControlsKey ] ) {
return null;
}
return <Fill { ...props } />;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { useEffect, useContext } from '@wordpress/element';
/**
* Internal dependencies
*/
import { useBlockEditContext } from '../block-edit/context';
import {
useBlockEditContext,
mayDisplayControlsKey,
} from '../block-edit/context';
import groups from './groups';

export default function InspectorControlsFill( {
Expand All @@ -33,13 +36,13 @@ export default function InspectorControlsFill( {
group = __experimentalGroup;
}

const { mayDisplayControls } = useBlockEditContext();
const context = useBlockEditContext();
const Fill = groups[ group ]?.Fill;
if ( ! Fill ) {
warning( `Unknown InspectorControls group "${ group }" provided.` );
return null;
}
if ( ! mayDisplayControls ) {
if ( ! context[ mayDisplayControlsKey ] ) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import deprecated from '@wordpress/deprecated';
* Internal dependencies
*/
import groups from './groups';
import { useBlockEditContext } from '../block-edit/context';
import {
useBlockEditContext,
mayDisplayControlsKey,
} from '../block-edit/context';
import { BlockSettingsButton } from '../block-settings';

export default function InspectorControlsFill( {
Expand All @@ -35,14 +38,14 @@ export default function InspectorControlsFill( {
);
group = __experimentalGroup;
}
const { mayDisplayControls } = useBlockEditContext();
const context = useBlockEditContext();

const Fill = groups[ group ]?.Fill;
if ( ! Fill ) {
warning( `Unknown InspectorControls group "${ group }" provided.` );
return null;
}
if ( ! mayDisplayControls ) {
if ( ! context[ mayDisplayControlsKey ] ) {
return null;
}

Expand Down
14 changes: 9 additions & 5 deletions packages/block-editor/src/hooks/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ import { addFilter } from '@wordpress/hooks';
/**
* Internal dependencies
*/
import { useBlockEditContext } from '../components/block-edit/context';
import {
useBlockEditContext,
mayDisplayControlsKey,
mayDisplayParentControlsKey,
} from '../components/block-edit/context';
import { useSettings } from '../components';
import { useSettingsForBlockElement } from '../components/global-styles/hooks';
import { getValueFromObjectPath, setImmutably } from '../utils/object';
Expand Down Expand Up @@ -379,8 +383,7 @@ export function createBlockEditFilter( features ) {
} );
const withBlockEditHooks = createHigherOrderComponent(
( OriginalBlockEdit ) => ( props ) => {
const { mayDisplayControls, mayDisplayParentControls } =
useBlockEditContext();
const context = useBlockEditContext();
// CAUTION: code added before this line will be executed for all
// blocks, not just those that support the feature! Code added
// above this line should be carefully evaluated for its impact on
Expand All @@ -394,8 +397,9 @@ export function createBlockEditFilter( features ) {
shareWithChildBlocks,
} = feature;
const shouldDisplayControls =
mayDisplayControls ||
( mayDisplayParentControls && shareWithChildBlocks );
context[ mayDisplayControlsKey ] ||
( context[ mayDisplayParentControlsKey ] &&
shareWithChildBlocks );

if (
! shouldDisplayControls ||
Expand Down

0 comments on commit 69c9d34

Please sign in to comment.