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 2 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 @@ -81,9 +81,9 @@ function useInsertionPoint( {
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 ) {
if ( ! isAppender && end && end !== destinationRootClientId ) {
kevin940726 marked this conversation as resolved.
Show resolved Hide resolved
return getBlockIndex( end, destinationRootClientId ) + 1;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/edit-widgets/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import useLastSelectedRootId from '../../hooks/use-last-selected-root-id';
function Header() {
const inserterButton = useRef();
const isLargeViewport = useViewportMatch( 'medium' );
const rootClientId = useLastSelectedRootId();
const isLastSelectedWidgetAreaOpen = useSelect(
( select ) =>
select( 'core/edit-widgets' ).getIsWidgetAreaOpen( rootClientId ),
Expand All @@ -37,7 +38,6 @@ 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
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
95 changes: 95 additions & 0 deletions packages/edit-widgets/src/components/layout/interface.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* 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 useLastSelectedRootId from '../../hooks/use-last-selected-root-id';

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

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 }
/>
</div>
</div>
</PopoverWrapper>
)
}
sidebar={
hasSidebarEnabled && (
<ComplementaryArea.Slot scope="core/edit-widgets" />
)
}
content={
<WidgetAreasBlockEditorContent
blockEditorSettings={ blockEditorSettings }
/>
}
/>
);
}

export default Interface;