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

[Widgets Editor] Fix insertion point in widget areas #25727

Merged
merged 3 commits into from
Oct 6, 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 @@ -32,6 +32,7 @@ function useInsertionPoint( {
clientId,
isAppender,
selectBlockOnInsert,
insertionIndex,
} ) {
const {
destinationRootClientId,
Expand Down Expand Up @@ -76,12 +77,16 @@ function useInsertionPoint( {
} = useDispatch( 'core/block-editor' );

function getInsertionIndex() {
if ( insertionIndex !== undefined ) {
return insertionIndex;
}

// If the clientId is defined, we insert at the position of the block.
if ( clientId ) {
return getBlockIndex( clientId, destinationRootClientId );
}

// If there a selected block, we insert after the selected block.
// If there's a selected block, and the selected block is not the destination root block, we insert after the selected block.
const end = getBlockSelectionEnd();
if ( ! isAppender && end ) {
return getBlockIndex( end, destinationRootClientId ) + 1;
Expand Down
8 changes: 6 additions & 2 deletions packages/block-editor/src/components/inserter/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ function InserterLibrary( {
isAppender,
showInserterHelpPanel,
showMostUsedBlocks = false,
__experimentalSelectBlockOnInsert: selectBlockOnInsert,
__experimentalSelectBlockOnInsert,
__experimentalInsertionIndex,
onSelect = noop,
} ) {
const destinationRootClientId = useSelect(
Expand All @@ -41,7 +42,10 @@ function InserterLibrary( {
isAppender={ isAppender }
showInserterHelpPanel={ showInserterHelpPanel }
showMostUsedBlocks={ showMostUsedBlocks }
__experimentalSelectBlockOnInsert={ selectBlockOnInsert }
__experimentalSelectBlockOnInsert={
__experimentalSelectBlockOnInsert
}
__experimentalInsertionIndex={ __experimentalInsertionIndex }
/>
);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function InserterMenu( {
clientId,
isAppender,
__experimentalSelectBlockOnInsert,
__experimentalInsertionIndex,
onSelect,
showInserterHelpPanel,
showMostUsedBlocks,
Expand All @@ -46,6 +47,7 @@ function InserterMenu( {
clientId,
isAppender,
selectBlockOnInsert: __experimentalSelectBlockOnInsert,
insertionIndex: __experimentalInsertionIndex,
} );
const { hasPatterns, hasReusableBlocks } = useSelect( ( select ) => {
const {
Expand Down
14 changes: 8 additions & 6 deletions packages/edit-widgets/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ import { useRef } from '@wordpress/element';
import SaveButton from '../save-button';
import UndoButton from './undo-redo/undo';
import RedoButton from './undo-redo/redo';
import useLastSelectedRootId from '../../hooks/use-last-selected-root-id';
import useLastSelectedWidgetArea from '../../hooks/use-last-selected-widget-area';

function Header() {
const inserterButton = useRef();
const isLargeViewport = useViewportMatch( 'medium' );
const widgetAreaClientId = useLastSelectedWidgetArea();
const isLastSelectedWidgetAreaOpen = useSelect(
( select ) =>
select( 'core/edit-widgets' ).getIsWidgetAreaOpen( rootClientId ),
[ rootClientId ]
select( 'core/edit-widgets' ).getIsWidgetAreaOpen(
widgetAreaClientId
),
[ widgetAreaClientId ]
);
const isInserterOpened = useSelect( ( select ) =>
select( 'core/edit-widgets' ).isInserterOpened()
Expand All @@ -37,17 +40,16 @@ function Header() {
'core/edit-widgets'
);
const { selectBlock } = useDispatch( 'core/block-editor' );
const rootClientId = useLastSelectedRootId();
const handleClick = () => {
if ( isInserterOpened ) {
// Focusing the inserter button closes the inserter popover
inserterButton.current.focus();
} else {
if ( ! isLastSelectedWidgetAreaOpen ) {
// Select the last selected block if hasn't already.
selectBlock( rootClientId );
selectBlock( widgetAreaClientId );
// Open the last selected widget area when opening the inserter.
setIsWidgetAreaOpen( rootClientId, true );
setIsWidgetAreaOpen( widgetAreaClientId, true );
}
// The DOM updates resulting from selectBlock() and setIsInserterOpened() calls are applied the
// same tick and pretty much in a random order. The inserter is closed if any other part of the
Expand Down
84 changes: 3 additions & 81 deletions packages/edit-widgets/src/components/layout/index.js
Original file line number Diff line number Diff line change
@@ -1,100 +1,22 @@
/**
* WordPress dependencies
*/
import { Popover, Button } from '@wordpress/components';
import { useViewportMatch } from '@wordpress/compose';
import { close } from '@wordpress/icons';
import { __experimentalLibrary as Library } from '@wordpress/block-editor';
import { useEffect } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { InterfaceSkeleton, ComplementaryArea } from '@wordpress/interface';
import { Popover } from '@wordpress/components';
import { PluginArea } from '@wordpress/plugins';

/**
* Internal dependencies
*/
import WidgetAreasBlockEditorProvider from '../widget-areas-block-editor-provider';
import Header from '../header';
import Sidebar from '../sidebar';
import WidgetAreasBlockEditorContent from '../widget-areas-block-editor-content';
import PopoverWrapper from './popover-wrapper';
import useLastSelectedRootId from '../../hooks/use-last-selected-root-id';
import Interface from './interface';

function Layout( { blockEditorSettings } ) {
const isMobileViewport = useViewportMatch( 'medium', '<' );
const isHugeViewport = useViewportMatch( 'huge', '>=' );
const { setIsInserterOpened, closeGeneralSidebar } = useDispatch(
'core/edit-widgets'
);
const rootClientId = useLastSelectedRootId();

const { hasSidebarEnabled, isInserterOpened } = useSelect( ( select ) => ( {
hasSidebarEnabled: !! select(
'core/interface'
).getActiveComplementaryArea( 'core/edit-widgets' ),
isInserterOpened: !! select( 'core/edit-widgets' ).isInserterOpened(),
} ) );

// Inserter and Sidebars are mutually exclusive
useEffect( () => {
if ( hasSidebarEnabled && ! isHugeViewport ) {
setIsInserterOpened( false );
}
}, [ hasSidebarEnabled, isHugeViewport ] );

useEffect( () => {
if ( isInserterOpened && ! isHugeViewport ) {
closeGeneralSidebar();
}
}, [ isInserterOpened, isHugeViewport ] );

return (
<WidgetAreasBlockEditorProvider
blockEditorSettings={ blockEditorSettings }
>
<InterfaceSkeleton
header={ <Header /> }
leftSidebar={
isInserterOpened && (
<PopoverWrapper
className="edit-widgets-layout__inserter-panel-popover-wrapper"
onClose={ () => setIsInserterOpened( false ) }
>
<div className="edit-widgets-layout__inserter-panel">
<div className="edit-widgets-layout__inserter-panel-header">
<Button
icon={ close }
onClick={ () =>
setIsInserterOpened( false )
}
/>
</div>
<div className="edit-widgets-layout__inserter-panel-content">
<Library
showInserterHelpPanel
onSelect={ () => {
if ( isMobileViewport ) {
setIsInserterOpened( false );
}
} }
rootClientId={ rootClientId }
/>
</div>
</div>
</PopoverWrapper>
)
}
sidebar={
hasSidebarEnabled && (
<ComplementaryArea.Slot scope="core/edit-widgets" />
)
}
content={
<WidgetAreasBlockEditorContent
blockEditorSettings={ blockEditorSettings }
/>
}
/>
<Interface blockEditorSettings={ blockEditorSettings } />
<Sidebar />
<Popover.Slot />
<PluginArea />
Expand Down
98 changes: 98 additions & 0 deletions packages/edit-widgets/src/components/layout/interface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/**
* WordPress dependencies
*/
import { Button } from '@wordpress/components';
import { useViewportMatch } from '@wordpress/compose';
import { close } from '@wordpress/icons';
import { __experimentalLibrary as Library } from '@wordpress/block-editor';
import { useEffect } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';
import { InterfaceSkeleton, ComplementaryArea } from '@wordpress/interface';

/**
* Internal dependencies
*/
import Header from '../header';
import WidgetAreasBlockEditorContent from '../widget-areas-block-editor-content';
import PopoverWrapper from './popover-wrapper';
import useWidgetLibraryInsertionPoint from '../../hooks/use-widget-library-insertion-point';

function Interface( { blockEditorSettings } ) {
const isMobileViewport = useViewportMatch( 'medium', '<' );
const isHugeViewport = useViewportMatch( 'huge', '>=' );
const { setIsInserterOpened, closeGeneralSidebar } = useDispatch(
'core/edit-widgets'
);
const { rootClientId, insertionIndex } = useWidgetLibraryInsertionPoint();

const { hasSidebarEnabled, isInserterOpened } = useSelect( ( select ) => ( {
hasSidebarEnabled: !! select(
'core/interface'
).getActiveComplementaryArea( 'core/edit-widgets' ),
isInserterOpened: !! select( 'core/edit-widgets' ).isInserterOpened(),
} ) );

// Inserter and Sidebars are mutually exclusive
useEffect( () => {
if ( hasSidebarEnabled && ! isHugeViewport ) {
setIsInserterOpened( false );
}
}, [ hasSidebarEnabled, isHugeViewport ] );

useEffect( () => {
if ( isInserterOpened && ! isHugeViewport ) {
closeGeneralSidebar();
}
}, [ isInserterOpened, isHugeViewport ] );

return (
<InterfaceSkeleton
header={ <Header /> }
leftSidebar={
isInserterOpened && (
<PopoverWrapper
className="edit-widgets-layout__inserter-panel-popover-wrapper"
onClose={ () => setIsInserterOpened( false ) }
>
<div className="edit-widgets-layout__inserter-panel">
<div className="edit-widgets-layout__inserter-panel-header">
<Button
icon={ close }
onClick={ () =>
setIsInserterOpened( false )
}
/>
</div>
<div className="edit-widgets-layout__inserter-panel-content">
<Library
showInserterHelpPanel
onSelect={ () => {
if ( isMobileViewport ) {
setIsInserterOpened( false );
}
} }
rootClientId={ rootClientId }
__experimentalInsertionIndex={
insertionIndex
}
/>
</div>
</div>
</PopoverWrapper>
)
}
sidebar={
hasSidebarEnabled && (
<ComplementaryArea.Slot scope="core/edit-widgets" />
)
}
content={
<WidgetAreasBlockEditorContent
blockEditorSettings={ blockEditorSettings }
/>
}
/>
);
}

export default Interface;
37 changes: 0 additions & 37 deletions packages/edit-widgets/src/hooks/use-last-selected-root-id.js

This file was deleted.

Loading