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

Insertion indicator: render after last block if none is specified #27472

Merged
merged 2 commits into from
Dec 3, 2020
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 @@ -7,7 +7,7 @@ import classnames from 'classnames';
/**
* WordPress dependencies
*/
import { createContext, useContext } from '@wordpress/element';
import { createContext } from '@wordpress/element';
import { withSelect } from '@wordpress/data';
import { getDefaultBlockName } from '@wordpress/blocks';

Expand All @@ -34,8 +34,6 @@ function BlockListAppender( {
selectedBlockClientId,
tagName: TagName = 'div',
} ) {
const appenderNodesMap = useContext( AppenderNodesContext );

if ( isLocked || CustomAppender === false ) {
return null;
}
Expand Down Expand Up @@ -100,15 +98,6 @@ function BlockListAppender( {
'wp-block',
className
) }
ref={ ( ref ) => {
if ( ref ) {
// Set the reference of the "Appender" with `rootClientId` as key.
appenderNodesMap.set( rootClientId || '', ref );
} else {
// If it un-mounts, cleanup the map.
appenderNodesMap.delete( rootClientId || '' );
}
} }
>
{ appender }
</TagName>
Expand Down
39 changes: 15 additions & 24 deletions packages/block-editor/src/components/block-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,10 @@ import { useRef, forwardRef } from '@wordpress/element';
* Internal dependencies
*/
import BlockListBlock from './block';
import BlockListAppender, {
AppenderNodesContext,
} from '../block-list-appender';
import BlockListAppender from '../block-list-appender';
import RootContainer from './root-container';
import useBlockDropZone from '../use-block-drop-zone';

/**
* A map to store the reference of each "Appenders" rendered with `rootClientId` as key.
*/
const appenderNodesMap = new Map();

/**
* If the block count exceeds the threshold, we disable the reordering animation
* to avoid laginess.
Expand All @@ -39,22 +32,20 @@ function BlockList(
const wrapperRef = ref || fallbackRef;

return (
<AppenderNodesContext.Provider value={ appenderNodesMap }>
<Container
ref={ wrapperRef }
className={ classnames(
'block-editor-block-list__layout',
className
) }
>
<BlockListItems
placeholder={ placeholder }
rootClientId={ rootClientId }
renderAppender={ renderAppender }
wrapperRef={ wrapperRef }
/>
</Container>
</AppenderNodesContext.Provider>
<Container
ref={ wrapperRef }
className={ classnames(
'block-editor-block-list__layout',
className
) }
>
<BlockListItems
placeholder={ placeholder }
rootClientId={ rootClientId }
renderAppender={ renderAppender }
wrapperRef={ wrapperRef }
/>
</Container>
);
}

Expand Down
40 changes: 19 additions & 21 deletions packages/block-editor/src/components/block-list/insertion-point.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,13 @@
* External dependencies
*/
import classnames from 'classnames';
import { last } from 'lodash';

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

Expand All @@ -24,7 +18,6 @@ import { placeCaretAtVerticalEdge } from '@wordpress/dom';
import Inserter from '../inserter';
import { getClosestTabbable } from '../writing-flow';
import { getBlockDOMNode } from '../../utils/dom';
import { AppenderNodesContext } from '../block-list-appender';

function InsertionPointInserter( {
clientId,
Expand Down Expand Up @@ -114,31 +107,36 @@ function InsertionPointPopover( {
containerRef,
showInsertionPoint,
} ) {
const appenderNodesMap = useContext( AppenderNodesContext );
const element = useMemo( () => {
if ( clientId ) {
const element = useSelect(
( select ) => {
const { getBlockOrder } = select( 'core/block-editor' );
const { ownerDocument } = containerRef.current;
return getBlockDOMNode( clientId, ownerDocument );
}
const targetClientId =
clientId || last( getBlockOrder( rootClientId ) );

return getBlockDOMNode( targetClientId, ownerDocument );
},
[ clientId, rootClientId ]
);

// Can't find the element, might be at the end of the block list, or inside an empty block list.
// We instead try to find the "Appender" and place the indicator above it.
// `rootClientId` could be null or undefined when there's no parent block, we normalize it to an empty string.
return appenderNodesMap.get( rootClientId || '' );
}, [ clientId, rootClientId, appenderNodesMap ] );
const position = clientId ? 'top' : 'bottom';
const className = classnames( 'block-editor-block-list__insertion-point', {
'is-insert-after': ! clientId,
} );

return (
<Popover
noArrow
animate={ false }
anchorRef={ element }
position="top right left"
position={ `${ position } right left` }
focusOnMount={ false }
className="block-editor-block-list__insertion-point-popover"
__unstableSlotName="block-toolbar"
__unstableForcePosition={ true }
>
<div
className="block-editor-block-list__insertion-point"
className={ className }
style={ { width: element?.offsetWidth } }
>
{ showInsertionPoint && (
Expand Down
4 changes: 3 additions & 1 deletion packages/components/src/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ const Popover = ( {
__unstableSlotName = SLOT_NAME,
__unstableObserveElement,
__unstableBoundaryParent,
__unstableForcePosition,
/* eslint-enable no-unused-vars */
...contentProps
} ) => {
Expand Down Expand Up @@ -361,7 +362,8 @@ const Popover = ( {
__unstableStickyBoundaryElement,
containerRef.current,
relativeOffsetTop,
boundaryElement
boundaryElement,
__unstableForcePosition
);

if (
Expand Down
22 changes: 15 additions & 7 deletions packages/components/src/popover/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const HEIGHT_OFFSET = 10; // used by the arrow and a bit of empty space
* position.
* @param {string} chosenYAxis yAxis to be used.
* @param {Element} boundaryElement Boundary element.
* @param {boolean} forcePosition Don't adjust position based on anchor.
*
* @return {Object} Popover xAxis position and constraints.
*/
Expand All @@ -25,7 +26,8 @@ export function computePopoverXAxisPosition(
corner,
stickyBoundaryElement,
chosenYAxis,
boundaryElement
boundaryElement,
forcePosition
) {
const { width } = contentSize;
const isRTL = document.documentElement.dir === 'rtl';
Expand Down Expand Up @@ -86,7 +88,7 @@ export function computePopoverXAxisPosition(
let chosenXAxis = xAxis;
let contentWidth = null;

if ( ! stickyBoundaryElement ) {
if ( ! stickyBoundaryElement && ! forcePosition ) {
if ( xAxis === 'center' && centerAlignment.contentWidth === width ) {
chosenXAxis = 'center';
} else if ( xAxis === 'left' && leftAlignment.contentWidth === width ) {
Expand Down Expand Up @@ -143,6 +145,7 @@ export function computePopoverXAxisPosition(
* @param {Element} anchorRef The anchor element.
* @param {Element} relativeOffsetTop If applicable, top offset of the
* relative positioned parent container.
* @param {boolean} forcePosition Don't adjust position based on anchor.
*
* @return {Object} Popover xAxis position and constraints.
*/
Expand All @@ -153,7 +156,8 @@ export function computePopoverYAxisPosition(
corner,
stickyBoundaryElement,
anchorRef,
relativeOffsetTop
relativeOffsetTop,
forcePosition
) {
const { height } = contentSize;

Expand Down Expand Up @@ -206,7 +210,7 @@ export function computePopoverYAxisPosition(
let chosenYAxis = yAxis;
let contentHeight = null;

if ( ! stickyBoundaryElement ) {
if ( ! stickyBoundaryElement && ! forcePosition ) {
if ( yAxis === 'middle' && middleAlignment.contentHeight === height ) {
chosenYAxis = 'middle';
} else if ( yAxis === 'top' && topAlignment.contentHeight === height ) {
Expand Down Expand Up @@ -259,6 +263,7 @@ export function computePopoverYAxisPosition(
* @param {number} relativeOffsetTop If applicable, top offset of the
* relative positioned parent container.
* @param {Element} boundaryElement Boundary element.
* @param {boolean} forcePosition Don't adjust position based on anchor.
*
* @return {Object} Popover position and constraints.
*/
Expand All @@ -269,7 +274,8 @@ export function computePopoverPosition(
stickyBoundaryElement,
anchorRef,
relativeOffsetTop,
boundaryElement
boundaryElement,
forcePosition
) {
const [ yAxis, xAxis = 'center', corner ] = position.split( ' ' );

Expand All @@ -280,7 +286,8 @@ export function computePopoverPosition(
corner,
stickyBoundaryElement,
anchorRef,
relativeOffsetTop
relativeOffsetTop,
forcePosition
);
const xAxisPosition = computePopoverXAxisPosition(
anchorRect,
Expand All @@ -289,7 +296,8 @@ export function computePopoverPosition(
corner,
stickyBoundaryElement,
yAxisPosition.yAxis,
boundaryElement
boundaryElement,
forcePosition
);

return {
Expand Down