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

Show inserter on click #24926

Closed
Closed
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
249 changes: 41 additions & 208 deletions packages/block-editor/src/components/block-list/insertion-point.js
Original file line number Diff line number Diff line change
@@ -1,232 +1,65 @@
/**
* External dependencies
*/
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useState, useRef } from '@wordpress/element';
import { Popover } from '@wordpress/components';
import { placeCaretAtVerticalEdge } from '@wordpress/dom';

/**
* Internal dependencies
*/
import Inserter from '../inserter';
import { getClosestTabbable } from '../writing-flow';
import { getBlockDOMNode } from '../../utils/dom';

function InsertionPointInserter( {
clientId,
setIsInserterForced,
containerRef,
} ) {
const ref = useRef();
// Hide the inserter above the selected block and during multi-selection.
const isInserterHidden = useSelect(
( select ) => {
const {
getMultiSelectedBlockClientIds,
getSelectedBlockClientId,
hasMultiSelection,
} = select( 'core/block-editor' );
const multiSelectedBlockClientIds = getMultiSelectedBlockClientIds();
const selectedBlockClientId = getSelectedBlockClientId();

return hasMultiSelection()
? multiSelectedBlockClientIds.includes( clientId )
: clientId === selectedBlockClientId;
},
[ clientId ]
);

function focusClosestTabbable( event ) {
const { clientX, clientY, target } = event;

// Only handle click on the wrapper specifically, and not an event
// bubbled from the inserter itself.
if ( target !== ref.current ) {
return;
}

const targetRect = target.getBoundingClientRect();
const isReverse = clientY < targetRect.top + targetRect.height / 2;
const blockNode = getBlockDOMNode( clientId );
const container = isReverse ? containerRef.current : blockNode;
const closest =
getClosestTabbable( blockNode, true, container ) || blockNode;
const rect = new window.DOMRect( clientX, clientY, 0, 16 );

placeCaretAtVerticalEdge( closest, isReverse, rect, false );
}

return (
/* eslint-disable-next-line jsx-a11y/no-static-element-interactions, jsx-a11y/click-events-have-key-events */
<div
ref={ ref }
onFocus={ () => setIsInserterForced( true ) }
onBlur={ () => setIsInserterForced( false ) }
onClick={ focusClosestTabbable }
// While ideally it would be enough to capture the
// bubbling focus event from the Inserter, due to the
// characteristics of click focusing of `button`s in
// Firefox and Safari, it is not reliable.
//
// See: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus
tabIndex={ -1 }
className={ classnames(
'block-editor-block-list__insertion-point-inserter',
{
'is-inserter-hidden': isInserterHidden,
}
) }
>
<Inserter
position="bottom center"
clientId={ clientId }
__experimentalIsQuick
/>
</div>
);
}

function InsertionPointPopover( {
clientId,
isInserterShown,
isInserterForced,
setIsInserterForced,
containerRef,
showInsertionPoint,
} ) {
const element = getBlockDOMNode( clientId );

return (
<Popover
noArrow
animate={ false }
anchorRef={ element }
position="top right left"
focusOnMount={ false }
className="block-editor-block-list__insertion-point-popover"
__unstableSlotName="block-toolbar"
>
<div
className="block-editor-block-list__insertion-point"
style={ { width: element?.offsetWidth } }
>
{ showInsertionPoint && (
<div className="block-editor-block-list__insertion-point-indicator" />
) }
{ ( isInserterShown || isInserterForced ) && (
<InsertionPointInserter
clientId={ clientId }
setIsInserterForced={ setIsInserterForced }
containerRef={ containerRef }
/>
) }
</div>
</Popover>
);
}

