Skip to content

Commit

Permalink
Borrow getInserterInitialTab changes from #57572
Browse files Browse the repository at this point in the history
Co-authored-by: ramon <ramonjd@gmail.com>
  • Loading branch information
noisysocks and ramonjd committed Jan 25, 2024
1 parent 8f883ee commit bfd9062
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 12 deletions.
1 change: 1 addition & 0 deletions docs/reference-guides/data/data-core-customize-widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ _Parameters_
- _value_ `boolean|Object`: Whether the inserter should be opened (true) or closed (false). To specify an insertion point, use an object.
- _value.rootClientId_ `string`: The root client ID to insert at.
- _value.insertionIndex_ `number`: The index to insert at.
- _value.initialTab_ `string`: The id of the tab to display first when the block editor inserter is opened. A category corresponds to one of the tab ids defined in packages/block-editor/src/components/inserter/tabs.js.

_Returns_

Expand Down
1 change: 1 addition & 0 deletions docs/reference-guides/data/data-core-edit-widgets.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ _Parameters_
- _value_ `boolean|Object`: Whether the inserter should be opened (true) or closed (false). To specify an insertion point, use an object.
- _value.rootClientId_ `string`: The root client ID to insert at.
- _value.insertionIndex_ `number`: The index to insert at.
- _value.initialTab_ `string`: The id of the tab to display first when the block editor inserter is opened. A category corresponds to one of the tab ids defined in packages/block-editor/src/components/inserter/tabs.js.

_Returns_

Expand Down
1 change: 1 addition & 0 deletions docs/reference-guides/data/data-core-editor.md
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,7 @@ _Parameters_
- _value_ `boolean|Object`: Whether the inserter should be opened (true) or closed (false). To specify an insertion point, use an object.
- _value.rootClientId_ `string`: The root client ID to insert at.
- _value.insertionIndex_ `number`: The index to insert at.
- _value.initialTab_ `string`: The id of the tab to display first when the block editor inserter is opened. A category corresponds to one of the tab ids defined in packages/block-editor/src/components/inserter/tabs.js.

_Returns_

