Skip to content

Commit

Permalink
Fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan committed Apr 24, 2024
1 parent 181fa47 commit 904f02a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 39 deletions.
85 changes: 48 additions & 37 deletions packages/block-editor/src/components/block-card/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { useSelect, useDispatch } from '@wordpress/data';
import BlockIcon from '../block-icon';
import { store as blockEditorStore } from '../../store';
import useBlockDisplayInformation from '../use-block-display-information';
import { unlock } from '../../lock-unlock';

function BlockCard( { title, icon, description, blockType, className } ) {
if ( blockType ) {
Expand All @@ -31,52 +32,62 @@ function BlockCard( { title, icon, description, blockType, className } ) {
( { title, icon, description } = blockType );
}

const parentClientId = useSelect( ( select ) => {
const {
getSelectedBlockClientId,
getBlockParentsByBlockName,
__unstableGetContentLockingParent,
} = select( blockEditorStore );
const { isParentNavigationBlock, parentClientId } = useSelect(
( select ) => {
const {
getSelectedBlockClientId,
getBlockParentsByBlockName,
getContentLockingParent,
} = unlock( select( blockEditorStore ) );

const _selectedBlockClientId = getSelectedBlockClientId();
let _parentClientId = getBlockParentsByBlockName(
_selectedBlockClientId,
'core/navigation',
true
)[ 0 ];
const _selectedBlockClientId = getSelectedBlockClientId();

if ( ! _parentClientId ) {
_parentClientId = __unstableGetContentLockingParent(
_selectedBlockClientId
);
}
let _parentClientId = getBlockParentsByBlockName(
_selectedBlockClientId,
'core/navigation',
true
)[ 0 ];
const _isParentNavigationBlock = !! _parentClientId;

return _parentClientId;
}, [] );
if ( ! _parentClientId ) {
_parentClientId = getContentLockingParent(
_selectedBlockClientId
);
}

return {
isParentNavigationBlock: _isParentNavigationBlock,
parentClientId: _parentClientId,
};
},
[]
);
const parentDisplayInfo = useBlockDisplayInformation( parentClientId );
const contentOnlyFills = useSlotFills( 'InspectorControlsContentOnly' );
const { selectBlock } = useDispatch( blockEditorStore );
const hasParentBackArrow =
isParentNavigationBlock ||
( parentClientId && !! contentOnlyFills?.length );

return (
<div className={ classnames( 'block-editor-block-card', className ) }>
{ !! contentOnlyFills?.length &&
parentClientId && ( // This is only used by the Navigation block for now. It's not ideal having Navigation block specific code here.
<Button
onClick={ () => selectBlock( parentClientId ) }
label={ sprintf(
// translators: %s: the block title of the parent.
__( 'Go to parent: %s' ),
parentDisplayInfo?.title
) }
style={
// TODO: This style override is also used in ToolsPanelHeader.
// It should be supported out-of-the-box by Button.
{ minWidth: 24, padding: 0 }
}
icon={ isRTL() ? chevronRight : chevronLeft }
size="small"
/>
) }
{ hasParentBackArrow && ( // This is only used by the Navigation block for now. It's not ideal having Navigation block specific code here.
<Button
onClick={ () => selectBlock( parentClientId ) }
label={ sprintf(
// translators: %s: the block title of the parent.
__( 'Go to parent: %s' ),
parentDisplayInfo?.title
) }
style={
// TODO: This style override is also used in ToolsPanelHeader.
// It should be supported out-of-the-box by Button.
{ minWidth: 24, padding: 0 }
}
icon={ isRTL() ? chevronRight : chevronLeft }
size="small"
/>
) }
<BlockIcon icon={ icon } showColors />
<div className="block-editor-block-card__content">
<h2 className="block-editor-block-card__title">{ title }</h2>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import useBlockInspectorAnimationSettings from './useBlockInspectorAnimationSett
import BlockInfo from '../block-info-slot-fill';
import BlockQuickNavigation from '../block-quick-navigation';
import { useBorderPanelLabel } from '../../hooks/border';
import { unlock } from '../../lock-unlock';

function BlockInspectorContentLockedUI( {
clientId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function BlockQuickNavigation( {
<BlockQuickNavigationItem
key={ clientId }
clientId={ clientId }
hasControls={ clientIdsWithControls.includes( clientId ) }
hasControls={ clientIdsWithControls?.includes( clientId ) }
/>
) ) }
</VStack>
Expand Down

0 comments on commit 904f02a

Please sign in to comment.