export default function InsertionPoint( { children, containerRef } ) {
const [ isInserterShown, setIsInserterShown ] = useState( false );
const [ isInserterForced, setIsInserterForced ] = useState( false );
const [ inserterClientId, setInserterClientId ] = useState( null );
const { isMultiSelecting, isInserterVisible, selectedClientId } = useSelect(
( select ) => {
const {
isMultiSelecting: _isMultiSelecting,
isBlockInsertionPointVisible,
getBlockInsertionPoint,
getBlockOrder,
} = select( 'core/block-editor' );

const insertionPoint = getBlockInsertionPoint();
const order = getBlockOrder( insertionPoint.rootClientId );

return {
isMultiSelecting: _isMultiSelecting(),
isInserterVisible: isBlockInsertionPointVisible(),
selectedClientId: order[ insertionPoint.index ],
};
},
[]
);

function onMouseMove( event ) {
if (
! event.target.classList.contains(
'block-editor-block-list__layout'
)
) {
if ( isInserterShown ) {
setIsInserterShown( false );
}
return;
}

const rect = event.target.getBoundingClientRect();
const offset = event.clientY - rect.top;
const element = Array.from( event.target.children ).find(
( blockEl ) => {
return blockEl.offsetTop > offset;
}
export default function InsertionPoint( { children } ) {
const { order, IP } = useSelect( ( select ) => {
const { getBlockInsertionPoint, getBlockOrder } = select(
'core/block-editor'
);

if ( ! element ) {
return;
}

const clientId = element.id.slice( 'block-'.length );
const insertionPoint = getBlockInsertionPoint();

if ( ! clientId ) {
return;
}
return {
IP: insertionPoint,
order: getBlockOrder( insertionPoint.rootClientId ),
};
} );

const elementRect = element.getBoundingClientRect();

if (
event.clientX > elementRect.right ||
event.clientX < elementRect.left
) {
if ( isInserterShown ) {
setIsInserterShown( false );
}
return;
}

setIsInserterShown( true );
setInserterClientId( clientId );
}
const clientId = order[ IP.index - 1 ];
const element = getBlockDOMNode( clientId );

const isVisible = isInserterShown || isInserterForced || isInserterVisible;
// should be visible only when the user has hovered over the inserter
const shouldShow = order.length !== IP.index;

return (
<>
{ ! isMultiSelecting && isVisible && (
<InsertionPointPopover
clientId={
isInserterVisible ? selectedClientId : inserterClientId
}
isInserterShown={ isInserterShown }
isInserterForced={ isInserterForced }
setIsInserterForced={ setIsInserterForced }
containerRef={ containerRef }
showInsertionPoint={ isInserterVisible }
/>
{ shouldShow && (
<Popover
anchorRef={ element }
position="bottom right left"
focusOnMount={ false }
>
<div
className="block-editor-block-list__insertion-point-inserter"
style={ { position: 'fixed' } }
>
<Inserter
position="bottom center"
clientId={ clientId }
__experimentalIsQuick
/>
</div>
<div
style={ {
left: '14px',
right: '14px',
backgroundColor: 'red',
height: '2px',
width: element?.offsetWidth,
} }
/>
</Popover>
) }
<div
onMouseMove={
! isInserterForced && ! isMultiSelecting
? onMouseMove
: undefined
}
>
{ children }
</div>
{ children }
</>
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,6 @@
.block-editor-block-list__insertion-point {
position: relative;
z-index: z-index(".block-editor-block-list__insertion-point");
margin-top: -$block-padding;
}

.block-editor-block-list__insertion-point-indicator {
Expand Down Expand Up @@ -428,6 +427,12 @@
}
}

.block-editor-block-list__insertion-point-indicator {
animation: block-editor-inserter__toggle__fade-in-animation-delayed 0.3s ease;
animation-fill-mode: forwards;
@include reduce-motion("animation");
}


.block-editor-block-list__insertion-point-inserter,
.block-editor-block-list__block-popover-inserter {
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/popover/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ $arrow-size: 8px;
left: 100%;
}

.components-popover:not([data-y-axis="middle"])[data-x-axis="right"] & {
margin-left: -($grid-unit-30 + $border-width);
}
//.components-popover:not([data-y-axis="middle"])[data-x-axis="right"] & {
// margin-left: -($grid-unit-30 + $border-width);
//}

.components-popover[data-x-axis="left"] & {
position: absolute;
Expand Down