Expand Down
2 changes: 2 additions & 0 deletions packages/block-editor/src/components/inserter/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function InserterLibrary(
showMostUsedBlocks = false,
__experimentalInsertionIndex,
__experimentalFilterValue,
initialInserterTab,
onSelect = noop,
shouldFocusBlock = false,
},
Expand All @@ -46,6 +47,7 @@ function InserterLibrary(
isAppender={ isAppender }
showInserterHelpPanel={ showInserterHelpPanel }
showMostUsedBlocks={ showMostUsedBlocks }
initialInserterTab={ initialInserterTab }
__experimentalInsertionIndex={ __experimentalInsertionIndex }
__experimentalFilterValue={ __experimentalFilterValue }
shouldFocusBlock={ shouldFocusBlock }
Expand Down
7 changes: 5 additions & 2 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ function InserterMenu(
showMostUsedBlocks,
__experimentalFilterValue = '',
shouldFocusBlock = true,
initialInserterTab,
},
ref
) {
Expand All @@ -56,8 +57,9 @@ function InserterMenu(
const [ patternFilter, setPatternFilter ] = useState( 'all' );
const [ selectedMediaCategory, setSelectedMediaCategory ] =
useState( null );
const [ selectedTab, setSelectedTab ] = useState( null );

const [ selectedTab, setSelectedTab ] = useState(
initialInserterTab || null
);
const [ destinationRootClientId, onInsertBlocks, onToggleInsertionPoint ] =
useInsertionPoint( {
rootClientId,
Expand Down Expand Up @@ -259,6 +261,7 @@ function InserterMenu(
showMedia={ showMedia }
onSelect={ handleSetSelectedTab }
tabsContents={ inserterTabsContents }
initialTabId={ initialInserterTab }
/>
) }
{ ! delayedFilterValue && ! showAsTabs && (
Expand Down
7 changes: 6 additions & 1 deletion packages/block-editor/src/components/inserter/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@ function InserterTabs( {
showMedia = false,
onSelect,
tabsContents,
initialTabId,
} ) {
const tabs = [
blocksTab,
showPatterns && patternsTab,
showMedia && mediaTab,
].filter( Boolean );

initialTabId = !! tabs.find( ( { name } ) => initialTabId === name )
? initialTabId
: 'blocks';

return (
<div className="block-editor-inserter__tabs">
<Tabs onSelect={ onSelect }>
<Tabs initialTabId={ initialTabId } onSelect={ onSelect }>
<Tabs.TabList>
{ tabs.map( ( tab ) => (
<Tabs.Tab key={ tab.name } tabId={ tab.name }>
Expand Down
2 changes: 2 additions & 0 deletions packages/customize-widgets/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* use an object.
* @param {string} value.rootClientId The root client ID to insert at.
* @param {number} value.insertionIndex The index to insert at.
* @param {string} value.initialTab The id of the tab to display first when the block editor inserter is opened.
* A category corresponds to one of the tab ids defined in packages/block-editor/src/components/inserter/tabs.js.
*
* @example
* ```js
Expand Down
2 changes: 2 additions & 0 deletions packages/edit-widgets/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,8 @@ export function setIsWidgetAreaOpen( clientId, isOpen ) {
* use an object.
* @param {string} value.rootClientId The root client ID to insert at.
* @param {number} value.insertionIndex The index to insert at.
* @param {string} value.initialTab The id of the tab to display first when the block editor inserter is opened.
* A category corresponds to one of the tab ids defined in packages/block-editor/src/components/inserter/tabs.js.
*
* @return {Object} Action object.
*/
Expand Down
21 changes: 13 additions & 8 deletions packages/editor/src/components/inserter-sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,18 @@ import { unlock } from '../../lock-unlock';
import { store as editorStore } from '../../store';

export default function InserterSidebar() {
const { insertionPoint, showMostUsedBlocks } = useSelect( ( select ) => {
const { getInsertionPoint } = unlock( select( editorStore ) );
const { get } = select( preferencesStore );
return {
insertionPoint: getInsertionPoint(),
showMostUsedBlocks: get( 'core', 'mostUsedBlocks' ),
};
}, [] );
const { insertionPoint, initialInserterTab, showMostUsedBlocks } =
useSelect( ( select ) => {
const { getInsertionPoint, getInserterInitialTab } = unlock(
select( editorStore )
);
const { get } = select( preferencesStore );
return {
insertionPoint: getInsertionPoint(),
initialInserterTab: getInserterInitialTab(),
showMostUsedBlocks: get( 'core', 'mostUsedBlocks' ),
};
}, [] );
const { setIsInserterOpened } = useDispatch( editorStore );

const isMobileViewport = useViewportMatch( 'medium', '<' );
Expand Down Expand Up @@ -64,6 +68,7 @@ export default function InserterSidebar() {
__experimentalInsertionIndex={
insertionPoint.insertionIndex
}
initialInserterTab={ initialInserterTab }
__experimentalFilterValue={ insertionPoint.filterValue }
ref={ libraryRef }
/>
Expand Down
2 changes: 2 additions & 0 deletions packages/editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,8 @@ export function removeEditorPanel( panelName ) {
* use an object.
* @param {string} value.rootClientId The root client ID to insert at.
* @param {number} value.insertionIndex The index to insert at.
* @param {string} value.initialTab The id of the tab to display first when the block editor inserter is opened.
* A category corresponds to one of the tab ids defined in packages/block-editor/src/components/inserter/tabs.js.
*
* @return {Object} Action object.
*/
Expand Down
21 changes: 20 additions & 1 deletion packages/editor/src/store/private-selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ const EMPTY_INSERTION_POINT = {
export const getInsertionPoint = createRegistrySelector(
( select ) => ( state ) => {
if ( typeof state.blockInserterPanel === 'object' ) {
return state.blockInserterPanel;
return {
rootClientId: state.blockInserterPanel?.rootClientId,
insertionIndex: state.blockInserterPanel?.insertionIndex,
filterValue: state.blockInserterPanel?.filterValue,
};
}

if ( getRenderingMode( state ) === 'template-locked' ) {
Expand All @@ -46,6 +50,21 @@ export const getInsertionPoint = createRegistrySelector(
}
);

/**
* Get the initial tab id for the inserter.
* A category corresponds to one of the tab ids defined in packages/block-editor/src/components/inserter/tabs.js.
*
* @param {Object} state Global application state.
*
* @return {string} The initial tab category to open when the inserter is opened.
*/
export const getInserterInitialTab = createRegistrySelector(
() => ( state ) =>
typeof state.blockInserterPanel === 'object'
? state.blockInserterPanel?.initialTab
: null
);

export function getListViewToggleRef( state ) {
return state.listViewToggleRef;
}

0 comments on commit bfd9062

Please sign in to comment.