templateContext.js
34 lines
| 1 | import { applyFilters } from '@wordpress/hooks'; |
| 2 | import { createContext } from '@wordpress/element'; |
| 3 | import { useInnerBlocksCount } from '../../hooks'; |
| 4 | import TemplateSelector from './index'; |
| 5 | |
| 6 | const defaultContext = { |
| 7 | label: 'Label', |
| 8 | instructions: 'Instructions...', |
| 9 | templates: [], |
| 10 | }; |
| 11 | |
| 12 | const TemplateContext = createContext( defaultContext ); |
| 13 | |
| 14 | export const withTemplateContext = ( WrappedComponent ) => ( ( props ) => { |
| 15 | const { clientId } = props; |
| 16 | const innerBlocksCount = useInnerBlocksCount( clientId ); |
| 17 | const templateContext = applyFilters( |
| 18 | 'generateblocks.editor.templateContext', |
| 19 | defaultContext, |
| 20 | props |
| 21 | ); |
| 22 | |
| 23 | return ( |
| 24 | <TemplateContext.Provider value={ templateContext }> |
| 25 | { 0 < templateContext.templates.length && 0 === innerBlocksCount |
| 26 | ? <WrappedComponent { ...props } ContentRenderer={ TemplateSelector } /> |
| 27 | : <WrappedComponent { ...props } /> |
| 28 | } |
| 29 | </TemplateContext.Provider> |
| 30 | ); |
| 31 | } ); |
| 32 | |
| 33 | export default TemplateContext; |
| 34 |