Skip to content

Commit

Permalink
Use last selected root id for the global inserter in widgets editor
Browse files Browse the repository at this point in the history
  • Loading branch information
adamziel committed Sep 16, 2020
1 parent 115eb5c commit 7169f8b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 22 deletions.
24 changes: 2 additions & 22 deletions packages/edit-widgets/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,20 @@ import {
} from '@wordpress/block-editor';
import { PinnedItems } from '@wordpress/interface';
import { useViewportMatch } from '@wordpress/compose';
import { useSelect } from '@wordpress/data';

/**
* Internal dependencies
*/
import SaveButton from '../save-button';
import useLastSelectedRootId from '../../hooks/use-last-selected-root-id';
import UndoButton from './undo-redo/undo';
import RedoButton from './undo-redo/redo';
import { buildWidgetAreasPostId, KIND, POST_TYPE } from '../../store/utils';

const inserterToggleProps = { isPrimary: true };

function Header( { isCustomizer } ) {
const isLargeViewport = useViewportMatch( 'medium' );
const rootClientId = useSelect( ( select ) => {
const { getBlockRootClientId, getBlockSelectionEnd } = select(
'core/block-editor'
);
const selectedRootId = getBlockRootClientId( getBlockSelectionEnd() );
if ( selectedRootId ) {
return selectedRootId;
}

// Default to the first widget area
const { getEntityRecord } = select( 'core' );
const widgetAreasPost = getEntityRecord(
KIND,
POST_TYPE,
buildWidgetAreasPostId()
);
if ( widgetAreasPost ) {
return widgetAreasPost?.blocks[ 0 ]?.clientId;
}
}, [] );
const rootClientId = useLastSelectedRootId();

return (
<>
Expand Down
45 changes: 45 additions & 0 deletions packages/edit-widgets/src/hooks/use-last-selected-root-id.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* WordPress dependencies
*/
import { useSelect } from '@wordpress/data';
import { useRef } from '@wordpress/element';

/**
* Internal dependencies
*/
import { buildWidgetAreasPostId, KIND, POST_TYPE } from '../store/utils';

const useLastSelectedRootId = () => {
const firstRootId = useSelect( ( select ) => {
// Default to the first widget area
const { getEntityRecord } = select( 'core' );
const widgetAreasPost = getEntityRecord(
KIND,
POST_TYPE,
buildWidgetAreasPostId()
);
if ( widgetAreasPost ) {
return widgetAreasPost?.blocks[ 0 ]?.clientId;
}
}, [] );

const lastSelectedRootIdRef = useRef();
if ( ! lastSelectedRootIdRef.current && firstRootId ) {
lastSelectedRootIdRef.current = firstRootId;
}

const selectedRootId = useSelect( ( select ) => {
const { getBlockRootClientId, getBlockSelectionEnd } = select(
'core/block-editor'
);
return getBlockRootClientId( getBlockSelectionEnd() );
}, [] );

if ( selectedRootId ) {
lastSelectedRootIdRef.current = selectedRootId;
}

return lastSelectedRootIdRef.current;
};

export default useLastSelectedRootId;

0 comments on commit 7169f8b

Please sign in to comment.