Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor Selected Block Tools #55737

Merged
merged 5 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import deprecated from '@wordpress/deprecated';
* Internal dependencies
*/
import InsertionPoint, { InsertionPointOpenRef } from './insertion-point';
import BlockPopover from './selected-block-popover';
import BlockPopover from './selected-block-tools';

export default function BlockToolsBackCompat( { children } ) {
const openRef = useContext( InsertionPointOpenRef );
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* Internal dependencies
*/
import BlockPopover from '../block-popover';
import useBlockToolbarPopoverProps from './use-block-toolbar-popover-props';
import Inserter from '../inserter';
import useSelectedBlockToolProps from './use-selected-block-tool-props';

export default function EmptyBlockInserter( {
clientId,
__unstableContentRef,
} ) {
const {
capturingClientId,
isInsertionPointVisible,
lastClientId,
rootClientId,
} = useSelectedBlockToolProps( clientId );

const popoverProps = useBlockToolbarPopoverProps( {
contentElement: __unstableContentRef?.current,
clientId,
} );

return (
<BlockPopover
clientId={ capturingClientId || clientId }
__unstableCoverTarget
bottomClientId={ lastClientId }
className={ classnames(
'block-editor-block-list__block-side-inserter-popover',
{
'is-insertion-point-visible': isInsertionPointVisible,
}
) }
__unstableContentRef={ __unstableContentRef }
resize={ false }
shift={ false }
{ ...popoverProps }
>
<div className="block-editor-block-list__empty-block-inserter">
<Inserter
position="bottom right"
rootClientId={ rootClientId }
clientId={ clientId }
__experimentalIsQuick
/>
</div>
</BlockPopover>
);
}
57 changes: 46 additions & 11 deletions packages/block-editor/src/components/block-tools/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,48 @@ import { useViewportMatch } from '@wordpress/compose';
import { Popover } from '@wordpress/components';
import { __unstableUseShortcutEventMatch as useShortcutEventMatch } from '@wordpress/keyboard-shortcuts';
import { useRef } from '@wordpress/element';
import { isUnmodifiedDefaultBlock } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import EmptyBlockInserter from './empty-block-inserter';
import {
InsertionPointOpenRef,
default as InsertionPoint,
} from './insertion-point';
import SelectedBlockPopover from './selected-block-popover';
import SelectedBlockTools from './selected-block-tools';
import { store as blockEditorStore } from '../../store';
import BlockContextualToolbar from './block-contextual-toolbar';
import usePopoverScroll from '../block-popover/use-popover-scroll';
import ZoomOutModeInserters from './zoom-out-mode-inserters';

function selector( select ) {
const { __unstableGetEditorMode, getSettings, isTyping } =
select( blockEditorStore );
const {
getSelectedBlockClientId,
getFirstMultiSelectedBlockClientId,
getBlock,
getSettings,
__unstableGetEditorMode,
isTyping,
} = select( blockEditorStore );

const clientId =
getSelectedBlockClientId() || getFirstMultiSelectedBlockClientId();

const { name = '', attributes = {} } = getBlock( clientId ) || {};

return {
isZoomOutMode: __unstableGetEditorMode() === 'zoom-out',
clientId,
hasFixedToolbar: getSettings().hasFixedToolbar,
hasSelectedBlock: clientId && name,
isTyping: isTyping(),
isZoomOutMode: __unstableGetEditorMode() === 'zoom-out',
showEmptyBlockSideInserter:
clientId &&
! isTyping() &&
__unstableGetEditorMode() === 'edit' &&
isUnmodifiedDefaultBlock( { name, attributes } ),
};
}

Expand All @@ -46,10 +66,14 @@ export default function BlockTools( {
...props
} ) {
const isLargeViewport = useViewportMatch( 'medium' );
const { hasFixedToolbar, isZoomOutMode, isTyping } = useSelect(
selector,
[]
);
const {
clientId,
hasFixedToolbar,
hasSelectedBlock,
isTyping,
isZoomOutMode,
showEmptyBlockSideInserter,
} = useSelect( selector, [] );
const isMatch = useShortcutEventMatch();
const { getSelectedBlockClientIds, getBlockRootClientId } =
useSelect( blockEditorStore );
Expand Down Expand Up @@ -142,11 +166,22 @@ export default function BlockTools( {
( hasFixedToolbar || ! isLargeViewport ) && (
<BlockContextualToolbar isFixed />
) }

{ showEmptyBlockSideInserter && (
<EmptyBlockInserter
__unstableContentRef={ __unstableContentRef }
clientId={ clientId }
/>
) }
{ /* Even if the toolbar is fixed, the block popover is still
needed for navigation and zoom-out mode. */ }
<SelectedBlockPopover
__unstableContentRef={ __unstableContentRef }
/>
{ ! showEmptyBlockSideInserter && hasSelectedBlock && (
<SelectedBlockTools
__unstableContentRef={ __unstableContentRef }
clientId={ clientId }
/>
) }

{ /* Used for the inline rich text toolbar. */ }
<Popover.Slot name="block-toolbar" ref={ blockToolbarRef } />
{ children }
Expand Down
Loading
Loading