Skip to content

Commit

Permalink
[edit-widgets] Make global inserter usable by passing correct rootCli…
Browse files Browse the repository at this point in the history
…entId to it (#24908)

* Pass correct rootClientId to the inserter

* Use last selected root id for the global inserter in widgets editor
  • Loading branch information
adamziel authored Sep 16, 2020
1 parent e4b6d70 commit f3cdac3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
9 changes: 2 additions & 7 deletions packages/edit-widgets/src/components/header/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +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';

const inserterToggleProps = { isPrimary: true };

function Header( { isCustomizer } ) {
const isLargeViewport = useViewportMatch( 'medium' );
const rootClientId = useSelect( ( select ) => {
const { getBlockRootClientId, getBlockSelectionEnd } = select(
'core/block-editor'
);
return getBlockRootClientId( getBlockSelectionEnd() );
}, [] );
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 f3cdac3

Please sign in to comment.