2021-09-22 15:00:19 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
|
|
|
import { __ } from '@wordpress/i18n';
|
2021-12-14 00:46:10 +00:00
|
|
|
import { useState, useEffect } from '@wordpress/element';
|
2021-09-24 15:13:39 +00:00
|
|
|
import { useDispatch, select } from '@wordpress/data';
|
2021-10-27 15:41:57 +00:00
|
|
|
import { ToolbarGroup, ToolbarDropdownMenu } from '@wordpress/components';
|
2022-02-01 16:54:38 +00:00
|
|
|
import { eye } from '@woocommerce/icons';
|
|
|
|
import { Icon } from '@wordpress/icons';
|
2021-09-24 15:13:39 +00:00
|
|
|
import { store as blockEditorStore } from '@wordpress/block-editor';
|
2021-09-22 15:00:19 +00:00
|
|
|
|
|
|
|
interface View {
|
|
|
|
view: string;
|
|
|
|
label: string;
|
|
|
|
icon: string | JSX.Element;
|
|
|
|
}
|
|
|
|
|
2022-01-08 01:19:01 +00:00
|
|
|
function getView( viewName: string, views: View[] ) {
|
|
|
|
return views.find( ( view ) => view.view === viewName );
|
|
|
|
}
|
|
|
|
|
2021-09-22 15:00:19 +00:00
|
|
|
export const useViewSwitcher = (
|
2021-09-24 15:13:39 +00:00
|
|
|
clientId: string,
|
2021-09-22 15:00:19 +00:00
|
|
|
views: View[]
|
|
|
|
): {
|
|
|
|
currentView: string;
|
2021-10-27 15:41:57 +00:00
|
|
|
component: JSX.Element;
|
2021-09-22 15:00:19 +00:00
|
|
|
} => {
|
2021-09-24 15:13:39 +00:00
|
|
|
const initialView = views[ 0 ];
|
2021-09-22 15:00:19 +00:00
|
|
|
const [ currentView, setCurrentView ] = useState( initialView );
|
2021-09-24 15:13:39 +00:00
|
|
|
const { selectBlock } = useDispatch( 'core/block-editor' );
|
2022-06-15 09:56:52 +00:00
|
|
|
const { getBlock, getSelectedBlockClientId, getBlockParentsByBlockName } =
|
|
|
|
select( blockEditorStore );
|
2021-12-14 00:46:10 +00:00
|
|
|
const selectedBlockClientId = getSelectedBlockClientId();
|
|
|
|
|
|
|
|
useEffect( () => {
|
2022-01-08 01:19:01 +00:00
|
|
|
const selectedBlock = getBlock( selectedBlockClientId );
|
|
|
|
|
|
|
|
if ( ! selectedBlock ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( currentView.view === selectedBlock.name ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-14 00:46:10 +00:00
|
|
|
const viewNames = views.map( ( { view } ) => view );
|
2022-01-08 01:19:01 +00:00
|
|
|
|
|
|
|
if ( viewNames.includes( selectedBlock.name ) ) {
|
|
|
|
const newView = getView( selectedBlock.name, views );
|
|
|
|
if ( newView ) {
|
|
|
|
return setCurrentView( newView );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 00:46:10 +00:00
|
|
|
const parentBlockIds = getBlockParentsByBlockName(
|
|
|
|
selectedBlockClientId,
|
|
|
|
viewNames
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( parentBlockIds.length !== 1 ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const parentBlock = getBlock( parentBlockIds[ 0 ] );
|
|
|
|
|
|
|
|
if ( currentView.view === parentBlock.name ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-08 01:19:01 +00:00
|
|
|
const newView = getView( parentBlock.name, views );
|
2021-12-14 00:46:10 +00:00
|
|
|
|
2022-01-08 01:19:01 +00:00
|
|
|
if ( newView ) {
|
|
|
|
setCurrentView( newView );
|
2021-12-14 00:46:10 +00:00
|
|
|
}
|
|
|
|
}, [
|
|
|
|
getBlockParentsByBlockName,
|
|
|
|
selectedBlockClientId,
|
|
|
|
getBlock,
|
|
|
|
currentView.view,
|
|
|
|
views,
|
|
|
|
] );
|
2021-09-22 15:00:19 +00:00
|
|
|
|
2021-10-27 15:41:57 +00:00
|
|
|
const ViewSwitcherComponent = (
|
|
|
|
<ToolbarGroup>
|
2021-09-24 15:13:39 +00:00
|
|
|
<ToolbarDropdownMenu
|
|
|
|
label={ __( 'Switch view', 'woo-gutenberg-products-block' ) }
|
|
|
|
text={ currentView.label }
|
2022-02-01 16:54:38 +00:00
|
|
|
icon={ <Icon icon={ eye } style={ { marginRight: '8px' } } /> }
|
2021-09-24 15:13:39 +00:00
|
|
|
controls={ views.map( ( view ) => ( {
|
|
|
|
...view,
|
2021-11-09 13:18:55 +00:00
|
|
|
title: <span>{ view.label }</span>,
|
2021-10-27 15:41:57 +00:00
|
|
|
isActive: view.view === currentView.view,
|
2021-09-24 15:13:39 +00:00
|
|
|
onClick: () => {
|
|
|
|
setCurrentView( view );
|
|
|
|
selectBlock(
|
|
|
|
getBlock( clientId ).innerBlocks.find(
|
|
|
|
( block: { name: string } ) =>
|
|
|
|
block.name === view.view
|
|
|
|
)?.clientId || clientId
|
|
|
|
);
|
|
|
|
},
|
|
|
|
} ) ) }
|
|
|
|
/>
|
2021-10-27 15:41:57 +00:00
|
|
|
</ToolbarGroup>
|
2021-09-22 15:00:19 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return {
|
|
|
|
currentView: currentView.view,
|
|
|
|
component: ViewSwitcherComponent,
|
|
|
|
};
|
|
|
|
};
|